Skip to content

Commit

Permalink
src: add missing TryCatch
Browse files Browse the repository at this point in the history
Otherwise re-entering V8 doesn't work as expected after exceptions
were thrown.

Refs: https://chromium-review.googlesource.com/c/v8/v8/+/5050065

Co-Authored-By: Toon Verwaest <verwaest@chromium.org>
Co-Authored-By: deepak1556 <hop2deep@gmail.com>
PR-URL: #51362
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Reviewed-By: Rafael Gonzaga <rafael.nunu@hotmail.com>
  • Loading branch information
3 people committed Mar 31, 2024
1 parent c22793d commit d9c47e9
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/js_stream.cc
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ using v8::Int32;
using v8::Isolate;
using v8::Local;
using v8::Object;
using v8::TryCatch;
using v8::Value;


Expand Down Expand Up @@ -169,6 +170,8 @@ void JSStream::ReadBuffer(const FunctionCallbackInfo<Value>& args) {
const char* data = buffer.data();
int len = buffer.length();

TryCatch try_catch(args.GetIsolate());

// Repeatedly ask the stream's owner for memory, copy the data that we
// just read from JS into those buffers and emit them as reads.
while (len != 0) {
Expand All @@ -182,6 +185,10 @@ void JSStream::ReadBuffer(const FunctionCallbackInfo<Value>& args) {
len -= static_cast<int>(avail);
wrap->EmitRead(avail, buf);
}

if (try_catch.HasCaught()) {
try_catch.ReThrow();
}
}


Expand Down
6 changes: 6 additions & 0 deletions src/node_contextify.cc
Original file line number Diff line number Diff line change
Expand Up @@ -462,13 +462,16 @@ bool ContextifyContext::IsStillInitializing(const ContextifyContext* ctx) {
void ContextifyContext::PropertyGetterCallback(
Local<Name> property,
const PropertyCallbackInfo<Value>& args) {
Environment* env = Environment::GetCurrent(args);
ContextifyContext* ctx = ContextifyContext::Get(args);

// Still initializing
if (IsStillInitializing(ctx)) return;

Local<Context> context = ctx->context();
Local<Object> sandbox = ctx->sandbox();

TryCatchScope try_catch(env);
MaybeLocal<Value> maybe_rv =
sandbox->GetRealNamedProperty(context, property);
if (maybe_rv.IsEmpty()) {
Expand All @@ -478,6 +481,9 @@ void ContextifyContext::PropertyGetterCallback(

Local<Value> rv;
if (maybe_rv.ToLocal(&rv)) {
if (try_catch.HasCaught() && !try_catch.HasTerminated()) {
try_catch.ReThrow();
}
if (rv == sandbox)
rv = ctx->global_proxy();

Expand Down
4 changes: 4 additions & 0 deletions src/node_messaging.cc
Original file line number Diff line number Diff line change
Expand Up @@ -916,6 +916,7 @@ Maybe<bool> MessagePort::PostMessage(Environment* env,
const TransferList& transfer_v) {
Isolate* isolate = env->isolate();
Local<Object> obj = object(isolate);
TryCatchScope try_catch(env);

std::shared_ptr<Message> msg = std::make_shared<Message>();

Expand All @@ -924,6 +925,9 @@ Maybe<bool> MessagePort::PostMessage(Environment* env,

Maybe<bool> serialization_maybe =
msg->Serialize(env, context, message_v, transfer_v, obj);
if (try_catch.HasCaught() && !try_catch.HasTerminated()) {
try_catch.ReThrow();
}
if (data_ == nullptr) {
return serialization_maybe;
}
Expand Down
5 changes: 5 additions & 0 deletions test/addons/make-callback-recurse/binding.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ using v8::FunctionCallbackInfo;
using v8::Isolate;
using v8::Local;
using v8::Object;
using v8::TryCatch;
using v8::Value;

namespace {
Expand All @@ -19,8 +20,12 @@ void MakeCallback(const FunctionCallbackInfo<Value>& args) {
Local<Object> recv = args[0].As<Object>();
Local<Function> method = args[1].As<Function>();

TryCatch try_catch(isolate);
node::MakeCallback(isolate, recv, method, 0, nullptr,
node::async_context{0, 0});
if (try_catch.HasCaught()) {
try_catch.ReThrow();
}
}

void Initialize(Local<Object> exports) {
Expand Down

0 comments on commit d9c47e9

Please sign in to comment.