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

@@ -92,6 +92,7 @@ test "Browser.Encoding.TextDecoder" {
.{ "d1.ignoreBOM", "false" },
.{ "d1.decode(new Uint8Array([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 },
.{

View File

@@ -953,9 +953,21 @@ pub fn Env(comptime State: type, comptime WebApis: type) type {
}
},
.slice => {
var force_u8 = false;
var array_buffer: ?v8.ArrayBuffer = null;
if (js_value.isTypedArray()) {
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 data = backing_store.getData();
const byte_len = backing_store.getByteLength();
@@ -964,7 +976,7 @@ pub fn Env(comptime State: type, comptime WebApis: type) type {
u8 => {
// need this sentinel check to keep the compiler happy
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)));
return arr_ptr[0..byte_len];
}