From 10ad01950ecca8d006041e99e1e34a88294daf26 Mon Sep 17 00:00:00 2001 From: Timothy Gu Date: Sun, 17 Dec 2017 13:38:15 -0800 Subject: [PATCH] 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 Reviewed-By: Jon Moss Reviewed-By: James M Snell --- doc/api/vm.md | 38 +++++- lib/vm.js | 65 +++++++++-- src/env-inl.h | 7 +- src/env.h | 11 +- src/inspector_agent.cc | 34 ++++-- src/inspector_agent.h | 4 +- src/node_contextify.cc | 36 +++++- test/sequential/test-inspector-contexts.js | 128 +++++++++++++++++---- 8 files changed, 264 insertions(+), 59 deletions(-) diff --git a/doc/api/vm.md b/doc/api/vm.md index 609ee6b895c91a..00a311da7f9198 100644 --- a/doc/api/vm.md +++ b/doc/api/vm.md @@ -175,6 +175,15 @@ added: v0.3.1 * `timeout` {number} Specifies the number of milliseconds to execute `code` before terminating execution. If execution is terminated, an [`Error`][] will be thrown. + * `contextName` {string} Human-readable name of the newly created context. + **Default:** `'VM Context i'`, where `i` is an ascending numerical index of + the created context. + * `contextOrigin` {string} [Origin][origin] corresponding to the newly + created context for display purposes. The origin should be formatted like a + URL, but with only the scheme, host, and port (if necessary), like the + value of the [`url.origin`][] property of a [`URL`][] object. Most notably, + this string should omit the trailing slash, as that denotes a path. + **Default:** `''`. First contextifies the given `sandbox`, runs the compiled code contained by the `vm.Script` object within the created sandbox, and returns the result. @@ -242,12 +251,22 @@ console.log(globalVar); // 1000 ``` -## vm.createContext([sandbox]) +## vm.createContext([sandbox[, options]]) * `sandbox` {Object} +* `options` {Object} + * `name` {string} Human-readable name of the newly created context. + **Default:** `'VM Context i'`, where `i` is an ascending numerical index of + the created context. + * `origin` {string} [Origin][origin] corresponding to the newly created + context for display purposes. The origin should be formatted like a URL, + but with only the scheme, host, and port (if necessary), like the value of + the [`url.origin`][] property of a [`URL`][] object. Most notably, this + string should omit the trailing slash, as that denotes a path. + **Default:** `''`. If given a `sandbox` object, the `vm.createContext()` method will [prepare that sandbox][contextified] so that it can be used in calls to @@ -282,6 +301,9 @@ web browser, the method can be used to create a single sandbox representing a window's global object, then run all `