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