Skip to content

Commit eb8d60b

Browse files
devsnektargos
authored andcommitted
src: move context bootstrap to js
PR-URL: #21518 Reviewed-By: Ben Noordhuis <[email protected]> Reviewed-By: Anna Henningsen <[email protected]>
1 parent 623cf11 commit eb8d60b

File tree

6 files changed

+33
-29
lines changed

6 files changed

+33
-29
lines changed

lib/internal/per_context.js

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
'use strict';
2+
3+
// node::NewContext calls this script
4+
5+
(function(global) {
6+
// https://github.com/nodejs/node/issues/14909
7+
delete global.Intl.v8BreakIterator;
8+
9+
// https://github.com/nodejs/node/issues/21219
10+
Object.defineProperty(global.Atomics, 'notify', {
11+
value: global.Atomics.wake,
12+
writable: true,
13+
enumerable: false,
14+
configurable: true,
15+
});
16+
}(this));

node.gyp

+1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
'node_lib_target_name%': 'node_lib',
2626
'node_intermediate_lib_type%': 'static_library',
2727
'library_files': [
28+
'lib/internal/per_context.js',
2829
'lib/internal/bootstrap/cache.js',
2930
'lib/internal/bootstrap/loaders.js',
3031
'lib/internal/bootstrap/node.js',

src/node.cc

+10-26
Original file line numberDiff line numberDiff line change
@@ -4046,35 +4046,19 @@ Local<Context> NewContext(Isolate* isolate,
40464046
auto context = Context::New(isolate, nullptr, object_template);
40474047
if (context.IsEmpty()) return context;
40484048
HandleScope handle_scope(isolate);
4049+
40494050
context->SetEmbedderData(
40504051
ContextEmbedderIndex::kAllowWasmCodeGeneration, True(isolate));
40514052

4052-
auto intl_key = FIXED_ONE_BYTE_STRING(isolate, "Intl");
4053-
auto break_iter_key = FIXED_ONE_BYTE_STRING(isolate, "v8BreakIterator");
4054-
Local<Value> intl_v;
4055-
if (context->Global()->Get(context, intl_key).ToLocal(&intl_v) &&
4056-
intl_v->IsObject()) {
4057-
Local<Object> intl = intl_v.As<Object>();
4058-
intl->Delete(context, break_iter_key).FromJust();
4059-
}
4060-
4061-
// https://github.com/nodejs/node/issues/21219
4062-
// TODO(devsnek): remove when v8 supports Atomics.notify
4063-
auto atomics_key = FIXED_ONE_BYTE_STRING(isolate, "Atomics");
4064-
Local<Value> atomics_v;
4065-
if (context->Global()->Get(context, atomics_key).ToLocal(&atomics_v) &&
4066-
atomics_v->IsObject()) {
4067-
Local<Object> atomics = atomics_v.As<Object>();
4068-
auto wake_key = FIXED_ONE_BYTE_STRING(isolate, "wake");
4069-
4070-
Local<Value> wake = atomics->Get(context, wake_key).ToLocalChecked();
4071-
auto notify_key = FIXED_ONE_BYTE_STRING(isolate, "notify");
4072-
4073-
v8::PropertyDescriptor desc(wake, true);
4074-
desc.set_enumerable(false);
4075-
desc.set_configurable(true);
4076-
4077-
atomics->DefineProperty(context, notify_key, desc).ToChecked();
4053+
{
4054+
// Run lib/internal/per_context.js
4055+
Context::Scope context_scope(context);
4056+
Local<String> per_context = NodePerContextSource(isolate);
4057+
v8::ScriptCompiler::Source per_context_src(per_context, nullptr);
4058+
Local<v8::Script> s = v8::ScriptCompiler::Compile(
4059+
context,
4060+
&per_context_src).ToLocalChecked();
4061+
s->Run(context).ToLocalChecked();
40784062
}
40794063

40804064
return context;

src/node.h

+1-3
Original file line numberDiff line numberDiff line change
@@ -241,9 +241,7 @@ class MultiIsolatePlatform : public v8::Platform {
241241
// Creates a new isolate with Node.js-specific settings.
242242
NODE_EXTERN v8::Isolate* NewIsolate(ArrayBufferAllocator* allocator);
243243

244-
// Creates a new context with Node.js-specific tweaks. Currently, it removes
245-
// the `v8BreakIterator` property from the global `Intl` object if present.
246-
// See https://github.com/nodejs/node/issues/14909 for more info.
244+
// Creates a new context with Node.js-specific tweaks.
247245
NODE_EXTERN v8::Local<v8::Context> NewContext(
248246
v8::Isolate* isolate,
249247
v8::Local<v8::ObjectTemplate> object_template =

src/node_javascript.h

+1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
namespace node {
3030

3131
void DefineJavaScript(Environment* env, v8::Local<v8::Object> target);
32+
v8::Local<v8::String> NodePerContextSource(v8::Isolate* isolate);
3233
v8::Local<v8::String> LoadersBootstrapperSource(Environment* env);
3334
v8::Local<v8::String> NodeBootstrapperSource(Environment* env);
3435

tools/js2c.py

+4
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,10 @@ def ReadMacros(lines):
189189
190190
}} // anonymous namespace
191191
192+
v8::Local<v8::String> NodePerContextSource(v8::Isolate* isolate) {{
193+
return internal_per_context_value.ToStringChecked(isolate);
194+
}}
195+
192196
v8::Local<v8::String> LoadersBootstrapperSource(Environment* env) {{
193197
return internal_bootstrap_loaders_value.ToStringChecked(env->isolate());
194198
}}

0 commit comments

Comments
 (0)