electron/shell/common/node_util.h
trop[bot] 6964b79e68
refactor: spanify image utils (#44178)
* refactor: electron::util::AddImageSkiaRepFromJPEG() takes a span arg

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

* refactor: electron::util::AddImageSkiaRepFromPNG() takes a span arg

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

* refactor: electron::util::AddImageSkiaRepFromBuffer() takes a span arg

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

* feat: add Node-buffer-to-base-span helper function

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

* refactor: electron::api::NativeImage::CreateFromPNG() now takes a span param

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

* refactor: electron::api::NativeImage::CreateFromJPEG() now takes a span param

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

* refactor: use base::as_byte_span()

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

* fix: -Wunsafe-buffer-usage warning in NativeImage::CreateFromNamedImage()

Warning fixed by this commit:

../../electron/shell/common/api/electron_api_native_image_mac.mm:131:11: error: function introduces unsafe buffer manipulation [-Werror,-Wunsafe-buffer-usage]
  131 |           {reinterpret_cast<const uint8_t*>((char*)[png_data bytes]),
      |           ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  132 |            [png_data length]});
      |            ~~~~~~~~~~~~~~~~~~
../../electron/shell/common/api/electron_api_native_image_mac.mm:131:11: note: See //docs/unsafe_buffers.md for help.

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

* chore: add // SAFETY comment for Node-buffer-to-span func

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

* chore: add // SAFETY comment for NSData-to-span func

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>
2024-10-10 13:24:26 -04:00

47 lines
1.5 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_view>
#include <vector>
#include "base/containers/span.h"
#include "v8/include/v8-forward.h"
namespace node {
class Environment;
}
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);
// 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);
} // namespace electron::util
#endif // ELECTRON_SHELL_COMMON_NODE_UTIL_H_