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,
|
2020-12-31 12:12:33 +00:00
|
|
|
ArrayPrototypeUnshift,
|
2019-11-30 15:55:29 +00:00
|
|
|
Symbol,
|
2020-11-07 09:27:46 +00:00
|
|
|
PromiseReject,
|
|
|
|
ReflectApply,
|
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,
|
2020-06-22 22:33:04 +00:00
|
|
|
MicrotaskQueue,
|
2017-07-17 05:06:23 +00:00
|
|
|
makeContext,
|
2018-03-02 06:11:55 +00:00
|
|
|
isContext: _isContext,
|
2020-02-07 17:49:43 +00:00
|
|
|
constants,
|
|
|
|
compileFunction: _compileFunction,
|
|
|
|
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;
|
2019-06-27 23:34:53 +00:00
|
|
|
const {
|
|
|
|
isArrayBufferView,
|
|
|
|
} = require('internal/util/types');
|
2018-12-16 07:39:35 +00:00
|
|
|
const {
|
|
|
|
validateInt32,
|
|
|
|
validateUint32,
|
2020-01-23 18:55:53 +00:00
|
|
|
validateString,
|
|
|
|
validateArray,
|
|
|
|
validateBoolean,
|
|
|
|
validateBuffer,
|
|
|
|
validateObject,
|
2020-06-26 10:14:48 +00:00
|
|
|
validateOneOf,
|
2018-12-16 07:39:35 +00:00
|
|
|
} = require('internal/validators');
|
2020-02-07 17:49:43 +00:00
|
|
|
const {
|
|
|
|
kVmBreakFirstLineSymbol,
|
|
|
|
emitExperimentalWarning,
|
|
|
|
} = require('internal/util');
|
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
|
|
|
|
2017-11-29 19:55:43 +00:00
|
|
|
class Script extends ContextifyScript {
|
2018-03-16 14:23:39 +00:00
|
|
|
constructor(code, options = {}) {
|
|
|
|
code = `${code}`;
|
|
|
|
if (typeof options === 'string') {
|
|
|
|
options = { filename: options };
|
2018-12-15 03:12:37 +00:00
|
|
|
} else if (typeof options !== 'object' || options === null) {
|
2018-03-16 14:23:39 +00:00
|
|
|
throw new ERR_INVALID_ARG_TYPE('options', 'Object', options);
|
|
|
|
}
|
|
|
|
|
|
|
|
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');
|
2018-09-18 05:28:41 +00:00
|
|
|
if (cachedData !== undefined && !isArrayBufferView(cachedData)) {
|
|
|
|
throw new ERR_INVALID_ARG_TYPE(
|
|
|
|
'options.cachedData',
|
|
|
|
['Buffer', 'TypedArray', 'DataView'],
|
|
|
|
cachedData
|
|
|
|
);
|
2018-03-16 14:23:39 +00:00
|
|
|
}
|
|
|
|
if (typeof produceCachedData !== 'boolean') {
|
|
|
|
throw new ERR_INVALID_ARG_TYPE('options.produceCachedData', 'boolean',
|
|
|
|
produceCachedData);
|
|
|
|
}
|
|
|
|
|
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,
|
|
|
|
parsingContext);
|
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
|
|
|
|
|
|
|
if (importModuleDynamically !== undefined) {
|
|
|
|
if (typeof importModuleDynamically !== 'function') {
|
|
|
|
throw new ERR_INVALID_ARG_TYPE('options.importModuleDynamically',
|
|
|
|
'function',
|
|
|
|
importModuleDynamically);
|
|
|
|
}
|
2019-09-09 17:20:16 +00:00
|
|
|
const { importModuleDynamicallyWrap } =
|
2019-10-05 03:08:00 +00:00
|
|
|
require('internal/vm/module');
|
2019-04-20 13:45:18 +00:00
|
|
|
const { callbackMap } = internalBinding('module_wrap');
|
2019-09-09 17:20:16 +00:00
|
|
|
callbackMap.set(this, {
|
|
|
|
importModuleDynamically:
|
|
|
|
importModuleDynamicallyWrap(importModuleDynamically),
|
|
|
|
});
|
2018-08-17 22:26:34 +00:00
|
|
|
}
|
2017-11-29 19:55:43 +00:00
|
|
|
}
|
|
|
|
|
2018-03-16 14:23:39 +00:00
|
|
|
runInThisContext(options) {
|
|
|
|
const { breakOnSigint, args } = getRunInContextArgs(options);
|
|
|
|
if (breakOnSigint && process.listenerCount('SIGINT') > 0) {
|
|
|
|
return sigintHandlersWrap(super.runInThisContext, this, args);
|
|
|
|
}
|
2020-12-31 12:12:33 +00:00
|
|
|
return ReflectApply(super.runInThisContext, 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);
|
2018-03-16 14:23:39 +00:00
|
|
|
const { breakOnSigint, args } = getRunInContextArgs(options);
|
2020-12-31 12:12:33 +00:00
|
|
|
ArrayPrototypeUnshift(args, contextifiedObject);
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function getRunInContextArgs(options = {}) {
|
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,
|
2019-01-30 22:13:45 +00:00
|
|
|
args: [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
|
|
|
}
|
|
|
|
|
2019-12-22 03:08:36 +00:00
|
|
|
function isContext(object) {
|
|
|
|
if (typeof object !== 'object' || object === null) {
|
|
|
|
throw new ERR_INVALID_ARG_TYPE('object', 'Object', object);
|
2018-03-02 06:11:55 +00:00
|
|
|
}
|
2019-12-22 03:08:36 +00:00
|
|
|
return _isContext(object);
|
2018-03-02 06:11:55 +00:00
|
|
|
}
|
|
|
|
|
2017-12-17 21:38:15 +00:00
|
|
|
let defaultContextNameIndex = 1;
|
2019-12-22 03:08:36 +00:00
|
|
|
function createContext(contextObject = {}, options = {}) {
|
|
|
|
if (isContext(contextObject)) {
|
|
|
|
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,
|
|
|
|
microtaskMode
|
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
|
|
|
}
|
|
|
|
|
2020-06-22 22:33:04 +00:00
|
|
|
let microtaskQueue = null;
|
|
|
|
if (microtaskMode !== undefined) {
|
2020-06-26 10:14:48 +00:00
|
|
|
validateOneOf(microtaskMode, 'options.microtaskMode',
|
|
|
|
['afterEvaluate', undefined]);
|
2020-06-22 22:33:04 +00:00
|
|
|
|
2020-06-26 10:14:48 +00:00
|
|
|
if (microtaskMode === 'afterEvaluate')
|
2020-06-22 22:33:04 +00:00
|
|
|
microtaskQueue = new MicrotaskQueue();
|
|
|
|
}
|
|
|
|
|
|
|
|
makeContext(contextObject, name, origin, strings, wasm, microtaskQueue);
|
2019-12-22 03:08:36 +00:00
|
|
|
return contextObject;
|
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,
|
2019-12-22 03:08:36 +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);
|
|
|
|
}
|
|
|
|
|
2018-06-28 06:35:19 +00:00
|
|
|
function compileFunction(code, params, options = {}) {
|
2018-12-16 07:39:35 +00:00
|
|
|
validateString(code, 'code');
|
2018-06-28 06:35:19 +00:00
|
|
|
if (params !== undefined) {
|
2020-01-23 18:55:53 +00:00
|
|
|
validateArray(params, 'params');
|
2019-11-22 17:04:46 +00:00
|
|
|
ArrayPrototypeForEach(params,
|
|
|
|
(param, i) => validateString(param, `params[${i}]`));
|
2018-06-28 06:35:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const {
|
|
|
|
filename = '',
|
|
|
|
columnOffset = 0,
|
|
|
|
lineOffset = 0,
|
|
|
|
cachedData = undefined,
|
|
|
|
produceCachedData = false,
|
|
|
|
parsingContext = undefined,
|
|
|
|
contextExtensions = [],
|
|
|
|
} = options;
|
|
|
|
|
2018-12-16 07:39:35 +00:00
|
|
|
validateString(filename, 'options.filename');
|
2018-06-28 06:35:19 +00:00
|
|
|
validateUint32(columnOffset, 'options.columnOffset');
|
|
|
|
validateUint32(lineOffset, 'options.lineOffset');
|
2020-01-23 18:55:53 +00:00
|
|
|
if (cachedData !== undefined)
|
|
|
|
validateBuffer(cachedData, 'options.cachedData');
|
|
|
|
validateBoolean(produceCachedData, 'options.produceCachedData');
|
2018-06-28 06:35:19 +00:00
|
|
|
if (parsingContext !== undefined) {
|
|
|
|
if (
|
|
|
|
typeof parsingContext !== 'object' ||
|
|
|
|
parsingContext === null ||
|
|
|
|
!isContext(parsingContext)
|
|
|
|
) {
|
|
|
|
throw new ERR_INVALID_ARG_TYPE(
|
|
|
|
'options.parsingContext',
|
|
|
|
'Context',
|
|
|
|
parsingContext
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2020-01-23 18:55:53 +00:00
|
|
|
validateArray(contextExtensions, 'options.contextExtensions');
|
2019-11-22 17:04:46 +00:00
|
|
|
ArrayPrototypeForEach(contextExtensions, (extension, i) => {
|
2020-01-23 18:55:53 +00:00
|
|
|
const name = `options.contextExtensions[${i}]`;
|
|
|
|
validateObject(extension, name, { nullable: true });
|
2018-06-28 06:35:19 +00:00
|
|
|
});
|
|
|
|
|
2019-07-19 14:30:09 +00:00
|
|
|
const result = _compileFunction(
|
2018-06-28 06:35:19 +00:00
|
|
|
code,
|
|
|
|
filename,
|
|
|
|
lineOffset,
|
|
|
|
columnOffset,
|
|
|
|
cachedData,
|
|
|
|
produceCachedData,
|
|
|
|
parsingContext,
|
|
|
|
contextExtensions,
|
|
|
|
params
|
|
|
|
);
|
2019-07-19 14:30:09 +00:00
|
|
|
|
|
|
|
if (produceCachedData) {
|
|
|
|
result.function.cachedDataProduced = result.cachedDataProduced;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (result.cachedData) {
|
|
|
|
result.function.cachedData = result.cachedData;
|
|
|
|
}
|
|
|
|
|
|
|
|
return result.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,
|
|
|
|
};
|
|
|
|
|
2020-02-07 17:49:43 +00:00
|
|
|
function measureMemory(options = {}) {
|
|
|
|
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
|
|
|
|
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,
|
2017-02-15 05:02:50 +00:00
|
|
|
};
|
2018-01-14 07:35:51 +00:00
|
|
|
|
2018-11-04 20:52:50 +00:00
|
|
|
if (require('internal/options').getOptionValue('--experimental-vm-modules')) {
|
2019-10-05 03:08:00 +00:00
|
|
|
const {
|
|
|
|
Module, SourceTextModule, SyntheticModule,
|
|
|
|
} = require('internal/vm/module');
|
|
|
|
module.exports.Module = Module;
|
2018-07-28 02:21:30 +00:00
|
|
|
module.exports.SourceTextModule = SourceTextModule;
|
2019-10-05 03:08:00 +00:00
|
|
|
module.exports.SyntheticModule = SyntheticModule;
|
2018-07-28 02:21:30 +00:00
|
|
|
}
|