|
| 1 | +#include "node_binding.h" |
| 2 | +#include "node_external_reference.h" |
| 3 | +#include "util-inl.h" |
| 4 | +#include "v8.h" |
| 5 | + |
| 6 | +namespace node { |
| 7 | +namespace webstreams { |
| 8 | + |
| 9 | +using v8::ArrayBuffer; |
| 10 | +using v8::ArrayBufferView; |
| 11 | +using v8::Context; |
| 12 | +using v8::FunctionCallbackInfo; |
| 13 | +using v8::Isolate; |
| 14 | +using v8::Local; |
| 15 | +using v8::Object; |
| 16 | +using v8::SharedArrayBuffer; |
| 17 | +using v8::Uint32; |
| 18 | +using v8::Uint8Array; |
| 19 | +using v8::Value; |
| 20 | + |
| 21 | +namespace { |
| 22 | + |
| 23 | +inline bool BufferIsDetached(Local<Value> buffer) { |
| 24 | + if (buffer->IsArrayBuffer()) { |
| 25 | + return buffer.As<ArrayBuffer>()->WasDetached(); |
| 26 | + } |
| 27 | + // SharedArrayBuffers cannot be detached. |
| 28 | + return false; |
| 29 | +} |
| 30 | + |
| 31 | +inline size_t BufferByteLength(Local<Value> buffer) { |
| 32 | + if (buffer->IsArrayBuffer()) { |
| 33 | + return buffer.As<ArrayBuffer>()->ByteLength(); |
| 34 | + } |
| 35 | + return buffer.As<SharedArrayBuffer>()->ByteLength(); |
| 36 | +} |
| 37 | + |
| 38 | +} // namespace |
| 39 | + |
| 40 | +// Equivalent to: |
| 41 | +// Reflect.get(view.constructor.prototype, 'buffer', view) |
| 42 | +// but uses the V8 API directly so it is immune to prototype tampering and |
| 43 | +// avoids the JS-side overhead of the defensive accessor in lib/internal/. |
| 44 | +void ArrayBufferViewGetBuffer(const FunctionCallbackInfo<Value>& args) { |
| 45 | + CHECK(args[0]->IsArrayBufferView()); |
| 46 | + args.GetReturnValue().Set(args[0].As<ArrayBufferView>()->Buffer()); |
| 47 | +} |
| 48 | + |
| 49 | +void ArrayBufferViewGetByteLength(const FunctionCallbackInfo<Value>& args) { |
| 50 | + CHECK(args[0]->IsArrayBufferView()); |
| 51 | + Local<ArrayBufferView> view = args[0].As<ArrayBufferView>(); |
| 52 | + args.GetReturnValue().Set(static_cast<double>(view->ByteLength())); |
| 53 | +} |
| 54 | + |
| 55 | +void ArrayBufferViewGetByteOffset(const FunctionCallbackInfo<Value>& args) { |
| 56 | + CHECK(args[0]->IsArrayBufferView()); |
| 57 | + Local<ArrayBufferView> view = args[0].As<ArrayBufferView>(); |
| 58 | + args.GetReturnValue().Set(static_cast<double>(view->ByteOffset())); |
| 59 | +} |
| 60 | + |
| 61 | +// Returns true iff bytes can be safely copied between the buffers given the |
| 62 | +// requested offsets and count. Matches lib/internal/webstreams/util.js: |
| 63 | +// toBuffer !== fromBuffer && |
| 64 | +// !toBuffer.detached && |
| 65 | +// !fromBuffer.detached && |
| 66 | +// toIndex + count <= toBuffer.byteLength && |
| 67 | +// fromIndex + count <= fromBuffer.byteLength |
| 68 | +void CanCopyArrayBuffer(const FunctionCallbackInfo<Value>& args) { |
| 69 | + CHECK(args[0]->IsArrayBuffer() || args[0]->IsSharedArrayBuffer()); |
| 70 | + CHECK(args[1]->IsUint32()); |
| 71 | + CHECK(args[2]->IsArrayBuffer() || args[2]->IsSharedArrayBuffer()); |
| 72 | + CHECK(args[3]->IsUint32()); |
| 73 | + CHECK(args[4]->IsUint32()); |
| 74 | + |
| 75 | + Local<Object> to_buffer = args[0].As<Object>(); |
| 76 | + Local<Object> from_buffer = args[2].As<Object>(); |
| 77 | + |
| 78 | + if (to_buffer->StrictEquals(from_buffer)) { |
| 79 | + args.GetReturnValue().Set(false); |
| 80 | + return; |
| 81 | + } |
| 82 | + if (BufferIsDetached(args[0]) || BufferIsDetached(args[2])) { |
| 83 | + args.GetReturnValue().Set(false); |
| 84 | + return; |
| 85 | + } |
| 86 | + |
| 87 | + uint32_t to_index = args[1].As<Uint32>()->Value(); |
| 88 | + uint32_t from_index = args[3].As<Uint32>()->Value(); |
| 89 | + uint32_t count = args[4].As<Uint32>()->Value(); |
| 90 | + |
| 91 | + size_t to_byte_length = BufferByteLength(args[0]); |
| 92 | + size_t from_byte_length = BufferByteLength(args[2]); |
| 93 | + |
| 94 | + bool ok = static_cast<uint64_t>(to_index) + count <= to_byte_length && |
| 95 | + static_cast<uint64_t>(from_index) + count <= from_byte_length; |
| 96 | + args.GetReturnValue().Set(ok); |
| 97 | +} |
| 98 | + |
| 99 | +// Equivalent to: |
| 100 | +// new Uint8Array(view.buffer.slice(view.byteOffset, |
| 101 | +// view.byteOffset + view.byteLength)) |
| 102 | +// Allocates a fresh ArrayBuffer with the view's bytes copied into it, then |
| 103 | +// returns a Uint8Array over the full new buffer. Avoids the JS-side |
| 104 | +// Reflect.get + slice round-trip. |
| 105 | +void CloneAsUint8Array(const FunctionCallbackInfo<Value>& args) { |
| 106 | + Isolate* isolate = args.GetIsolate(); |
| 107 | + CHECK(args[0]->IsArrayBufferView()); |
| 108 | + Local<ArrayBufferView> view = args[0].As<ArrayBufferView>(); |
| 109 | + size_t byte_length = view->ByteLength(); |
| 110 | + Local<ArrayBuffer> new_buffer = ArrayBuffer::New(isolate, byte_length); |
| 111 | + if (byte_length > 0) { |
| 112 | + size_t copied = view->CopyContents(new_buffer->Data(), byte_length); |
| 113 | + CHECK_EQ(copied, byte_length); |
| 114 | + } |
| 115 | + args.GetReturnValue().Set(Uint8Array::New(new_buffer, 0, byte_length)); |
| 116 | +} |
| 117 | + |
| 118 | +void Initialize(Local<Object> target, |
| 119 | + Local<Value> unused, |
| 120 | + Local<Context> context, |
| 121 | + void* priv) { |
| 122 | + SetMethod(context, target, "arrayBufferViewGetBuffer", |
| 123 | + ArrayBufferViewGetBuffer); |
| 124 | + SetMethod(context, target, "arrayBufferViewGetByteLength", |
| 125 | + ArrayBufferViewGetByteLength); |
| 126 | + SetMethod(context, target, "arrayBufferViewGetByteOffset", |
| 127 | + ArrayBufferViewGetByteOffset); |
| 128 | + SetMethod(context, target, "canCopyArrayBuffer", CanCopyArrayBuffer); |
| 129 | + SetMethod(context, target, "cloneAsUint8Array", CloneAsUint8Array); |
| 130 | +} |
| 131 | + |
| 132 | +void RegisterExternalReferences(ExternalReferenceRegistry* registry) { |
| 133 | + registry->Register(ArrayBufferViewGetBuffer); |
| 134 | + registry->Register(ArrayBufferViewGetByteLength); |
| 135 | + registry->Register(ArrayBufferViewGetByteOffset); |
| 136 | + registry->Register(CanCopyArrayBuffer); |
| 137 | + registry->Register(CloneAsUint8Array); |
| 138 | +} |
| 139 | + |
| 140 | +} // namespace webstreams |
| 141 | +} // namespace node |
| 142 | + |
| 143 | +NODE_BINDING_CONTEXT_AWARE_INTERNAL(webstreams, node::webstreams::Initialize) |
| 144 | +NODE_BINDING_EXTERNAL_REFERENCE(webstreams, |
| 145 | + node::webstreams::RegisterExternalReferences) |
0 commit comments