Map ArrayBuffer and ArrayBufferView to u8.

Depends on https://github.com/lightpanda-io/zig-v8-fork/pull/86

Built ontop of https://github.com/lightpanda-io/browser/pull/906 just because
this is the feature that uses it.
This commit is contained in:
Karl Seguin
2025-07-21 19:46:57 +08:00
parent 77b6377473
commit ef427fa966
3 changed files with 17 additions and 4 deletions

View File

@@ -13,8 +13,8 @@
.hash = "tigerbeetle_io-0.0.0-ViLgxpyRBAB5BMfIcj3KMXfbJzwARs9uSl8aRy2OXULd", .hash = "tigerbeetle_io-0.0.0-ViLgxpyRBAB5BMfIcj3KMXfbJzwARs9uSl8aRy2OXULd",
}, },
.v8 = .{ .v8 = .{
.url = "https://github.com/lightpanda-io/zig-v8-fork/archive/10025d52cb1d33434f18634c326e67038fd927d5.tar.gz", .url = "https://github.com/lightpanda-io/zig-v8-fork/archive/b22911e02e4884a76acf52aa9aff2ba169d05b40.tar.gz",
.hash = "v8-0.0.0-xddH6-vCAwCtIRfzEw1zKu0TnMbDrFltZ8I0x-PALyIh", .hash = "v8-0.0.0-xddH69zCAwAzm1u5cQVa-uG5ib2y6PpENXCl8yEYdUYk",
}, },
//.v8 = .{ .path = "../zig-v8-fork" }, //.v8 = .{ .path = "../zig-v8-fork" },
//.tigerbeetle_io = .{ .path = "../tigerbeetle-io" }, //.tigerbeetle_io = .{ .path = "../tigerbeetle-io" },

View File

@@ -92,6 +92,7 @@ test "Browser.Encoding.TextDecoder" {
.{ "d1.ignoreBOM", "false" }, .{ "d1.ignoreBOM", "false" },
.{ "d1.decode(new Uint8Array([240, 160, 174, 183]))", "𠮷" }, .{ "d1.decode(new Uint8Array([240, 160, 174, 183]))", "𠮷" },
.{ "d1.decode(new Uint8Array([0xEF, 0xBB, 0xBF, 240, 160, 174, 183]))", "𠮷" }, .{ "d1.decode(new Uint8Array([0xEF, 0xBB, 0xBF, 240, 160, 174, 183]))", "𠮷" },
.{ "d1.decode(new Uint8Array([49, 50]).buffer)", "12" },
.{ "let d2 = new TextDecoder('utf8', {fatal: true})", null }, .{ "let d2 = new TextDecoder('utf8', {fatal: true})", null },
.{ .{

View File

@@ -953,9 +953,21 @@ pub fn Env(comptime State: type, comptime WebApis: type) type {
} }
}, },
.slice => { .slice => {
var force_u8 = false;
var array_buffer: ?v8.ArrayBuffer = null;
if (js_value.isTypedArray()) { if (js_value.isTypedArray()) {
const buffer_view = js_value.castTo(v8.ArrayBufferView); const buffer_view = js_value.castTo(v8.ArrayBufferView);
const buffer = buffer_view.getBuffer(); array_buffer = buffer_view.getBuffer();
} else if (js_value.isArrayBufferView()) {
force_u8 = true;
const buffer_view = js_value.castTo(v8.ArrayBufferView);
array_buffer = buffer_view.getBuffer();
} else if (js_value.isArrayBuffer()) {
force_u8 = true;
array_buffer = js_value.castTo(v8.ArrayBuffer);
}
if (array_buffer) |buffer| {
const backing_store = v8.BackingStore.sharedPtrGet(&buffer.getBackingStore()); const backing_store = v8.BackingStore.sharedPtrGet(&buffer.getBackingStore());
const data = backing_store.getData(); const data = backing_store.getData();
const byte_len = backing_store.getByteLength(); const byte_len = backing_store.getByteLength();
@@ -964,7 +976,7 @@ pub fn Env(comptime State: type, comptime WebApis: type) type {
u8 => { u8 => {
// need this sentinel check to keep the compiler happy // need this sentinel check to keep the compiler happy
if (ptr.sentinel() == null) { if (ptr.sentinel() == null) {
if (js_value.isUint8Array() or js_value.isUint8ClampedArray()) { if (force_u8 or js_value.isUint8Array() or js_value.isUint8ClampedArray()) {
const arr_ptr = @as([*]u8, @alignCast(@ptrCast(data))); const arr_ptr = @as([*]u8, @alignCast(@ptrCast(data)));
return arr_ptr[0..byte_len]; return arr_ptr[0..byte_len];
} }