mirror of
https://github.com/lightpanda-io/browser.git
synced 2025-10-30 07:31:47 +00:00
Unify the Zig and JS events using an intrusive node.
The approach borrows heavily from Zig's new LinkedList API. The main benefit is that it unifies how event callbacks are done. When the Page.windowClick event was added, the Event structure was changed to a union, supporting a distinct Zig and JS event. This new approach more or less treats everything like a Zig event. A JS event is just a Zig struct that has a Env.Callback which it can invoke in its handle method. The intrusive nature of the EventNode means that what used to be 1 or 2 allocations is now 0 or 1. It also has the benefit of making netsurf completely unaware of Env.Callbacks.
This commit is contained in:
@@ -1200,7 +1200,7 @@ pub fn Env(comptime S: type, comptime types: anytype) type {
|
||||
pub const Callback = struct {
|
||||
id: usize,
|
||||
executor: *Executor,
|
||||
_this: ?v8.Object = null,
|
||||
this: ?v8.Object = null,
|
||||
func: PersistentFunction,
|
||||
|
||||
// We use this when mapping a JS value to a Zig object. We can't
|
||||
@@ -1215,8 +1215,13 @@ pub fn Env(comptime S: type, comptime types: anytype) type {
|
||||
exception: []const u8,
|
||||
};
|
||||
|
||||
pub fn setThis(self: *Callback, value: anytype) !void {
|
||||
self._this = try self.executor.valueToExistingObject(value);
|
||||
pub fn withThis(self: *const Callback, value: anytype) !Callback {
|
||||
return .{
|
||||
.id = self.id,
|
||||
.func = self.func,
|
||||
.executor = self.executor,
|
||||
.this = try self.executor.valueToExistingObject(value),
|
||||
};
|
||||
}
|
||||
|
||||
pub fn call(self: *const Callback, args: anytype) !void {
|
||||
@@ -1264,7 +1269,7 @@ pub fn Env(comptime S: type, comptime types: anytype) type {
|
||||
}
|
||||
|
||||
fn getThis(self: *const Callback) v8.Object {
|
||||
return self._this orelse self.executor.context.getGlobal();
|
||||
return self.this orelse self.executor.context.getGlobal();
|
||||
}
|
||||
|
||||
// debug/helper to print the source of the JS callback
|
||||
|
||||
Reference in New Issue
Block a user