-
-
Notifications
You must be signed in to change notification settings - Fork 31.7k
lib,src: add source map support for global eval #43428
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
'use strict'; | ||
|
||
const common = require('../common.js'); | ||
|
||
const bench = common.createBenchmark(main, { | ||
method: ['without-sourcemap', 'sourcemap'], | ||
n: [1e6], | ||
}); | ||
|
||
const sourceWithoutSourceMap = ` | ||
'use strict'; | ||
(function() { | ||
let a = 1; | ||
for (let i = 0; i < 1000; i++) { | ||
a++; | ||
} | ||
return a; | ||
})(); | ||
`; | ||
const sourceWithSourceMap = ` | ||
${sourceWithoutSourceMap} | ||
//# sourceMappingURL=https://ci.nodejs.org/405 | ||
`; | ||
|
||
function evalN(n, source) { | ||
bench.start(); | ||
for (let i = 0; i < n; i++) { | ||
eval(source); | ||
} | ||
bench.end(n); | ||
} | ||
|
||
function main({ n, method }) { | ||
switch (method) { | ||
case 'without-sourcemap': | ||
process.setSourceMapsEnabled(false); | ||
evalN(n, sourceWithoutSourceMap); | ||
break; | ||
case 'sourcemap': | ||
process.setSourceMapsEnabled(true); | ||
evalN(n, sourceWithSourceMap); | ||
break; | ||
default: | ||
throw new Error(`Unexpected method "${method}"`); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -454,6 +454,38 @@ void OnFatalError(const char* location, const char* message) { | |
ABORT(); | ||
} | ||
|
||
v8::ModifyCodeGenerationFromStringsResult ModifyCodeGenerationFromStrings( | ||
bcoe marked this conversation as resolved.
Show resolved
Hide resolved
|
||
v8::Local<v8::Context> context, | ||
v8::Local<v8::Value> source, | ||
bool is_code_like) { | ||
HandleScope scope(context->GetIsolate()); | ||
|
||
Environment* env = Environment::GetCurrent(context); | ||
if (env->source_maps_enabled()) { | ||
// We do not expect the maybe_cache_generated_source_map to throw any more | ||
// exceptions. If it does, just ignore it. | ||
errors::TryCatchScope try_catch(env); | ||
Local<Function> maybe_cache_source_map = | ||
env->maybe_cache_generated_source_map(); | ||
Local<Value> argv[1] = {source}; | ||
|
||
MaybeLocal<Value> maybe_cached = maybe_cache_source_map->Call( | ||
context, context->Global(), arraysize(argv), argv); | ||
if (maybe_cached.IsEmpty()) { | ||
DCHECK(try_catch.HasCaught()); | ||
} | ||
} | ||
|
||
Local<Value> allow_code_gen = context->GetEmbedderData( | ||
ContextEmbedderIndex::kAllowCodeGenerationFromStrings); | ||
bool codegen_allowed = | ||
allow_code_gen->IsUndefined() || allow_code_gen->IsTrue(); | ||
return { | ||
codegen_allowed, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If no callback is provided, is There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If the value set in The callback is called only when There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Prior to this, did Any concerns about performance hits we might observe now that we're calling a handler? Perhaps it would be worth adding a benchmark? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @bcoe thank you for your suggestion. I added a new fast-path on if sourcemap is enabled in ModifyCodeGenerationFromStrings and ran a benchmark against the main branch. Now the difference when sourcemap is not enabled can be slight if the dynamic code is not trivial. However, with sourcemap enabled, the difference can be significant. But I suppose this is acceptable since sourcemap comes with costs.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. thanks for adding the benchmark 👌 I'm curious, how much more does it slow down if you have the noop closure, without the fast path?
I think this is to be expected. Unrelated to this PR, I'm becoming convinced we should stop performing the logic that loads the original source from disk for providing visual context, as I believe this is one of the major slow downs. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The slowdown can be up to ~25% without the fast path with this micro benchmark.
Yeah, when sourcemap support is not enabled, the visual context is not prepended to the value of the stack property. It's just being prepended when the error is being printed by the runtime like fatal exceptions. While accessing stack property is one of the most commonly used paths, improving the performance of it is always a good motivation. Are you planning to work on it? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I don't think I'll have cycles to work on this in the short term, I'll CC you on the pertinent issue. |
||
{}, | ||
}; | ||
} | ||
|
||
namespace errors { | ||
|
||
TryCatchScope::~TryCatchScope() { | ||
|
@@ -836,6 +868,13 @@ static void SetSourceMapsEnabled(const FunctionCallbackInfo<Value>& args) { | |
env->set_source_maps_enabled(args[0].As<Boolean>()->Value()); | ||
} | ||
|
||
static void SetMaybeCacheGeneratedSourceMap( | ||
const FunctionCallbackInfo<Value>& args) { | ||
Environment* env = Environment::GetCurrent(args); | ||
CHECK(args[0]->IsFunction()); | ||
env->set_maybe_cache_generated_source_map(args[0].As<Function>()); | ||
} | ||
|
||
static void SetEnhanceStackForFatalException( | ||
const FunctionCallbackInfo<Value>& args) { | ||
Environment* env = Environment::GetCurrent(args); | ||
|
@@ -870,6 +909,7 @@ static void TriggerUncaughtException(const FunctionCallbackInfo<Value>& args) { | |
void RegisterExternalReferences(ExternalReferenceRegistry* registry) { | ||
registry->Register(SetPrepareStackTraceCallback); | ||
registry->Register(SetSourceMapsEnabled); | ||
registry->Register(SetMaybeCacheGeneratedSourceMap); | ||
registry->Register(SetEnhanceStackForFatalException); | ||
registry->Register(NoSideEffectsToString); | ||
registry->Register(TriggerUncaughtException); | ||
|
@@ -883,6 +923,9 @@ void Initialize(Local<Object> target, | |
env->SetMethod( | ||
target, "setPrepareStackTraceCallback", SetPrepareStackTraceCallback); | ||
env->SetMethod(target, "setSourceMapsEnabled", SetSourceMapsEnabled); | ||
env->SetMethod(target, | ||
"setMaybeCacheGeneratedSourceMap", | ||
SetMaybeCacheGeneratedSourceMap); | ||
env->SetMethod(target, | ||
"setEnhanceStackForFatalException", | ||
SetEnhanceStackForFatalException); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
// Flags: --enable-source-maps | ||
|
||
'use strict'; | ||
require('../common'); | ||
const fs = require('fs'); | ||
|
||
const content = fs.readFileSync(require.resolve('../fixtures/source-map/tabs.js'), 'utf8'); | ||
eval(content); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
ReferenceError: alert is not defined | ||
at Object.eval (*tabs.coffee:26:2) | ||
at eval (*tabs.coffee:1:14) | ||
at Object.<anonymous> (*source_map_eval.js:8:1) | ||
at Module._compile (node:internal/modules/cjs/loader:*) | ||
at Module._extensions..js (node:internal/modules/cjs/loader:*) | ||
at Module.load (node:internal/modules/cjs/loader:*) | ||
at Module._load (node:internal/modules/cjs/loader:*) | ||
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:*) | ||
at node:internal/main/run_main_module:* | ||
|
||
Node.js * |
Uh oh!
There was an error while loading. Please reload this page.