Skip to content

Lightpanda: the headless browser designed for AI and automation

internal: simplify v8.Value serialization

Karl Seguinea0443cec980
diff --git a/build.zig.zon b/build.zig.zon
index ea2aad0ba..30573a7fa 100644
--- a/build.zig.zon
+++ b/build.zig.zon
@@ -5,8 +5,8 @@
.minimum_zig_version = "0.16.0",
.dependencies = .{
.v8 = .{
- .url = "https://github.com/lightpanda-io/zig-v8-fork/archive/31b233ce6162c18eb37cacd001473057f75daad9.tar.gz",
- .hash = "v8-0.0.0-xddH62IdAwCZid1vyAzg_8IiWG4ovW7DbcnSvZd12I9p",
+ .url = "https://github.com/lightpanda-io/zig-v8-fork/archive/94ee8cb5cb12b228334c412a2bd527b6e3bd29d8.tar.gz",
+ .hash = "v8-0.0.0-xddH6_InAwDcJ7Pli8GL_Cemp_p7CoMkkrljHHXny9Uc",
},
// .v8 = .{ .path = "../zig-v8-fork" },
.brotli = .{
diff --git a/src/browser/Session.zig b/src/browser/Session.zig
index 039c983a4..b0998fbec 100644
--- a/src/browser/Session.zig
+++ b/src/browser/Session.zig
@@ -1055,3 +1055,31 @@ test "Session: retiring a pending page destroys it once" {
// Would deinit `pending` twice if it had been queued twice.
session.processDestroyQueues();
}
+
+test "Session: console capture runs no page JS" {
+ const js = @import("js/js.zig");
+
+ const session = testing.test_session;
+ try session.enableConsoleCapture();
+ defer {
+ session.notification.unregister(.console_message, session);
+ session._console_capture = false;
+ session._console_messages.clearRetainingCapacity();
+ }
+
+ const frame = try testing.createFrame();
+ defer session.closeAllPages();
+
+ var ls: js.Local.Scope = undefined;
+ frame.js.localScope(&ls);
+ defer ls.deinit();
+ _ = try ls.local.exec(
+ \\globalThis.probed = 0;
+ \\const probe = { toString() { globalThis.probed++; console.log('inner'); return 'outer'; } };
+ \\console.log('head', probe, 10n, Symbol('s'));
+ , null);
+
+ try testing.expectEqualSlices(u8, "[log] head [object Object] 10n Symbol(s)\n", session.drainConsoleMessages());
+ const probed = try ls.local.exec("globalThis.probed", null);
+ try testing.expectEqual(0, try probed.toF64());
+}
diff --git a/src/browser/js/Caller.zig b/src/browser/js/Caller.zig
index cfccc9a95..7cf091dc3 100644
--- a/src/browser/js/Caller.zig
+++ b/src/browser/js/Caller.zig
@@ -658,7 +658,7 @@ fn serializeFunctionArgs(local: *const Local, info: FunctionCallbackInfo) ![]con
for (0..info.length()) |i| {
try buf.writer.print("{s}{d} - ", .{ separator, i + 1 });
const js_value = info.getArg(@intCast(i), local);
- try local.debugValue(js_value, &buf.writer);
+ try js_value.format(&buf.writer);
}
return buf.written();
}
diff --git a/src/browser/js/Local.zig b/src/browser/js/Local.zig
index 0dbc0ad11..1dc926183 100644
--- a/src/browser/js/Local.zig
+++ b/src/browser/js/Local.zig
@@ -1535,111 +1535,6 @@ pub fn createPromiseResolver(self: *const Local) js.PromiseResolver {
return js.PromiseResolver.init(self);
}
-pub fn debugValue(self: *const Local, js_val: js.Value, writer: *std.Io.Writer) !void {
- // _debugValue walks arbitrary, caller-supplied object graphs (e.g. a
- // rejected promise's reason) via raw property gets. A getter or Proxy
- // trap encountered along the way can throw; without a TryCatch here,
- // that leaves the isolate's exception flag set after we return, and the
- // next unrelated JS entry point trips V8's has_exception() debug check.
- var try_catch: js.TryCatch = undefined;
- try_catch.init(self);
- defer try_catch.deinit();
-
- var seen: std.AutoHashMapUnmanaged(u32, void) = .empty;
- return self._debugValue(js_val, &seen, 0, writer) catch error.WriteFailed;
-}
-
-fn _debugValue(self: *const Local, js_val: js.Value, seen: *std.AutoHashMapUnmanaged(u32, void), depth: usize, writer: *std.Io.Writer) !void {
- if (js_val.isNull()) {
- // I think null can sometimes appear as an object, so check this and
- // handle it first.
- return writer.writeAll("null");
- }
-
- if (!js_val.isObject()) {
- // handle these explicitly, so we don't include the type (we only want to include
- // it when there's some ambiguity, e.g. the string "true")
- if (js_val.isUndefined()) {
- return writer.writeAll("undefined");
- }
- if (js_val.isTrue()) {
- return writer.writeAll("true");
- }
- if (js_val.isFalse()) {
- return writer.writeAll("false");
- }
-
- if (js_val.isSymbol()) {
- const symbol_handle = v8.v8__Symbol__Description(@ptrCast(js_val.handle), self.isolate.handle).?;
- if (v8.v8__Value__IsUndefined(symbol_handle)) {
- return writer.writeAll("undefined (symbol)");
- }
- return writer.print("{f} (symbol)", .{js.String{ .local = self, .handle = @ptrCast(symbol_handle) }});
- }
- const js_val_str = try js_val.toStringSlice();
- if (js_val_str.len > 2000) {
- try writer.writeAll(js_val_str[0..2000]);
- try writer.writeAll(" ... (truncated)");
- } else {
- try writer.writeAll(js_val_str);
- }
- return writer.print(" ({f})", .{js_val.typeOf()});
- }
-
- const js_obj = js_val.toObject();
- {
- // explicit scope because gop will become invalid in recursive call
- const obj_id: u32 = @bitCast(v8.v8__Object__GetIdentityHash(js_obj.handle));
- const gop = try seen.getOrPut(self.call_arena, obj_id);
- if (gop.found_existing) {
- return writer.writeAll("<circular>\n");
- }
- gop.value_ptr.* = {};
- }
-
- if (depth > 20) {
- return writer.writeAll("...deeply nested object...");
- }
-
- const names_arr = js_obj.getOwnPropertyNames() catch {
- return writer.writeAll("...invalid object...");
- };
- const len = names_arr.len();
-
- const own_len = blk: {
- const own_names = js_obj.getOwnPropertyNames() catch break :blk 0;
- break :blk own_names.len();
- };
-
- if (own_len == 0) {
- const js_val_str = try js_val.toStringSlice();
- if (js_val_str.len > 2000) {
- try writer.writeAll(js_val_str[0..2000]);
- return writer.writeAll(" ... (truncated)");
- }
- return writer.writeAll(js_val_str);
- }
-
- const all_len = js_obj.getPropertyNames().len();
- try writer.print("({d}/{d})", .{ own_len, all_len });
- for (0..len) |i| {
- if (i == 0) {
- try writer.writeByte('\n');
- }
- const field_name = try names_arr.get(@intCast(i));
- const name = try field_name.toStringSlice();
- try writer.splatByteAll(' ', depth);
- try writer.writeAll(name);
- try writer.writeAll(": ");
-
- const field_val = try js_obj.get(name);
- try self._debugValue(field_val, seen, depth + 1, writer);
- if (i != len - 1) {
- try writer.writeByte('\n');
- }
- }
-}
-
// == Misc ==
pub fn parseJSON(self: *const Local, json: []const u8) !js.Value {
const string_handle = self.isolate.initStringHandle(json);
diff --git a/src/browser/js/Object.zig b/src/browser/js/Object.zig
index 075006606..da165d0f6 100644
--- a/src/browser/js/Object.zig
+++ b/src/browser/js/Object.zig
@@ -91,11 +91,7 @@ pub fn toValue(self: Object) js.Value {
}
pub fn format(self: Object, writer: *std.Io.Writer) !void {
- if (comptime lp.IS_DEBUG) {
- return self.local.ctx.debugValue(self.toValue(), writer);
- }
- const str = self.toString() catch return error.WriteFailed;
- return writer.writeAll(str);
+ return self.toValue().format(writer);
}
pub fn persist(self: Object) !Global {
diff --git a/src/browser/js/Value.zig b/src/browser/js/Value.zig
index f4738e100..e43f6062b 100644
--- a/src/browser/js/Value.zig
+++ b/src/browser/js/Value.zig
@@ -718,13 +718,187 @@ pub fn toBigInt(self: Value) js.BigInt {
}
pub fn format(self: Value, writer: *std.Io.Writer) !void {
- if (comptime lp.IS_DEBUG) {
- return self.local.debugValue(self, writer);
- }
- const js_str = self.toString() catch return error.WriteFailed;
- return js_str.format(writer);
+ const inert: Inert = .{ .value = self };
+ return inert.format(writer);
}
+// Stringify without running JS, avoiding potential side effects (e.g. getters,
+// proxies, ...).
+const Inert = struct {
+ value: Value,
+
+ const max_array_depth = 32;
+ const max_array_items = 1_000;
+
+ pub fn format(self: Inert, writer: *std.Io.Writer) !void {
+ const local = self.value.local;
+ // We might still end up calling an interceptor via
+ // GetOwnPropertyDescriptor, which can throw.
+ var try_catch: js.TryCatch = undefined;
+ try_catch.init(local);
+ defer try_catch.deinit();
+
+ var state: State = .{ .local = local };
+ return state.write(self.value.handle, writer);
+ }
+
+ const State = struct {
+ depth: u32 = 0,
+ local: *const js.Local,
+ items_left: u32 = max_array_items,
+ arrays: [max_array_depth]*const v8.Value = undefined,
+
+ fn write(self: *State, handle: *const v8.Value, writer: *std.Io.Writer) std.Io.Writer.Error!void {
+ const local = self.local;
+ const isolate = local.isolate.handle;
+
+ if (v8.v8__Value__IsString(handle)) {
+ return self.writeString(@ptrCast(handle), writer);
+ }
+ if (v8.v8__Value__IsStringObject(handle)) {
+ return self.writeString(v8.v8__StringObject__ValueOf(@ptrCast(handle)).?, writer);
+ }
+ if (v8.v8__Value__IsNumberObject(handle)) {
+ return self.write(@ptrCast(v8.v8__Number__New(isolate, v8.v8__NumberObject__ValueOf(@ptrCast(handle))).?), writer);
+ }
+ if (v8.v8__Value__IsBooleanObject(handle)) {
+ return writer.writeAll(if (v8.v8__BooleanObject__ValueOf(@ptrCast(handle))) "true" else "false");
+ }
+ if (v8.v8__Value__IsBigIntObject(handle)) {
+ return self.write(@ptrCast(v8.v8__BigIntObject__ValueOf(@ptrCast(handle)).?), writer);
+ }
+ if (v8.v8__Value__IsSymbolObject(handle)) {
+ return self.write(@ptrCast(v8.v8__SymbolObject__ValueOf(@ptrCast(handle)).?), writer);
+ }
+ if (v8.v8__Value__IsSymbol(handle)) {
+ try writer.writeAll("Symbol(");
+ const description = v8.v8__Symbol__Description(@ptrCast(handle), isolate).?;
+ if (v8.v8__Value__IsString(description)) {
+ try self.writeString(@ptrCast(description), writer);
+ }
+ return writer.writeByte(')');
+ }
+ if (v8.v8__Value__IsArray(handle)) {
+ return self.writeArray(handle, writer);
+ }
+ if (v8.v8__Value__IsProxy(handle)) {
+ return writer.writeAll("[object Proxy]");
+ }
+ if (v8.v8__Value__IsDate(handle)) {
+ return self.writeString(v8.v8__Date__ToISOString(@ptrCast(handle)).?, writer);
+ }
+ if (v8.v8__Value__IsRegExp(handle)) {
+ try writer.writeByte('/');
+ try self.writeString(v8.v8__RegExp__GetSource(@ptrCast(handle)).?, writer);
+ try writer.writeByte('/');
+ const flags: u32 = @intCast(v8.v8__RegExp__GetFlags(@ptrCast(handle)));
+ for (regexp_flags) |flag| {
+ if (flags & flag[0] != 0) {
+ try writer.writeByte(flag[1]);
+ }
+ }
+ return;
+ }
+ if (v8.v8__Value__IsFunction(handle)) {
+ const source = v8.v8__Function__FunctionProtoToString(@ptrCast(handle), local.handle) orelse {
+ return writer.writeAll("function");
+ };
+ return self.writeString(source, writer);
+ }
+ if (v8.v8__Value__IsNativeError(handle)) {
+ try self.writeString(v8.v8__Object__GetConstructorName(@ptrCast(handle)).?, writer);
+ const message = self.ownDataProperty(@ptrCast(handle), "message") orelse return;
+ if (v8.v8__Value__IsUndefined(message)) {
+ return;
+ }
+ if (v8.v8__Value__IsString(message) and v8.v8__String__Length(@ptrCast(message)) == 0) {
+ return;
+ }
+ try writer.writeAll(": ");
+ return self.write(message, writer);
+ }
+ if (v8.v8__Value__IsObject(handle)) {
+ try writer.writeAll("[object ");
+ try self.writeString(v8.v8__Object__GetConstructorName(@ptrCast(handle)).?, writer);
+ return writer.writeByte(']');
+ }
+
+ // number, bigint, boolean, null, undefined: converting a primitive runs no JS
+ const str = v8.v8__Value__ToString(handle, local.handle) orelse return error.WriteFailed;
+ try self.writeString(str, writer);
+ if (v8.v8__Value__IsBigInt(handle)) {
+ try writer.writeByte('n');
+ }
+ }
+
+ fn writeArray(self: *State, handle: *const v8.Value, writer: *std.Io.Writer) std.Io.Writer.Error!void {
+ for (self.arrays[0..self.depth]) |seen| {
+ if (v8.v8__Value__StrictEquals(seen, handle)) {
+ return;
+ }
+ }
+
+ const len = v8.v8__Array__Length(@ptrCast(handle));
+ if (len > self.items_left or self.depth == max_array_depth) {
+ // V8's builder drops the whole message here; a summary keeps the rest.
+ return writer.print("Array({d})", .{len});
+ }
+ self.items_left -= len;
+ self.arrays[self.depth] = handle;
+ self.depth += 1;
+ defer self.depth -= 1;
+
+ var key_buf: [10]u8 = undefined;
+ for (0..len) |i| {
+ if (i != 0) {
+ try writer.writeByte(',');
+ }
+ const key = std.fmt.bufPrint(&key_buf, "{d}", .{i}) catch unreachable;
+ const element = self.ownDataProperty(@ptrCast(handle), key) orelse continue;
+ if (v8.v8__Value__IsNullOrUndefined(element)) {
+ continue;
+ }
+ try self.write(element, writer);
+ }
+ }
+
+ fn writeString(self: *State, handle: *const v8.String, writer: *std.Io.Writer) std.Io.Writer.Error!void {
+ const str: js.String = .{ .local = self.local, .handle = handle };
+ return str.format(writer);
+ }
+
+ fn ownDataProperty(self: *State, object: *const v8.Object, key: []const u8) ?*const v8.Value {
+ const local = self.local;
+ const descriptor = v8.v8__Object__GetOwnPropertyDescriptor(object, local.handle, local.isolate.initStringHandle(key)) orelse return null;
+ if (v8.v8__Value__IsObject(descriptor) == false) {
+ return null;
+ }
+
+ // An accessor descriptor has no own `value`, and a Get would then reach Object.prototype.
+ const value_key = local.isolate.initStringHandle("value");
+ var has: v8.MaybeBool = undefined;
+ v8.v8__Object__HasOwnProperty(@ptrCast(descriptor), local.handle, value_key, &has);
+ if (has.has_value == false or has.value == false) {
+ return null;
+ }
+ return v8.v8__Object__Get(@ptrCast(descriptor), local.handle, value_key);
+ }
+ };
+
+ // Same order as RegExp.prototype.flags, plus V8's `l`.
+ const regexp_flags = [_]struct { u32, u8 }{
+ .{ v8.kRegExpHasIndices, 'd' },
+ .{ v8.kRegExpGlobal, 'g' },
+ .{ v8.kRegExpIgnoreCase, 'i' },
+ .{ v8.kRegExpLinear, 'l' },
+ .{ v8.kRegExpMultiline, 'm' },
+ .{ v8.kRegExpDotAll, 's' },
+ .{ v8.kRegExpUnicode, 'u' },
+ .{ v8.kRegExpUnicodeSets, 'v' },
+ .{ v8.kRegExpSticky, 'y' },
+ };
+};
+
// The JS iteration protocol (@@iterator)
pub fn iterator(self: Value) !?Iterator {
if (!self.isObject()) {
@@ -791,6 +965,62 @@ pub const Global = struct {
};
const testing = @import("../../testing.zig");
+test "Value: inert formatting runs no page JS" {
+ const frame = try testing.createFrame();
+ defer testing.test_session.closeAllPages();
+
+ var ls: js.Local.Scope = undefined;
+ frame.js.localScope(&ls);
+ defer ls.deinit();
+
+ _ = try ls.local.exec(
+ \\globalThis.probed = 0;
+ \\globalThis.probe = function() { globalThis.probed++; return 'probed'; };
+ , null);
+
+ const cases = [_]struct { expr: []const u8, expected: []const u8 }{
+ .{ .expr = "'str'", .expected = "str" },
+ .{ .expr = "1.5", .expected = "1.5" },
+ .{ .expr = "-0", .expected = "0" },
+ .{ .expr = "NaN", .expected = "NaN" },
+ .{ .expr = "true", .expected = "true" },
+ .{ .expr = "null", .expected = "null" },
+ .{ .expr = "undefined", .expected = "undefined" },
+ .{ .expr = "10n", .expected = "10n" },
+ .{ .expr = "Symbol('s')", .expected = "Symbol(s)" },
+ .{ .expr = "Symbol()", .expected = "Symbol()" },
+ .{ .expr = "new Number(42)", .expected = "42" },
+ .{ .expr = "new String('w')", .expected = "w" },
+ .{ .expr = "new Boolean(false)", .expected = "false" },
+ .{ .expr = "Object(5n)", .expected = "5n" },
+ .{ .expr = "Object(Symbol('q'))", .expected = "Symbol(q)" },
+ .{ .expr = "({ toString: probe, valueOf: probe, [Symbol.toPrimitive]: probe })", .expected = "[object Object]" },
+ .{ .expr = "Object.defineProperty({}, Symbol.toStringTag, { get: probe })", .expected = "[object Object]" },
+ .{ .expr = "(() => { const d = document.createElement('div'); Object.defineProperty(d, 'id', { get: probe }); return d; })()", .expected = "[object HTMLDivElement]" },
+ .{ .expr = "new (class Foo {})()", .expected = "[object Foo]" },
+ .{ .expr = "new Proxy({}, { get: probe, getOwnPropertyDescriptor: probe, getPrototypeOf: probe })", .expected = "[object Proxy]" },
+ .{ .expr = "[1, null, undefined, 'a', [2, [3]]]", .expected = "1,,,a,2,3" },
+ .{ .expr = "(() => { const a = [1]; a.push(a); return a; })()", .expected = "1," },
+ .{ .expr = "Object.defineProperty([1], 1, { get: probe })", .expected = "1," },
+ .{ .expr = "Object.assign(new TypeError('boom'), { toString: probe })", .expected = "TypeError: boom" },
+ .{ .expr = "new RangeError()", .expected = "RangeError" },
+ .{ .expr = "Object.defineProperty(new Error(), 'message', { get: probe })", .expected = "Error" },
+ .{ .expr = "Object.defineProperty(Object.assign(new Date(0), { toString: probe }), Symbol.toPrimitive, { value: probe })", .expected = "1970-01-01T00:00:00.000Z" },
+ .{ .expr = "new Date(NaN)", .expected = "Invalid Date" },
+ .{ .expr = "Object.assign(/a+/gi, { toString: probe })", .expected = "/a+/gi" },
+ .{ .expr = "Object.assign(function named() {}, { toString: probe })", .expected = "function named() {}" },
+ };
+ for (cases) |case| {
+ const value = try ls.local.exec(case.expr, null);
+ const out = try std.fmt.allocPrint(testing.allocator, "{f}", .{value});
+ defer testing.allocator.free(out);
+ try testing.expectEqualSlices(u8, case.expected, out);
+ }
+
+ const probed = try ls.local.exec("globalThis.probed", null);
+ try testing.expectEqual(0, try probed.toF64());
+}
+
test "Value: persisted handle early-release swap-removes and fixes up indices" {
const frame = try testing.createFrame();
defer testing.test_session.closeAllPages();
diff --git a/src/server/cdp/domains/console.zig b/src/server/cdp/domains/console.zig
index 3dae85090..1076d3b28 100644
--- a/src/server/cdp/domains/console.zig
+++ b/src/server/cdp/domains/console.zig
@@ -66,9 +66,7 @@ pub fn consoleMessage(arena: Allocator, bc: *CDP.BrowserContext, event: *const N
const w = &aw.writer;
for (event.values, 0..) |v, i| {
if (i != 0) try w.writeByte(' ');
-
- const js_str = try v.toString();
- try js_str.format(w);
+ try v.format(w);
}
return bc.cdp.sendEvent("Console.messageAdded", ConsoleMessage{
diff --git a/src/server/cdp/domains/runtime.zig b/src/server/cdp/domains/runtime.zig
index a8aaf1658..e36f1466e 100644
--- a/src/server/cdp/domains/runtime.zig
+++ b/src/server/cdp/domains/runtime.zig
@@ -271,7 +271,7 @@ test "cdp.runtime: consoleAPICalled only carries values for primitives" {
try testing.expectEqual(0, try probed.toF64());
}
-test "cdp.runtime: console calls made while a notification is being built" {
+test "cdp.runtime: console notifications run no page JS" {
testing.silenceLog(&.{.js});
var ctx = try testing.context();
@@ -285,19 +285,17 @@ test "cdp.runtime: console calls made while a notification is being built" {
var ls: js.Local.Scope = undefined;
frame.js.localScope(&ls);
defer ls.deinit();
- // Legacy Console formatting can re-enter the notification handlers.
_ = try ls.local.exec(
- \\const inner = 'x'.repeat(120);
- \\const probe = { toString() { console.log(inner); console.log(inner); return 'outer'; } };
+ \\globalThis.probed = 0;
+ \\const probe = { toString() { globalThis.probed++; console.log('inner'); return 'outer'; } };
\\console.log('head-marker', probe, 'tail-marker-'.repeat(20));
, null);
- const inner = "x" ** 120;
const tail = "tail-marker-" ** 20;
- try ctx.expectSentEvent("Console.messageAdded", .{ .level = "log", .text = inner }, .{});
- try ctx.expectSentEvent("Console.messageAdded", .{ .level = "log", .text = "head-marker outer " ++ tail }, .{});
- try ctx.expectSentEvent("Runtime.consoleAPICalled", .{ .type = "log", .args = .{.{ .type = "string", .value = inner }} }, .{});
+ try ctx.expectSentEvent("Console.messageAdded", .{ .level = "log", .text = "head-marker [object Object] " ++ tail }, .{});
try ctx.expectSentEvent("Runtime.consoleAPICalled", .{ .type = "log", .args = .{ .{ .type = "string", .value = "head-marker" }, .{ .type = "object", .className = "Object" }, .{ .type = "string", .value = tail } } }, .{});
+ const probed = try ls.local.exec("globalThis.probed", null);
+ try testing.expectEqual(0, try probed.toF64());
try testing.expectEqual(0, ctx.cdp().notification_depth);
try testing.expectEqual(0, ctx.cdp().link.send_depth);
try ctx.processMessage(.{ .id = 62, .method = "Runtime.evaluate", .params = .{ .expression = "6 * 7" } });