2022-09-26 16:55:59 +00:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const {
|
2023-10-11 02:50:50 +00:00
|
|
|
ReflectApply,
|
2023-10-05 01:27:11 +00:00
|
|
|
Symbol,
|
2022-09-26 16:55:59 +00:00
|
|
|
} = primordials;
|
|
|
|
|
|
|
|
const {
|
2023-10-11 02:50:50 +00:00
|
|
|
ContextifyScript,
|
2022-09-26 16:55:59 +00:00
|
|
|
compileFunction,
|
|
|
|
isContext: _isContext,
|
|
|
|
} = internalBinding('contextify');
|
2023-10-11 02:50:50 +00:00
|
|
|
const {
|
|
|
|
runInContext,
|
|
|
|
} = ContextifyScript.prototype;
|
2023-10-05 01:27:11 +00:00
|
|
|
const {
|
|
|
|
default_host_defined_options,
|
2023-10-05 21:08:44 +00:00
|
|
|
vm_dynamic_import_missing_flag,
|
2023-10-05 01:27:11 +00:00
|
|
|
} = internalBinding('symbols');
|
2022-09-26 16:55:59 +00:00
|
|
|
const {
|
|
|
|
validateFunction,
|
|
|
|
validateObject,
|
2023-10-02 12:56:39 +00:00
|
|
|
kValidateObjectAllowArray,
|
2022-09-26 16:55:59 +00:00
|
|
|
} = require('internal/validators');
|
|
|
|
|
2023-10-05 21:08:44 +00:00
|
|
|
const {
|
|
|
|
getOptionValue,
|
|
|
|
} = require('internal/options');
|
|
|
|
|
|
|
|
|
2022-09-26 16:55:59 +00:00
|
|
|
function isContext(object) {
|
2023-10-02 12:56:39 +00:00
|
|
|
validateObject(object, 'object', kValidateObjectAllowArray);
|
2022-09-26 16:55:59 +00:00
|
|
|
|
|
|
|
return _isContext(object);
|
|
|
|
}
|
|
|
|
|
2023-11-01 06:05:13 +00:00
|
|
|
function getHostDefinedOptionId(importModuleDynamically, hint) {
|
2023-10-05 01:27:11 +00:00
|
|
|
if (importModuleDynamically !== undefined) {
|
|
|
|
// Check that it's either undefined or a function before we pass
|
|
|
|
// it into the native constructor.
|
|
|
|
validateFunction(importModuleDynamically,
|
|
|
|
'options.importModuleDynamically');
|
|
|
|
}
|
|
|
|
if (importModuleDynamically === undefined) {
|
|
|
|
// We need a default host defined options that are the same for all
|
|
|
|
// scripts not needing custom module callbacks so that the isolate
|
|
|
|
// compilation cache can be hit.
|
|
|
|
return default_host_defined_options;
|
|
|
|
}
|
2023-10-05 21:08:44 +00:00
|
|
|
// We should've thrown here immediately when we introduced
|
|
|
|
// --experimental-vm-modules and importModuleDynamically, but since
|
|
|
|
// users are already using this callback to throw a similar error,
|
|
|
|
// we also defer the error to the time when an actual import() is called
|
|
|
|
// to avoid breaking them. To ensure that the isolate compilation
|
|
|
|
// cache can still be hit, use a constant sentinel symbol here.
|
|
|
|
if (!getOptionValue('--experimental-vm-modules')) {
|
|
|
|
return vm_dynamic_import_missing_flag;
|
|
|
|
}
|
|
|
|
|
2023-11-01 06:05:13 +00:00
|
|
|
return Symbol(hint);
|
2023-10-05 01:27:11 +00:00
|
|
|
}
|
|
|
|
|
2023-10-11 02:50:50 +00:00
|
|
|
function registerImportModuleDynamically(referrer, importModuleDynamically) {
|
|
|
|
const { importModuleDynamicallyWrap } = require('internal/vm/module');
|
|
|
|
const { registerModule } = require('internal/modules/esm/utils');
|
|
|
|
registerModule(referrer, {
|
|
|
|
__proto__: null,
|
|
|
|
importModuleDynamically:
|
|
|
|
importModuleDynamicallyWrap(importModuleDynamically),
|
2022-09-26 16:55:59 +00:00
|
|
|
});
|
2023-10-11 02:50:50 +00:00
|
|
|
}
|
2022-09-26 16:55:59 +00:00
|
|
|
|
2023-10-11 02:50:50 +00:00
|
|
|
function internalCompileFunction(
|
|
|
|
code, filename, lineOffset, columnOffset,
|
|
|
|
cachedData, produceCachedData, parsingContext, contextExtensions,
|
|
|
|
params, hostDefinedOptionId, importModuleDynamically) {
|
2022-09-26 16:55:59 +00:00
|
|
|
const result = compileFunction(
|
|
|
|
code,
|
|
|
|
filename,
|
|
|
|
lineOffset,
|
|
|
|
columnOffset,
|
|
|
|
cachedData,
|
|
|
|
produceCachedData,
|
|
|
|
parsingContext,
|
|
|
|
contextExtensions,
|
2023-02-14 17:45:16 +00:00
|
|
|
params,
|
2023-10-05 01:27:11 +00:00
|
|
|
hostDefinedOptionId,
|
2022-09-26 16:55:59 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
if (produceCachedData) {
|
|
|
|
result.function.cachedDataProduced = result.cachedDataProduced;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (result.cachedData) {
|
|
|
|
result.function.cachedData = result.cachedData;
|
|
|
|
}
|
|
|
|
|
2023-01-25 20:06:55 +00:00
|
|
|
if (typeof result.cachedDataRejected === 'boolean') {
|
|
|
|
result.function.cachedDataRejected = result.cachedDataRejected;
|
|
|
|
}
|
|
|
|
|
2022-09-26 16:55:59 +00:00
|
|
|
if (importModuleDynamically !== undefined) {
|
2023-10-11 02:50:50 +00:00
|
|
|
registerImportModuleDynamically(result.function, importModuleDynamically);
|
2022-09-26 16:55:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2023-10-11 02:50:50 +00:00
|
|
|
function makeContextifyScript(code,
|
|
|
|
filename,
|
|
|
|
lineOffset,
|
|
|
|
columnOffset,
|
|
|
|
cachedData,
|
|
|
|
produceCachedData,
|
|
|
|
parsingContext,
|
|
|
|
hostDefinedOptionId,
|
|
|
|
importModuleDynamically) {
|
|
|
|
let script;
|
|
|
|
// Calling `ReThrow()` on a native TryCatch does not generate a new
|
|
|
|
// abort-on-uncaught-exception check. A dummy try/catch in JS land
|
|
|
|
// protects against that.
|
|
|
|
try { // eslint-disable-line no-useless-catch
|
|
|
|
script = new ContextifyScript(code,
|
|
|
|
filename,
|
|
|
|
lineOffset,
|
|
|
|
columnOffset,
|
|
|
|
cachedData,
|
|
|
|
produceCachedData,
|
|
|
|
parsingContext,
|
|
|
|
hostDefinedOptionId);
|
|
|
|
} catch (e) {
|
|
|
|
throw e; /* node-do-not-add-exception-line */
|
|
|
|
}
|
|
|
|
|
|
|
|
if (importModuleDynamically !== undefined) {
|
|
|
|
registerImportModuleDynamically(script, importModuleDynamically);
|
|
|
|
}
|
|
|
|
return script;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Internal version of vm.Script.prototype.runInThisContext() which skips
|
|
|
|
// argument validation.
|
|
|
|
function runScriptInThisContext(script, displayErrors, breakOnFirstLine) {
|
|
|
|
return ReflectApply(
|
|
|
|
runInContext,
|
|
|
|
script,
|
|
|
|
[
|
|
|
|
null, // sandbox - use current context
|
|
|
|
-1, // timeout
|
|
|
|
displayErrors, // displayErrors
|
|
|
|
false, // breakOnSigint
|
|
|
|
breakOnFirstLine, // breakOnFirstLine
|
|
|
|
],
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-09-26 16:55:59 +00:00
|
|
|
module.exports = {
|
2023-10-05 01:27:11 +00:00
|
|
|
getHostDefinedOptionId,
|
2022-09-26 16:55:59 +00:00
|
|
|
internalCompileFunction,
|
|
|
|
isContext,
|
2023-10-11 02:50:50 +00:00
|
|
|
makeContextifyScript,
|
|
|
|
registerImportModuleDynamically,
|
|
|
|
runScriptInThisContext,
|
2022-09-26 16:55:59 +00:00
|
|
|
};
|