node_util.cc 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637
  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. node::Environment* optional_env) {
  14. v8::Isolate* isolate = context->GetIsolate();
  15. v8::TryCatch try_catch(isolate);
  16. v8::MaybeLocal<v8::Function> compiled =
  17. node::builtins::BuiltinLoader::LookupAndCompile(context, id, parameters,
  18. optional_env);
  19. if (compiled.IsEmpty()) {
  20. return v8::MaybeLocal<v8::Value>();
  21. }
  22. v8::Local<v8::Function> fn = compiled.ToLocalChecked().As<v8::Function>();
  23. v8::MaybeLocal<v8::Value> ret = fn->Call(
  24. context, v8::Null(isolate), arguments->size(), arguments->data());
  25. // This will only be caught when something has gone terrible wrong as all
  26. // electron scripts are wrapped in a try {} catch {} by webpack
  27. if (try_catch.HasCaught()) {
  28. LOG(ERROR) << "Failed to CompileAndCall electron script: " << id;
  29. }
  30. return ret;
  31. }
  32. } // namespace electron::util