electron/shell/common/node_util.h
trop[bot] 5a4ef1cc33
refactor: make node Buffers more friendly to base::span / std::span (#46778)
* refactor: add electron::Buffer namespace; move the Buffer as_byte_span() into it

Co-authored-by: Charles Kerr <charles@charleskerr.com>

* feat: add electron::Buffer::Copy()

a span-friendly version of node::Buffer::Copy()

Co-authored-by: Charles Kerr <charles@charleskerr.com>

* refactor: use electron::Buffer::Copy() in electron_api_base_window.cc

Co-authored-by: Charles Kerr <charles@charleskerr.com>

* refactor: use electron::Buffer::Copy() in electron_api_data_pipe_holder.cc

Co-authored-by: Charles Kerr <charles@charleskerr.com>

* refactor: use electron::Buffer::Copy() in electron_api_safe_storage.cc

Co-authored-by: Charles Kerr <charles@charleskerr.com>

* refactor: use electron::Buffer::Copy() in electron_api_clipboard.cc

Co-authored-by: Charles Kerr <charles@charleskerr.com>

* refactor: use electron::Buffer::Copy() in osr_converter.cc

Co-authored-by: Charles Kerr <charles@charleskerr.com>

* refactor: use electron::Buffer::Copy() in electron_api_native_image.cc

Co-authored-by: Charles Kerr <charles@charleskerr.com>

* refactor: use electron::Buffer::Copy() in net_converter.cc

Co-authored-by: Charles Kerr <charles@charleskerr.com>

* refactor: use electron::Buffer::Copy() in electron_api_web_contents.cc

Co-authored-by: Charles Kerr <charles@charleskerr.com>

* refactor: make NewEmptyBuffer() return a Local<Value>

Co-authored-by: Charles Kerr <charles@charleskerr.com>

---------

Co-authored-by: trop[bot] <37223003+trop[bot]@users.noreply.github.com>
Co-authored-by: Charles Kerr <charles@charleskerr.com>
2025-04-25 10:24:43 -05:00

75 lines
2.6 KiB
C++

// 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 <string>
#include <string_view>
#include <vector>
#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 JavscriptEnvironment's isolate
void EmitWarning(std::string_view warning_msg, std::string_view warning_type);
// 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<v8::Value> CompileAndCall(
v8::Local<v8::Context> context,
const char* id,
std::vector<v8::Local<v8::String>>* parameters,
std::vector<v8::Local<v8::Value>>* arguments);
// Wrapper for node::CreateEnvironment that logs failure
node::Environment* CreateEnvironment(v8::Isolate* isolate,
node::IsolateData* isolate_data,
v8::Local<v8::Context> context,
const std::vector<std::string>& args,
const std::vector<std::string>& 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<uint8_t> as_byte_span(
v8::Local<v8::Value> node_buffer);
// span-friendly version of node::Buffer::Copy()
[[nodiscard]] v8::MaybeLocal<v8::Object> Copy(v8::Isolate* isolate,
base::span<const char> data);
// span-friendly version of node::Buffer::Copy()
[[nodiscard]] v8::MaybeLocal<v8::Object> Copy(v8::Isolate* isolate,
base::span<const uint8_t> data);
} // namespace electron::Buffer
#endif // ELECTRON_SHELL_COMMON_NODE_UTIL_H_