node_util.cc 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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 "gin/converter.h"
  7. #include "shell/common/node_includes.h"
  8. namespace electron::util {
  9. v8::MaybeLocal<v8::Value> CompileAndCall(
  10. v8::Local<v8::Context> context,
  11. const char* id,
  12. std::vector<v8::Local<v8::String>>* parameters,
  13. std::vector<v8::Local<v8::Value>>* arguments) {
  14. v8::Isolate* isolate = context->GetIsolate();
  15. v8::TryCatch try_catch(isolate);
  16. thread_local node::builtins::BuiltinLoader builtin_loader;
  17. v8::MaybeLocal<v8::Function> compiled = builtin_loader.LookupAndCompile(
  18. context, id, parameters, node::Realm::GetCurrent(context));
  19. if (compiled.IsEmpty())
  20. return v8::MaybeLocal<v8::Value>();
  21. v8::Local<v8::Function> fn = compiled.ToLocalChecked().As<v8::Function>();
  22. v8::MaybeLocal<v8::Value> ret = fn->Call(
  23. context, v8::Null(isolate), arguments->size(), arguments->data());
  24. // This will only be caught when something has gone terrible wrong as all
  25. // electron scripts are wrapped in a try {} catch {} by webpack
  26. if (try_catch.HasCaught()) {
  27. std::string msg = "no error message";
  28. if (!try_catch.Message().IsEmpty()) {
  29. gin::ConvertFromV8(isolate, try_catch.Message()->Get(), &msg);
  30. } else if (try_catch.HasTerminated()) {
  31. msg = "script execution has been terminated";
  32. }
  33. LOG(ERROR) << "Failed to CompileAndCall electron script (" << id
  34. << "): " << msg;
  35. }
  36. return ret;
  37. }
  38. } // namespace electron::util