Lightpanda: the headless browser designed for AI and automation
js: support definer/descriptor interceptors; fix HTMLCollection expandos
Francis Bouvier15a8a81e6496
diff --git a/src/browser/js/Caller.zig b/src/browser/js/Caller.zigindex a6e8903a4..fb6ed5f6b 100644--- a/src/browser/js/Caller.zig+++ b/src/browser/js/Caller.zig@@ -388,11 +388,7 @@ fn _getIndexQuery(comptime T: type, local: *const Local, func: anytype, idx: u32if (@typeInfo(F).@"fn".params.len == 3) {@field(args, "2") = getGlobalArg(@TypeOf(args.@"2"), local.ctx);}- if (@call(.auto, func, args) == false) {- return js.Intercepted.no;- }- info.getReturnValue().set(try local.zigValueToJs(@as(u32, v8.None), .{}));- return js.Intercepted.yes;+ return queryReturn(local, @call(.auto, func, args), info);}pub fn getNamedQuery(self: *Caller, comptime T: type, func: anytype, name: *const v8.Name, handle: *const v8.PropertyCallbackInfo) u32 {@@ -417,12 +413,33 @@ fn _getNamedQuery(comptime T: type, local: *const Local, func: anytype, name: *cif (@typeInfo(F).@"fn".params.len == 3) {@field(args, "2") = getGlobalArg(@TypeOf(args.@"2"), local.ctx);}- if (@call(.auto, func, args) == false) {- return js.Intercepted.no;+ return queryReturn(local, @call(.auto, func, args), info);+}++// A query callback either returns a bool (true -> the property exists as an+// enumerable, writable, configurable data property, PropertyAttribute.None)+// or the v8.PropertyAttribute bits directly (e.g. v8.ReadOnly).+// error.NotHandled falls through to the ordinary property lookup.+fn queryReturn(local: *const Local, ret: anytype, info: PropertyCallbackInfo) !u32 {+ const val = switch (@typeInfo(@TypeOf(ret))) {+ .error_union => |eu| ret catch |err| {+ if (comptime isInErrorSet(error.NotHandled, eu.error_set)) {+ if (err == error.NotHandled) {+ return js.Intercepted.no;+ }+ }+ return err;+ },+ else => ret,+ };+ if (@TypeOf(val) == bool) {+ if (val == false) {+ return js.Intercepted.no;+ }+ info.getReturnValue().set(try local.zigValueToJs(@as(u32, v8.None), .{}));+ } else {+ info.getReturnValue().set(try local.zigValueToJs(@as(u32, val), .{}));}- // The property exists as a supported property name; report it as an- // enumerable, writable, configurable data property (PropertyAttribute.None).- info.getReturnValue().set(try local.zigValueToJs(@as(u32, v8.None), .{}));return js.Intercepted.yes;}diff --git a/src/browser/js/Snapshot.zig b/src/browser/js/Snapshot.zigindex 581b1257e..63ad37b0f 100644--- a/src/browser/js/Snapshot.zig+++ b/src/browser/js/Snapshot.zig@@ -454,12 +454,15 @@ fn countExternalReferences() comptime_int {if (value.setter != null) count += 1;if (value.deleter != null) count += 1;if (value.query != null) count += 1;+ if (value.definer != null) count += 1;} else if (T == bridge.NamedIndexed) {count += 1;if (value.setter != null) count += 1;if (value.deleter != null) count += 1;- if (value.enumerator != null) count += 1;if (value.query != null) count += 1;+ if (value.definer != null) count += 1;+ if (value.descriptor != null) count += 1;+ if (value.enumerator != null) count += 1;}}}@@ -547,6 +550,10 @@ fn collectExternalReferences() [countExternalReferences()]isize {references[idx] = @bitCast(@intFromPtr(query));idx += 1;}+ if (value.definer) |definer| {+ references[idx] = @bitCast(@intFromPtr(definer));+ idx += 1;+ }} else if (T == bridge.NamedIndexed) {references[idx] = @bitCast(@intFromPtr(value.getter));idx += 1;@@ -558,14 +565,22 @@ fn collectExternalReferences() [countExternalReferences()]isize {references[idx] = @bitCast(@intFromPtr(deleter));idx += 1;}- if (value.enumerator) |enumerator| {- references[idx] = @bitCast(@intFromPtr(enumerator));- idx += 1;- }if (value.query) |query| {references[idx] = @bitCast(@intFromPtr(query));idx += 1;}+ if (value.definer) |definer| {+ references[idx] = @bitCast(@intFromPtr(definer));+ idx += 1;+ }+ if (value.descriptor) |descriptor| {+ references[idx] = @bitCast(@intFromPtr(descriptor));+ idx += 1;+ }+ if (value.enumerator) |enumerator| {+ references[idx] = @bitCast(@intFromPtr(enumerator));+ idx += 1;+ }}}}@@ -852,7 +867,7 @@ fn attachClass(comptime JsApi: type, comptime flatten: bool, isolate: *v8.Isolat.setter = value.setter,.query = value.query,.deleter = value.deleter,- .definer = null,+ .definer = if (value.definer) |definer| @ptrCast(definer) else null,.descriptor = null,.index_of = null,.data = null,@@ -867,8 +882,8 @@ fn attachClass(comptime JsApi: type, comptime flatten: bool, isolate: *v8.Isolat.query = value.query,.deleter = value.deleter,.enumerator = value.enumerator,- .definer = null,- .descriptor = null,+ .definer = if (value.definer) |definer| @ptrCast(definer) else null,+ .descriptor = if (value.descriptor) |descriptor| @ptrCast(descriptor) else null,.data = null,.flags = v8.kOnlyInterceptStrings | v8.kNonMasking,};diff --git a/src/browser/js/bridge.zig b/src/browser/js/bridge.zigindex 3a18fbcfb..7e7c5acab 100644--- a/src/browser/js/bridge.zig+++ b/src/browser/js/bridge.zig@@ -46,15 +46,23 @@ pub fn Builder(comptime T: type) type {}pub fn indexed(comptime getter_func: anytype, comptime enumerator_func: anytype, comptime opts: Indexed.Opts) Indexed {- return Indexed.init(T, getter_func, null, null, null, enumerator_func, opts);+ return Indexed.init(T, getter_func, null, null, null, null, enumerator_func, opts);}pub fn indexedReadWrite(comptime getter_func: anytype, setter_func: anytype, deleter_func: anytype, query_func: anytype, comptime enumerator_func: anytype, comptime opts: Indexed.Opts) Indexed {- return Indexed.init(T, getter_func, setter_func, deleter_func, query_func, enumerator_func, opts);+ return Indexed.init(T, getter_func, setter_func, deleter_func, query_func, null, enumerator_func, opts);+ }++ pub fn indexedFull(comptime getter_func: anytype, setter_func: anytype, deleter_func: anytype, query_func: anytype, definer_func: anytype, comptime enumerator_func: anytype, comptime opts: Indexed.Opts) Indexed {+ return Indexed.init(T, getter_func, setter_func, deleter_func, query_func, definer_func, enumerator_func, opts);}pub fn namedIndexed(comptime getter_func: anytype, setter_func: anytype, deleter_func: anytype, enumerator_func: anytype, query_func: anytype, comptime opts: NamedIndexed.Opts) NamedIndexed {- return NamedIndexed.init(T, getter_func, setter_func, deleter_func, enumerator_func, query_func, opts);+ return NamedIndexed.init(T, getter_func, setter_func, deleter_func, enumerator_func, query_func, null, null, opts);+ }++ pub fn namedIndexedFull(comptime getter_func: anytype, setter_func: anytype, deleter_func: anytype, enumerator_func: anytype, query_func: anytype, definer_func: anytype, descriptor_func: anytype, comptime opts: NamedIndexed.Opts) NamedIndexed {+ return NamedIndexed.init(T, getter_func, setter_func, deleter_func, enumerator_func, query_func, definer_func, descriptor_func, opts);}pub fn iterator(comptime func: anytype, comptime opts: Iterator.Opts) Iterator {@@ -268,13 +276,16 @@ pub const Indexed = struct {setter: ?*const fn (idx: u32, c_value: ?*const v8.Value, handle: ?*const v8.PropertyCallbackInfo) callconv(.c) u32 = null,deleter: ?*const fn (idx: u32, handle: ?*const v8.PropertyCallbackInfo) callconv(.c) u32 = null,query: ?*const fn (idx: u32, handle: ?*const v8.PropertyCallbackInfo) callconv(.c) u32 = null,+ // v8 expects an Intercepted (u32) return; the stale void-returning+ // typedef in binding.h is papered over with a @ptrCast at install time.+ definer: ?*const fn (idx: u32, desc: ?*v8.PropertyDescriptor, handle: ?*const v8.PropertyCallbackInfo) callconv(.c) u32 = null,const Opts = struct {as_typed_array: bool = false,null_as_undefined: bool = false,};- fn init(comptime T: type, comptime getter: anytype, setter: anytype, deleter: anytype, query: anytype, comptime enumerator: anytype, comptime opts: Opts) Indexed {+ fn init(comptime T: type, comptime getter: anytype, setter: anytype, deleter: anytype, query: anytype, definer: anytype, comptime enumerator: anytype, comptime opts: Opts) Indexed {var indexed = Indexed{.enumerator = null,.getter = struct {@@ -359,6 +370,22 @@ pub const Indexed = struct {}.wrap;}+ if (@typeInfo(@TypeOf(definer)) != .null) {+ indexed.definer = struct {+ fn wrap(idx: u32, _: ?*v8.PropertyDescriptor, handle: ?*const v8.PropertyCallbackInfo) callconv(.c) u32 {+ const v8_isolate = v8.v8__PropertyCallbackInfo__GetIsolate(handle).?;+ var caller: Caller = undefined;+ if (!caller.init(v8_isolate)) {+ return js.Intercepted.no;+ }+ defer caller.deinit();++ // same (self, index) -> bool shape as a deleter+ return caller.deleteIndex(T, definer, idx, handle.?, .{});+ }+ }.wrap;+ }+return indexed;}};@@ -367,8 +394,12 @@ pub const NamedIndexed = struct {getter: *const fn (c_name: ?*const v8.Name, handle: ?*const v8.PropertyCallbackInfo) callconv(.c) u32,setter: ?*const fn (c_name: ?*const v8.Name, c_value: ?*const v8.Value, handle: ?*const v8.PropertyCallbackInfo) callconv(.c) u32 = null,deleter: ?*const fn (c_name: ?*const v8.Name, handle: ?*const v8.PropertyCallbackInfo) callconv(.c) u32 = null,- enumerator: ?*const fn (handle: ?*const v8.PropertyCallbackInfo) callconv(.c) u32 = null,query: ?*const fn (c_name: ?*const v8.Name, handle: ?*const v8.PropertyCallbackInfo) callconv(.c) u32 = null,+ // v8 expects an Intercepted (u32) return; the stale void-returning+ // typedefs in binding.h are papered over with a @ptrCast at install time.+ definer: ?*const fn (c_name: ?*const v8.Name, desc: ?*v8.PropertyDescriptor, handle: ?*const v8.PropertyCallbackInfo) callconv(.c) u32 = null,+ descriptor: ?*const fn (c_name: ?*const v8.Name, handle: ?*const v8.PropertyCallbackInfo) callconv(.c) u32 = null,+ enumerator: ?*const fn (handle: ?*const v8.PropertyCallbackInfo) callconv(.c) u32 = null,const Opts = struct {as_typed_array: bool = false,@@ -379,7 +410,7 @@ pub const NamedIndexed = struct {ce_reactions: bool = false,};- fn init(comptime T: type, comptime getter: anytype, setter: anytype, deleter: anytype, enumerator: anytype, query: anytype, comptime opts: Opts) NamedIndexed {+ fn init(comptime T: type, comptime getter: anytype, setter: anytype, deleter: anytype, enumerator: anytype, query: anytype, definer: anytype, descriptor: anytype, comptime opts: Opts) NamedIndexed {const getter_fn = struct {fn wrap(c_name: ?*const v8.Name, handle: ?*const v8.PropertyCallbackInfo) callconv(.c) u32 {const v8_isolate = v8.v8__PropertyCallbackInfo__GetIsolate(handle).?;@@ -452,6 +483,35 @@ pub const NamedIndexed = struct {}}.wrap;+ const definer_fn = if (@typeInfo(@TypeOf(definer)) == .null) null else struct {+ fn wrap(c_name: ?*const v8.Name, _: ?*v8.PropertyDescriptor, handle: ?*const v8.PropertyCallbackInfo) callconv(.c) u32 {+ const v8_isolate = v8.v8__PropertyCallbackInfo__GetIsolate(handle).?;+ var caller: Caller = undefined;+ if (!caller.init(v8_isolate)) {+ return js.Intercepted.no;+ }+ defer caller.deinit();++ // same (self, name) -> bool shape as a deleter+ return caller.deleteNamedIndex(T, definer, c_name.?, handle.?, .{});+ }+ }.wrap;++ const descriptor_fn = if (@typeInfo(@TypeOf(descriptor)) == .null) null else struct {+ fn wrap(c_name: ?*const v8.Name, handle: ?*const v8.PropertyCallbackInfo) callconv(.c) u32 {+ const v8_isolate = v8.v8__PropertyCallbackInfo__GetIsolate(handle).?;+ var caller: Caller = undefined;+ if (!caller.init(v8_isolate)) {+ return js.Intercepted.no;+ }+ defer caller.deinit();++ // same (self, name) -> value shape as a getter; the returned+ // struct is converted to a JS property descriptor object+ return caller.getNamedIndex(T, descriptor, c_name.?, handle.?, .{});+ }+ }.wrap;+const enumerator_fn = if (@typeInfo(@TypeOf(enumerator)) == .null) null else struct {fn wrap(handle: ?*const v8.PropertyCallbackInfo) callconv(.c) u32 {const v8_isolate = v8.v8__PropertyCallbackInfo__GetIsolate(handle).?;@@ -482,8 +542,10 @@ pub const NamedIndexed = struct {.getter = getter_fn,.setter = setter_fn,.deleter = deleter_fn,- .enumerator = enumerator_fn,.query = query_fn,+ .definer = definer_fn,+ .descriptor = descriptor_fn,+ .enumerator = enumerator_fn,};}};diff --git a/src/browser/webapi/collections/HTMLCollection.zig b/src/browser/webapi/collections/HTMLCollection.zigindex 7706f944c..0d545b5c3 100644--- a/src/browser/webapi/collections/HTMLCollection.zig+++ b/src/browser/webapi/collections/HTMLCollection.zig@@ -147,8 +147,8 @@ pub const JsApi = struct {};pub const length = bridge.accessor(HTMLCollection.length, null, .{});- pub const @"[int]" = bridge.indexedReadWrite(HTMLCollection.getAtIndex, null, deleteAtIndex, null, getIndexes, .{ .null_as_undefined = true });- pub const @"[str]" = bridge.namedIndexed(struct {+ pub const @"[int]" = bridge.indexedFull(HTMLCollection.getAtIndex, setAtIndex, deleteAtIndex, queryAtIndex, defineAtIndex, getIndexes, .{ .null_as_undefined = true });+ pub const @"[str]" = bridge.namedIndexedFull(struct {pub fn wrap(self: *HTMLCollection, name: []const u8, frame: *Frame) !?*Element {if (name.len == 0) {return error.NotHandled;@@ -156,7 +156,61 @@ pub const JsApi = struct {return self.getByName(name, frame) orelse error.NotHandled;}- }.wrap, null, deleteByName, getNames, null, .{ .null_as_undefined = true });+ }.wrap, null, deleteByName, getNames, null, defineByName, describeByName, .{ .null_as_undefined = true });++ // HTMLCollection has no indexed setter: per Web IDL, assigning to or+ // defining any array index property fails (TypeError in strict mode).+ fn setAtIndex(_: *HTMLCollection, _: u32, _: js.Value) bool {+ return false;+ }++ fn defineAtIndex(_: *HTMLCollection, _: u32) bool {+ return false;+ }++ // Supported indexed properties are enumerable, configurable, read-only.+ fn queryAtIndex(self: *HTMLCollection, idx: u32, frame: *Frame) !u32 {+ if (idx < self.length(frame)) {+ return js.v8.ReadOnly;+ }+ return error.NotHandled;+ }++ // Redefining a supported named property fails; unsupported names follow+ // the ordinary path, so expandos remain allowed. Note there is no named+ // setter and no named query: a direct assignment falls through to the+ // ordinary [[Set]] which ends up in defineByName, while an assignment+ // through a derived object (the collection as prototype) must ignore the+ // named property entirely and create an expando on the receiver.+ fn defineByName(self: *HTMLCollection, name: []const u8, frame: *Frame) !bool {+ if (name.len > 0 and self.getByName(name, frame) != null) {+ return false;+ }+ return error.NotHandled;+ }++ // Named properties are [LegacyUnenumerableNamedProperties]: not+ // enumerable, configurable, read-only.+ fn describeByName(self: *HTMLCollection, name: []const u8, frame: *Frame) !Descriptor {+ if (name.len > 0) {+ if (self.getByName(name, frame)) |element| {+ return .{+ .value = element,+ .writable = false,+ .enumerable = false,+ .configurable = true,+ };+ }+ }+ return error.NotHandled;+ }++ const Descriptor = struct {+ value: *Element,+ writable: bool,+ enumerable: bool,+ configurable: bool,+ };fn getIndexes(self: *HTMLCollection, exec: *const Execution) !js.Array {const frame = switch (exec.js.global) {