2019-09-06 05:52:54 +00:00
|
|
|
// Copyright (c) 2019 GitHub, Inc.
|
2015-06-23 11:46:37 +00:00
|
|
|
// Use of this source code is governed by the MIT license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
2019-09-06 05:52:54 +00:00
|
|
|
#include "shell/common/gin_helper/event_emitter_caller.h"
|
2015-06-23 11:46:37 +00:00
|
|
|
|
2020-06-17 17:08:10 +00:00
|
|
|
#include "shell/common/gin_helper/microtasks_scope.h"
|
2019-06-19 20:46:59 +00:00
|
|
|
#include "shell/common/node_includes.h"
|
2015-08-07 07:18:33 +00:00
|
|
|
|
2022-06-29 19:55:47 +00:00
|
|
|
namespace gin_helper::internal {
|
2015-06-23 11:46:37 +00:00
|
|
|
|
2024-09-18 18:55:53 +00:00
|
|
|
v8::Local<v8::Value> CallMethodWithArgs(
|
|
|
|
v8::Isolate* isolate,
|
|
|
|
v8::Local<v8::Object> obj,
|
|
|
|
const char* method,
|
|
|
|
const base::span<v8::Local<v8::Value>> args) {
|
2024-09-09 21:30:34 +00:00
|
|
|
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}};
|
2024-01-26 18:53:07 +00:00
|
|
|
|
2015-08-07 07:18:33 +00:00
|
|
|
// Perform microtask checkpoint after running JavaScript.
|
2024-08-05 13:24:27 +00:00
|
|
|
gin_helper::MicrotasksScope microtasks_scope{
|
|
|
|
isolate, obj->GetCreationContextChecked()->GetMicrotaskQueue(), true,
|
|
|
|
v8::MicrotasksScope::kRunMicrotasks};
|
2024-06-07 08:06:00 +00:00
|
|
|
|
|
|
|
// node::MakeCallback will also run pending tasks in Node.js.
|
2018-04-18 01:55:30 +00:00
|
|
|
v8::MaybeLocal<v8::Value> ret = node::MakeCallback(
|
2024-09-18 18:55:53 +00:00
|
|
|
isolate, obj, method, args.size(), args.data(), {0, 0});
|
2024-06-07 08:06:00 +00:00
|
|
|
|
2017-11-15 07:21:47 +00:00
|
|
|
// If the JS function throws an exception (doesn't return a value) the result
|
2017-11-16 19:09:35 +00:00
|
|
|
// 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
|
2017-11-16 19:15:53 +00:00
|
|
|
// not handle the event
|
2024-09-09 21:30:34 +00:00
|
|
|
if (v8::Local<v8::Value> localRet; ret.ToLocal(&localRet))
|
|
|
|
return handle_scope.Escape(localRet);
|
2024-06-07 08:06:00 +00:00
|
|
|
|
2024-09-09 21:30:34 +00:00
|
|
|
return handle_scope.Escape(v8::Boolean::New(isolate, false));
|
2015-06-23 11:46:37 +00:00
|
|
|
}
|
|
|
|
|
2022-06-29 19:55:47 +00:00
|
|
|
} // namespace gin_helper::internal
|