refactor: make node Buffers more friendly to base::span / std::span (#46724)

* refactor: add electron::Buffer namespace; move the Buffer as_byte_span() into it

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

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

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

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

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

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

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

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

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

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

* refactor: make NewEmptyBuffer() return a Local<Value>
This commit is contained in:
Charles Kerr 2025-04-25 08:00:09 -05:00 committed by GitHub
parent 1976e935e7
commit 06a99d6770
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
10 changed files with 93 additions and 72 deletions

View file

@ -34,6 +34,7 @@
#include "shell/common/gin_converters/value_converter.h"
#include "shell/common/gin_helper/promise.h"
#include "shell/common/node_includes.h"
#include "shell/common/node_util.h"
#include "shell/common/v8_util.h"
namespace gin {
@ -524,11 +525,11 @@ v8::Local<v8::Value> Converter<network::ResourceRequestBody>::ToV8(
}
case network::mojom::DataElement::Tag::kBytes: {
upload_data.Set("type", "rawData");
const auto& bytes = element.As<network::DataElementBytes>().bytes();
const char* data = reinterpret_cast<const char*>(bytes.data());
upload_data.Set(
"bytes",
node::Buffer::Copy(isolate, data, bytes.size()).ToLocalChecked());
electron::Buffer::Copy(
isolate, element.As<network::DataElementBytes>().bytes())
.ToLocalChecked());
break;
}
case network::mojom::DataElement::Tag::kDataPipe: {

View file

@ -111,12 +111,10 @@ v8::Local<v8::Value> Converter<electron::OffscreenSharedTextureValue>::ToV8(
dict.Set("metadata", ConvertToV8(isolate, metadata));
#if BUILDFLAG(IS_WIN) || BUILDFLAG(IS_MAC)
auto handle_buf = node::Buffer::Copy(
isolate,
reinterpret_cast<char*>(
const_cast<uintptr_t*>(&val.shared_texture_handle)),
sizeof(val.shared_texture_handle));
dict.Set("sharedTextureHandle", handle_buf.ToLocalChecked());
dict.Set("sharedTextureHandle",
electron::Buffer::Copy(
isolate, base::byte_span_from_ref(val.shared_texture_handle))
.ToLocalChecked());
#elif BUILDFLAG(IS_LINUX)
auto v8_planes = base::ToVector(val.planes, [isolate](const auto& plane) {
gin::Dictionary v8_plane(isolate, v8::Object::New(isolate));