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