node/lib/vm.js

419 lines
13 KiB
JavaScript
Raw Normal View History

// 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.
'use strict';
const {
ArrayPrototypeForEach,
ObjectFreeze,
PromiseReject,
ReflectApply,
Symbol,
} = primordials;
const {
ContextifyScript,
makeContext,
constants,
measureMemory: _measureMemory,
} = internalBinding('contextify');
const {
ERR_CONTEXT_NOT_INITIALIZED,
ERR_INVALID_ARG_TYPE,
} = require('internal/errors').codes;
const {
validateArray,
validateBoolean,
validateBuffer,
validateInt32,
validateOneOf,
validateObject,
validateString,
validateStringArray,
validateUint32,
kValidateObjectAllowArray,
kValidateObjectAllowNullable,
} = require('internal/validators');
const {
emitExperimentalWarning,
kEmptyObject,
kVmBreakFirstLineSymbol,
} = require('internal/util');
const {
getHostDefinedOptionId,
internalCompileFunction,
isContext: _isContext,
registerImportModuleDynamically,
} = require('internal/vm');
const {
vm_dynamic_import_main_context_default,
vm_context_no_contextify,
} = internalBinding('symbols');
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
/**
* 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);
}
class Script extends ContextifyScript {
constructor(code, options = kEmptyObject) {
code = `${code}`;
if (typeof options === 'string') {
options = { filename: options };
} else {
validateObject(options, 'options');
}
const {
filename = 'evalmachine.<anonymous>',
lineOffset = 0,
columnOffset = 0,
cachedData,
produceCachedData = false,
importModuleDynamically,
[kParsingContext]: parsingContext,
} = options;
validateString(filename, 'options.filename');
validateInt32(lineOffset, 'options.lineOffset');
validateInt32(columnOffset, 'options.columnOffset');
if (cachedData !== undefined) {
validateBuffer(cachedData, 'options.cachedData');
}
validateBoolean(produceCachedData, 'options.produceCachedData');
const hostDefinedOptionId =
getHostDefinedOptionId(importModuleDynamically, filename);
// 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
super(code,
filename,
lineOffset,
columnOffset,
cachedData,
produceCachedData,
parsingContext,
hostDefinedOptionId);
} catch (e) {
throw e; /* node-do-not-add-exception-line */
}
registerImportModuleDynamically(this, importModuleDynamically);
}
runInThisContext(options) {
const { breakOnSigint, args } = getRunInContextArgs(null, options);
if (breakOnSigint && process.listenerCount('SIGINT') > 0) {
return sigintHandlersWrap(super.runInContext, this, args);
}
return ReflectApply(super.runInContext, this, args);
}
runInContext(contextifiedObject, options) {
validateContext(contextifiedObject);
const { breakOnSigint, args } = getRunInContextArgs(
contextifiedObject,
options,
);
if (breakOnSigint && process.listenerCount('SIGINT') > 0) {
return sigintHandlersWrap(super.runInContext, this, args);
}
return ReflectApply(super.runInContext, this, args);
}
runInNewContext(contextObject, options) {
const context = createContext(contextObject, getContextOptions(options));
return this.runInContext(context, options);
}
}
function validateContext(contextifiedObject) {
if (!isContext(contextifiedObject)) {
throw new ERR_INVALID_ARG_TYPE('contextifiedObject', 'vm.Context',
contextifiedObject);
}
}
function getRunInContextArgs(contextifiedObject, options = kEmptyObject) {
validateObject(options, 'options');
let timeout = options.timeout;
if (timeout === undefined) {
timeout = -1;
} else {
validateUint32(timeout, 'options.timeout', true);
}
const {
displayErrors = true,
breakOnSigint = false,
[kVmBreakFirstLineSymbol]: breakFirstLine = false,
} = options;
validateBoolean(displayErrors, 'options.displayErrors');
validateBoolean(breakOnSigint, 'options.breakOnSigint');
return {
breakOnSigint,
args: [
contextifiedObject,
timeout,
displayErrors,
breakOnSigint,
breakFirstLine,
],
};
}
function getContextOptions(options) {
if (!options)
return {};
const contextOptions = {
name: options.contextName,
origin: options.contextOrigin,
codeGeneration: undefined,
microtaskMode: options.microtaskMode,
};
if (contextOptions.name !== undefined)
validateString(contextOptions.name, 'options.contextName');
if (contextOptions.origin !== undefined)
validateString(contextOptions.origin, 'options.contextOrigin');
if (options.contextCodeGeneration !== undefined) {
validateObject(options.contextCodeGeneration,
'options.contextCodeGeneration');
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 };
}
if (options.microtaskMode !== undefined)
validateString(options.microtaskMode, 'options.microtaskMode');
return contextOptions;
}
let defaultContextNameIndex = 1;
function createContext(contextObject = {}, options = kEmptyObject) {
if (contextObject !== vm_context_no_contextify && isContext(contextObject)) {
return contextObject;
}
validateObject(options, 'options');
const {
name = `VM Context ${defaultContextNameIndex++}`,
origin,
codeGeneration,
microtaskMode,
importModuleDynamically,
} = options;
validateString(name, 'options.name');
if (origin !== undefined)
validateString(origin, 'options.origin');
if (codeGeneration !== undefined)
validateObject(codeGeneration, 'options.codeGeneration');
let strings = true;
let wasm = true;
if (codeGeneration !== undefined) {
({ strings = true, wasm = true } = codeGeneration);
validateBoolean(strings, 'options.codeGeneration.strings');
validateBoolean(wasm, 'options.codeGeneration.wasm');
}
validateOneOf(microtaskMode,
'options.microtaskMode',
['afterEvaluate', undefined]);
const microtaskQueue = (microtaskMode === 'afterEvaluate');
const hostDefinedOptionId =
getHostDefinedOptionId(importModuleDynamically, name);
const result = makeContext(contextObject, name, origin, strings, wasm, microtaskQueue, hostDefinedOptionId);
// Register the context scope callback after the context was initialized.
registerImportModuleDynamically(result, importModuleDynamically);
return result;
}
function createScript(code, options) {
return new Script(code, options);
}
// Remove all SIGINT listeners and re-attach them after the wrapped function
// has executed, so that caught SIGINT are handled by the listeners again.
function sigintHandlersWrap(fn, thisArg, argsArray) {
const sigintListeners = process.rawListeners('SIGINT');
process.removeAllListeners('SIGINT');
try {
return ReflectApply(fn, thisArg, argsArray);
} finally {
// Add using the public methods so that the `newListener` handler of
// process can re-attach the listeners.
ArrayPrototypeForEach(sigintListeners, (listener) => {
process.addListener('SIGINT', listener);
});
}
}
function runInContext(code, contextifiedObject, options) {
validateContext(contextifiedObject);
if (typeof options === 'string') {
options = {
filename: options,
[kParsingContext]: contextifiedObject,
};
} else {
options = { ...options, [kParsingContext]: contextifiedObject };
}
return createScript(code, options)
.runInContext(contextifiedObject, options);
}
function runInNewContext(code, contextObject, options) {
if (typeof options === 'string') {
options = { filename: options };
}
contextObject = createContext(contextObject, getContextOptions(options));
options = { ...options, [kParsingContext]: contextObject };
return createScript(code, options).runInNewContext(contextObject, options);
}
function runInThisContext(code, options) {
if (typeof options === 'string') {
options = { filename: options };
}
return createScript(code, options).runInThisContext(options);
}
function compileFunction(code, params, options = kEmptyObject) {
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;
}
const measureMemoryModes = {
summary: constants.measureMemory.mode.SUMMARY,
detailed: constants.measureMemory.mode.DETAILED,
};
const measureMemoryExecutions = {
default: constants.measureMemory.execution.DEFAULT,
eager: constants.measureMemory.execution.EAGER,
};
function measureMemory(options = kEmptyObject) {
emitExperimentalWarning('vm.measureMemory');
validateObject(options, 'options');
const { mode = 'summary', execution = 'default' } = options;
validateOneOf(mode, 'options.mode', ['summary', 'detailed']);
validateOneOf(execution, 'options.execution', ['default', 'eager']);
const result = _measureMemory(measureMemoryModes[mode],
measureMemoryExecutions[execution]);
if (result === undefined) {
return PromiseReject(new ERR_CONTEXT_NOT_INITIALIZED());
}
return result;
}
const vmConstants = {
__proto__: null,
USE_MAIN_CONTEXT_DEFAULT_LOADER: vm_dynamic_import_main_context_default,
DONT_CONTEXTIFY: vm_context_no_contextify,
};
ObjectFreeze(vmConstants);
module.exports = {
Script,
createContext,
createScript,
runInContext,
runInNewContext,
runInThisContext,
isContext,
compileFunction,
measureMemory,
constants: vmConstants,
};
// 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.