Skip to content

Commit c957adb

Browse files
devsnekBridgeAR
authored andcommitted
src: use custom TryCatch subclass
PR-URL: #24751 Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
1 parent 5f58928 commit c957adb

10 files changed

+116
-75
lines changed

src/async_wrap.cc

+5-4
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ using v8::WeakCallbackInfo;
5858
using v8::WeakCallbackType;
5959

6060
using AsyncHooks = node::Environment::AsyncHooks;
61+
using TryCatchScope = node::errors::TryCatchScope;
6162

6263
namespace node {
6364

@@ -91,7 +92,7 @@ struct AsyncWrapObject : public AsyncWrap {
9192
static void DestroyAsyncIdsCallback(Environment* env, void* data) {
9293
Local<Function> fn = env->async_hooks_destroy_function();
9394

94-
FatalTryCatch try_catch(env);
95+
TryCatchScope try_catch(env, TryCatchScope::CatchMode::kFatal);
9596

9697
do {
9798
std::vector<double> destroy_async_id_list;
@@ -127,7 +128,7 @@ void Emit(Environment* env, double async_id, AsyncHooks::Fields type,
127128

128129
HandleScope handle_scope(env->isolate());
129130
Local<Value> async_id_value = Number::New(env->isolate(), async_id);
130-
FatalTryCatch try_catch(env);
131+
TryCatchScope try_catch(env, TryCatchScope::CatchMode::kFatal);
131132
USE(fn->Call(env->context(), Undefined(env->isolate()), 1, &async_id_value));
132133
}
133134

@@ -685,7 +686,7 @@ void AsyncWrap::EmitAsyncInit(Environment* env,
685686
object,
686687
};
687688

688-
FatalTryCatch try_catch(env);
689+
TryCatchScope try_catch(env, TryCatchScope::CatchMode::kFatal);
689690
USE(init_fn->Call(env->context(), object, arraysize(argv), argv));
690691
}
691692

@@ -788,7 +789,7 @@ Local<Object> AsyncWrap::GetOwner(Environment* env, Local<Object> obj) {
788789
EscapableHandleScope handle_scope(env->isolate());
789790
CHECK(!obj.IsEmpty());
790791

791-
TryCatch ignore_exceptions(env->isolate());
792+
TryCatchScope ignore_exceptions(env);
792793
while (true) {
793794
Local<Value> owner;
794795
if (!obj->Get(env->context(),

src/env.cc

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include "async_wrap.h"
22
#include "node_buffer.h"
33
#include "node_context_data.h"
4+
#include "node_errors.h"
45
#include "node_file.h"
56
#include "node_internals.h"
67
#include "node_native_module.h"
@@ -15,6 +16,7 @@
1516

1617
namespace node {
1718

19+
using errors::TryCatchScope;
1820
using v8::Context;
1921
using v8::EmbedderGraph;
2022
using v8::External;
@@ -36,7 +38,6 @@ using v8::StackTrace;
3638
using v8::String;
3739
using v8::Symbol;
3840
using v8::TracingController;
39-
using v8::TryCatch;
4041
using v8::Undefined;
4142
using v8::Value;
4243
using worker::Worker;
@@ -156,7 +157,7 @@ void Environment::TrackingTraceStateObserver::UpdateTraceCategoryState() {
156157
Local<Function> cb = env_->trace_category_state_function();
157158
if (cb.IsEmpty())
158159
return;
159-
TryCatch try_catch(isolate);
160+
TryCatchScope try_catch(env_);
160161
try_catch.SetVerbose(true);
161162
cb->Call(env_->context(), Undefined(isolate),
162163
0, nullptr).ToLocalChecked();
@@ -577,7 +578,7 @@ void Environment::RunAndClearNativeImmediates() {
577578
std::vector<NativeImmediateCallback> list;
578579
native_immediate_callbacks_.swap(list);
579580
auto drain_list = [&]() {
580-
TryCatch try_catch(isolate());
581+
TryCatchScope try_catch(this);
581582
for (auto it = list.begin(); it != list.end(); ++it) {
582583
#ifdef DEBUG
583584
v8::SealHandleScope seal_handle_scope(isolate());
@@ -642,7 +643,7 @@ void Environment::RunTimers(uv_timer_t* handle) {
642643
// impossible for us to end up in an infinite loop due to how the JS-side
643644
// is structured.
644645
do {
645-
TryCatch try_catch(env->isolate());
646+
TryCatchScope try_catch(env);
646647
try_catch.SetVerbose(true);
647648
ret = cb->Call(env->context(), process, 1, &arg);
648649
} while (ret.IsEmpty() && env->can_call_into_js());

src/js_stream.cc

+8-6
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,15 @@
33
#include "async_wrap.h"
44
#include "env-inl.h"
55
#include "node_buffer.h"
6+
#include "node_errors.h"
67
#include "node_internals.h"
78
#include "stream_base-inl.h"
89
#include "v8.h"
910

1011
namespace node {
1112

13+
using errors::TryCatchScope;
14+
1215
using v8::Array;
1316
using v8::Context;
1417
using v8::FunctionCallbackInfo;
@@ -18,7 +21,6 @@ using v8::Int32;
1821
using v8::Local;
1922
using v8::Object;
2023
using v8::String;
21-
using v8::TryCatch;
2224
using v8::Value;
2325

2426

@@ -42,7 +44,7 @@ bool JSStream::IsAlive() {
4244
bool JSStream::IsClosing() {
4345
HandleScope scope(env()->isolate());
4446
Context::Scope context_scope(env()->context());
45-
TryCatch try_catch(env()->isolate());
47+
TryCatchScope try_catch(env());
4648
Local<Value> value;
4749
if (!MakeCallback(env()->isclosing_string(), 0, nullptr).ToLocal(&value)) {
4850
if (!try_catch.HasTerminated())
@@ -56,7 +58,7 @@ bool JSStream::IsClosing() {
5658
int JSStream::ReadStart() {
5759
HandleScope scope(env()->isolate());
5860
Context::Scope context_scope(env()->context());
59-
TryCatch try_catch(env()->isolate());
61+
TryCatchScope try_catch(env());
6062
Local<Value> value;
6163
int value_int = UV_EPROTO;
6264
if (!MakeCallback(env()->onreadstart_string(), 0, nullptr).ToLocal(&value) ||
@@ -71,7 +73,7 @@ int JSStream::ReadStart() {
7173
int JSStream::ReadStop() {
7274
HandleScope scope(env()->isolate());
7375
Context::Scope context_scope(env()->context());
74-
TryCatch try_catch(env()->isolate());
76+
TryCatchScope try_catch(env());
7577
Local<Value> value;
7678
int value_int = UV_EPROTO;
7779
if (!MakeCallback(env()->onreadstop_string(), 0, nullptr).ToLocal(&value) ||
@@ -91,7 +93,7 @@ int JSStream::DoShutdown(ShutdownWrap* req_wrap) {
9193
req_wrap->object()
9294
};
9395

94-
TryCatch try_catch(env()->isolate());
96+
TryCatchScope try_catch(env());
9597
Local<Value> value;
9698
int value_int = UV_EPROTO;
9799
if (!MakeCallback(env()->onshutdown_string(),
@@ -126,7 +128,7 @@ int JSStream::DoWrite(WriteWrap* w,
126128
bufs_arr
127129
};
128130

129-
TryCatch try_catch(env()->isolate());
131+
TryCatchScope try_catch(env());
130132
Local<Value> value;
131133
int value_int = UV_EPROTO;
132134
if (!MakeCallback(env()->onwrite_string(),

src/module_wrap.cc

+5-4
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
namespace node {
1515
namespace loader {
1616

17+
using errors::TryCatchScope;
18+
1719
using node::contextify::ContextifyContext;
1820
using node::url::URL;
1921
using node::url::URL_FLAGS_FAILED;
@@ -40,7 +42,6 @@ using v8::Promise;
4042
using v8::ScriptCompiler;
4143
using v8::ScriptOrigin;
4244
using v8::String;
43-
using v8::TryCatch;
4445
using v8::Undefined;
4546
using v8::Value;
4647

@@ -135,7 +136,7 @@ void ModuleWrap::New(const FunctionCallbackInfo<Value>& args) {
135136
}
136137

137138
Environment::ShouldNotAbortOnUncaughtScope no_abort_scope(env);
138-
TryCatch try_catch(isolate);
139+
TryCatchScope try_catch(env);
139140
Local<Module> module;
140141

141142
Local<PrimitiveArray> host_defined_options =
@@ -244,7 +245,7 @@ void ModuleWrap::Instantiate(const FunctionCallbackInfo<Value>& args) {
244245
ASSIGN_OR_RETURN_UNWRAP(&obj, args.This());
245246
Local<Context> context = obj->context_.Get(isolate);
246247
Local<Module> module = obj->module_.Get(isolate);
247-
TryCatch try_catch(isolate);
248+
TryCatchScope try_catch(env);
248249
Maybe<bool> ok = module->InstantiateModule(context, ResolveCallback);
249250

250251
// clear resolve cache on instantiate
@@ -279,7 +280,7 @@ void ModuleWrap::Evaluate(const FunctionCallbackInfo<Value>& args) {
279280
bool break_on_sigint = args[1]->IsTrue();
280281

281282
Environment::ShouldNotAbortOnUncaughtScope no_abort_scope(env);
282-
TryCatch try_catch(isolate);
283+
TryCatchScope try_catch(env);
283284

284285
bool timed_out = false;
285286
bool received_signal = false;

src/node.cc

+4-4
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ typedef int mode_t;
116116

117117
namespace node {
118118

119+
using errors::TryCatchScope;
119120
using native_module::NativeModuleLoader;
120121
using options_parser::kAllowedInEnvironment;
121122
using options_parser::kDisallowedInEnvironment;
@@ -154,7 +155,6 @@ using v8::SealHandleScope;
154155
using v8::SideEffectType;
155156
using v8::String;
156157
using v8::TracingController;
157-
using v8::TryCatch;
158158
using v8::Undefined;
159159
using v8::V8;
160160
using v8::Value;
@@ -746,7 +746,7 @@ static MaybeLocal<Value> ExecuteString(Environment* env,
746746
Local<String> source,
747747
Local<String> filename) {
748748
EscapableHandleScope scope(env->isolate());
749-
TryCatch try_catch(env->isolate());
749+
TryCatchScope try_catch(env);
750750

751751
// try_catch must be nonverbose to disable FatalException() handler,
752752
// we will handle exceptions ourself.
@@ -1341,7 +1341,7 @@ static MaybeLocal<Function> GetBootstrapper(
13411341
Local<String> script_name) {
13421342
EscapableHandleScope scope(env->isolate());
13431343

1344-
TryCatch try_catch(env->isolate());
1344+
TryCatchScope try_catch(env);
13451345

13461346
// Disable verbose mode to stop FatalException() handler from trying
13471347
// to handle the exception. Errors this early in the start-up phase
@@ -1386,7 +1386,7 @@ static bool ExecuteBootstrapper(Environment* env, Local<Function> bootstrapper,
13861386
void LoadEnvironment(Environment* env) {
13871387
HandleScope handle_scope(env->isolate());
13881388

1389-
TryCatch try_catch(env->isolate());
1389+
TryCatchScope try_catch(env);
13901390
// Disable verbose mode to stop FatalException() handler from trying
13911391
// to handle the exception. Errors this early in the start-up phase
13921392
// are not safe to ignore.

src/node_contextify.cc

+6-5
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131
namespace node {
3232
namespace contextify {
3333

34+
using errors::TryCatchScope;
35+
3436
using v8::Array;
3537
using v8::ArrayBuffer;
3638
using v8::ArrayBufferView;
@@ -63,7 +65,6 @@ using v8::ScriptCompiler;
6365
using v8::ScriptOrigin;
6466
using v8::String;
6567
using v8::Symbol;
66-
using v8::TryCatch;
6768
using v8::Uint32;
6869
using v8::UnboundScript;
6970
using v8::Value;
@@ -245,7 +246,7 @@ void ContextifyContext::MakeContext(const FunctionCallbackInfo<Value>& args) {
245246
CHECK(args[4]->IsBoolean());
246247
options.allow_code_gen_wasm = args[4].As<Boolean>();
247248

248-
TryCatch try_catch(env->isolate());
249+
TryCatchScope try_catch(env);
249250
ContextifyContext* context = new ContextifyContext(env, sandbox, options);
250251

251252
if (try_catch.HasCaught()) {
@@ -705,7 +706,7 @@ void ContextifyScript::New(const FunctionCallbackInfo<Value>& args) {
705706
if (source.GetCachedData() != nullptr)
706707
compile_options = ScriptCompiler::kConsumeCodeCache;
707708

708-
TryCatch try_catch(isolate);
709+
TryCatchScope try_catch(env);
709710
Environment::ShouldNotAbortOnUncaughtScope no_abort_scope(env);
710711
Context::Scope scope(parsing_context);
711712

@@ -863,7 +864,7 @@ bool ContextifyScript::EvalMachine(Environment* env,
863864
"Script methods can only be called on script instances.");
864865
return false;
865866
}
866-
TryCatch try_catch(env->isolate());
867+
TryCatchScope try_catch(env);
867868
ContextifyScript* wrapped_script;
868869
ASSIGN_OR_RETURN_UNWRAP(&wrapped_script, args.Holder(), false);
869870
Local<UnboundScript> unbound_script =
@@ -1012,7 +1013,7 @@ void ContextifyContext::CompileFunction(
10121013
options = ScriptCompiler::kConsumeCodeCache;
10131014
}
10141015

1015-
TryCatch try_catch(isolate);
1016+
TryCatchScope try_catch(env);
10161017
Context::Scope scope(parsing_context);
10171018

10181019
// Read context extensions from buffer

src/node_contextify.h

+3-2
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@
33

44
#if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
55

6-
#include "node_internals.h"
7-
#include "node_context_data.h"
86
#include "base_object-inl.h"
7+
#include "node_context_data.h"
8+
#include "node_errors.h"
9+
#include "node_internals.h"
910

1011
namespace node {
1112
namespace contextify {

0 commit comments

Comments
 (0)