2017-01-03 21:16:48 +00:00
|
|
|
// Copyright Joyent, Inc. and other Node contributors.
|
|
|
|
//
|
|
|
|
// Permission is hereby granted, free of charge, to any person obtaining a
|
|
|
|
// copy of this software and associated documentation files (the
|
|
|
|
// "Software"), to deal in the Software without restriction, including
|
|
|
|
// without limitation the rights to use, copy, modify, merge, publish,
|
|
|
|
// distribute, sublicense, and/or sell copies of the Software, and to permit
|
|
|
|
// persons to whom the Software is furnished to do so, subject to the
|
|
|
|
// following conditions:
|
|
|
|
//
|
|
|
|
// The above copyright notice and this permission notice shall be included
|
|
|
|
// in all copies or substantial portions of the Software.
|
|
|
|
//
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
|
|
|
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
|
|
|
|
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
|
|
|
|
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
|
|
|
|
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
|
|
|
|
// USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
|
2014-11-22 15:59:48 +00:00
|
|
|
'use strict';
|
|
|
|
|
2019-11-22 17:04:46 +00:00
|
|
|
const {
|
|
|
|
ArrayPrototypeForEach,
|
2024-02-01 11:45:42 +00:00
|
|
|
ObjectFreeze,
|
2020-11-07 09:27:46 +00:00
|
|
|
PromiseReject,
|
|
|
|
ReflectApply,
|
2024-04-21 16:53:08 +00:00
|
|
|
Symbol,
|
2019-11-22 17:04:46 +00:00
|
|
|
} = primordials;
|
2019-04-05 09:11:26 +00:00
|
|
|
|
2017-07-17 05:06:23 +00:00
|
|
|
const {
|
2017-11-29 19:55:43 +00:00
|
|
|
ContextifyScript,
|
2017-07-17 05:06:23 +00:00
|
|
|
makeContext,
|
2020-02-07 17:49:43 +00:00
|
|
|
constants,
|
|
|
|
measureMemory: _measureMemory,
|
2018-08-20 20:42:08 +00:00
|
|
|
} = internalBinding('contextify');
|
2018-08-17 22:26:34 +00:00
|
|
|
const {
|
2020-02-07 17:49:43 +00:00
|
|
|
ERR_CONTEXT_NOT_INITIALIZED,
|
2018-08-17 22:26:34 +00:00
|
|
|
ERR_INVALID_ARG_TYPE,
|
|
|
|
} = require('internal/errors').codes;
|
2018-12-16 07:39:35 +00:00
|
|
|
const {
|
2023-10-11 02:50:50 +00:00
|
|
|
validateArray,
|
2020-01-23 18:55:53 +00:00
|
|
|
validateBoolean,
|
2023-02-06 15:44:04 +00:00
|
|
|
validateBuffer,
|
2021-05-16 11:22:48 +00:00
|
|
|
validateInt32,
|
2020-06-26 10:14:48 +00:00
|
|
|
validateOneOf,
|
2023-10-11 02:50:50 +00:00
|
|
|
validateObject,
|
2021-05-16 11:22:48 +00:00
|
|
|
validateString,
|
2023-10-11 02:50:50 +00:00
|
|
|
validateStringArray,
|
2021-05-16 11:22:48 +00:00
|
|
|
validateUint32,
|
2024-02-16 08:12:35 +00:00
|
|
|
kValidateObjectAllowArray,
|
2023-10-11 02:50:50 +00:00
|
|
|
kValidateObjectAllowNullable,
|
2018-12-16 07:39:35 +00:00
|
|
|
} = require('internal/validators');
|
2020-02-07 17:49:43 +00:00
|
|
|
const {
|
|
|
|
emitExperimentalWarning,
|
2022-05-21 09:57:12 +00:00
|
|
|
kEmptyObject,
|
|
|
|
kVmBreakFirstLineSymbol,
|
2020-02-07 17:49:43 +00:00
|
|
|
} = require('internal/util');
|
2022-09-26 16:55:59 +00:00
|
|
|
const {
|
2023-10-05 01:27:11 +00:00
|
|
|
getHostDefinedOptionId,
|
2022-09-26 16:55:59 +00:00
|
|
|
internalCompileFunction,
|
2024-02-16 08:12:35 +00:00
|
|
|
isContext: _isContext,
|
2023-10-11 02:50:50 +00:00
|
|
|
registerImportModuleDynamically,
|
2022-09-26 16:55:59 +00:00
|
|
|
} = require('internal/vm');
|
2024-02-01 11:45:42 +00:00
|
|
|
const {
|
|
|
|
vm_dynamic_import_main_context_default,
|
2024-08-29 09:05:03 +00:00
|
|
|
vm_context_no_contextify,
|
2024-02-01 11:45:42 +00:00
|
|
|
} = internalBinding('symbols');
|
2018-07-30 20:11:46 +00:00
|
|
|
const kParsingContext = Symbol('script parsing context');
|
vm, core, module: re-do vm to fix known issues
As documented in #3042 and in [1], the existing vm implementation has
many problems. All of these are solved by @brianmcd's [contextify][2]
package. This commit uses contextify as a conceptual base and its code
core to overhaul the vm module and fix its many edge cases and caveats.
Functionally, this fixes #3042. In particular:
- A context is now indistinguishable from the object it is based on
(the "sandbox"). A context is simply a sandbox that has been marked
by the vm module, via `vm.createContext`, with special internal
information that allows scripts to be run inside of it.
- Consequently, items added to the context from anywhere are
immediately visible to all code that can access that context, both
inside and outside the virtual machine.
This commit also smooths over the API very slightly:
- Parameter defaults are now uniformly triggered via `undefined`, per
ES6 semantics and previous discussion at [3].
- Several undocumented and problematic features have been removed, e.g.
the conflation of `vm.Script` with `vm` itself, and the fact that
`Script` instances also had all static `vm` methods. The API is now
exactly as documented (although arguably the existence of the
`vm.Script` export is not yet documented, just the `Script` class
itself).
In terms of implementation, this replaces node_script.cc with
node_contextify.cc, which is derived originally from [4] (see [5]) but
has since undergone extensive modifications and iterations to expose
the most useful C++ API and use the coding conventions and utilities of
Node core.
The bindings exposed by `process.binding('contextify')`
(node_contextify.cc) replace those formerly exposed by
`process.binding('evals')` (node_script.cc). They are:
- ContextifyScript(code, [filename]), with methods:
- runInThisContext()
- runInContext(sandbox, [timeout])
- makeContext(sandbox)
From this, the vm.js file builds the entire documented vm module API.
node.js and module.js were modified to use this new native binding, or
the vm module itself where possible. This introduces an extra line or
two into the stack traces of module compilation (and thus into most
stack traces), explaining the changed tests.
The tests were also updated slightly, with all vm-related simple tests
consolidated as test/simple/test-vm-* (some of them were formerly
test/simple/test-script-*). At the same time they switched from
`common.debug` to `console.error` and were updated to use
`assert.throws` instead of rolling their own error-testing methods.
New tests were also added, of course, demonstrating the new
capabilities and fixes.
[1]: http://nodejs.org/docs/v0.10.16/api/vm.html#vm_caveats
[2]: https://github.com/brianmcd/contextify
[3]: https://github.com/joyent/node/issues/5323#issuecomment-20250726
[4]: https://github.com/kkoopa/contextify/blob/bf123f3ef960f0943d1e30bda02e3163a004e964/src/contextify.cc
[5]: https://gist.github.com/domenic/6068120
2013-07-27 04:34:12 +00:00
|
|
|
|
2024-02-16 08:12:35 +00:00
|
|
|
/**
|
|
|
|
* Check if object is a context object created by vm.createContext().
|
|
|
|
* @throws {TypeError} If object is not an object in the first place, throws TypeError.
|
|
|
|
* @param {object} object Object to check.
|
|
|
|
* @returns {boolean}
|
|
|
|
*/
|
|
|
|
function isContext(object) {
|
|
|
|
validateObject(object, 'object', kValidateObjectAllowArray);
|
|
|
|
|
|
|
|
return _isContext(object);
|
|
|
|
}
|
|
|
|
|
2017-11-29 19:55:43 +00:00
|
|
|
class Script extends ContextifyScript {
|
2022-05-21 09:57:12 +00:00
|
|
|
constructor(code, options = kEmptyObject) {
|
2018-03-16 14:23:39 +00:00
|
|
|
code = `${code}`;
|
|
|
|
if (typeof options === 'string') {
|
|
|
|
options = { filename: options };
|
2021-01-21 11:03:00 +00:00
|
|
|
} else {
|
|
|
|
validateObject(options, 'options');
|
2018-03-16 14:23:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const {
|
|
|
|
filename = 'evalmachine.<anonymous>',
|
|
|
|
lineOffset = 0,
|
|
|
|
columnOffset = 0,
|
|
|
|
cachedData,
|
|
|
|
produceCachedData = false,
|
2018-08-17 22:26:34 +00:00
|
|
|
importModuleDynamically,
|
|
|
|
[kParsingContext]: parsingContext,
|
2018-03-16 14:23:39 +00:00
|
|
|
} = options;
|
|
|
|
|
2018-12-16 07:39:35 +00:00
|
|
|
validateString(filename, 'options.filename');
|
2018-04-15 09:16:50 +00:00
|
|
|
validateInt32(lineOffset, 'options.lineOffset');
|
|
|
|
validateInt32(columnOffset, 'options.columnOffset');
|
2023-02-06 15:44:04 +00:00
|
|
|
if (cachedData !== undefined) {
|
|
|
|
validateBuffer(cachedData, 'options.cachedData');
|
2018-03-16 14:23:39 +00:00
|
|
|
}
|
2021-01-18 11:39:25 +00:00
|
|
|
validateBoolean(produceCachedData, 'options.produceCachedData');
|
2018-03-16 14:23:39 +00:00
|
|
|
|
2023-10-05 01:27:11 +00:00
|
|
|
const hostDefinedOptionId =
|
|
|
|
getHostDefinedOptionId(importModuleDynamically, filename);
|
2017-11-29 19:55:43 +00:00
|
|
|
// 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.
|
2018-12-27 02:05:21 +00:00
|
|
|
try { // eslint-disable-line no-useless-catch
|
2018-03-16 14:23:39 +00:00
|
|
|
super(code,
|
|
|
|
filename,
|
|
|
|
lineOffset,
|
|
|
|
columnOffset,
|
|
|
|
cachedData,
|
|
|
|
produceCachedData,
|
2023-10-05 00:11:04 +00:00
|
|
|
parsingContext,
|
2023-10-05 01:27:11 +00:00
|
|
|
hostDefinedOptionId);
|
2017-11-29 19:55:43 +00:00
|
|
|
} catch (e) {
|
|
|
|
throw e; /* node-do-not-add-exception-line */
|
|
|
|
}
|
2018-08-17 22:26:34 +00:00
|
|
|
|
2024-02-01 11:45:42 +00:00
|
|
|
registerImportModuleDynamically(this, importModuleDynamically);
|
2017-11-29 19:55:43 +00:00
|
|
|
}
|
|
|
|
|
2018-03-16 14:23:39 +00:00
|
|
|
runInThisContext(options) {
|
2022-07-11 08:08:41 +00:00
|
|
|
const { breakOnSigint, args } = getRunInContextArgs(null, options);
|
2018-03-16 14:23:39 +00:00
|
|
|
if (breakOnSigint && process.listenerCount('SIGINT') > 0) {
|
2022-07-11 08:08:41 +00:00
|
|
|
return sigintHandlersWrap(super.runInContext, this, args);
|
2018-03-16 14:23:39 +00:00
|
|
|
}
|
2022-07-11 08:08:41 +00:00
|
|
|
return ReflectApply(super.runInContext, this, args);
|
2018-03-16 14:23:39 +00:00
|
|
|
}
|
2016-05-08 01:28:47 +00:00
|
|
|
|
2019-12-22 03:08:36 +00:00
|
|
|
runInContext(contextifiedObject, options) {
|
|
|
|
validateContext(contextifiedObject);
|
2022-07-11 08:08:41 +00:00
|
|
|
const { breakOnSigint, args } = getRunInContextArgs(
|
|
|
|
contextifiedObject,
|
|
|
|
options,
|
|
|
|
);
|
2018-03-16 14:23:39 +00:00
|
|
|
if (breakOnSigint && process.listenerCount('SIGINT') > 0) {
|
2020-12-31 12:12:33 +00:00
|
|
|
return sigintHandlersWrap(super.runInContext, this, args);
|
2018-03-16 14:23:39 +00:00
|
|
|
}
|
2020-12-31 12:12:33 +00:00
|
|
|
return ReflectApply(super.runInContext, this, args);
|
2016-05-08 01:28:47 +00:00
|
|
|
}
|
|
|
|
|
2019-12-22 03:08:36 +00:00
|
|
|
runInNewContext(contextObject, options) {
|
|
|
|
const context = createContext(contextObject, getContextOptions(options));
|
2018-03-16 14:23:39 +00:00
|
|
|
return this.runInContext(context, options);
|
2016-05-08 01:28:47 +00:00
|
|
|
}
|
2018-03-16 14:23:39 +00:00
|
|
|
}
|
2016-05-08 01:28:47 +00:00
|
|
|
|
2019-12-22 03:08:36 +00:00
|
|
|
function validateContext(contextifiedObject) {
|
2020-01-23 18:55:53 +00:00
|
|
|
if (!isContext(contextifiedObject)) {
|
2019-12-22 03:08:36 +00:00
|
|
|
throw new ERR_INVALID_ARG_TYPE('contextifiedObject', 'vm.Context',
|
|
|
|
contextifiedObject);
|
2018-03-16 14:23:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-11 08:08:41 +00:00
|
|
|
function getRunInContextArgs(contextifiedObject, options = kEmptyObject) {
|
2020-01-23 18:55:53 +00:00
|
|
|
validateObject(options, 'options');
|
2018-03-16 14:23:39 +00:00
|
|
|
|
|
|
|
let timeout = options.timeout;
|
|
|
|
if (timeout === undefined) {
|
|
|
|
timeout = -1;
|
2018-04-15 09:16:50 +00:00
|
|
|
} else {
|
|
|
|
validateUint32(timeout, 'options.timeout', true);
|
2018-03-16 14:23:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const {
|
|
|
|
displayErrors = true,
|
2019-01-30 22:13:45 +00:00
|
|
|
breakOnSigint = false,
|
|
|
|
[kVmBreakFirstLineSymbol]: breakFirstLine = false,
|
2018-03-16 14:23:39 +00:00
|
|
|
} = options;
|
|
|
|
|
2020-01-23 18:55:53 +00:00
|
|
|
validateBoolean(displayErrors, 'options.displayErrors');
|
|
|
|
validateBoolean(breakOnSigint, 'options.breakOnSigint');
|
2018-03-16 14:23:39 +00:00
|
|
|
|
|
|
|
return {
|
|
|
|
breakOnSigint,
|
2022-07-11 08:08:41 +00:00
|
|
|
args: [
|
|
|
|
contextifiedObject,
|
|
|
|
timeout,
|
|
|
|
displayErrors,
|
|
|
|
breakOnSigint,
|
|
|
|
breakFirstLine,
|
|
|
|
],
|
2018-03-16 14:23:39 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2017-12-17 21:38:15 +00:00
|
|
|
function getContextOptions(options) {
|
2020-01-23 18:55:53 +00:00
|
|
|
if (!options)
|
|
|
|
return {};
|
|
|
|
const contextOptions = {
|
|
|
|
name: options.contextName,
|
|
|
|
origin: options.contextOrigin,
|
|
|
|
codeGeneration: undefined,
|
2020-06-22 22:33:04 +00:00
|
|
|
microtaskMode: options.microtaskMode,
|
2020-01-23 18:55:53 +00:00
|
|
|
};
|
|
|
|
if (contextOptions.name !== undefined)
|
|
|
|
validateString(contextOptions.name, 'options.contextName');
|
|
|
|
if (contextOptions.origin !== undefined)
|
|
|
|
validateString(contextOptions.origin, 'options.contextOrigin');
|
|
|
|
if (options.contextCodeGeneration !== undefined) {
|
2018-02-26 20:29:41 +00:00
|
|
|
validateObject(options.contextCodeGeneration,
|
|
|
|
'options.contextCodeGeneration');
|
2020-01-23 18:55:53 +00:00
|
|
|
const { strings, wasm } = options.contextCodeGeneration;
|
|
|
|
if (strings !== undefined)
|
|
|
|
validateBoolean(strings, 'options.contextCodeGeneration.strings');
|
|
|
|
if (wasm !== undefined)
|
|
|
|
validateBoolean(wasm, 'options.contextCodeGeneration.wasm');
|
|
|
|
contextOptions.codeGeneration = { strings, wasm };
|
2017-12-17 21:38:15 +00:00
|
|
|
}
|
2020-06-22 22:33:04 +00:00
|
|
|
if (options.microtaskMode !== undefined)
|
|
|
|
validateString(options.microtaskMode, 'options.microtaskMode');
|
2020-01-23 18:55:53 +00:00
|
|
|
return contextOptions;
|
2017-12-17 21:38:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
let defaultContextNameIndex = 1;
|
2022-05-21 09:57:12 +00:00
|
|
|
function createContext(contextObject = {}, options = kEmptyObject) {
|
2024-08-29 09:05:03 +00:00
|
|
|
if (contextObject !== vm_context_no_contextify && isContext(contextObject)) {
|
2019-12-22 03:08:36 +00:00
|
|
|
return contextObject;
|
2012-02-28 01:19:12 +00:00
|
|
|
}
|
|
|
|
|
2020-01-23 18:55:53 +00:00
|
|
|
validateObject(options, 'options');
|
2018-03-16 14:23:39 +00:00
|
|
|
|
|
|
|
const {
|
|
|
|
name = `VM Context ${defaultContextNameIndex++}`,
|
|
|
|
origin,
|
2020-06-22 22:33:04 +00:00
|
|
|
codeGeneration,
|
2023-02-12 18:26:21 +00:00
|
|
|
microtaskMode,
|
2023-11-01 06:05:13 +00:00
|
|
|
importModuleDynamically,
|
2018-03-16 14:23:39 +00:00
|
|
|
} = options;
|
|
|
|
|
2018-12-16 07:39:35 +00:00
|
|
|
validateString(name, 'options.name');
|
|
|
|
if (origin !== undefined)
|
|
|
|
validateString(origin, 'options.origin');
|
2020-01-23 18:55:53 +00:00
|
|
|
if (codeGeneration !== undefined)
|
|
|
|
validateObject(codeGeneration, 'options.codeGeneration');
|
2018-03-16 14:23:39 +00:00
|
|
|
|
|
|
|
let strings = true;
|
|
|
|
let wasm = true;
|
|
|
|
if (codeGeneration !== undefined) {
|
|
|
|
({ strings = true, wasm = true } = codeGeneration);
|
2020-01-23 18:55:53 +00:00
|
|
|
validateBoolean(strings, 'options.codeGeneration.strings');
|
|
|
|
validateBoolean(wasm, 'options.codeGeneration.wasm');
|
2018-03-16 14:23:39 +00:00
|
|
|
}
|
|
|
|
|
2023-04-28 12:31:18 +00:00
|
|
|
validateOneOf(microtaskMode,
|
|
|
|
'options.microtaskMode',
|
|
|
|
['afterEvaluate', undefined]);
|
2023-08-16 20:18:46 +00:00
|
|
|
const microtaskQueue = (microtaskMode === 'afterEvaluate');
|
2020-06-22 22:33:04 +00:00
|
|
|
|
2023-11-01 06:05:13 +00:00
|
|
|
const hostDefinedOptionId =
|
|
|
|
getHostDefinedOptionId(importModuleDynamically, name);
|
|
|
|
|
2024-08-29 09:05:03 +00:00
|
|
|
const result = makeContext(contextObject, name, origin, strings, wasm, microtaskQueue, hostDefinedOptionId);
|
2023-11-01 06:05:13 +00:00
|
|
|
// Register the context scope callback after the context was initialized.
|
2024-08-29 09:05:03 +00:00
|
|
|
registerImportModuleDynamically(result, importModuleDynamically);
|
|
|
|
return result;
|
2017-02-15 05:02:50 +00:00
|
|
|
}
|
2013-08-24 19:45:02 +00:00
|
|
|
|
2017-02-15 05:02:50 +00:00
|
|
|
function createScript(code, options) {
|
|
|
|
return new Script(code, options);
|
|
|
|
}
|
2016-05-08 01:28:47 +00:00
|
|
|
|
|
|
|
// Remove all SIGINT listeners and re-attach them after the wrapped function
|
|
|
|
// has executed, so that caught SIGINT are handled by the listeners again.
|
2017-01-15 03:43:33 +00:00
|
|
|
function sigintHandlersWrap(fn, thisArg, argsArray) {
|
2017-11-25 18:26:28 +00:00
|
|
|
const sigintListeners = process.rawListeners('SIGINT');
|
2016-05-08 01:28:47 +00:00
|
|
|
|
|
|
|
process.removeAllListeners('SIGINT');
|
|
|
|
|
|
|
|
try {
|
2020-11-07 09:27:46 +00:00
|
|
|
return ReflectApply(fn, thisArg, argsArray);
|
2016-05-08 01:28:47 +00:00
|
|
|
} finally {
|
|
|
|
// Add using the public methods so that the `newListener` handler of
|
|
|
|
// process can re-attach the listeners.
|
2020-12-31 12:12:33 +00:00
|
|
|
ArrayPrototypeForEach(sigintListeners, (listener) => {
|
2016-05-08 01:28:47 +00:00
|
|
|
process.addListener('SIGINT', listener);
|
2020-12-31 12:12:33 +00:00
|
|
|
});
|
2016-05-08 01:28:47 +00:00
|
|
|
}
|
|
|
|
}
|
2017-02-15 05:02:50 +00:00
|
|
|
|
2019-12-22 03:08:36 +00:00
|
|
|
function runInContext(code, contextifiedObject, options) {
|
|
|
|
validateContext(contextifiedObject);
|
2017-07-17 05:06:23 +00:00
|
|
|
if (typeof options === 'string') {
|
|
|
|
options = {
|
|
|
|
filename: options,
|
2023-02-12 18:26:21 +00:00
|
|
|
[kParsingContext]: contextifiedObject,
|
2017-07-17 05:06:23 +00:00
|
|
|
};
|
|
|
|
} else {
|
2019-12-22 03:08:36 +00:00
|
|
|
options = { ...options, [kParsingContext]: contextifiedObject };
|
2017-07-17 05:06:23 +00:00
|
|
|
}
|
2017-02-15 05:02:50 +00:00
|
|
|
return createScript(code, options)
|
2019-12-22 03:08:36 +00:00
|
|
|
.runInContext(contextifiedObject, options);
|
2017-02-15 05:02:50 +00:00
|
|
|
}
|
|
|
|
|
2019-12-22 03:08:36 +00:00
|
|
|
function runInNewContext(code, contextObject, options) {
|
2017-07-17 05:06:23 +00:00
|
|
|
if (typeof options === 'string') {
|
2017-12-17 21:38:15 +00:00
|
|
|
options = { filename: options };
|
2017-07-17 05:06:23 +00:00
|
|
|
}
|
2019-12-22 03:08:36 +00:00
|
|
|
contextObject = createContext(contextObject, getContextOptions(options));
|
|
|
|
options = { ...options, [kParsingContext]: contextObject };
|
|
|
|
return createScript(code, options).runInNewContext(contextObject, options);
|
2017-02-15 05:02:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function runInThisContext(code, options) {
|
2018-03-16 14:23:39 +00:00
|
|
|
if (typeof options === 'string') {
|
|
|
|
options = { filename: options };
|
|
|
|
}
|
2017-02-15 05:02:50 +00:00
|
|
|
return createScript(code, options).runInThisContext(options);
|
|
|
|
}
|
|
|
|
|
2022-05-21 09:57:12 +00:00
|
|
|
function compileFunction(code, params, options = kEmptyObject) {
|
2023-10-11 02:50:50 +00:00
|
|
|
validateString(code, 'code');
|
|
|
|
if (params !== undefined) {
|
|
|
|
validateStringArray(params, 'params');
|
|
|
|
}
|
|
|
|
const {
|
|
|
|
filename = '',
|
|
|
|
columnOffset = 0,
|
|
|
|
lineOffset = 0,
|
|
|
|
cachedData = undefined,
|
|
|
|
produceCachedData = false,
|
|
|
|
parsingContext = undefined,
|
|
|
|
contextExtensions = [],
|
|
|
|
importModuleDynamically,
|
|
|
|
} = options;
|
|
|
|
|
|
|
|
validateString(filename, 'options.filename');
|
|
|
|
validateInt32(columnOffset, 'options.columnOffset');
|
|
|
|
validateInt32(lineOffset, 'options.lineOffset');
|
|
|
|
if (cachedData !== undefined)
|
|
|
|
validateBuffer(cachedData, 'options.cachedData');
|
|
|
|
validateBoolean(produceCachedData, 'options.produceCachedData');
|
|
|
|
if (parsingContext !== undefined) {
|
|
|
|
if (
|
|
|
|
typeof parsingContext !== 'object' ||
|
|
|
|
parsingContext === null ||
|
|
|
|
!isContext(parsingContext)
|
|
|
|
) {
|
|
|
|
throw new ERR_INVALID_ARG_TYPE(
|
|
|
|
'options.parsingContext',
|
|
|
|
'Context',
|
|
|
|
parsingContext,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
validateArray(contextExtensions, 'options.contextExtensions');
|
|
|
|
ArrayPrototypeForEach(contextExtensions, (extension, i) => {
|
|
|
|
const name = `options.contextExtensions[${i}]`;
|
|
|
|
validateObject(extension, name, kValidateObjectAllowNullable);
|
|
|
|
});
|
|
|
|
|
|
|
|
const hostDefinedOptionId =
|
|
|
|
getHostDefinedOptionId(importModuleDynamically, filename);
|
|
|
|
|
|
|
|
return internalCompileFunction(
|
|
|
|
code, filename, lineOffset, columnOffset,
|
|
|
|
cachedData, produceCachedData, parsingContext, contextExtensions,
|
|
|
|
params, hostDefinedOptionId, importModuleDynamically,
|
|
|
|
).function;
|
2018-06-28 06:35:19 +00:00
|
|
|
}
|
|
|
|
|
2020-02-07 17:49:43 +00:00
|
|
|
const measureMemoryModes = {
|
|
|
|
summary: constants.measureMemory.mode.SUMMARY,
|
|
|
|
detailed: constants.measureMemory.mode.DETAILED,
|
|
|
|
};
|
|
|
|
|
2020-04-22 02:28:20 +00:00
|
|
|
const measureMemoryExecutions = {
|
|
|
|
default: constants.measureMemory.execution.DEFAULT,
|
|
|
|
eager: constants.measureMemory.execution.EAGER,
|
|
|
|
};
|
|
|
|
|
2022-05-21 09:57:12 +00:00
|
|
|
function measureMemory(options = kEmptyObject) {
|
2020-02-07 17:49:43 +00:00
|
|
|
emitExperimentalWarning('vm.measureMemory');
|
|
|
|
validateObject(options, 'options');
|
2020-04-22 02:28:20 +00:00
|
|
|
const { mode = 'summary', execution = 'default' } = options;
|
2020-06-26 10:14:48 +00:00
|
|
|
validateOneOf(mode, 'options.mode', ['summary', 'detailed']);
|
|
|
|
validateOneOf(execution, 'options.execution', ['default', 'eager']);
|
2020-04-22 02:28:20 +00:00
|
|
|
const result = _measureMemory(measureMemoryModes[mode],
|
|
|
|
measureMemoryExecutions[execution]);
|
2020-02-07 17:49:43 +00:00
|
|
|
if (result === undefined) {
|
|
|
|
return PromiseReject(new ERR_CONTEXT_NOT_INITIALIZED());
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
2018-06-28 06:35:19 +00:00
|
|
|
|
2024-02-01 11:45:42 +00:00
|
|
|
const vmConstants = {
|
|
|
|
__proto__: null,
|
|
|
|
USE_MAIN_CONTEXT_DEFAULT_LOADER: vm_dynamic_import_main_context_default,
|
2024-08-29 09:05:03 +00:00
|
|
|
DONT_CONTEXTIFY: vm_context_no_contextify,
|
2024-02-01 11:45:42 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
ObjectFreeze(vmConstants);
|
|
|
|
|
2017-02-15 05:02:50 +00:00
|
|
|
module.exports = {
|
|
|
|
Script,
|
|
|
|
createContext,
|
|
|
|
createScript,
|
|
|
|
runInContext,
|
|
|
|
runInNewContext,
|
|
|
|
runInThisContext,
|
2018-01-14 07:35:51 +00:00
|
|
|
isContext,
|
2018-06-28 06:35:19 +00:00
|
|
|
compileFunction,
|
2020-02-07 17:49:43 +00:00
|
|
|
measureMemory,
|
2024-02-01 11:45:42 +00:00
|
|
|
constants: vmConstants,
|
2017-02-15 05:02:50 +00:00
|
|
|
};
|
2018-01-14 07:35:51 +00:00
|
|
|
|
2021-05-13 16:58:19 +00:00
|
|
|
// The vm module is patched to include vm.Module, vm.SourceTextModule
|
|
|
|
// and vm.SyntheticModule in the pre-execution phase when
|
|
|
|
// --experimental-vm-modules is on.
|