node_util.cc 1.6 KB

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