-
-
Notifications
You must be signed in to change notification settings - Fork 35.8k
stream: move WHATWG byte-stream helpers to C++ #63570
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -96,6 +96,7 @@ | |
| V(v8) \ | ||
| V(wasi) \ | ||
| V(wasm_web_api) \ | ||
| V(webstreams) \ | ||
| V(watchdog) \ | ||
| V(worker) \ | ||
| V(zlib) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,134 @@ | ||
| #include "env-inl.h" | ||
| #include "node_binding.h" | ||
| #include "node_errors.h" | ||
| #include "node_external_reference.h" | ||
| #include "util-inl.h" | ||
| #include "v8.h" | ||
|
|
||
| namespace node { | ||
| namespace webstreams { | ||
|
|
||
| using v8::ArrayBuffer; | ||
| using v8::ArrayBufferView; | ||
| using v8::Context; | ||
| using v8::FunctionCallbackInfo; | ||
| using v8::Isolate; | ||
| using v8::Local; | ||
| using v8::Name; | ||
| using v8::Null; | ||
| using v8::Number; | ||
| using v8::Object; | ||
| using v8::Uint32; | ||
| using v8::Uint8Array; | ||
| using v8::Value; | ||
|
|
||
| // Returns { buffer, byteOffset, byteLength } in a single binding crossing, | ||
| // equivalent to reading the three properties via | ||
| // Reflect.get(view.constructor.prototype, ..., view). Uses the V8 API | ||
| // directly so it is immune to prototype tampering and avoids the JS-side | ||
| // overhead of the defensive accessors in lib/internal/. | ||
| void GetArrayBufferView(const FunctionCallbackInfo<Value>& args) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Given that these aren't specific to web streams, it might make sense to just include in them in the existing |
||
| Isolate* isolate = args.GetIsolate(); | ||
| CHECK(args[0]->IsArrayBufferView()); | ||
| Local<ArrayBufferView> view = args[0].As<ArrayBufferView>(); | ||
| Local<Name> names[] = { | ||
| FIXED_ONE_BYTE_STRING(isolate, "buffer"), | ||
| FIXED_ONE_BYTE_STRING(isolate, "byteOffset"), | ||
| FIXED_ONE_BYTE_STRING(isolate, "byteLength"), | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If you're not going to use a cached |
||
| }; | ||
| Local<Value> values[] = { | ||
| view->Buffer(), | ||
| Number::New(isolate, static_cast<double>(view->ByteOffset())), | ||
| Number::New(isolate, static_cast<double>(view->ByteLength())), | ||
| }; | ||
| args.GetReturnValue().Set( | ||
| Object::New(isolate, Null(isolate), names, values, arraysize(names))); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Using a cached
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Alternatively, returning these as an |
||
| } | ||
|
|
||
| // Returns true iff bytes can be safely copied between the buffers given the | ||
| // requested offsets and count. Matches lib/internal/webstreams/util.js: | ||
| // toBuffer !== fromBuffer && | ||
| // !toBuffer.detached && | ||
| // !fromBuffer.detached && | ||
| // toIndex + count <= toBuffer.byteLength && | ||
| // fromIndex + count <= fromBuffer.byteLength | ||
| void CanCopyArrayBuffer(const FunctionCallbackInfo<Value>& args) { | ||
| CHECK(args[0]->IsArrayBuffer() || args[0]->IsSharedArrayBuffer()); | ||
| CHECK(args[1]->IsUint32()); | ||
| CHECK(args[2]->IsArrayBuffer() || args[2]->IsSharedArrayBuffer()); | ||
| CHECK(args[3]->IsUint32()); | ||
| CHECK(args[4]->IsUint32()); | ||
|
Comment on lines
+56
to
+60
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Note that this will crash the process with large buffers, eg. new ReadableStream({
type: 'bytes',
pull(controller) { controller.enqueue(new Uint8Array(2 ** 32)) },
}).getReader({ mode: 'byob' }).read(new Uint8Array(2 ** 32))The byte lengths should probably be validated as Numbers and read as int64_t with |
||
|
|
||
| // SharedArrayBuffer handles are interoperable with ArrayBuffer handles in | ||
| // V8, so we can use the ArrayBuffer accessors uniformly. WasDetached() | ||
| // always returns false on a SAB. | ||
| Local<ArrayBuffer> to_buffer = args[0].As<ArrayBuffer>(); | ||
| Local<ArrayBuffer> from_buffer = args[2].As<ArrayBuffer>(); | ||
|
|
||
| if (to_buffer->StrictEquals(from_buffer)) { | ||
| args.GetReturnValue().Set(false); | ||
| return; | ||
| } | ||
| if (to_buffer->WasDetached() || from_buffer->WasDetached()) { | ||
| args.GetReturnValue().Set(false); | ||
| return; | ||
| } | ||
|
|
||
| uint32_t to_index = args[1].As<Uint32>()->Value(); | ||
| uint32_t from_index = args[3].As<Uint32>()->Value(); | ||
| uint32_t count = args[4].As<Uint32>()->Value(); | ||
|
|
||
| size_t to_byte_length = to_buffer->ByteLength(); | ||
| size_t from_byte_length = from_buffer->ByteLength(); | ||
|
|
||
| bool ok = static_cast<uint64_t>(to_index) + count <= to_byte_length && | ||
| static_cast<uint64_t>(from_index) + count <= from_byte_length; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should this guard against overflows?
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. defensively, probably. In practice overflow is extremely unlikely so I'd say it's likely optional. |
||
| args.GetReturnValue().Set(ok); | ||
| } | ||
|
|
||
| // Equivalent to: | ||
| // new Uint8Array(view.buffer.slice(view.byteOffset, | ||
| // view.byteOffset + view.byteLength)) | ||
| // Allocates a fresh ArrayBuffer with the view's bytes copied into it, then | ||
| // returns a Uint8Array over the full new buffer. Avoids the JS-side | ||
| // Reflect.get + slice round-trip. | ||
| void CloneAsUint8Array(const FunctionCallbackInfo<Value>& args) { | ||
| Environment* env = Environment::GetCurrent(args); | ||
| Isolate* isolate = env->isolate(); | ||
| CHECK(args[0]->IsArrayBufferView()); | ||
| Local<ArrayBufferView> view = args[0].As<ArrayBufferView>(); | ||
| size_t byte_length = view->ByteLength(); | ||
| Local<ArrayBuffer> new_buffer; | ||
| if (!ArrayBuffer::MaybeNew(isolate, byte_length).ToLocal(&new_buffer)) { | ||
| // MaybeNew does not schedule an exception on allocation failure. | ||
| env->ThrowRangeError("Array buffer allocation failed"); | ||
| return; | ||
| } | ||
| if (byte_length > 0) { | ||
| size_t copied = view->CopyContents(new_buffer->Data(), byte_length); | ||
| CHECK_EQ(copied, byte_length); | ||
| } | ||
| args.GetReturnValue().Set(Uint8Array::New(new_buffer, 0, byte_length)); | ||
| } | ||
|
|
||
| void Initialize(Local<Object> target, | ||
| Local<Value> unused, | ||
| Local<Context> context, | ||
| void* priv) { | ||
| SetMethod(context, target, "getArrayBufferView", GetArrayBufferView); | ||
| SetMethod(context, target, "canCopyArrayBuffer", CanCopyArrayBuffer); | ||
| SetMethod(context, target, "cloneAsUint8Array", CloneAsUint8Array); | ||
| } | ||
|
|
||
| void RegisterExternalReferences(ExternalReferenceRegistry* registry) { | ||
| registry->Register(GetArrayBufferView); | ||
| registry->Register(CanCopyArrayBuffer); | ||
| registry->Register(CloneAsUint8Array); | ||
| } | ||
|
|
||
| } // namespace webstreams | ||
| } // namespace node | ||
|
|
||
| NODE_BINDING_CONTEXT_AWARE_INTERNAL(webstreams, node::webstreams::Initialize) | ||
| NODE_BINDING_EXTERNAL_REFERENCE(webstreams, | ||
| node::webstreams::RegisterExternalReferences) | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: matching convention....