Skip to content

Commit 89b291b

Browse files
committed
bootstrap: consolidate global properties definition
`globalThis.process` and `globalThis.Buffer` has been re-defined with a getter/setter pair. `atob` and `bota` are defined as enumerable properties according to WebIDL definition.
1 parent c977ad6 commit 89b291b

File tree

5 files changed

+107
-92
lines changed

5 files changed

+107
-92
lines changed

lib/internal/bootstrap/browser.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,13 @@ defineOperation(globalThis, 'clearTimeout', timers.clearTimeout);
6565
defineOperation(globalThis, 'setInterval', timers.setInterval);
6666
defineOperation(globalThis, 'setTimeout', timers.setTimeout);
6767

68+
const buffer = require('buffer');
69+
defineOperation(globalThis, 'atob', buffer.atob);
70+
defineOperation(globalThis, 'btoa', buffer.btoa);
71+
72+
// https://www.w3.org/TR/FileAPI/#dfn-Blob
73+
exposeInterface(globalThis, 'Blob', buffer.Blob);
74+
6875
// https://www.w3.org/TR/hr-time-2/#the-performance-attribute
6976
defineReplacableAttribute(globalThis, 'performance',
7077
require('perf_hooks').performance);

lib/internal/bootstrap/node.js

Lines changed: 3 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ const {
4545
FunctionPrototypeCall,
4646
JSONParse,
4747
ObjectDefineProperty,
48-
ObjectDefineProperties,
4948
ObjectGetPrototypeOf,
5049
ObjectPreventExtensions,
5150
ObjectSetPrototypeOf,
@@ -377,14 +376,8 @@ function setupProcessObject() {
377376
configurable: false,
378377
value: 'process'
379378
});
380-
// Make process globally available to users by putting it on the global proxy
381-
ObjectDefineProperty(globalThis, 'process', {
382-
__proto__: null,
383-
value: process,
384-
enumerable: false,
385-
writable: true,
386-
configurable: true
387-
});
379+
380+
// globalThis.process is been installed at internal/bootstrap/pre_execution.js
388381
}
389382

