dawn/node: Fix array buffer view conversion

A Napi::TypedArray may have a non-zero offset on its underlying
ArrayBuffer backing buffer. Also use the length of the typed array,
not the backing buffer.

Change-Id: Icaa310ef8f87393b4d7582ba0a0afb2b87318664
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/121820
Reviewed-by: Ben Clayton <bclayton@google.com>
Kokoro: Kokoro <noreply+kokoro@google.com>
Commit-Queue: James Price <jrprice@google.com>
This commit is contained in:
James Price 2023-02-27 21:21:02 +00:00 committed by Dawn LUCI CQ
parent 148e7fab1c
commit ae3e9adc1b
1 changed files with 9 additions and 4 deletions

View File

@ -153,15 +153,20 @@ bool Converter::Convert(wgpu::ImageCopyBuffer& out, const interop::GPUImageCopyB
bool Converter::Convert(BufferSource& out, interop::BufferSource in) { bool Converter::Convert(BufferSource& out, interop::BufferSource in) {
out = {}; out = {};
if (auto* view = std::get_if<interop::ArrayBufferView>(&in)) { if (auto* view = std::get_if<interop::ArrayBufferView>(&in)) {
std::visit( return std::visit(
[&](auto&& v) { [&](auto&& v) {
auto arr = v.ArrayBuffer(); auto arr = v.ArrayBuffer();
out.data = arr.Data(); if (v.ByteOffset() + v.ByteLength() > arr.ByteLength()) {
out.size = arr.ByteLength(); Napi::Error::New(env, "offset + length exceeds underlying buffer size")
.ThrowAsJavaScriptException();
return false;
}
out.data = static_cast<uint8_t*>(arr.Data()) + v.ByteOffset();
out.size = v.ByteLength();
out.bytesPerElement = v.ElementSize(); out.bytesPerElement = v.ElementSize();
return true;
}, },
*view); *view);
return true;
} }
if (auto* arr = std::get_if<interop::ArrayBuffer>(&in)) { if (auto* arr = std::get_if<interop::ArrayBuffer>(&in)) {
out.data = arr->Data(); out.data = arr->Data();