// Copyright (c) 2019 GitHub, Inc. // Use of this source code is governed by the MIT license that can be // found in the LICENSE file. #ifndef ELECTRON_SHELL_COMMON_NODE_UTIL_H_ #define ELECTRON_SHELL_COMMON_NODE_UTIL_H_ #include #include #include #include "base/containers/span.h" #include "v8/include/v8-forward.h" namespace node { class Environment; class IsolateData; struct ThreadId; namespace EnvironmentFlags { enum Flags : uint64_t; } } // namespace node namespace electron::util { // Emit a warning via node's process.emitWarning() void EmitWarning(v8::Isolate* isolate, std::string_view warning_msg, std::string_view warning_type); // Emit a warning via node's process.emitWarning(), // using JavascriptEnvironment's isolate void EmitWarning(std::string_view warning_msg, std::string_view warning_type); // Emit a deprecation warning via node's process.emitWarning() void EmitDeprecationWarning(v8::Isolate* isolate, std::string_view warning_msg, std::string_view deprecation_code = ""); // Emit a deprecation warning via node's process.emitWarning(), // using JavascriptEnvironment's isolate void EmitDeprecationWarning(std::string_view warning_msg, std::string_view deprecation_code = ""); // Run a script with JS source bundled inside the binary as if it's wrapped // in a function called with a null receiver and arguments specified in C++. // The returned value is empty if an exception is encountered. // JS code run with this method can assume that their top-level // declarations won't affect the global scope. v8::MaybeLocal CompileAndCall( v8::Local context, const char* id, std::vector>* parameters, std::vector>* arguments); // Wrapper for node::CreateEnvironment that logs failure node::Environment* CreateEnvironment(v8::Isolate* isolate, node::IsolateData* isolate_data, v8::Local context, const std::vector& args, const std::vector& exec_args, node::EnvironmentFlags::Flags env_flags, std::string_view process_type = ""); } // namespace electron::util namespace electron::Buffer { // Convenience function to view a Node buffer's data as a base::span(). // Analogous to base::as_byte_span() [[nodiscard]] base::span as_byte_span( v8::Local node_buffer); // span-friendly version of node::Buffer::Copy() [[nodiscard]] v8::MaybeLocal Copy(v8::Isolate* isolate, base::span data); // span-friendly version of node::Buffer::Copy() [[nodiscard]] v8::MaybeLocal Copy(v8::Isolate* isolate, base::span data); } // namespace electron::Buffer #endif // ELECTRON_SHELL_COMMON_NODE_UTIL_H_