diff --git a/lib/internal/bootstrap/browser.js b/lib/internal/bootstrap/browser.js index 92c98a7fa9cc55..4df2fa0a495749 100644 --- a/lib/internal/bootstrap/browser.js +++ b/lib/internal/bootstrap/browser.js @@ -65,6 +65,13 @@ defineOperation(globalThis, 'clearTimeout', timers.clearTimeout); defineOperation(globalThis, 'setInterval', timers.setInterval); defineOperation(globalThis, 'setTimeout', timers.setTimeout); +const buffer = require('buffer'); +defineOperation(globalThis, 'atob', buffer.atob); +defineOperation(globalThis, 'btoa', buffer.btoa); + +// https://www.w3.org/TR/FileAPI/#dfn-Blob +exposeInterface(globalThis, 'Blob', buffer.Blob); + // https://www.w3.org/TR/hr-time-2/#the-performance-attribute defineReplacableAttribute(globalThis, 'performance', require('perf_hooks').performance); diff --git a/lib/internal/bootstrap/node.js b/lib/internal/bootstrap/node.js index e58538b34cb17e..b434efe0cf4d53 100644 --- a/lib/internal/bootstrap/node.js +++ b/lib/internal/bootstrap/node.js @@ -377,14 +377,6 @@ function setupProcessObject() { configurable: false, value: 'process' }); - // Make process globally available to users by putting it on the global proxy - ObjectDefineProperty(globalThis, 'process', { - __proto__: null, - value: process, - enumerable: false, - writable: true, - configurable: true - }); } function setupGlobalProxy() { @@ -399,10 +391,7 @@ function setupGlobalProxy() { function setupBuffer() { const { - Blob, Buffer, - atob, - btoa, } = require('buffer'); const bufferBinding = internalBinding('buffer'); @@ -410,35 +399,4 @@ function setupBuffer() { bufferBinding.setBufferPrototype(Buffer.prototype); delete bufferBinding.setBufferPrototype; delete bufferBinding.zeroFill; - - ObjectDefineProperties(globalThis, { - 'Blob': { - __proto__: null, - value: Blob, - enumerable: false, - writable: true, - configurable: true, - }, - 'Buffer': { - __proto__: null, - value: Buffer, - enumerable: false, - writable: true, - configurable: true, - }, - 'atob': { - __proto__: null, - value: atob, - enumerable: false, - writable: true, - configurable: true, - }, - 'btoa': { - __proto__: null, - value: btoa, - enumerable: false, - writable: true, - configurable: true, - }, - }); } diff --git a/src/api/environment.cc b/src/api/environment.cc index ccdcdefb20fe84..8b914c0082ea8d 100644 --- a/src/api/environment.cc +++ b/src/api/environment.cc @@ -447,9 +447,10 @@ MaybeLocal LoadEnvironment( // TODO(addaleax): Avoid having a global table for all scripts. std::string name = "embedder_main_" + std::to_string(env->thread_id()); - native_module::NativeModuleEnv::Add( + bool added = native_module::NativeModuleEnv::Add( name.c_str(), UnionBytes(**main_utf16, main_utf16->length())); + CHECK(added); env->set_main_utf16(std::move(main_utf16)); std::vector> params = { env->process_string(), diff --git a/test/cctest/test_linked_binding.cc b/test/cctest/test_linked_binding.cc index 7e40068b5db799..dcdd8554d77838 100644 --- a/test/cctest/test_linked_binding.cc +++ b/test/cctest/test_linked_binding.cc @@ -28,15 +28,18 @@ TEST_F(LinkedBindingTest, SimpleTest) { v8::Local context = isolate_->GetCurrentContext(); - const char* run_script = + v8::Local completion_value = node::LoadEnvironment(*test_env, [&](const node::StartExecutionCallbackInfo& info) -> v8::MaybeLocal { + const char* run_script = "process._linkedBinding('cctest_linkedbinding').key"; - v8::Local script = v8::Script::Compile( - context, - v8::String::NewFromOneByte(isolate_, - reinterpret_cast(run_script)) - .ToLocalChecked()) - .ToLocalChecked(); - v8::Local completion_value = script->Run(context).ToLocalChecked(); + v8::Local script = v8::Script::Compile( + context, + v8::String::NewFromOneByte(isolate_, + reinterpret_cast(run_script)) + .ToLocalChecked()) + .ToLocalChecked(); + return script->Run(context); + }).ToLocalChecked(); + v8::String::Utf8Value utf8val(isolate_, completion_value); CHECK_NOT_NULL(*utf8val); CHECK_EQ(strcmp(*utf8val, "value"), 0); @@ -69,15 +72,18 @@ TEST_F(LinkedBindingTest, LocallyDefinedLinkedBindingTest) { v8::Local context = isolate_->GetCurrentContext(); - const char* run_script = - "process._linkedBinding('local_linked').key"; - v8::Local script = v8::Script::Compile( - context, - v8::String::NewFromOneByte(isolate_, - reinterpret_cast(run_script)) - .ToLocalChecked()) - .ToLocalChecked(); - v8::Local completion_value = script->Run(context).ToLocalChecked(); + v8::Local completion_value = node::LoadEnvironment(*test_env, [&](const node::StartExecutionCallbackInfo& info) -> v8::MaybeLocal { + const char* run_script = + "process._linkedBinding('local_linked').key"; + v8::Local script = v8::Script::Compile( + context, + v8::String::NewFromOneByte(isolate_, + reinterpret_cast(run_script)) + .ToLocalChecked()) + .ToLocalChecked(); + return script->Run(context); + }).ToLocalChecked(); + v8::String::Utf8Value utf8val(isolate_, completion_value); CHECK_NOT_NULL(*utf8val); CHECK_EQ(strcmp(*utf8val, "value"), 0); @@ -113,15 +119,18 @@ TEST_F(LinkedBindingTest, LocallyDefinedLinkedBindingNapiTest) { v8::Local context = isolate_->GetCurrentContext(); - const char* run_script = - "process._linkedBinding('local_linked_napi').hello"; - v8::Local script = v8::Script::Compile( - context, - v8::String::NewFromOneByte(isolate_, - reinterpret_cast(run_script)) - .ToLocalChecked()) - .ToLocalChecked(); - v8::Local completion_value = script->Run(context).ToLocalChecked(); + v8::Local completion_value = node::LoadEnvironment(*test_env, [&](const node::StartExecutionCallbackInfo& info) -> v8::MaybeLocal { + const char* run_script = + "process._linkedBinding('local_linked_napi').hello"; + v8::Local script = v8::Script::Compile( + context, + v8::String::NewFromOneByte(isolate_, + reinterpret_cast(run_script)) + .ToLocalChecked()) + .ToLocalChecked(); + return script->Run(context); + }).ToLocalChecked(); + v8::String::Utf8Value utf8val(isolate_, completion_value); CHECK_NOT_NULL(*utf8val); CHECK_EQ(strcmp(*utf8val, "world"), 0); @@ -169,17 +178,18 @@ TEST_F(LinkedBindingTest, LocallyDefinedLinkedBindingNapiInstanceDataTest) { AddLinkedBinding(*test_env, local_linked_napi_id); v8::Local context = isolate_->GetCurrentContext(); + v8::Local completion_value = node::LoadEnvironment(*test_env, [&](const node::StartExecutionCallbackInfo& info) -> v8::MaybeLocal { + const char* run_script = + "process._linkedBinding('local_linked_napi_id').hello"; + v8::Local script = v8::Script::Compile( + context, + v8::String::NewFromOneByte(isolate_, + reinterpret_cast(run_script)) + .ToLocalChecked()) + .ToLocalChecked(); + return script->Run(context); + }).ToLocalChecked(); - const char* run_script = - "process._linkedBinding('local_linked_napi_id').hello"; - v8::Local script = v8::Script::Compile( - context, - v8::String::NewFromOneByte(isolate_, - reinterpret_cast(run_script)) - .ToLocalChecked()) - .ToLocalChecked(); - v8::Local completion_value = - script->Run(context).ToLocalChecked(); CHECK(completion_value->IsExternal()); instance_data = static_cast( completion_value.As()->Value()); @@ -206,16 +216,19 @@ TEST_F(LinkedBindingTest, ManyBindingsTest) { v8::Local context = isolate_->GetCurrentContext(); - const char* run_script = - "for (let i = 1; i <= 5; i++)process._linkedBinding(`local_linked${i}`);" - "process._linkedBinding('local_linked_napi').hello"; - v8::Local script = v8::Script::Compile( - context, - v8::String::NewFromOneByte(isolate_, - reinterpret_cast(run_script)) - .ToLocalChecked()) - .ToLocalChecked(); - v8::Local completion_value = script->Run(context).ToLocalChecked(); + v8::Local completion_value = node::LoadEnvironment(*test_env, [&](const node::StartExecutionCallbackInfo& info) -> v8::MaybeLocal { + const char* run_script = + "for (let i = 1; i <= 5; i++)process._linkedBinding(`local_linked${i}`);" + "process._linkedBinding('local_linked_napi').hello"; + v8::Local script = v8::Script::Compile( + context, + v8::String::NewFromOneByte(isolate_, + reinterpret_cast(run_script)) + .ToLocalChecked()) + .ToLocalChecked(); + return script->Run(context); + }).ToLocalChecked(); + v8::String::Utf8Value utf8val(isolate_, completion_value); CHECK_NOT_NULL(*utf8val); CHECK_EQ(strcmp(*utf8val, "world"), 0); diff --git a/test/parallel/test-global.js b/test/parallel/test-global.js index 3585062bf310e5..5437751cbcc520 100644 --- a/test/parallel/test-global.js +++ b/test/parallel/test-global.js @@ -49,6 +49,8 @@ builtinModules.forEach((moduleName) => { 'clearImmediate', 'clearInterval', 'clearTimeout', + 'atob', + 'btoa', 'performance', 'setImmediate', 'setInterval',