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
|
|
|
|
2016-10-26 09:10:15 +00:00
|
|
|
v8::Local<v8::Value> CallMethodWithArgs(v8::Isolate* isolate,
|
|
|
|
v8::Local<v8::Object> obj,
|
|
|
|
const char* method,
|
|
|
|
ValueVector* args) {
|
2024-06-07 08:06:00 +00:00
|
|
|
// An active node::Environment is required for node::MakeCallback.
|
2024-01-26 18:53:07 +00:00
|
|
|
std::unique_ptr<node::CallbackScope> callback_scope;
|
|
|
|
if (node::Environment::GetCurrent(isolate)) {
|
|
|
|
v8::HandleScope handle_scope(isolate);
|
|
|
|
callback_scope = std::make_unique<node::CallbackScope>(
|
|
|
|
isolate, v8::Object::New(isolate), node::async_context{0, 0});
|
2024-06-07 08:06:00 +00:00
|
|
|
} else {
|
|
|
|
return v8::Boolean::New(isolate, false);
|
2024-01-26 18:53:07 +00:00
|
|
|
}
|
|
|
|
|
2015-08-07 07:18:33 +00:00
|
|
|
// Perform microtask checkpoint after running JavaScript.
|
2023-01-11 16:59:32 +00:00
|
|
|
gin_helper::MicrotasksScope microtasks_scope(
|
|
|
|
isolate, obj->GetCreationContextChecked()->GetMicrotaskQueue(), true);
|
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(
|
2020-04-02 23:07:56 +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
|
2017-11-15 07:21:47 +00:00
|
|
|
v8::Local<v8::Value> localRet;
|
2024-06-07 08:06:00 +00:00
|
|
|
if (ret.ToLocal(&localRet))
|
2017-11-15 07:21:47 +00:00
|
|
|
return localRet;
|
2024-06-07 08:06:00 +00:00
|
|
|
|
2017-11-16 19:09:35 +00:00
|
|
|
return 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
|