diff --git a/lib/internal/bootstrap/loaders.js b/lib/internal/bootstrap/loaders.js index dff6ce661a4640..f3779cce7f4113 100644 --- a/lib/internal/bootstrap/loaders.js +++ b/lib/internal/bootstrap/loaders.js @@ -109,7 +109,7 @@ // Create this WeakMap in js-land because V8 has no C++ API for WeakMap internalBinding('module_wrap').callbackMap = new WeakMap(); - const { ContextifyScript } = internalBinding('contextify'); + const { compileFunction } = internalBinding('contextify'); // Set up NativeModule function NativeModule(id) { @@ -120,7 +120,6 @@ this.exportKeys = undefined; this.loaded = false; this.loading = false; - this.script = null; // The ContextifyScript of the module } NativeModule._source = getInternalBinding('natives'); @@ -220,25 +219,23 @@ return NativeModule._source[id]; }; - NativeModule.wrap = function(script) { - return NativeModule.wrapper[0] + script + NativeModule.wrapper[1]; - }; - - NativeModule.wrapper = [ - '(function (exports, require, module, process, internalBinding) {', - '\n});' - ]; - const getOwn = (target, property, receiver) => { return ReflectApply(ObjectHasOwnProperty, target, [property]) ? ReflectGet(target, property, receiver) : undefined; }; + NativeModule.parameters = Object.freeze([ + 'exports', + 'require', + 'module', + 'process', + 'internalBinding' + ]); + NativeModule.prototype.compile = function() { const id = this.id; - let source = NativeModule.getSource(id); - source = NativeModule.wrap(source); + const source = NativeModule.getSource(id); this.loading = true; @@ -270,29 +267,30 @@ const cache = codeCacheHash[id] && (codeCacheHash[id] === sourceHash[id]) ? codeCache[id] : undefined; - // (code, filename, lineOffset, columnOffset - // cachedData, produceCachedData, parsingContext) - const script = new ContextifyScript( - source, this.filename, 0, 0, - cache, false, undefined + // TODO(@joyee): figure out way to expose wrapped source for code cache + const fn = compileFunction( + source, // source code + this.filename, // filename + 0, // line offset + 0, // column offset + cache, // cached data + false, // produce cached data + undefined, // parsing context + undefined, // context extensions + NativeModule.parameters // arguments to the function ); - // This will be used to create code cache in tools/generate_code_cache.js - this.script = script; - // One of these conditions may be false when any of the inputs // of the `node_js2c` target in node.gyp is modified. // FIXME(joyeecheung): Figure out how to resolve the dependency issue. // When the code cache was introduced we were at a point where refactoring // node.gyp may not be worth the effort. - if (!cache || script.cachedDataRejected) { + if (!cache || fn.cachedDataRejected) { compiledWithoutCache.push(this.id); } else { compiledWithCache.push(this.id); } - // Arguments: timeout, displayErrors, breakOnSigint - const fn = script.runInThisContext(-1, true, false); const requireFn = this.id.startsWith('internal/deps/') ? NativeModule.requireForDeps : NativeModule.require; diff --git a/src/node_contextify.cc b/src/node_contextify.cc index 3fbc616fda0284..4f824b7ea77868 100644 --- a/src/node_contextify.cc +++ b/src/node_contextify.cc @@ -1099,12 +1099,23 @@ void ContextifyContext::CompileFunction( env->cached_data_string(), buf.ToLocalChecked()).IsNothing()) return; } + + // Report if cache was produced. if (fun->Set( parsing_context, env->cached_data_produced_string(), Boolean::New(isolate, cached_data_produced)).IsNothing()) return; } + // Report if cache was rejected. + if (options == ScriptCompiler::kConsumeCodeCache) { + if (fun->Set(parsing_context, + env->cached_data_rejected_string(), + Boolean::New(isolate, source.GetCachedData()->rejected)) + .IsNothing()) + return; + } + args.GetReturnValue().Set(fun); }