Skip to content

Lightpanda: the headless browser designed for AI and automation

Past the null check, js.NullableString behaves just like []const u8

Karl Seguine27447424e5c
diff --git a/src/browser/js/Local.zig b/src/browser/js/Local.zig
index 9aaf6b5c3..d280681cd 100644
--- a/src/browser/js/Local.zig
+++ b/src/browser/js/Local.zig
@@ -518,7 +518,7 @@ pub fn jsValueToZig(self: *const Local, comptime T: type, js_val: js.Value) !T {
if (js_val.isUndefined()) {
return null;
}
- return .{ .value = try js_val.toStringSlice() };
+ return .{ .value = try self.jsValueToZig([]const u8, js_val) };
}
if (comptime o.child == js.Object) {
@@ -552,7 +552,7 @@ pub fn jsValueToZig(self: *const Local, comptime T: type, js_val: js.Value) !T {
}
},
.slice => {
- if (ptr.sentinel() == null) {
+ if (ptr.sentinel() == null and ptr.child != u8) {
if (try jsValueToTypedArray(ptr.child, js_val)) |value| {
return value;
}
@@ -1054,13 +1054,8 @@ fn probeJsValueToZig(self: *const Local, comptime T: type, js_val: js.Value) !Pr
return .{ .invalid = {} };
},
.slice => {
- if (js_val.isTypedArray()) {
+ if (js_val.isTypedArray() and ptr.child != u8) {
switch (ptr.child) {
- u8 => if (ptr.sentinel() == null) {
- if (js_val.isUint8Array() or js_val.isUint8ClampedArray()) {
- return .{ .ok = {} };
- }
- },
i8 => if (js_val.isInt8Array()) {
return .{ .ok = {} };
},
diff --git a/src/browser/js/WasmStreaming.zig b/src/browser/js/WasmStreaming.zig
index 7d53a3139..c987dbe17 100644
--- a/src/browser/js/WasmStreaming.zig
+++ b/src/browser/js/WasmStreaming.zig
@@ -101,8 +101,8 @@ fn onResponse(self: *WasmStreaming, value: js.Value, exec: *js.Execution) void {
) catch |err| self.abortZigError(local, err);
}
-fn onBytes(self: *WasmStreaming, bytes: []const u8) void {
- v8.v8__WasmStreaming__OnBytesReceived(&self.handle, bytes.ptr, bytes.len);
+fn onBytes(self: *WasmStreaming, bytes: js.TypedArray(u8)) void {
+ v8.v8__WasmStreaming__OnBytesReceived(&self.handle, bytes.values.ptr, bytes.values.len);
v8.v8__WasmStreaming__Finish(&self.handle);
self.release();
}
diff --git a/src/browser/tests/domexception.html b/src/browser/tests/domexception.html
index 627ba68a6..57d254a9a 100644
--- a/src/browser/tests/domexception.html
+++ b/src/browser/tests/domexception.html
@@ -40,7 +40,7 @@
{
// message/name are WebIDL DOMStrings: a non-string argument is stringified
// (ToString), never adopted as raw bytes. A TypedArray stringifies via its
- // Array-inherited toString, so its contents are joined, not read as bytes.
+ // Array-inherited toString, so its contents are joined.
const ex = new DOMException(new Uint8Array([65, 66, 67]), 'NotFoundError');
testing.expectEqual('65,66,67', ex.message);
testing.expectEqual('NotFoundError', ex.name);
@@ -54,6 +54,7 @@
// null stringifies to "null"; a missing/undefined argument uses the default.
testing.expectEqual('null', new DOMException(null).message);
+ testing.expectEqual('null', new DOMException('m', null).name);
testing.expectEqual('', new DOMException(undefined).message);
testing.expectEqual('Error', new DOMException(undefined, undefined).name);
}
diff --git a/src/browser/webapi/encoding/TextDecoder.zig b/src/browser/webapi/encoding/TextDecoder.zig
index e052bdff5..634c2511e 100644
--- a/src/browser/webapi/encoding/TextDecoder.zig
+++ b/src/browser/webapi/encoding/TextDecoder.zig
@@ -112,9 +112,9 @@ const DecodeOpts = struct {
stream: bool = false,
};
-pub fn decode(self: *TextDecoder, input_: ?[]const u8, opts_: ?DecodeOpts, exec: *const js.Execution) ![]const u8 {
+pub fn decode(self: *TextDecoder, input_: ?js.AllowSharedBufferSource, opts_: ?DecodeOpts, exec: *const js.Execution) ![]const u8 {
const opts: DecodeOpts = opts_ orelse .{};
- const input = input_ orelse "";
+ const input: []const u8 = if (input_) |i| i.bytes else "";
if (opts.stream) {
// Streaming mode: create decoder if needed, keep it alive
diff --git a/src/browser/webapi/encoding/TextEncoder.zig b/src/browser/webapi/encoding/TextEncoder.zig
index 6a11ab183..27eaa1b81 100644
--- a/src/browser/webapi/encoding/TextEncoder.zig
+++ b/src/browser/webapi/encoding/TextEncoder.zig
@@ -62,7 +62,7 @@ pub fn encodeInto(_: *const TextEncoder, source_: js.Value, destination_: js.Val
if (!destination_.isUint8Array()) {
return error.InvalidArgument;
}
- const dest = try destination_.toZig([]u8);
+ const dest = (try destination_.toZig(js.AllowSharedBufferSource)).bytes;
// V8 encodes straight into the destination, never writing a partial
// sequence, and replaces lone surrogates as the USVString conversion would.
diff --git a/src/browser/webapi/net/Response.zig b/src/browser/webapi/net/Response.zig
index b0faf7b81..e3f431e6a 100644
--- a/src/browser/webapi/net/Response.zig
+++ b/src/browser/webapi/net/Response.zig
@@ -584,7 +584,7 @@ const StreamConsumer = struct {
// A chunk that is not a Uint8Array is a TypeError.
return self.finish(local, "Response body chunk is not a Uint8Array");
}
- const chunk_copy = try self.arena.dupe(u8, try local.jsValueToZig([]u8, value));
+ const chunk_copy = try self.arena.dupe(u8, (try local.jsValueToZig(js.TypedArray(u8), value)).values);
try self.chunks.append(self.arena, chunk_copy);
self.total_len += chunk_copy.len;
try self.pumpRead();