From 511dece7ff6f9c8f30cc1f0de4e63d188cd3c5f4 Mon Sep 17 00:00:00 2001 From: "trop[bot]" <37223003+trop[bot]@users.noreply.github.com> Date: Mon, 9 Sep 2024 17:30:34 -0400 Subject: [PATCH] fix: out-of-scope Local handle in node::CallbackScope (#43640) refactor: use an EscapableHandleScope Co-authored-by: trop[bot] <37223003+trop[bot]@users.noreply.github.com> Co-authored-by: Charles Kerr --- .../common/gin_helper/event_emitter_caller.cc | 24 +++++++++---------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/shell/common/gin_helper/event_emitter_caller.cc b/shell/common/gin_helper/event_emitter_caller.cc index 124a300cb4..0c7b055194 100644 --- a/shell/common/gin_helper/event_emitter_caller.cc +++ b/shell/common/gin_helper/event_emitter_caller.cc @@ -13,15 +13,14 @@ v8::Local CallMethodWithArgs(v8::Isolate* isolate, v8::Local obj, const char* method, ValueVector* args) { - // An active node::Environment is required for node::MakeCallback. - std::unique_ptr callback_scope; - if (node::Environment::GetCurrent(isolate)) { - v8::HandleScope handle_scope(isolate); - callback_scope = std::make_unique( - isolate, v8::Object::New(isolate), node::async_context{0, 0}); - } else { - return v8::Boolean::New(isolate, false); - } + v8::EscapableHandleScope handle_scope{isolate}; + + // CallbackScope and MakeCallback both require an active node::Environment + if (!node::Environment::GetCurrent(isolate)) + return handle_scope.Escape(v8::Boolean::New(isolate, false)); + + node::CallbackScope callback_scope{isolate, v8::Object::New(isolate), + node::async_context{0, 0}}; // Perform microtask checkpoint after running JavaScript. gin_helper::MicrotasksScope microtasks_scope{ @@ -36,11 +35,10 @@ v8::Local CallMethodWithArgs(v8::Isolate* isolate, // of MakeCallback will be empty and therefore ToLocal will be false, in this // case we need to return "false" as that indicates that the event emitter did // not handle the event - v8::Local localRet; - if (ret.ToLocal(&localRet)) - return localRet; + if (v8::Local localRet; ret.ToLocal(&localRet)) + return handle_scope.Escape(localRet); - return v8::Boolean::New(isolate, false); + return handle_scope.Escape(v8::Boolean::New(isolate, false)); } } // namespace gin_helper::internal