diff options
author | Timothy Gu <timothygu99@gmail.com> | 2017-12-17 13:38:15 -0800 |
---|---|---|
committer | Timothy Gu <timothygu99@gmail.com> | 2017-12-23 14:05:18 +0800 |
commit | 2cb21451627a24be2bdeaee276d47ff94b15539e (patch) | |
tree | 42dc38919994b73d408e0af6eec72a817c98df4e /lib | |
parent | c339931d8b3dbf3fb2a4fa4164bbf585759831c0 (diff) | |
download | android-node-v8-2cb21451627a24be2bdeaee276d47ff94b15539e.tar.gz android-node-v8-2cb21451627a24be2bdeaee276d47ff94b15539e.tar.bz2 android-node-v8-2cb21451627a24be2bdeaee276d47ff94b15539e.zip |
vm: allow modifying context name in inspector
The `auxData` field is not exposed to JavaScript, as DevTools uses it
for its `isDefault` parameter, which is implemented faithfully,
contributing to the nice indentation in the context selection panel.
Without the indentation, when `Target` domain gets implemented (along
with a single Inspector for cluster) in #16627, subprocesses and VM
contexts will be mixed up, causing confusion.
PR-URL: https://github.com/nodejs/node/pull/17720
Refs: https://github.com/nodejs/node/pull/14231#issuecomment-315924067
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: Jon Moss <me@jonathanmoss.me>
Reviewed-By: James M Snell <jasnell@gmail.com>
Diffstat (limited to 'lib')
-rw-r--r-- | lib/vm.js | 65 |
1 files changed, 53 insertions, 12 deletions
@@ -29,6 +29,8 @@ const { isContext, } = process.binding('contextify'); +const errors = require('internal/errors'); + // The binding provides a few useful primitives: // - Script(code, { filename = "evalmachine.anonymous", // displayErrors = true } = {}) @@ -73,18 +75,61 @@ Script.prototype.runInContext = function(contextifiedSandbox, options) { }; Script.prototype.runInNewContext = function(sandbox, options) { - var context = createContext(sandbox); + const context = createContext(sandbox, getContextOptions(options)); return this.runInContext(context, options); }; -function createContext(sandbox) { +function getContextOptions(options) { + const contextOptions = options ? { + name: options.contextName, + origin: options.contextOrigin + } : {}; + if (contextOptions.name !== undefined && + typeof contextOptions.name !== 'string') { + throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'options.contextName', + 'string', contextOptions.name); + } + if (contextOptions.origin !== undefined && + typeof contextOptions.origin !== 'string') { + throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'options.contextOrigin', + 'string', contextOptions.origin); + } + return contextOptions; +} + +let defaultContextNameIndex = 1; +function createContext(sandbox, options) { if (sandbox === undefined) { sandbox = {}; } else if (isContext(sandbox)) { return sandbox; } - makeContext(sandbox); + if (options !== undefined) { + if (typeof options !== 'object' || options === null) { + throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'options', + 'object', options); + } + options = { + name: options.name, + origin: options.origin + }; + if (options.name === undefined) { + options.name = `VM Context ${defaultContextNameIndex++}`; + } else if (typeof options.name !== 'string') { + throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'options.name', + 'string', options.name); + } + if (options.origin !== undefined && typeof options.origin !== 'string') { + throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'options.origin', + 'string', options.origin); + } + } else { + options = { + name: `VM Context ${defaultContextNameIndex++}` + }; + } + makeContext(sandbox, options); return sandbox; } @@ -126,17 +171,13 @@ function runInContext(code, contextifiedSandbox, options) { } function runInNewContext(code, sandbox, options) { - sandbox = createContext(sandbox); if (typeof options === 'string') { - options = { - filename: options, - [kParsingContext]: sandbox - }; - } else { - options = Object.assign({}, options, { - [kParsingContext]: sandbox - }); + options = { filename: options }; } + sandbox = createContext(sandbox, getContextOptions(options)); + options = Object.assign({}, options, { + [kParsingContext]: sandbox + }); return createScript(code, options).runInNewContext(sandbox, options); } |