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

lib: modify NativeModule to use compileFunction #23837

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
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
ryzokuken marked this conversation as resolved.
Show resolved Hide resolved
}

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;

ryzokuken marked this conversation as resolved.
Show resolved Hide 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
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
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
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']
ryzokuken marked this conversation as resolved.
Show resolved Hide resolved
ryzokuken marked this conversation as resolved.
Show resolved Hide resolved
);

// 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) {
ryzokuken marked this conversation as resolved.
Show resolved Hide resolved
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