mirror of
https://github.com/lightpanda-io/browser.git
synced 2026-02-04 14:33:47 +00:00
dispatch events with proper this
This commit is contained in:
@@ -186,7 +186,7 @@ pub fn dispatchWithFunction(self: *EventManager, target: *EventTarget, event: *E
|
|||||||
|
|
||||||
if (function_) |func| {
|
if (function_) |func| {
|
||||||
event._current_target = target;
|
event._current_target = target;
|
||||||
if (func.call(void, .{event})) {
|
if (func.callWithThis(void, target, .{event})) {
|
||||||
was_dispatched = true;
|
was_dispatched = true;
|
||||||
} else |err| {
|
} else |err| {
|
||||||
// a non-JS error
|
// a non-JS error
|
||||||
@@ -349,7 +349,7 @@ fn dispatchPhase(self: *EventManager, list: *std.DoublyLinkedList, current_targe
|
|||||||
}
|
}
|
||||||
|
|
||||||
switch (listener.function) {
|
switch (listener.function) {
|
||||||
.value => |value| try value.call(void, .{event}),
|
.value => |value| try value.callWithThis(void, current_target, .{event}),
|
||||||
.string => |string| {
|
.string => |string| {
|
||||||
const str = try page.call_arena.dupeZ(u8, string.str());
|
const str = try page.call_arena.dupeZ(u8, string.str());
|
||||||
try self.page.js.eval(str, null);
|
try self.page.js.eval(str, null);
|
||||||
|
|||||||
@@ -203,27 +203,6 @@ fn trackCallback(self: *Context, pf: PersistentFunction) !void {
|
|||||||
return self.callbacks.append(self.arena, pf);
|
return self.callbacks.append(self.arena, pf);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Given an anytype, turns it into a v8.Object. The anytype could be:
|
|
||||||
// 1 - A V8.object already
|
|
||||||
// 2 - Our js.Object wrapper around a V8.Object
|
|
||||||
// 3 - A zig instance that has previously been given to V8
|
|
||||||
// (i.e., the value has to be known to the executor)
|
|
||||||
pub fn valueToExistingObject(self: *const Context, value: anytype) !v8.Object {
|
|
||||||
if (@TypeOf(value) == v8.Object) {
|
|
||||||
return value;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (@TypeOf(value) == js.Object) {
|
|
||||||
return value.js_obj;
|
|
||||||
}
|
|
||||||
|
|
||||||
const persistent_object = self.identity_map.get(@intFromPtr(value)) orelse {
|
|
||||||
return error.InvalidThisForCallback;
|
|
||||||
};
|
|
||||||
|
|
||||||
return persistent_object.castToObject();
|
|
||||||
}
|
|
||||||
|
|
||||||
// == Executors ==
|
// == Executors ==
|
||||||
pub fn eval(self: *Context, src: []const u8, name: ?[]const u8) !void {
|
pub fn eval(self: *Context, src: []const u8, name: ?[]const u8) !void {
|
||||||
_ = try self.exec(src, name);
|
_ = try self.exec(src, name);
|
||||||
|
|||||||
@@ -129,7 +129,16 @@ pub fn callWithThis(self: *const Function, comptime T: type, this: anytype, args
|
|||||||
context.call_depth = call_depth + 1;
|
context.call_depth = call_depth + 1;
|
||||||
defer context.call_depth = call_depth;
|
defer context.call_depth = call_depth;
|
||||||
|
|
||||||
const js_this = try context.valueToExistingObject(this);
|
const js_this = blk: {
|
||||||
|
if (@TypeOf(this) == v8.Object) {
|
||||||
|
break :blk this;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (@TypeOf(this) == js.Object) {
|
||||||
|
break :blk this.js_obj;
|
||||||
|
}
|
||||||
|
break :blk try context.zigValueToJs(this, .{});
|
||||||
|
};
|
||||||
|
|
||||||
const aargs = if (comptime @typeInfo(@TypeOf(args)) == .null) struct {}{} else args;
|
const aargs = if (comptime @typeInfo(@TypeOf(args)) == .null) struct {}{} else args;
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user