Skip to content
37 changes: 13 additions & 24 deletions lib/internal/bootstrap/loaders.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -120,7 +120,6 @@
this.exportKeys = undefined;
this.loaded = false;
this.loading = false;
this.script = null; // The ContextifyScript of the module
Comment thread
ryzokuken marked this conversation as resolved.
Outdated
}

NativeModule._source = getInternalBinding('natives');
Expand Down Expand Up @@ -220,15 +219,6 @@
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) :
Expand All @@ -237,8 +227,7 @@

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;

Expand Down Expand Up @@ -270,29 +259,29 @@
const cache = codeCacheHash[id] &&
(codeCacheHash[id] === sourceHash[id]) ? codeCache[id] : undefined;

Comment thread
ryzokuken marked this conversation as resolved.
// (code, filename, lineOffset, columnOffset
// cachedData, produceCachedData, parsingContext)
const script = new ContextifyScript(
source, this.filename, 0, 0,
cache, false, undefined
const fn = compileFunction(
source,
this.filename,
0,
0,
cache,
false,
undefined,
[],
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

☝️ I think contextExtensions can be undefined too.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@jdalton I used an empty array since that's the default value I used in vm.compileFunction and wanted to make this as consistent as I could. I just rechecked the code to make sure, the two should be functionally similar, resulting in an empty std::vector<Local<Object>> context_extensions.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

My point was more reducing throw away object creation.

['exports', 'require', 'module', 'process', 'internalBinding']
Comment thread
ryzokuken marked this conversation as resolved.
Outdated
Comment thread
ryzokuken marked this conversation as resolved.
Outdated
);

// 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) {
Comment thread
ryzokuken marked this conversation as resolved.
Outdated
if (!cache) {
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;
Expand Down