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,
|
|
|
|
} = internalBinding('contextify');
|
2023-10-11 02:50:50 +00:00
|
|
|
const {
|
|
|
|
runInContext,
|
|
|
|
} = ContextifyScript.prototype;
|
2023-10-05 01:27:11 +00:00
|
|
|
const {
|
2024-02-01 11:45:42 +00:00
|
|
|
vm_dynamic_import_default_internal,
|
|
|
|
vm_dynamic_import_main_context_default,
|
|
|
|
vm_dynamic_import_no_callback,
|
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,
|
|
|
|
} = require('internal/validators');
|
|
|
|
|
2023-10-05 21:08:44 +00:00
|
|
|
const {
|
|
|
|
getOptionValue,
|
|
|
|
} = require('internal/options');
|
2024-02-16 08:12:35 +00:00
|
|
|
const {
|
|
|
|
privateSymbols: {
|
|
|
|
contextify_context_private_symbol,
|
|
|
|
},
|
|
|
|
} = internalBinding('util');
|
2023-10-05 21:08:44 +00:00
|
|
|
|
2024-02-19 01:19:44 +00:00
|
|
|
/**
|
|
|
|
* Checks if the given object is a context object.
|
|
|
|
* @param {object} object - The object to check.
|
|
|
|
* @returns {boolean} - Returns true if the object is a context object, else false.
|
|
|
|
*/
|
2022-09-26 16:55:59 +00:00
|
|
|
function isContext(object) {
|
2024-02-16 08:12:35 +00:00
|
|
|
return object[contextify_context_private_symbol] !== undefined;
|
2022-09-26 16:55:59 +00:00
|
|
|
}
|
|
|
|
|
2024-02-19 01:19:44 +00:00
|
|
|
/**
|
|
|
|
* Retrieves the host-defined option ID based on the provided importModuleDynamically and hint.
|
|
|
|
* @param {import('internal/modules/esm/utils').ImportModuleDynamicallyCallback | undefined} importModuleDynamically -
|
|
|
|
* The importModuleDynamically function or undefined.
|
|
|
|
* @param {string} hint - The hint for the option ID.
|
|
|
|
* @returns {symbol | import('internal/modules/esm/utils').ImportModuleDynamicallyCallback} - The host-defined option
|
|
|
|
* ID.
|
|
|
|
*/
|
2023-11-01 06:05:13 +00:00
|
|
|
function getHostDefinedOptionId(importModuleDynamically, hint) {
|
2024-02-01 11:45:42 +00:00
|
|
|
if (importModuleDynamically === vm_dynamic_import_main_context_default ||
|
|
|
|
importModuleDynamically === vm_dynamic_import_default_internal) {
|
|
|
|
return importModuleDynamically;
|
|
|
|
}
|
|
|
|
|
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.
|
2024-02-01 11:45:42 +00:00
|
|
|
return vm_dynamic_import_no_callback;
|
2023-10-05 01:27:11 +00:00
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
2024-02-19 01:19:44 +00:00
|
|
|
/**
|
|
|
|
* Registers a dynamically imported module for customization.
|
|
|
|
* @param {string} referrer - The path of the referrer module.
|
|
|
|
* @param {import('internal/modules/esm/utils').ImportModuleDynamicallyCallback} importModuleDynamically - The
|
|
|
|
* dynamically imported module function to be registered.
|
|
|
|
*/
|
2023-10-11 02:50:50 +00:00
|
|
|
function registerImportModuleDynamically(referrer, importModuleDynamically) {
|
2024-02-01 11:45:42 +00:00
|
|
|
// If it's undefined or certain known symbol, there's no customization so
|
|
|
|
// no need to register anything.
|
|
|
|
if (importModuleDynamically === undefined ||
|
|
|
|
importModuleDynamically === vm_dynamic_import_main_context_default ||
|
|
|
|
importModuleDynamically === vm_dynamic_import_default_internal) {
|
|
|
|
return;
|
|
|
|
}
|
2023-10-11 02:50:50 +00:00
|
|
|
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
|
|
|
|
2024-02-19 01:19:44 +00:00
|
|
|
/**
|
|
|
|
* Compiles a function from the given code string.
|
|
|
|
* @param {string} code - The code string to compile.
|
|
|
|
* @param {string} filename - The filename to use for the compiled function.
|
|
|
|
* @param {number} lineOffset - The line offset to use for the compiled function.
|
|
|
|
* @param {number} columnOffset - The column offset to use for the compiled function.
|
|
|
|
* @param {Buffer} [cachedData=undefined] - The cached data to use for the compiled function.
|
|
|
|
* @param {boolean} produceCachedData - Whether to produce cached data for the compiled function.
|
|
|
|
* @param {ReturnType<import('vm').createContext} [parsingContext=undefined] - The parsing context to use for the
|
|
|
|
* compiled function.
|
|
|
|
* @param {object[]} [contextExtensions=[]] - An array of context extensions to use for the compiled function.
|
|
|
|
* @param {string[]} [params] - An optional array of parameter names for the compiled function.
|
|
|
|
* @param {symbol} hostDefinedOptionId - A symbol referenced by the field `host_defined_option_symbol`.
|
|
|
|
* @param {import('internal/modules/esm/utils').ImportModuleDynamicallyCallback} [importModuleDynamically] -
|
|
|
|
* A function to use for dynamically importing modules.
|
|
|
|
* @returns {object} An object containing the compiled function and any associated data.
|
|
|
|
* @throws {TypeError} If any of the arguments are of the wrong type.
|
|
|
|
* @throws {ERR_INVALID_ARG_TYPE} If the parsing context is not a valid context object.
|
|
|
|
*/
|
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;
|
|
|
|
}
|
|
|
|
|
2024-02-01 11:45:42 +00:00
|
|
|
registerImportModuleDynamically(result.function, importModuleDynamically);
|
2022-09-26 16:55:59 +00:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2024-02-19 01:19:44 +00:00
|
|
|
/**
|
|
|
|
* Creates a contextify script.
|
|
|
|
* @param {string} code - The code of the script.
|
|
|
|
* @param {string} filename - The filename of the script.
|
|
|
|
* @param {number} lineOffset - The line offset of the script.
|
|
|
|
* @param {number} columnOffset - The column offset of the script.
|
|
|
|
* @param {Buffer} cachedData - The cached data of the script.
|
|
|
|
* @param {boolean} produceCachedData - Indicates whether to produce cached data.
|
|
|
|
* @param {object} parsingContext - The parsing context of the script.
|
|
|
|
* @param {number} hostDefinedOptionId - The host-defined option ID.
|
|
|
|
* @param {boolean} importModuleDynamically - Indicates whether to import modules dynamically.
|
|
|
|
* @returns {ContextifyScript} The created contextify script.
|
|
|
|
*/
|
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 */
|
|
|
|
}
|
|
|
|
|
2024-02-01 11:45:42 +00:00
|
|
|
registerImportModuleDynamically(script, importModuleDynamically);
|
2023-10-11 02:50:50 +00:00
|
|
|
return script;
|
|
|
|
}
|
|
|
|
|
2024-02-19 01:19:44 +00:00
|
|
|
/**
|
|
|
|
* Runs a script in the current context.
|
|
|
|
* Internal version of `vm.Script.prototype.runInThisContext()` which skips argument validation.
|
|
|
|
* @param {ReturnType<makeContextifyScript>} script - The script to run.
|
|
|
|
* @param {boolean} displayErrors - Whether to display errors.
|
|
|
|
* @param {boolean} breakOnFirstLine - Whether to break on the first line.
|
|
|
|
*/
|
2023-10-11 02:50:50 +00:00
|
|
|
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
|
|
|
};
|