Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

move per-context changes from c++ to js #21518

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion lib/internal/bootstrap/cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ module.exports = {
// the code cache is also used when compiling these
// two files.
'internal/bootstrap/loaders',
'internal/bootstrap/node'
'internal/bootstrap/node',
'internal/per_context',
]
};
52 changes: 52 additions & 0 deletions lib/internal/per_context.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
'use strict';

// node::NewContext calls this script

(function(global) {
// https://github.com/nodejs/node/issues/14909
delete global.Intl.v8BreakIterator;

// https://github.com/nodejs/node/issues/21219
// Adds Atomics.notify and warns on first usage of Atomics.wake

const AtomicsWake = global.Atomics.wake;
const ReflectApply = global.Reflect.apply;

// wrap for function.name
function notify(...args) {
return ReflectApply(AtomicsWake, this, args);
}

const warning = 'Atomics.wake will be removed in a future version, ' +
'use Atomics.notify instead.';

let wakeWarned = false;
function wake(...args) {
if (!wakeWarned) {
wakeWarned = true;

if (global.process !== undefined) {
global.process.emitWarning(warning, 'Atomics');
} else {
global.console.error(`Atomics: ${warning}`);
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I’m not sure, is there any reason why we wouldn’t want to do a normal deprecation cycle for these (i.e. make this PR semver-major)?

Copy link
Member Author

@devsnek devsnek Jun 25, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wasn't quite sure where to document it and like @rwaldon said it's not very widely used. I'm ok with removing the message though

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should this still be considered semver major?


return ReflectApply(AtomicsWake, this, args);
}

global.Object.defineProperties(global.Atomics, {
notify: {
value: notify,
writable: true,
enumerable: false,
configurable: true,
},
wake: {
value: wake,
writable: true,
enumerable: false,
configurable: true,
},
});
}(this));
1 change: 1 addition & 0 deletions node.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
'node_lib_target_name%': 'node_lib',
'node_intermediate_lib_type%': 'static_library',
'library_files': [
'lib/internal/per_context.js',
'lib/internal/bootstrap/cache.js',
'lib/internal/bootstrap/loaders.js',
'lib/internal/bootstrap/node.js',
Expand Down
36 changes: 10 additions & 26 deletions src/node.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3497,35 +3497,19 @@ Local<Context> NewContext(Isolate* isolate,
auto context = Context::New(isolate, nullptr, object_template);
if (context.IsEmpty()) return context;
HandleScope handle_scope(isolate);

context->SetEmbedderData(
ContextEmbedderIndex::kAllowWasmCodeGeneration, True(isolate));

auto intl_key = FIXED_ONE_BYTE_STRING(isolate, "Intl");
auto break_iter_key = FIXED_ONE_BYTE_STRING(isolate, "v8BreakIterator");
Local<Value> intl_v;
if (context->Global()->Get(context, intl_key).ToLocal(&intl_v) &&
intl_v->IsObject()) {
Local<Object> intl = intl_v.As<Object>();
intl->Delete(context, break_iter_key).FromJust();
}

// https://github.com/nodejs/node/issues/21219
// TODO(devsnek): remove when v8 supports Atomics.notify
auto atomics_key = FIXED_ONE_BYTE_STRING(isolate, "Atomics");
Local<Value> atomics_v;
if (context->Global()->Get(context, atomics_key).ToLocal(&atomics_v) &&
atomics_v->IsObject()) {
Local<Object> atomics = atomics_v.As<Object>();
auto wake_key = FIXED_ONE_BYTE_STRING(isolate, "wake");

Local<Value> wake = atomics->Get(context, wake_key).ToLocalChecked();
auto notify_key = FIXED_ONE_BYTE_STRING(isolate, "notify");

v8::PropertyDescriptor desc(wake, true);
desc.set_enumerable(false);
desc.set_configurable(true);

atomics->DefineProperty(context, notify_key, desc).ToChecked();
{
// Run lib/internal/per_context.js
Context::Scope context_scope(context);
Local<String> per_context = NodePerContextSource(isolate);
v8::ScriptCompiler::Source per_context_src(per_context, nullptr);
Local<v8::Script> s = v8::ScriptCompiler::Compile(
context,
&per_context_src).ToLocalChecked();
s->Run(context).ToLocalChecked();
}

return context;
Expand Down
4 changes: 1 addition & 3 deletions src/node.h
Original file line number Diff line number Diff line change
Expand Up @@ -241,9 +241,7 @@ class MultiIsolatePlatform : public v8::Platform {
// Creates a new isolate with Node.js-specific settings.
NODE_EXTERN v8::Isolate* NewIsolate(ArrayBufferAllocator* allocator);

// Creates a new context with Node.js-specific tweaks. Currently, it removes
// the `v8BreakIterator` property from the global `Intl` object if present.
// See https://github.com/nodejs/node/issues/14909 for more info.
// Creates a new context with Node.js-specific tweaks.
NODE_EXTERN v8::Local<v8::Context> NewContext(
v8::Isolate* isolate,
v8::Local<v8::ObjectTemplate> object_template =
Expand Down
1 change: 1 addition & 0 deletions src/node_javascript.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
namespace node {

void DefineJavaScript(Environment* env, v8::Local<v8::Object> target);
v8::Local<v8::String> NodePerContextSource(v8::Isolate* isolate);
v8::Local<v8::String> LoadersBootstrapperSource(Environment* env);
v8::Local<v8::String> NodeBootstrapperSource(Environment* env);

Expand Down
15 changes: 12 additions & 3 deletions test/parallel/test-atomics-notify.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
'use strict';

require('../common');
const { expectWarning, noWarnCode } = require('../common');

const assert = require('assert');
const { runInNewContext } = require('vm');

assert.strictEqual(Atomics.wake, Atomics.notify);
assert.strictEqual(typeof Atomics.wake, 'function');
assert.strictEqual(typeof Atomics.notify, 'function');

assert(runInNewContext('Atomics.wake === Atomics.notify'));
assert.strictEqual(runInNewContext('typeof Atomics.wake'), 'function');
assert.strictEqual(runInNewContext('typeof Atomics.notify'), 'function');

expectWarning(
'Atomics',
'Atomics.wake will be removed in a future version, ' +
'use Atomics.notify instead.', noWarnCode);

Atomics.wake(new Int32Array(new SharedArrayBuffer(4)), 0, 0);
4 changes: 4 additions & 0 deletions tools/js2c.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,10 @@ def ReadMacros(lines):

}} // anonymous namespace

v8::Local<v8::String> NodePerContextSource(v8::Isolate* isolate) {{
return internal_per_context_value.ToStringChecked(isolate);
}}

v8::Local<v8::String> LoadersBootstrapperSource(Environment* env) {{
return internal_bootstrap_loaders_value.ToStringChecked(env->isolate());
}}
Expand Down