2019-10-23 16:26:32 +00:00
|
|
|
// Copyright (c) 2019 GitHub, Inc.
|
|
|
|
// Use of this source code is governed by the MIT license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
|
|
|
#include "shell/common/node_util.h"
|
2021-08-18 20:34:15 +00:00
|
|
|
|
2020-09-04 21:53:49 +00:00
|
|
|
#include "base/logging.h"
|
2019-10-23 16:26:32 +00:00
|
|
|
#include "shell/common/node_includes.h"
|
|
|
|
|
2022-06-29 19:55:47 +00:00
|
|
|
namespace electron::util {
|
2019-10-23 16:26:32 +00:00
|
|
|
|
|
|
|
v8::MaybeLocal<v8::Value> CompileAndCall(
|
|
|
|
v8::Local<v8::Context> context,
|
|
|
|
const char* id,
|
|
|
|
std::vector<v8::Local<v8::String>>* parameters,
|
|
|
|
std::vector<v8::Local<v8::Value>>* arguments,
|
|
|
|
node::Environment* optional_env) {
|
|
|
|
v8::Isolate* isolate = context->GetIsolate();
|
2020-09-04 21:53:49 +00:00
|
|
|
v8::TryCatch try_catch(isolate);
|
2019-10-23 16:26:32 +00:00
|
|
|
v8::MaybeLocal<v8::Function> compiled =
|
|
|
|
node::native_module::NativeModuleEnv::LookupAndCompile(
|
|
|
|
context, id, parameters, optional_env);
|
|
|
|
if (compiled.IsEmpty()) {
|
|
|
|
return v8::MaybeLocal<v8::Value>();
|
|
|
|
}
|
|
|
|
v8::Local<v8::Function> fn = compiled.ToLocalChecked().As<v8::Function>();
|
2020-09-04 21:53:49 +00:00
|
|
|
v8::MaybeLocal<v8::Value> ret = fn->Call(
|
|
|
|
context, v8::Null(isolate), arguments->size(), arguments->data());
|
|
|
|
// This will only be caught when something has gone terrible wrong as all
|
2020-10-20 19:10:15 +00:00
|
|
|
// electron scripts are wrapped in a try {} catch {} by webpack
|
2020-09-04 21:53:49 +00:00
|
|
|
if (try_catch.HasCaught()) {
|
|
|
|
LOG(ERROR) << "Failed to CompileAndCall electron script: " << id;
|
|
|
|
}
|
|
|
|
return ret;
|
2019-10-23 16:26:32 +00:00
|
|
|
}
|
|
|
|
|
2022-06-29 19:55:47 +00:00
|
|
|
} // namespace electron::util
|