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

typings: lib/internal/vm.js #50112

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 58 additions & 2 deletions lib/internal/vm.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,23 @@ const {
},
} = internalBinding('util');

/**
* Checks if the given object is a context object.
GeoffreyBooth marked this conversation as resolved.
Show resolved Hide resolved
* @param {object} object - The object to check.
* @returns {boolean} - Returns true if the object is a context object, else false.
*/
function isContext(object) {
return object[contextify_context_private_symbol] !== undefined;
}

/**
* Retrieves the host-defined option ID based on the provided importModuleDynamically and hint.
GeoffreyBooth marked this conversation as resolved.
Show resolved Hide resolved
* @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
GeoffreyBooth marked this conversation as resolved.
Show resolved Hide resolved
GeoffreyBooth marked this conversation as resolved.
Show resolved Hide resolved
* ID.
*/
function getHostDefinedOptionId(importModuleDynamically, hint) {
if (importModuleDynamically === vm_dynamic_import_main_context_default ||
importModuleDynamically === vm_dynamic_import_default_internal) {
Expand Down Expand Up @@ -66,6 +79,12 @@ function getHostDefinedOptionId(importModuleDynamically, hint) {
return Symbol(hint);
}

/**
* Registers a dynamically imported module for customization.
GeoffreyBooth marked this conversation as resolved.
Show resolved Hide resolved
* @param {string} referrer - The path of the referrer module.
* @param {import('internal/modules/esm/utils').ImportModuleDynamicallyCallback} importModuleDynamically - The
GeoffreyBooth marked this conversation as resolved.
Show resolved Hide resolved
* dynamically imported module function to be registered.
*/
function registerImportModuleDynamically(referrer, importModuleDynamically) {
// If it's undefined or certain known symbol, there's no customization so
// no need to register anything.
Expand All @@ -83,6 +102,25 @@ function registerImportModuleDynamically(referrer, importModuleDynamically) {
});
}

/**
* 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.
GeoffreyBooth marked this conversation as resolved.
Show resolved Hide resolved
* @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] -
GeoffreyBooth marked this conversation as resolved.
Show resolved Hide resolved
* 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.
*/
function internalCompileFunction(
code, filename, lineOffset, columnOffset,
cachedData, produceCachedData, parsingContext, contextExtensions,
Expand Down Expand Up @@ -117,6 +155,19 @@ function internalCompileFunction(
return result;
}

/**
* 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.
GeoffreyBooth marked this conversation as resolved.
Show resolved Hide resolved
* @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.
GeoffreyBooth marked this conversation as resolved.
Show resolved Hide resolved
* @param {boolean} importModuleDynamically - Indicates whether to import modules dynamically.
GeoffreyBooth marked this conversation as resolved.
Show resolved Hide resolved
* @returns {ContextifyScript} The created contextify script.
*/
function makeContextifyScript(code,
filename,
lineOffset,
Expand Down Expand Up @@ -147,8 +198,13 @@ function makeContextifyScript(code,
return script;
}

// Internal version of vm.Script.prototype.runInThisContext() which skips
// argument validation.
/**
* 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.
*/
function runScriptInThisContext(script, displayErrors, breakOnFirstLine) {
return ReflectApply(
runInContext,
Expand Down