node_util.cc 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  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. #include "third_party/electron_node/src/node_native_module_env.h"
  8. namespace electron {
  9. namespace util {
  10. v8::MaybeLocal<v8::Value> CompileAndCall(
  11. v8::Local<v8::Context> context,
  12. const char* id,
  13. std::vector<v8::Local<v8::String>>* parameters,
  14. std::vector<v8::Local<v8::Value>>* arguments,
  15. node::Environment* optional_env) {
  16. v8::Isolate* isolate = context->GetIsolate();
  17. v8::TryCatch try_catch(isolate);
  18. v8::MaybeLocal<v8::Function> compiled =
  19. node::native_module::NativeModuleEnv::LookupAndCompile(
  20. context, id, parameters, optional_env);
  21. if (compiled.IsEmpty()) {
  22. return v8::MaybeLocal<v8::Value>();
  23. }
  24. v8::Local<v8::Function> fn = compiled.ToLocalChecked().As<v8::Function>();
  25. v8::MaybeLocal<v8::Value> ret = fn->Call(
  26. context, v8::Null(isolate), arguments->size(), arguments->data());
  27. // This will only be caught when something has gone terrible wrong as all
  28. // electron scripts are wrapped in a try {} catch {} by webpack
  29. if (try_catch.HasCaught()) {
  30. LOG(ERROR) << "Failed to CompileAndCall electron script: " << id;
  31. }
  32. return ret;
  33. }
  34. } // namespace util
  35. } // namespace electron