123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081 |
- From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
- From: Shelley Vohr <[email protected]>
- Date: Wed, 16 Aug 2023 19:15:29 +0200
- Subject: fix: assert module in the renderer process
- When creating a Node.js Environment, embedders have the option to disable Node.js'
- default overriding of Error.prepareStackTrace. However, the assert module depends on
- a WeakMap that is populated with the error stacktraces in the overridden function.
- This adds handling to fall back to the default implementation if Error.prepareStackTrace
- if the override has been disabled.
- This will be upstreamed.
- diff --git a/lib/internal/assert/utils.js b/lib/internal/assert/utils.js
- index 59b5a16f1309a5e4055bccfdb7a529045ad30402..bfdaf6211466a01b64b7942f7b16c480283278ff 100644
- --- a/lib/internal/assert/utils.js
- +++ b/lib/internal/assert/utils.js
- @@ -25,6 +25,7 @@ const AssertionError = require('internal/assert/assertion_error');
- const { openSync, closeSync, readSync } = require('fs');
- const { EOL } = require('internal/constants');
- const { BuiltinModule } = require('internal/bootstrap/realm');
- +const { getEmbedderOptions } = require('internal/options');
- const { isError } = require('internal/util');
-
- const errorCache = new SafeMap();
- @@ -167,8 +168,16 @@ function getErrMessage(message, fn) {
- ErrorCaptureStackTrace(err, fn);
- if (errorStackTraceLimitIsWritable) Error.stackTraceLimit = tmpLimit;
-
- - overrideStackTrace.set(err, (_, stack) => stack);
- - const call = err.stack[0];
- + let call;
- + if (getEmbedderOptions().hasPrepareStackTraceCallback) {
- + overrideStackTrace.set(err, (_, stack) => stack);
- + call = err.stack[0];
- + } else {
- + const tmpPrepare = Error.prepareStackTrace;
- + Error.prepareStackTrace = (_, stack) => stack;
- + call = err.stack[0];
- + Error.prepareStackTrace = tmpPrepare;
- + }
-
- let filename = call.getFileName();
- const line = call.getLineNumber() - 1;
- diff --git a/src/api/environment.cc b/src/api/environment.cc
- index 89ce587cac4506c4218a9316fe0b68070a7a8504..b74fb837fc1cfee839c8b5b8c0b9a6f805881206 100644
- --- a/src/api/environment.cc
- +++ b/src/api/environment.cc
- @@ -247,6 +247,9 @@ void SetIsolateErrorHandlers(v8::Isolate* isolate, const IsolateSettings& s) {
- auto* prepare_stack_trace_cb = s.prepare_stack_trace_callback ?
- s.prepare_stack_trace_callback : PrepareStackTraceCallback;
- isolate->SetPrepareStackTraceCallback(prepare_stack_trace_cb);
- + } else {
- + auto env = Environment::GetCurrent(isolate);
- + env->set_prepare_stack_trace_callback(Local<Function>());
- }
- }
-
- diff --git a/src/node_options.cc b/src/node_options.cc
- index f70e0917f6caa66210107cdb2ef891685563ba96..ec419cf96a14989338e3261b85c92b81ba8b50d9 100644
- --- a/src/node_options.cc
- +++ b/src/node_options.cc
- @@ -1517,14 +1517,16 @@ void GetEmbedderOptions(const FunctionCallbackInfo<Value>& args) {
- }
- Isolate* isolate = args.GetIsolate();
-
- - constexpr size_t kOptionsSize = 4;
- + constexpr size_t kOptionsSize = 5;
- std::array<Local<Name>, kOptionsSize> names = {
- + FIXED_ONE_BYTE_STRING(env->isolate(), "hasPrepareStackTraceCallback"),
- FIXED_ONE_BYTE_STRING(env->isolate(), "shouldNotRegisterESMLoader"),
- FIXED_ONE_BYTE_STRING(env->isolate(), "noGlobalSearchPaths"),
- FIXED_ONE_BYTE_STRING(env->isolate(), "noBrowserGlobals"),
- FIXED_ONE_BYTE_STRING(env->isolate(), "hasEmbedderPreload")};
-
- std::array<Local<Value>, kOptionsSize> values = {
- + Boolean::New(isolate, env->prepare_stack_trace_callback().IsEmpty()),
- Boolean::New(isolate, env->should_not_register_esm_loader()),
- Boolean::New(isolate, env->no_global_search_paths()),
- Boolean::New(isolate, env->no_browser_globals()),
|