node_util.cc 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. // Copyright (c) 2019 GitHub, Inc.
  2. // Use of this source code is governed by the MIT license that can be
  3. // found in the LICENSE file.
  4. #include "shell/common/node_util.h"
  5. #include "base/logging.h"
  6. #include "shell/common/node_includes.h"
  7. namespace electron::util {
  8. v8::MaybeLocal<v8::Value> CompileAndCall(
  9. v8::Local<v8::Context> context,
  10. const char* id,
  11. std::vector<v8::Local<v8::String>>* parameters,
  12. std::vector<v8::Local<v8::Value>>* arguments) {
  13. v8::Isolate* isolate = context->GetIsolate();
  14. v8::TryCatch try_catch(isolate);
  15. thread_local node::builtins::BuiltinLoader builtin_loader;
  16. v8::MaybeLocal<v8::Function> compiled = builtin_loader.LookupAndCompile(
  17. context, id, parameters, node::Realm::GetCurrent(context));
  18. if (compiled.IsEmpty())
  19. return v8::MaybeLocal<v8::Value>();
  20. v8::Local<v8::Function> fn = compiled.ToLocalChecked().As<v8::Function>();
  21. v8::MaybeLocal<v8::Value> ret = fn->Call(
  22. context, v8::Null(isolate), arguments->size(), arguments->data());
  23. // This will only be caught when something has gone terrible wrong as all
  24. // electron scripts are wrapped in a try {} catch {} by webpack
  25. if (try_catch.HasCaught()) {
  26. LOG(ERROR) << "Failed to CompileAndCall electron script: " << id;
  27. }
  28. return ret;
  29. }
  30. } // namespace electron::util