aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Jarred Sumner <709451+Jarred-Sumner@users.noreply.github.com> 2023-01-28 03:50:32 -0800
committerGravatar Jarred Sumner <709451+Jarred-Sumner@users.noreply.github.com> 2023-01-28 03:50:32 -0800
commit188c1f1f752911495a1405baf123fd1febf4b288 (patch)
tree4610f24bbe11ab762ac19e3ffae7b150adf02b29
parent812490e4a487683ae5ec4de24b3735bfcad11170 (diff)
downloadbun-188c1f1f752911495a1405baf123fd1febf4b288.tar.gz
bun-188c1f1f752911495a1405baf123fd1febf4b288.tar.zst
bun-188c1f1f752911495a1405baf123fd1febf4b288.zip
[buffer] Use `length` instead of `byteLength` for non-uint8array and non-arraybuffer
-rw-r--r--src/bun.js/bindings/JSBuffer.cpp34
1 files changed, 30 insertions, 4 deletions
diff --git a/src/bun.js/bindings/JSBuffer.cpp b/src/bun.js/bindings/JSBuffer.cpp
index 5c451eb2c..19d0f7b92 100644
--- a/src/bun.js/bindings/JSBuffer.cpp
+++ b/src/bun.js/bindings/JSBuffer.cpp
@@ -1882,8 +1882,6 @@ JSC_DEFINE_HOST_FUNCTION(constructJSBuffer, (JSC::JSGlobalObject * lexicalGlobal
return constructBufferFromStringAndEncoding(lexicalGlobalObject, distinguishingArg, encodingArg);
}
- case Uint8ArrayType:
- case Uint8ClampedArrayType:
case Uint16ArrayType:
case Uint32ArrayType:
case Int8ArrayType:
@@ -1892,8 +1890,36 @@ JSC_DEFINE_HOST_FUNCTION(constructJSBuffer, (JSC::JSGlobalObject * lexicalGlobal
case Float32ArrayType:
case Float64ArrayType:
case BigInt64ArrayType:
- case BigUint64ArrayType:
- case DataViewType: {
+ case BigUint64ArrayType: {
+ // byteOffset and byteLength are ignored in this case, which is consitent with Node.js and new Uint8Array()
+ JSC::JSArrayBufferView* view = jsCast<JSC::JSArrayBufferView*>(distinguishingArg.asCell());
+
+ void* data = view->vector();
+ size_t byteLength = view->length();
+
+ if (UNLIKELY(!data)) {
+ throwException(globalObject, throwScope, createRangeError(globalObject, "Buffer is detached"_s));
+ return JSValue::encode({});
+ }
+
+ auto* subclassStructure = globalObject->JSBufferSubclassStructure();
+ auto* uint8Array = JSC::JSUint8Array::createUninitialized(lexicalGlobalObject, subclassStructure, byteLength);
+ if (UNLIKELY(!uint8Array)) {
+ throwOutOfMemoryError(globalObject, throwScope);
+ return JSValue::encode({});
+ }
+
+ if (byteLength) {
+ uint8Array->setFromTypedArray(lexicalGlobalObject, 0, view, 0, byteLength, CopyType::LeftToRight);
+ }
+
+ RELEASE_AND_RETURN(throwScope, JSC::JSValue::encode(uint8Array));
+ break;
+ }
+
+ case DataViewType:
+ case Uint8ArrayType:
+ case Uint8ClampedArrayType: {
// byteOffset and byteLength are ignored in this case, which is consitent with Node.js and new Uint8Array()
JSC::JSArrayBufferView* view = jsCast<JSC::JSArrayBufferView*>(distinguishingArg.asCell());