Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 9 additions & 22 deletions src/node_wasm_web_api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
namespace node {
namespace wasm_web_api {

using v8::ArrayBuffer;
using v8::ArrayBufferView;
using v8::Context;
using v8::Function;
using v8::FunctionCallbackInfo;
Expand All @@ -17,6 +15,7 @@ using v8::Isolate;
using v8::Local;
using v8::MaybeLocal;
using v8::Object;
using v8::Uint8Array;
using v8::Value;
using v8::WasmStreaming;

Expand Down Expand Up @@ -98,28 +97,16 @@ void WasmStreamingObject::Push(const FunctionCallbackInfo<Value>& args) {
CHECK_EQ(args.Length(), 1);
Local<Value> chunk = args[0];

// The start of the memory section backing the ArrayBuffer(View), the offset
// of the ArrayBuffer(View) within the memory section, and its size in bytes.
const void* bytes;
size_t offset;
size_t size;

if (chunk->IsArrayBufferView()) [[likely]] {
Local<ArrayBufferView> view = chunk.As<ArrayBufferView>();
bytes = view->Buffer()->Data();
offset = view->ByteOffset();
size = view->ByteLength();
} else if (chunk->IsArrayBuffer()) [[likely]] {
Local<ArrayBuffer> buffer = chunk.As<ArrayBuffer>();
bytes = buffer->Data();
offset = 0;
size = buffer->ByteLength();
} else {
return node::THROW_ERR_INVALID_ARG_TYPE(
Environment::GetCurrent(args),
"chunk must be an ArrayBufferView or an ArrayBuffer");
if (!chunk->IsUint8Array()) [[unlikely]] {
return node::THROW_ERR_INVALID_ARG_TYPE(Environment::GetCurrent(args),
"chunk must be a Uint8Array");
}

Local<Uint8Array> view = chunk.As<Uint8Array>();
const void* bytes = view->Buffer()->Data();
size_t offset = view->ByteOffset();
size_t size = view->ByteLength();

// Forward the data to V8. Internally, V8 will make a copy.
obj->streaming_->OnBytesReceived(static_cast<const uint8_t*>(bytes) + offset,
size);
Expand Down
Loading