node_util.cc 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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/compiler_specific.h"
  6. #include "base/logging.h"
  7. #include "gin/converter.h"
  8. #include "gin/dictionary.h"
  9. #include "shell/browser/javascript_environment.h"
  10. #include "shell/common/gin_converters/callback_converter.h"
  11. #include "shell/common/node_includes.h"
  12. namespace electron::util {
  13. v8::MaybeLocal<v8::Value> CompileAndCall(
  14. v8::Local<v8::Context> context,
  15. const char* id,
  16. std::vector<v8::Local<v8::String>>* parameters,
  17. std::vector<v8::Local<v8::Value>>* arguments) {
  18. v8::Isolate* isolate = context->GetIsolate();
  19. v8::TryCatch try_catch(isolate);
  20. thread_local node::builtins::BuiltinLoader builtin_loader;
  21. v8::MaybeLocal<v8::Function> compiled = builtin_loader.LookupAndCompile(
  22. context, id, parameters, node::Realm::GetCurrent(context));
  23. if (compiled.IsEmpty())
  24. return {};
  25. v8::Local<v8::Function> fn = compiled.ToLocalChecked().As<v8::Function>();
  26. v8::MaybeLocal<v8::Value> ret = fn->Call(
  27. context, v8::Null(isolate), arguments->size(), arguments->data());
  28. // This will only be caught when something has gone terrible wrong as all
  29. // electron scripts are wrapped in a try {} catch {} by webpack
  30. if (try_catch.HasCaught()) {
  31. std::string msg = "no error message";
  32. if (!try_catch.Message().IsEmpty()) {
  33. gin::ConvertFromV8(isolate, try_catch.Message()->Get(), &msg);
  34. } else if (try_catch.HasTerminated()) {
  35. msg = "script execution has been terminated";
  36. }
  37. LOG(ERROR) << "Failed to CompileAndCall electron script (" << id
  38. << "): " << msg;
  39. }
  40. return ret;
  41. }
  42. void EmitWarning(const std::string_view warning_msg,
  43. const std::string_view warning_type) {
  44. EmitWarning(JavascriptEnvironment::GetIsolate(), warning_msg, warning_type);
  45. }
  46. void EmitWarning(v8::Isolate* isolate,
  47. const std::string_view warning_msg,
  48. const std::string_view warning_type) {
  49. v8::HandleScope scope{isolate};
  50. gin::Dictionary process{
  51. isolate, node::Environment::GetCurrent(isolate)->process_object()};
  52. base::RepeatingCallback<void(std::string_view, std::string_view,
  53. std::string_view)>
  54. emit_warning;
  55. process.Get("emitWarning", &emit_warning);
  56. emit_warning.Run(warning_msg, warning_type, "");
  57. }
  58. // SAFETY: There is no node::Buffer API that passes the UNSAFE_BUFFER_USAGE
  59. // test, so let's isolate the unsafe API use into this function. Instead of
  60. // calling `Buffer::Data()` and `Buffer::Length()` directly, the rest of our
  61. // code should prefer to use spans returned by this function.
  62. base::span<uint8_t> as_byte_span(v8::Local<v8::Value> node_buffer) {
  63. auto* data = reinterpret_cast<uint8_t*>(node::Buffer::Data(node_buffer));
  64. const auto size = node::Buffer::Length(node_buffer);
  65. return UNSAFE_BUFFERS(base::span{data, size});
  66. }
  67. } // namespace electron::util