Skip to content

Commit e6a939d

Browse files
committed
more comments
1 parent 574b6f7 commit e6a939d

File tree

6 files changed

+35
-45
lines changed

6 files changed

+35
-45
lines changed

doc/api/vm.md

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,11 @@ added: v0.3.1
179179
**Default:** `'VM Context i'`, where `i` is an ascending numerical index of
180180
the created context.
181181
* `contextOrigin` {string} [Origin][origin] corresponding to the newly
182-
created context for display purposes. **Default:** `''`.
182+
created context for display purposes. The origin should be formatted like a
183+
URL, but with only the scheme, host, and port (if necessary), like the
184+
value of the [`url.origin`][] property of a [`URL`][] object. Most notably,
185+
this string should omit the trailing slash, as that denotes a path.
186+
**Default:** `''`.
183187

184188
First contextifies the given `sandbox`, runs the compiled code contained by
185189
the `vm.Script` object within the created sandbox, and returns the result.
@@ -373,7 +377,11 @@ added: v0.3.1
373377
**Default:** `'VM Context i'`, where `i` is an ascending numerical index of
374378
the created context.
375379
* `contextOrigin` {string} [Origin][origin] corresponding to the newly
376-
created context for display purposes. **Default:** `''`.
380+
created context for display purposes. The origin should be formatted like a
381+
URL, but with only the scheme, host, and port (if necessary), like the
382+
value of the [`url.origin`][] property of a [`URL`][] object. Most notably,
383+
this string should omit the trailing slash, as that denotes a path.
384+
**Default:** `''`.
377385

378386
The `vm.runInNewContext()` first contextifies the given `sandbox` object (or
379387
creates a new `sandbox` if passed as `undefined`), compiles the `code`, runs it
@@ -499,10 +507,12 @@ associating it with the `sandbox` object is what this document refers to as
499507
"contextifying" the `sandbox`.
500508

