Skip to content

Commit ef427fa

Browse files
committed
Map ArrayBuffer and ArrayBufferView to u8.
Depends on lightpanda-io/zig-v8-fork#86 Built ontop of #906 just because this is the feature that uses it.
1 parent 77b6377 commit ef427fa

File tree

3 files changed

+17
-4
lines changed

3 files changed

+17
-4
lines changed

build.zig.zon

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
.hash = "tigerbeetle_io-0.0.0-ViLgxpyRBAB5BMfIcj3KMXfbJzwARs9uSl8aRy2OXULd",
1414
},
1515
.v8 = .{
16-
.url = "https://github.com/lightpanda-io/zig-v8-fork/archive/10025d52cb1d33434f18634c326e67038fd927d5.tar.gz",
17-
.hash = "v8-0.0.0-xddH6-vCAwCtIRfzEw1zKu0TnMbDrFltZ8I0x-PALyIh",
16+
.url = "https://github.com/lightpanda-io/zig-v8-fork/archive/b22911e02e4884a76acf52aa9aff2ba169d05b40.tar.gz",
17+
.hash = "v8-0.0.0-xddH69zCAwAzm1u5cQVa-uG5ib2y6PpENXCl8yEYdUYk",
1818
},
1919
//.v8 = .{ .path = "../zig-v8-fork" },
2020
//.tigerbeetle_io = .{ .path = "../tigerbeetle-io" },

src/browser/encoding/TextDecoder.zig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ test "Browser.Encoding.TextDecoder" {
9292
.{ "d1.ignoreBOM", "false" },
9393
.{ "d1.decode(new Uint8Array([240, 160, 174, 183]))", "𠮷" },
9494
.{ "d1.decode(new Uint8Array([0xEF, 0xBB, 0xBF, 240, 160, 174, 183]))", "𠮷" },
95+
.{ "d1.decode(new Uint8Array([49, 50]).buffer)", "12" },
9596

9697
.{ "let d2 = new TextDecoder('utf8', {fatal: true})", null },
9798
.{

src/runtime/js.zig

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -953,9 +953,21 @@ pub fn Env(comptime State: type, comptime WebApis: type) type {
953953
}
954954
},
955955
.slice => {
956+
var force_u8 = false;
957+
var array_buffer: ?v8.ArrayBuffer = null;
956958
if (js_value.isTypedArray()) {
957959
const buffer_view = js_value.castTo(v8.ArrayBufferView);
958-
const buffer = buffer_view.getBuffer();
960+
array_buffer = buffer_view.getBuffer();
961+
} else if (js_value.isArrayBufferView()) {
962+
force_u8 = true;
963+
const buffer_view = js_value.castTo(v8.ArrayBufferView);
964+
array_buffer = buffer_view.getBuffer();
965+
} else if (js_value.isArrayBuffer()) {
966+
force_u8 = true;
967+
array_buffer = js_value.castTo(v8.ArrayBuffer);
968+
}
969+
970+
if (array_buffer) |buffer| {
959971
const backing_store = v8.BackingStore.sharedPtrGet(&buffer.getBackingStore());
960972
const data = backing_store.getData();
961973
const byte_len = backing_store.getByteLength();
@@ -964,7 +976,7 @@ pub fn Env(comptime State: type, comptime WebApis: type) type {
964976
u8 => {
965977
// need this sentinel check to keep the compiler happy
966978
if (ptr.sentinel() == null) {
967-
if (js_value.isUint8Array() or js_value.isUint8ClampedArray()) {
979+
if (force_u8 or js_value.isUint8Array() or js_value.isUint8ClampedArray()) {
968980
const arr_ptr = @as([*]u8, @alignCast(@ptrCast(data)));
969981
return arr_ptr[0..byte_len];
970982
}

0 commit comments

Comments
 (0)