390383
function setupGlobalProxy() {
@@ -399,10 +392,7 @@ function setupGlobalProxy() {
399392

400393
function setupBuffer() {
401394
const {
402-
Blob,
403395
Buffer,
404-
atob,
405-
btoa,
406396
} = require('buffer');
407397
const bufferBinding = internalBinding('buffer');
408398

@@ -411,34 +401,5 @@ function setupBuffer() {
411401
delete bufferBinding.setBufferPrototype;
412402
delete bufferBinding.zeroFill;
413403

414-
ObjectDefineProperties(globalThis, {
415-
'Blob': {
416-
__proto__: null,
417-
value: Blob,
418-
enumerable: false,
419-
writable: true,
420-
configurable: true,
421-
},
422-
'Buffer': {
423-
__proto__: null,
424-
value: Buffer,
425-
enumerable: false,
426-
writable: true,
427-
configurable: true,
428-
},
429-
'atob': {
430-
__proto__: null,
431-
value: atob,
432-
enumerable: false,
433-
writable: true,
434-
configurable: true,
435-
},
436-
'btoa': {
437-
__proto__: null,
438-
value: btoa,
439-
enumerable: false,
440-
writable: true,
441-
configurable: true,
442-
},
443-
});
404+
// globalThis.buffer is been installed at internal/bootstrap/pre_execution.js
444405
}

src/api/environment.cc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -447,9 +447,9 @@ MaybeLocal<Value> LoadEnvironment(
447447

448448
// TODO(addaleax): Avoid having a global table for all scripts.
449449
std::string name = "embedder_main_" + std::to_string(env->thread_id());
450-
native_module::NativeModuleEnv::Add(
451-
name.c_str(),
452-
UnionBytes(**main_utf16, main_utf16->length()));
450+
bool added = native_module::NativeModuleEnv::Add(
451+
name.c_str(), UnionBytes(**main_utf16, main_utf16->length()));
452+
CHECK(added);
453453
env->set_main_utf16(std::move(main_utf16));
454454
std::vector<Local<String>> params = {
455455
env->process_string(),

test/cctest/test_linked_binding.cc

Lines changed: 92 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,24 @@ TEST_F(LinkedBindingTest, SimpleTest) {
2828

2929
v8::Local<v8::Context> context = isolate_->GetCurrentContext();
3030

31-
const char* run_script =
32-
"process._linkedBinding('cctest_linkedbinding').key";
33-
v8::Local<v8::Script> script = v8::Script::Compile(
34-
context,
35-
v8::String::NewFromOneByte(isolate_,
36-
reinterpret_cast<const uint8_t*>(run_script))
37-
.ToLocalChecked())
38-
.ToLocalChecked();
39-
v8::Local<v8::Value> completion_value = script->Run(context).ToLocalChecked();
31+
v8::Local<v8::Value> completion_value =
32+
node::LoadEnvironment(
33+
*test_env,
34+
[&](const node::StartExecutionCallbackInfo& info)
35+
-> v8::MaybeLocal<v8::Value> {
36+
const char* run_script =
37+
"process._linkedBinding('cctest_linkedbinding').key";
38+
v8::Local<v8::Script> script =
39+
v8::Script::Compile(
40+
context,
41+
v8::String::NewFromOneByte(
42+
isolate_, reinterpret_cast<const uint8_t*>(run_script))
43+
.ToLocalChecked())
44+
.ToLocalChecked();
45+
return script->Run(context);
46+
})
47+
.ToLocalChecked();
48+
4049
v8::String::Utf8Value utf8val(isolate_, completion_value);
4150
CHECK_NOT_NULL(*utf8val);
4251
CHECK_EQ(strcmp(*utf8val, "value"), 0);
@@ -69,15 +78,24 @@ TEST_F(LinkedBindingTest, LocallyDefinedLinkedBindingTest) {
6978

7079
v8::Local<v8::Context> context = isolate_->GetCurrentContext();
7180

72-
const char* run_script =
73-
"process._linkedBinding('local_linked').key";
74-
v8::Local<v8::Script> script = v8::Script::Compile(
75-
context,
76-
v8::String::NewFromOneByte(isolate_,
77-
reinterpret_cast<const uint8_t*>(run_script))
78-
.ToLocalChecked())
79-
.ToLocalChecked();
80-
v8::Local<v8::Value> completion_value = script->Run(context).ToLocalChecked();
81+
v8::Local<v8::Value> completion_value =
82+
node::LoadEnvironment(
83+
*test_env,
84+
[&](const node::StartExecutionCallbackInfo& info)
85+
-> v8::MaybeLocal<v8::Value> {
86+
const char* run_script =
87+
"process._linkedBinding('local_linked').key";
88+
v8::Local<v8::Script> script =
89+
v8::Script::Compile(
90+
context,
91+
v8::String::NewFromOneByte(
92+
isolate_, reinterpret_cast<const uint8_t*>(run_script))
93+
.ToLocalChecked())
94+
.ToLocalChecked();
95+
return script->Run(context);
96+
})
97+
.ToLocalChecked();
98+
8199
v8::String::Utf8Value utf8val(isolate_, completion_value);
82100
CHECK_NOT_NULL(*utf8val);
83101
CHECK_EQ(strcmp(*utf8val, "value"), 0);
@@ -113,15 +131,24 @@ TEST_F(LinkedBindingTest, LocallyDefinedLinkedBindingNapiTest) {
113131

114132
v8::Local<v8::Context> context = isolate_->GetCurrentContext();
115133

116-
const char* run_script =
117-
"process._linkedBinding('local_linked_napi').hello";
118-
v8::Local<v8::Script> script = v8::Script::Compile(
119-
context,
120-
v8::String::NewFromOneByte(isolate_,
121-
reinterpret_cast<const uint8_t*>(run_script))
122-
.ToLocalChecked())
123-
.ToLocalChecked();
124-
v8::Local<v8::Value> completion_value = script->Run(context).ToLocalChecked();
134+
v8::Local<v8::Value> completion_value =
135+
node::LoadEnvironment(
136+
*test_env,
137+
[&](const node::StartExecutionCallbackInfo& info)
138+
-> v8::MaybeLocal<v8::Value> {
139+
const char* run_script =
140+
"process._linkedBinding('local_linked_napi').hello";
141+
v8::Local<v8::Script> script =
142+
v8::Script::Compile(
143+
context,
144+
v8::String::NewFromOneByte(
145+
isolate_, reinterpret_cast<const uint8_t*>(run_script))
146+
.ToLocalChecked())
147+
.ToLocalChecked();
148+
return script->Run(context);
149+
})
150+
.ToLocalChecked();
151+
125152
v8::String::Utf8Value utf8val(isolate_, completion_value);
126153
CHECK_NOT_NULL(*utf8val);
127154
CHECK_EQ(strcmp(*utf8val, "world"), 0);
@@ -169,17 +196,25 @@ TEST_F(LinkedBindingTest, LocallyDefinedLinkedBindingNapiInstanceDataTest) {
169196
AddLinkedBinding(*test_env, local_linked_napi_id);
170197

171198
v8::Local<v8::Context> context = isolate_->GetCurrentContext();
172-
173-
const char* run_script =
174-
"process._linkedBinding('local_linked_napi_id').hello";
175-
v8::Local<v8::Script> script = v8::Script::Compile(
176-
context,
177-
v8::String::NewFromOneByte(isolate_,
178-
reinterpret_cast<const uint8_t*>(run_script))
179-
.ToLocalChecked())
180-
.ToLocalChecked();
181199
v8::Local<v8::Value> completion_value =
182-
script->Run(context).ToLocalChecked();
200+
node::LoadEnvironment(
201+
*test_env,
202+
[&](const node::StartExecutionCallbackInfo& info)
203+
-> v8::MaybeLocal<v8::Value> {
204+
const char* run_script =
205+
"process._linkedBinding('local_linked_napi_id').hello";
206+
v8::Local<v8::Script> script =
207+
v8::Script::Compile(
208+
context,
209+
v8::String::NewFromOneByte(
210+
isolate_,
211+
reinterpret_cast<const uint8_t*>(run_script))
212+
.ToLocalChecked())
213+
.ToLocalChecked();
214+
return script->Run(context);
215+
})
216+
.ToLocalChecked();
217+
183218
CHECK(completion_value->IsExternal());
184219
instance_data = static_cast<int*>(
185220
completion_value.As<v8::External>()->Value());
@@ -206,16 +241,26 @@ TEST_F(LinkedBindingTest, ManyBindingsTest) {
206241

207242
v8::Local<v8::Context> context = isolate_->GetCurrentContext();
208243

209-
const char* run_script =
210-
"for (let i = 1; i <= 5; i++)process._linkedBinding(`local_linked${i}`);"
211-
"process._linkedBinding('local_linked_napi').hello";
212-
v8::Local<v8::Script> script = v8::Script::Compile(
213-
context,
214-
v8::String::NewFromOneByte(isolate_,
215-
reinterpret_cast<const uint8_t*>(run_script))
216-
.ToLocalChecked())
217-
.ToLocalChecked();
218-
v8::Local<v8::Value> completion_value = script->Run(context).ToLocalChecked();
244+
v8::Local<v8::Value> completion_value =
245+
node::LoadEnvironment(
246+
*test_env,
247+
[&](const node::StartExecutionCallbackInfo& info)
248+
-> v8::MaybeLocal<v8::Value> {
249+
const char* run_script =
250+
"for (let i = 1; i <= 5; "
251+
"i++)process._linkedBinding(`local_linked${i}`);"
252+
"process._linkedBinding('local_linked_napi').hello";
253+
v8::Local<v8::Script> script =
254+
v8::Script::Compile(
255+
context,
256+
v8::String::NewFromOneByte(
257+
isolate_, reinterpret_cast<const uint8_t*>(run_script))
258+
.ToLocalChecked())
259+
.ToLocalChecked();
260+
return script->Run(context);
261+
})
262+
.ToLocalChecked();
263+
219264
v8::String::Utf8Value utf8val(isolate_, completion_value);
220265
CHECK_NOT_NULL(*utf8val);
221266
CHECK_EQ(strcmp(*utf8val, "world"), 0);

test/parallel/test-global.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ builtinModules.forEach((moduleName) => {
4949
'clearImmediate',
5050
'clearInterval',
5151
'clearTimeout',
52+
'atob',
53+
'btoa',
5254
'performance',
5355
'setImmediate',
5456
'setInterval',

0 commit comments

Comments
 (0)