From b9a97c2ff76ed2f9bd5fa03fe92b1bf641d9ae3d Mon Sep 17 00:00:00 2001 From: Joyee Cheung Date: Mon, 26 Nov 2018 21:50:19 +0800 Subject: [PATCH] src: use NativeModuleLoader to compile per_context.js This patch introduces a NativeModuleLoader::CompileAndCall that can run a JS script under `lib/` as a function called with a null receiver and arguments specified from the C++ layer. Since all our bootstrappers are wrapped in functions in the source to avoid leaking variables into the global scope anyway, this allows us to remove that extra indentation in the JS source code. As a start we move the compilation and execution of per_context.js to NativeModuleLoader::CompileAndCall(). This patch also changes the return value of NativeModuleLoader::LookupAndCompile() to a MaybeLocal since the caller has to take care of the result being empty anyway. This patch reverts the previous design of having the NativeModuleLoader::Compile() method magically know about the parameters of the function - until we have tooling in-place to guess the parameter names in the source with some annotation, it's more readable to allow the caller to specify the parameters along with the arguments values. PR-URL: https://github.com/nodejs/node/pull/24660 Reviewed-By: Refael Ackermann Reviewed-By: Gus Caplan --- lib/internal/per_context.js | 64 ++++++++++----------- src/node.cc | 19 ++++--- src/node_native_module.cc | 107 ++++++++++++++++++++---------------- src/node_native_module.h | 54 ++++++++++++++---- 4 files changed, 144 insertions(+), 100 deletions(-) diff --git a/lib/internal/per_context.js b/lib/internal/per_context.js index ddf874c7eac656..87fc8d247eb933 100644 --- a/lib/internal/per_context.js +++ b/lib/internal/per_context.js @@ -1,44 +1,44 @@ +// arguments: global + 'use strict'; // node::NewContext calls this script -(function(global) { - // https://github.com/nodejs/node/issues/14909 - if (global.Intl) delete global.Intl.v8BreakIterator; +// https://github.com/nodejs/node/issues/14909 +if (global.Intl) delete global.Intl.v8BreakIterator; - // https://github.com/nodejs/node/issues/21219 - // Adds Atomics.notify and warns on first usage of Atomics.wake - // https://github.com/v8/v8/commit/c79206b363 adds Atomics.notify so - // now we alias Atomics.wake to notify so that we can remove it - // semver major without worrying about V8. +// https://github.com/nodejs/node/issues/21219 +// Adds Atomics.notify and warns on first usage of Atomics.wake +// https://github.com/v8/v8/commit/c79206b363 adds Atomics.notify so +// now we alias Atomics.wake to notify so that we can remove it +// semver major without worrying about V8. - const AtomicsNotify = global.Atomics.notify; - const ReflectApply = global.Reflect.apply; +const AtomicsNotify = global.Atomics.notify; +const ReflectApply = global.Reflect.apply; - const warning = 'Atomics.wake will be removed in a future version, ' + - 'use Atomics.notify instead.'; +const warning = 'Atomics.wake will be removed in a future version, ' + + 'use Atomics.notify instead.'; - let wakeWarned = false; - function wake(typedArray, index, count) { - if (!wakeWarned) { - wakeWarned = true; +let wakeWarned = false; +function wake(typedArray, index, count) { + if (!wakeWarned) { + wakeWarned = true; - if (global.process !== undefined) { - global.process.emitWarning(warning, 'Atomics'); - } else { - global.console.error(`Atomics: ${warning}`); - } + if (global.process !== undefined) { + global.process.emitWarning(warning, 'Atomics'); + } else { + global.console.error(`Atomics: ${warning}`); } - - return ReflectApply(AtomicsNotify, this, arguments); } - global.Object.defineProperties(global.Atomics, { - wake: { - value: wake, - writable: true, - enumerable: false, - configurable: true, - }, - }); -}(this)); + return ReflectApply(AtomicsNotify, this, arguments); +} + +global.Object.defineProperties(global.Atomics, { + wake: { + value: wake, + writable: true, + enumerable: false, + configurable: true, + }, +}); diff --git a/src/node.cc b/src/node.cc index 425c1f875dd983..e01baf9726a45e 100644 --- a/src/node.cc +++ b/src/node.cc @@ -162,7 +162,6 @@ using v8::ObjectTemplate; using v8::PropertyAttribute; using v8::ReadOnly; using v8::Script; -using v8::ScriptCompiler; using v8::ScriptOrigin; using v8::SealHandleScope; using v8::SideEffectType; @@ -2500,14 +2499,16 @@ Local NewContext(Isolate* isolate, // Run lib/internal/per_context.js Context::Scope context_scope(context); - // TODO(joyeecheung): use NativeModuleLoader::Compile - Local per_context = - per_process_loader.GetSource(isolate, "internal/per_context"); - ScriptCompiler::Source per_context_src(per_context, nullptr); - Local