node_util.h 1.5 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. #ifndef ELECTRON_SHELL_COMMON_NODE_UTIL_H_
  5. #define ELECTRON_SHELL_COMMON_NODE_UTIL_H_
  6. #include <string_view>
  7. #include <vector>
  8. #include "base/containers/span.h"
  9. #include "v8/include/v8-forward.h"
  10. namespace node {
  11. class Environment;
  12. }
  13. namespace electron::util {
  14. // Emit a warning via node's process.emitWarning()
  15. void EmitWarning(v8::Isolate* isolate,
  16. std::string_view warning_msg,
  17. std::string_view warning_type);
  18. // Emit a warning via node's process.emitWarning(),
  19. // using JavscriptEnvironment's isolate
  20. void EmitWarning(std::string_view warning_msg, std::string_view warning_type);
  21. // Run a script with JS source bundled inside the binary as if it's wrapped
  22. // in a function called with a null receiver and arguments specified in C++.
  23. // The returned value is empty if an exception is encountered.
  24. // JS code run with this method can assume that their top-level
  25. // declarations won't affect the global scope.
  26. v8::MaybeLocal<v8::Value> CompileAndCall(
  27. v8::Local<v8::Context> context,
  28. const char* id,
  29. std::vector<v8::Local<v8::String>>* parameters,
  30. std::vector<v8::Local<v8::Value>>* arguments);
  31. // Convenience function to view a Node buffer's data as a base::span().
  32. // Analogous to base::as_byte_span()
  33. [[nodiscard]] base::span<uint8_t> as_byte_span(
  34. v8::Local<v8::Value> node_buffer);
  35. } // namespace electron::util
  36. #endif // ELECTRON_SHELL_COMMON_NODE_UTIL_H_