501509
[`Error`]: errors.html#errors_class_error
510+
[`URL`]: url.html#url_class_url
502511
[`eval()`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/eval
503512
[`script.runInContext()`]: #vm_script_runincontext_contextifiedsandbox_options
504513
[`script.runInThisContext()`]: #vm_script_runinthiscontext_options
505-
[`vm.createContext()`]: #vm_vm_createcontext_sandbox
514+
[`url.origin`]: https://nodejs.org/api/url.html#url_url_origin
515+
[`vm.createContext()`]: #vm_vm_createcontext_sandbox_options
506516
[`vm.runInContext()`]: #vm_vm_runincontext_code_contextifiedsandbox_options
507517
[`vm.runInThisContext()`]: #vm_vm_runinthiscontext_code_options
508518
[V8 Embedder's Guide]: https://github.com/v8/v8/wiki/Embedder's%20Guide#contexts

lib/vm.js

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,11 @@ Script.prototype.runInContext = function(contextifiedSandbox, options) {
7575
};
7676

7777
Script.prototype.runInNewContext = function(sandbox, options) {
78+
const context = createContext(sandbox, getContextOptions(options));
79+
return this.runInContext(context, options);
80+
};
81+
82+
function getContextOptions(options) {
7883
const contextOptions = options ? {
7984
name: options.contextName,
8085
origin: options.contextOrigin
@@ -89,9 +94,8 @@ Script.prototype.runInNewContext = function(sandbox, options) {
8994
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'options.contextOrigin',
9095
'string', contextOptions.origin);
9196
}
92-
const context = createContext(sandbox, contextOptions);
93-
return this.runInContext(context, options);
94-
};
97+
return contextOptions;
98+
}
9599

96100
let defaultContextNameIndex = 1;
97101
function createContext(sandbox, options) {
@@ -170,21 +174,7 @@ function runInNewContext(code, sandbox, options) {
170174
if (typeof options === 'string') {
171175
options = { filename: options };
172176
}
173-
const contextOptions = options ? {
174-
name: options.contextName,
175-
origin: options.contextOrigin
176-
} : {};
177-
if (contextOptions.name !== undefined &&
178-
typeof contextOptions.name !== 'string') {
179-
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'options.contextName',
180-
'string', contextOptions.name);
181-
}
182-
if (contextOptions.origin !== undefined &&
183-
typeof contextOptions.origin !== 'string') {
184-
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'options.contextOrigin',
185-
'string', contextOptions.origin);
186-
}
187-
sandbox = createContext(sandbox, contextOptions);
177+
sandbox = createContext(sandbox, getContextOptions(options));
188178
options = Object.assign({}, options, {
189179
[kParsingContext]: sandbox
190180
});

src/env-inl.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@ inline Environment::Environment(IsolateData* isolate_data,
296296

297297
set_module_load_list_array(v8::Array::New(isolate()));
298298

299-
AssignToContext(context, ContextInfo(v8::String::Empty(isolate())));
299+
AssignToContext(context, ContextInfo(""));
300300

301301
destroy_async_id_list_.reserve(512);
302302
performance_state_ = Calloc<performance::performance_state>(1);

src/env.h

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -361,11 +361,9 @@ class IsolateData {
361361
};
362362

363363
struct ContextInfo {
364-
explicit ContextInfo(v8::Local<v8::String> name) : name(name) {
365-
CHECK(!name.IsEmpty());
366-
}
367-
v8::Local<v8::String> name;
368-
v8::Local<v8::String> origin;
364+
explicit ContextInfo(std::string name) : name(name) {}
365+
std::string name;
366+
std::string origin;
369367
bool is_default = false;
370368
};
371369

src/inspector_agent.cc

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ using v8::Function;
2929
using v8::HandleScope;
3030
using v8::Isolate;
3131
using v8::Local;
32-
using v8::NewStringType;
3332
using v8::Object;
3433
using v8::Persistent;
3534
using v8::String;
@@ -55,9 +54,8 @@ class StartIoTask : public v8::Task {
5554
Agent* agent;
5655
};
5756

58-
template <typename T>
5957
std::unique_ptr<StringBuffer> ToProtocolString(Isolate* isolate,
60-
Local<T> value) {
58+
Local<Value> value) {
6159
TwoByteValue buffer(isolate, value);
6260
return StringBuffer::create(StringView(*buffer, buffer.length()));
6361
}
@@ -307,10 +305,7 @@ class NodeInspectorClient : public V8InspectorClient {
307305
running_nested_loop_(false) {
308306
client_ = V8Inspector::create(env->isolate(), this);
309307
// TODO(bnoordhuis) Make name configurable from src/node.cc.
310-
ContextInfo info(
311-
String::NewFromUtf8(env->isolate(),
312-
GetHumanReadableProcessName().c_str(),
313-
NewStringType::kNormal).ToLocalChecked());
308+
ContextInfo info(GetHumanReadableProcessName());
314309
info.is_default = true;
315310
contextCreated(env->context(), info);
316311
}
@@ -343,17 +338,13 @@ class NodeInspectorClient : public V8InspectorClient {
343338
}
344339

345340
void contextCreated(Local<Context> context, const ContextInfo& info) {
346-
std::unique_ptr<StringBuffer> name_buffer =
347-
ToProtocolString(env_->isolate(), info.name);
348-
std::unique_ptr<StringBuffer> origin_buffer;
341+
auto name_buffer = Utf8ToStringView(info.name);
342+
auto origin_buffer = Utf8ToStringView(info.origin);
349343
std::unique_ptr<StringBuffer> aux_data_buffer;
350344

351345
v8_inspector::V8ContextInfo v8info(
352346
context, CONTEXT_GROUP_ID, name_buffer->string());
353-
if (!info.origin.IsEmpty()) {
354-
origin_buffer = ToProtocolString(env_->isolate(), info.origin);
355-
v8info.origin = origin_buffer->string();
356-
}
347+
v8info.origin = origin_buffer->string();
357348

358349
if (info.is_default) {
359350
aux_data_buffer = Utf8ToStringView("{\"isDefault\":true}");

src/node_contextify.cc

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -210,20 +210,21 @@ class ContextifyContext {
210210
ctx->Global());
211211

212212
Local<Value> name =
213-
options_obj->Get(env->context(),
214-
FIXED_ONE_BYTE_STRING(env->isolate(), "name"))
213+
options_obj->Get(env->context(), env->name_string())
215214
.ToLocalChecked();
216215
CHECK(name->IsString());
216+
Utf8Value name_val(env->isolate(), name);
217217

218-
ContextInfo info(name.As<String>());
218+
ContextInfo info(*name_val);
219219

220220
Local<Value> origin =
221221
options_obj->Get(env->context(),
222222
FIXED_ONE_BYTE_STRING(env->isolate(), "origin"))
223223
.ToLocalChecked();
224224
if (!origin->IsUndefined()) {
225225
CHECK(origin->IsString());
226-
info.origin = origin.As<String>();
226+
Utf8Value origin_val(env->isolate(), origin);
227+
info.origin = *origin_val;
227228
}
228229

229230
env->AssignToContext(ctx, info);

0 commit comments

Comments
 (0)