From d02ad40d42fa4431960e6c4a8cbda1b8cd015c74 Mon Sep 17 00:00:00 2001 From: Anna Henningsen Date: Wed, 30 Jan 2019 23:13:45 +0100 Subject: [PATCH] inspector,vm: remove --eval wrapper Report the actual source code when running with `--eval` and `--inspect-brk`, by telling the vm module to break on the first line of the script being executed rather than wrapping the source code in a function. PR-URL: https://github.com/nodejs/node/pull/25832 Reviewed-By: Eugene Ostroukhov Reviewed-By: Joyee Cheung --- lib/internal/process/execution.js | 30 +++++++++++-------- lib/internal/util.js | 3 +- lib/vm.js | 6 ++-- src/node_contextify.cc | 27 +++++++++++++++-- src/node_contextify.h | 1 + ...spector-async-hook-setup-at-inspect-brk.js | 4 +-- ...spector-async-stack-traces-promise-then.js | 10 +++---- ...spector-async-stack-traces-set-interval.js | 4 +-- test/sequential/test-inspector-break-e.js | 2 +- .../test-inspector-scriptparsed-context.js | 10 +++---- 10 files changed, 63 insertions(+), 34 deletions(-) diff --git a/lib/internal/process/execution.js b/lib/internal/process/execution.js index 3691f8f9189453..7aac90d79d7248 100644 --- a/lib/internal/process/execution.js +++ b/lib/internal/process/execution.js @@ -33,26 +33,30 @@ function tryGetCwd() { } } -function evalScript(name, body, breakFristLine) { +function evalScript(name, body, breakFirstLine) { const CJSModule = require('internal/modules/cjs/loader'); - if (breakFristLine) { - const fn = `function() {\n\n${body};\n\n}`; - body = `process.binding('inspector').callAndPauseOnStart(${fn}, {})`; - } + const { kVmBreakFirstLineSymbol } = require('internal/util'); const cwd = tryGetCwd(); const module = new CJSModule(name); module.filename = path.join(cwd, name); module.paths = CJSModule._nodeModulePaths(cwd); - const script = `global.__filename = ${JSON.stringify(name)};\n` + - 'global.exports = exports;\n' + - 'global.module = module;\n' + - 'global.__dirname = __dirname;\n' + - 'global.require = require;\n' + - 'return require("vm").runInThisContext(' + - `${JSON.stringify(body)}, { filename: ` + - `${JSON.stringify(name)}, displayErrors: true });\n`; + global.kVmBreakFirstLineSymbol = kVmBreakFirstLineSymbol; + const script = ` + global.__filename = ${JSON.stringify(name)}; + global.exports = exports; + global.module = module; + global.__dirname = __dirname; + global.require = require; + const { kVmBreakFirstLineSymbol } = global; + delete global.kVmBreakFirstLineSymbol; + return require("vm").runInThisContext( + ${JSON.stringify(body)}, { + filename: ${JSON.stringify(name)}, + displayErrors: true, + [kVmBreakFirstLineSymbol]: ${!!breakFirstLine} + });\n`; const result = module._compile(script, `${name}-wrapper`); if (require('internal/options').getOptionValue('--print')) { console.log(result); diff --git a/lib/internal/util.js b/lib/internal/util.js index fb3eed6f5e0dde..b3fdff27627fa8 100644 --- a/lib/internal/util.js +++ b/lib/internal/util.js @@ -418,5 +418,6 @@ module.exports = { // Used by the buffer module to capture an internal reference to the // default isEncoding implementation, just in case userland overrides it. kIsEncodingSymbol: Symbol('kIsEncodingSymbol'), - kExpandStackSymbol: Symbol('kExpandStackSymbol') + kExpandStackSymbol: Symbol('kExpandStackSymbol'), + kVmBreakFirstLineSymbol: Symbol('kVmBreakFirstLineSymbol') }; diff --git a/lib/vm.js b/lib/vm.js index 464724071a49ca..b2875d1e1134d8 100644 --- a/lib/vm.js +++ b/lib/vm.js @@ -38,6 +38,7 @@ const { validateUint32, validateString } = require('internal/validators'); +const { kVmBreakFirstLineSymbol } = require('internal/util'); const kParsingContext = Symbol('script parsing context'); const ArrayForEach = Function.call.bind(Array.prototype.forEach); @@ -175,7 +176,8 @@ function getRunInContextArgs(options = {}) { const { displayErrors = true, - breakOnSigint = false + breakOnSigint = false, + [kVmBreakFirstLineSymbol]: breakFirstLine = false, } = options; if (typeof displayErrors !== 'boolean') { @@ -189,7 +191,7 @@ function getRunInContextArgs(options = {}) { return { breakOnSigint, - args: [timeout, displayErrors, breakOnSigint] + args: [timeout, displayErrors, breakOnSigint, breakFirstLine] }; } diff --git a/src/node_contextify.cc b/src/node_contextify.cc index b7c7f38eb6f695..952acfe06f4b85 100644 --- a/src/node_contextify.cc +++ b/src/node_contextify.cc @@ -795,7 +795,9 @@ void ContextifyScript::RunInThisContext( TRACE_EVENT_NESTABLE_ASYNC_BEGIN0( TRACING_CATEGORY_NODE2(vm, script), "RunInThisContext", wrapped_script); - CHECK_EQ(args.Length(), 3); + // TODO(addaleax): Use an options object or otherwise merge this with + // RunInContext(). + CHECK_EQ(args.Length(), 4); CHECK(args[0]->IsNumber()); int64_t timeout = args[0]->IntegerValue(env->context()).FromJust(); @@ -806,8 +808,16 @@ void ContextifyScript::RunInThisContext( CHECK(args[2]->IsBoolean()); bool break_on_sigint = args[2]->IsTrue(); + CHECK(args[3]->IsBoolean()); + bool break_on_first_line = args[3]->IsTrue(); + // Do the eval within this context - EvalMachine(env, timeout, display_errors, break_on_sigint, args); + EvalMachine(env, + timeout, + display_errors, + break_on_sigint, + break_on_first_line, + args); TRACE_EVENT_NESTABLE_ASYNC_END0( TRACING_CATEGORY_NODE2(vm, script), "RunInThisContext", wrapped_script); @@ -819,7 +829,7 @@ void ContextifyScript::RunInContext(const FunctionCallbackInfo& args) { ContextifyScript* wrapped_script; ASSIGN_OR_RETURN_UNWRAP(&wrapped_script, args.Holder()); - CHECK_EQ(args.Length(), 4); + CHECK_EQ(args.Length(), 5); CHECK(args[0]->IsObject()); Local sandbox = args[0].As(); @@ -843,12 +853,16 @@ void ContextifyScript::RunInContext(const FunctionCallbackInfo& args) { CHECK(args[3]->IsBoolean()); bool break_on_sigint = args[3]->IsTrue(); + CHECK(args[4]->IsBoolean()); + bool break_on_first_line = args[4]->IsTrue(); + // Do the eval within the context Context::Scope context_scope(contextify_context->context()); EvalMachine(contextify_context->env(), timeout, display_errors, break_on_sigint, + break_on_first_line, args); TRACE_EVENT_NESTABLE_ASYNC_END0( @@ -859,6 +873,7 @@ bool ContextifyScript::EvalMachine(Environment* env, const int64_t timeout, const bool display_errors, const bool break_on_sigint, + const bool break_on_first_line, const FunctionCallbackInfo& args) { if (!env->can_call_into_js()) return false; @@ -874,6 +889,12 @@ bool ContextifyScript::EvalMachine(Environment* env, PersistentToLocal::Default(env->isolate(), wrapped_script->script_); Local