-
-
Notifications
You must be signed in to change notification settings - Fork 35.7k
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
Open
mcollina
wants to merge
1
commit into
nodejs:main
Choose a base branch
from
mcollina:stream/webstreams-cpp-helpers
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+161
−37
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,145 @@ | ||
| #include "node_binding.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::Object; | ||
| using v8::SharedArrayBuffer; | ||
| using v8::Uint32; | ||
| using v8::Uint8Array; | ||
| using v8::Value; | ||
|
|
||
| namespace { | ||
|
|
||
| inline bool BufferIsDetached(Local<Value> buffer) { | ||
| if (buffer->IsArrayBuffer()) { | ||
| return buffer.As<ArrayBuffer>()->WasDetached(); | ||
| } | ||
| // SharedArrayBuffers cannot be detached. | ||
| return false; | ||
| } | ||
|
|
||
| inline size_t BufferByteLength(Local<Value> buffer) { | ||
| if (buffer->IsArrayBuffer()) { | ||
| return buffer.As<ArrayBuffer>()->ByteLength(); | ||
| } | ||
| return buffer.As<SharedArrayBuffer>()->ByteLength(); | ||
| } | ||
|
|
||
| } // namespace | ||
|
|
||
| // Equivalent to: | ||
| // Reflect.get(view.constructor.prototype, 'buffer', view) | ||
| // but uses the V8 API directly so it is immune to prototype tampering and | ||
| // avoids the JS-side overhead of the defensive accessor in lib/internal/. | ||
| void ArrayBufferViewGetBuffer(const FunctionCallbackInfo<Value>& args) { | ||
| CHECK(args[0]->IsArrayBufferView()); | ||
| args.GetReturnValue().Set(args[0].As<ArrayBufferView>()->Buffer()); | ||
| } | ||
|
|
||
| void ArrayBufferViewGetByteLength(const FunctionCallbackInfo<Value>& args) { | ||
| CHECK(args[0]->IsArrayBufferView()); | ||
| Local<ArrayBufferView> view = args[0].As<ArrayBufferView>(); | ||
| args.GetReturnValue().Set(static_cast<double>(view->ByteLength())); | ||
| } | ||
|
|
||
| void ArrayBufferViewGetByteOffset(const FunctionCallbackInfo<Value>& args) { | ||
| CHECK(args[0]->IsArrayBufferView()); | ||
| Local<ArrayBufferView> view = args[0].As<ArrayBufferView>(); | ||
| args.GetReturnValue().Set(static_cast<double>(view->ByteOffset())); | ||
| } | ||
|
|
||
| // 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()); | ||
|
|
||
| Local<Object> to_buffer = args[0].As<Object>(); | ||
| Local<Object> from_buffer = args[2].As<Object>(); | ||
|
|
||
| if (to_buffer->StrictEquals(from_buffer)) { | ||
| args.GetReturnValue().Set(false); | ||
| return; | ||
| } | ||
| if (BufferIsDetached(args[0]) || BufferIsDetached(args[2])) { | ||
| 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 = BufferByteLength(args[0]); | ||
| size_t from_byte_length = BufferByteLength(args[2]); | ||
|
|
||
| bool ok = static_cast<uint64_t>(to_index) + count <= to_byte_length && | ||
| static_cast<uint64_t>(from_index) + count <= from_byte_length; | ||
| 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) { | ||
| Isolate* isolate = args.GetIsolate(); | ||
| CHECK(args[0]->IsArrayBufferView()); | ||
| Local<ArrayBufferView> view = args[0].As<ArrayBufferView>(); | ||
| size_t byte_length = view->ByteLength(); | ||
| Local<ArrayBuffer> new_buffer = ArrayBuffer::New(isolate, byte_length); | ||
| 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, "arrayBufferViewGetBuffer", | ||
| ArrayBufferViewGetBuffer); | ||
| SetMethod(context, target, "arrayBufferViewGetByteLength", | ||
| ArrayBufferViewGetByteLength); | ||
| SetMethod(context, target, "arrayBufferViewGetByteOffset", | ||
| ArrayBufferViewGetByteOffset); | ||
| SetMethod(context, target, "canCopyArrayBuffer", CanCopyArrayBuffer); | ||
| SetMethod(context, target, "cloneAsUint8Array", CloneAsUint8Array); | ||
| } | ||
|
|
||
| void RegisterExternalReferences(ExternalReferenceRegistry* registry) { | ||
| registry->Register(ArrayBufferViewGetBuffer); | ||
| registry->Register(ArrayBufferViewGetByteLength); | ||
| registry->Register(ArrayBufferViewGetByteOffset); | ||
| 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) | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Should this guard against overflows?