aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Jarred Sumner <709451+Jarred-Sumner@users.noreply.github.com> 2022-07-11 04:34:38 -0700
committerGravatar Jarred Sumner <709451+Jarred-Sumner@users.noreply.github.com> 2022-07-11 04:34:38 -0700
commitd6d8c0b80dc689de05d8dfe4306e6966f6ffb07e (patch)
treeb78189b9d900c1c73b1f4effab8bc6774cd4c5e2
parentd9491fe022779de98ccc39a2de6c1196280fac39 (diff)
downloadbun-d6d8c0b80dc689de05d8dfe4306e6966f6ffb07e.tar.gz
bun-d6d8c0b80dc689de05d8dfe4306e6966f6ffb07e.tar.zst
bun-d6d8c0b80dc689de05d8dfe4306e6966f6ffb07e.zip
Fixes https://github.com/oven-sh/bun/issues/561
-rw-r--r--src/bun.js/bindings/bindings.cpp46
-rw-r--r--src/bun.js/bindings/exports.zig4
-rw-r--r--test/bun.js/inspect.test.js20
3 files changed, 56 insertions, 14 deletions
diff --git a/src/bun.js/bindings/bindings.cpp b/src/bun.js/bindings/bindings.cpp
index 8c495a066..1d4c2b171 100644
--- a/src/bun.js/bindings/bindings.cpp
+++ b/src/bun.js/bindings/bindings.cpp
@@ -965,7 +965,9 @@ bool JSC__JSValue__asArrayBuffer_(JSC__JSValue JSValue0, JSC__JSGlobalObject* ar
JSC::JSUint8Array* typedArray = JSC::jsCast<JSC::JSUint8Array*>(value);
arg2->len = typedArray->length();
arg2->byte_len = typedArray->byteLength();
- arg2->offset = typedArray->byteOffset();
+ // the offset is already set by vector()
+ // https://github.com/oven-sh/bun/issues/561
+ arg2->offset = 0;
arg2->cell_type = JSC::JSType::Uint8ArrayType;
arg2->ptr = (char*)typedArray->vector();
return true;
@@ -983,7 +985,9 @@ bool JSC__JSValue__asArrayBuffer_(JSC__JSValue JSValue0, JSC__JSGlobalObject* ar
JSC::JSInt8Array* typedArray = JSC::jsCast<JSC::JSInt8Array*>(value);
arg2->len = typedArray->length();
arg2->byte_len = typedArray->byteLength();
- arg2->offset = typedArray->byteOffset();
+ // the offset is already set by vector()
+ // https://github.com/oven-sh/bun/issues/561
+ arg2->offset = 0;
arg2->cell_type = JSC::JSType::Int8ArrayType;
arg2->ptr = (char*)typedArray->vector();
return true;
@@ -993,7 +997,9 @@ bool JSC__JSValue__asArrayBuffer_(JSC__JSValue JSValue0, JSC__JSGlobalObject* ar
JSC::JSUint8ClampedArray* typedArray = JSC::jsCast<JSC::JSUint8ClampedArray*>(value);
arg2->len = typedArray->length();
arg2->byte_len = typedArray->byteLength();
- arg2->offset = typedArray->byteOffset();
+ // the offset is already set by vector()
+ // https://github.com/oven-sh/bun/issues/561
+ arg2->offset = 0;
arg2->cell_type = JSC::JSType::Uint8ClampedArrayType;
arg2->ptr = (char*)typedArray->vector();
return true;
@@ -1002,7 +1008,9 @@ bool JSC__JSValue__asArrayBuffer_(JSC__JSValue JSValue0, JSC__JSGlobalObject* ar
JSC::JSInt16Array* typedArray = JSC::jsCast<JSC::JSInt16Array*>(value);
arg2->len = typedArray->length();
arg2->byte_len = typedArray->byteLength();
- arg2->offset = typedArray->byteOffset();
+ // the offset is already set by vector()
+ // https://github.com/oven-sh/bun/issues/561
+ arg2->offset = 0;
arg2->cell_type = JSC::JSType::Int16ArrayType;
arg2->ptr = (char*)typedArray->vector();
return true;
@@ -1011,7 +1019,9 @@ bool JSC__JSValue__asArrayBuffer_(JSC__JSValue JSValue0, JSC__JSGlobalObject* ar
JSC::JSUint16Array* typedArray = JSC::jsCast<JSC::JSUint16Array*>(value);
arg2->len = typedArray->length();
arg2->byte_len = typedArray->byteLength();
- arg2->offset = typedArray->byteOffset();
+ // the offset is already set by vector()
+ // https://github.com/oven-sh/bun/issues/561
+ arg2->offset = 0;
arg2->cell_type = JSC::JSType::Uint16ArrayType;
arg2->ptr = (char*)typedArray->vector();
return true;
@@ -1020,7 +1030,9 @@ bool JSC__JSValue__asArrayBuffer_(JSC__JSValue JSValue0, JSC__JSGlobalObject* ar
JSC::JSInt32Array* typedArray = JSC::jsCast<JSC::JSInt32Array*>(value);
arg2->len = typedArray->length();
arg2->byte_len = typedArray->byteLength();
- arg2->offset = typedArray->byteOffset();
+ // the offset is already set by vector()
+ // https://github.com/oven-sh/bun/issues/561
+ arg2->offset = 0;
arg2->cell_type = JSC::JSType::Int32ArrayType;
arg2->ptr = (char*)typedArray->vector();
return true;
@@ -1029,7 +1041,9 @@ bool JSC__JSValue__asArrayBuffer_(JSC__JSValue JSValue0, JSC__JSGlobalObject* ar
JSC::JSUint32Array* typedArray = JSC::jsCast<JSC::JSUint32Array*>(value);
arg2->len = typedArray->length();
arg2->byte_len = typedArray->byteLength();
- arg2->offset = typedArray->byteOffset();
+ // the offset is already set by vector()
+ // https://github.com/oven-sh/bun/issues/561
+ arg2->offset = 0;
arg2->cell_type = JSC::JSType::Uint32ArrayType;
arg2->ptr = (char*)typedArray->vector();
return true;
@@ -1038,7 +1052,9 @@ bool JSC__JSValue__asArrayBuffer_(JSC__JSValue JSValue0, JSC__JSGlobalObject* ar
JSC::JSFloat32Array* typedArray = JSC::jsCast<JSC::JSFloat32Array*>(value);
arg2->len = typedArray->length();
arg2->byte_len = typedArray->byteLength();
- arg2->offset = typedArray->byteOffset();
+ // the offset is already set by vector()
+ // https://github.com/oven-sh/bun/issues/561
+ arg2->offset = 0;
arg2->cell_type = JSC::JSType::Float32ArrayType;
arg2->ptr = (char*)typedArray->vector();
return true;
@@ -1047,7 +1063,9 @@ bool JSC__JSValue__asArrayBuffer_(JSC__JSValue JSValue0, JSC__JSGlobalObject* ar
JSC::JSFloat64Array* typedArray = JSC::jsCast<JSC::JSFloat64Array*>(value);
arg2->len = typedArray->length();
arg2->byte_len = typedArray->byteLength();
- arg2->offset = typedArray->byteOffset();
+ // the offset is already set by vector()
+ // https://github.com/oven-sh/bun/issues/561
+ arg2->offset = 0;
arg2->cell_type = JSC::JSType::Float64ArrayType;
arg2->ptr = (char*)typedArray->vector();
return true;
@@ -1056,7 +1074,9 @@ bool JSC__JSValue__asArrayBuffer_(JSC__JSValue JSValue0, JSC__JSGlobalObject* ar
JSC::JSBigInt64Array* typedArray = JSC::jsCast<JSC::JSBigInt64Array*>(value);
arg2->len = typedArray->length();
arg2->byte_len = typedArray->byteLength();
- arg2->offset = typedArray->byteOffset();
+ // the offset is already set by vector()
+ // https://github.com/oven-sh/bun/issues/561
+ arg2->offset = 0;
arg2->cell_type = JSC::JSType::BigInt64ArrayType;
arg2->ptr = (char*)typedArray->vector();
return true;
@@ -1065,7 +1085,9 @@ bool JSC__JSValue__asArrayBuffer_(JSC__JSValue JSValue0, JSC__JSGlobalObject* ar
JSC::JSBigUint64Array* typedArray = JSC::jsCast<JSC::JSBigUint64Array*>(value);
arg2->len = typedArray->length();
arg2->byte_len = typedArray->byteLength();
- arg2->offset = typedArray->byteOffset();
+ // the offset is already set by vector()
+ // https://github.com/oven-sh/bun/issues/561
+ arg2->offset = 0;
arg2->cell_type = JSC::JSType::BigUint64ArrayType;
arg2->ptr = (char*)typedArray->vector();
return true;
@@ -1075,7 +1097,7 @@ bool JSC__JSValue__asArrayBuffer_(JSC__JSValue JSValue0, JSC__JSGlobalObject* ar
if (JSC::JSArrayBufferView* view = JSC::jsDynamicCast<JSC::JSArrayBufferView*>(value)) {
arg2->len = view->length();
arg2->byte_len = view->byteLength();
- arg2->offset = view->byteOffset();
+ arg2->offset = 0;
arg2->cell_type = view->type();
arg2->ptr = (char*)view->vector();
return true;
diff --git a/src/bun.js/bindings/exports.zig b/src/bun.js/bindings/exports.zig
index d9891f394..3a00ea210 100644
--- a/src/bun.js/bindings/exports.zig
+++ b/src/bun.js/bindings/exports.zig
@@ -2141,10 +2141,10 @@ pub const ZigConsoleClient = struct {
const arrayBuffer = value.asArrayBuffer(this.globalThis).?;
writer.writeAll(std.mem.span(@tagName(arrayBuffer.typed_array_type)));
- writer.print("({d}) [ ", .{arrayBuffer.len});
-
const slice = arrayBuffer.slice();
+ writer.print("({d}) [ ", .{slice.len});
+
if (slice.len > 0) {
writer.print(comptime Output.prettyFmt("<r><yellow>{d}<r>", enable_ansi_colors), .{slice[0]});
var leftover = slice[1..];
diff --git a/test/bun.js/inspect.test.js b/test/bun.js/inspect.test.js
index bf5021c33..d58b8a6bc 100644
--- a/test/bun.js/inspect.test.js
+++ b/test/bun.js/inspect.test.js
@@ -1,5 +1,25 @@
import { it, expect } from "bun:test";
+// https://github.com/oven-sh/bun/issues/561
+it("TypedArray prints", () => {
+ // TODO: add tests for all variants of typed arrays
+ // even if the code is the same for each implementation, we should test it
+ const buffer = new Uint8Array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
+
+ const input = Bun.inspect(buffer);
+
+ expect(input).toBe(
+ `Uint8Array(${buffer.length}) [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ]`
+ );
+ for (let i = 1; i < buffer.length + 1; i++) {
+ expect(Bun.inspect(buffer.subarray(i))).toBe(
+ `Uint8Array(${buffer.length - i}) [ ` +
+ [...buffer.subarray(i)].join(", ") +
+ " ]"
+ );
+ }
+});
+
it("jsx with two elements", () => {
const input = Bun.inspect(
<div hello="quoted">