node_util.cc 1.3 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. namespace electron {
  8. namespace 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::native_module::NativeModuleEnv::LookupAndCompile(
  19. context, id, parameters, 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. LOG(ERROR) << "Failed to CompileAndCall electron script: " << id;
  30. }
  31. return ret;
  32. }
  33. } // namespace util
  34. } // namespace electron