Skip to content

Commit

Permalink
Merge pull request #199 from lightpanda-io/setter-callback
Browse files Browse the repository at this point in the history
accept Callback for setter
  • Loading branch information
francisbouvier authored Feb 15, 2024
2 parents 193042d + d9c071c commit 8fe165b
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 17 deletions.
36 changes: 28 additions & 8 deletions src/engines/v8/callback.zig
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const NativeContext = internal.NativeContext;

const JSObjectID = @import("v8.zig").JSObjectID;
const setNativeType = @import("generate.zig").setNativeType;
const CallbackInfo = @import("generate.zig").CallbackInfo;

// TODO: Make this JS engine agnostic
// by providing a common interface
Expand All @@ -35,7 +36,8 @@ pub const FuncSync = struct {
pub fn init(
alloc: std.mem.Allocator,
comptime func: refl.Func,
info: v8.FunctionCallbackInfo,
raw_value: ?*const v8.C_Value,
info: CallbackInfo,
isolate: v8.Isolate,
) !FuncSync {

Expand All @@ -54,12 +56,24 @@ pub const FuncSync = struct {
// var js_args: [func.args_callback_nb]v8.Value = undefined;
var js_args = try alloc.alloc(v8.Value, func.args_callback_nb);
for (js_args_indexes, 0..) |index, i| {
js_args[i] = info.getArg(@as(u32, @intCast(index - func.index_offset)));
js_args[i] = info.getArg(raw_value, index, func.index_offset) orelse unreachable;
}

var idx = func.callback_index.?;
if (idx > 0) idx = idx - 1; // -1 because of self

// retrieve callback function
const js_func_index = func.callback_index.? - func.index_offset - 1; // -1 because of self
const js_func_val = info.getArg(js_func_index);
const js_func_val = info.getArg(
raw_value,
idx,
func.index_offset,
) orelse unreachable;

std.debug.print("idx: {d}, offset: {d}, {any}\n", .{
func.callback_index.?,
idx,
js_func_val,
});
if (!js_func_val.isFunction()) {
return error.JSWrongType;
}
Expand Down Expand Up @@ -113,13 +127,19 @@ pub const Func = struct {
alloc: std.mem.Allocator,
nat_ctx: *NativeContext,
comptime func: refl.Func,
info: v8.FunctionCallbackInfo,
raw_value: ?*const v8.C_Value,
info: CallbackInfo,
isolate: v8.Isolate,
) !Func {
var idx = func.callback_index.?;
if (idx > 0) idx = idx - 1; // -1 because of self

// retrieve callback function
const js_func_index = func.callback_index.? - func.index_offset - 1; // -1 because of self
const js_func_val = info.getArg(js_func_index);
const js_func_val = info.getArg(
raw_value,
idx,
func.index_offset,
) orelse unreachable;
if (!js_func_val.isFunction()) {
return error.JSWrongType;
}
Expand Down Expand Up @@ -148,7 +168,7 @@ pub const Func = struct {

// retrieve callback arguments
for (js_args_indexes, 0..) |index, i| {
const js_arg = info.getArg(@as(u32, @intCast(index - func.index_offset)));
const js_arg = info.getArg(raw_value, index, func.index_offset) orelse unreachable;
const js_arg_pers = PersistentValue.init(isolate, js_arg);
js_args_pers[i] = js_arg_pers;
}
Expand Down
15 changes: 6 additions & 9 deletions src/engines/v8/generate.zig
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ fn getArg(
return value;
}

const CallbackInfo = union(enum) {
pub const CallbackInfo = union(enum) {
func_cbk: v8.FunctionCallbackInfo,
prop_cbk: v8.PropertyCallbackInfo,

Expand Down Expand Up @@ -241,7 +241,7 @@ const CallbackInfo = union(enum) {
};
}

fn getArg(
pub fn getArg(
self: CallbackInfo,
raw_value: ?*const v8.C_Value,
index: usize,
Expand Down Expand Up @@ -308,22 +308,19 @@ fn getArgs(

// non-variadic arg
value = switch (arg.T) {

// special cases for callback arguments
// TODO: because thoses functions requires a v8.FunctionCallbackInfo
// they will not be available for generators with v8.PropertyCallbackInfo
// (getters and setters)
cbk.Func => cbk.Func.init(
alloc,
nat_ctx,
func,
cbk_info.func_cbk,
raw_value,
cbk_info,
isolate,
) catch unreachable,
cbk.FuncSync => cbk.FuncSync.init(
alloc,
func,
cbk_info.func_cbk,
raw_value,
cbk_info,
isolate,
) catch unreachable,
cbk.Arg => cbk.Arg{}, // stage1: we need type
Expand Down
14 changes: 14 additions & 0 deletions src/tests/cbk_test.zig
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,12 @@ pub const Window = struct {
// ignore the error to let the JS msg
}

pub fn get_cbk(_: Window) void {}

pub fn set_cbk(_: *Window, callback: Callback) !void {
callback.call(.{}) catch {};
}

pub fn deinit(_: *Window, _: std.mem.Allocator) void {}
};

Expand Down Expand Up @@ -212,4 +218,12 @@ pub fn exec(
},
};
try tests.checkCases(js_env, &cases_cbk_async_with_nat_arg);

// setter cbk
var cases_cbk_setter_arg = [_]tests.Case{
.{ .src = "let v = 0", .ex = "undefined" },
.{ .src = "window.cbk = () => {v++};", .ex = "() => {v++}" },
.{ .src = "v", .ex = "1" },
};
try tests.checkCases(js_env, &cases_cbk_setter_arg);
}

0 comments on commit 8fe165b

Please sign in to comment.