|
| 1 | +const std = @import("std"); |
| 2 | + |
| 3 | +const v8 = @import("v8"); // TODO: remove |
| 4 | + |
| 5 | +const refl = @import("reflect.zig"); |
| 6 | +const utils = @import("utils.zig"); |
| 7 | +const Loop = @import("loop.zig").SingleThreaded; |
| 8 | + |
| 9 | +// TODO: Make this JS engine agnostic |
| 10 | +// by providing a common interface |
| 11 | + |
| 12 | +pub const Arg = struct { |
| 13 | + // TODO: it's required to have a non-empty struct |
| 14 | + // otherwise LLVM emits a warning |
| 15 | + // "stack frame size (x) exceeds limit (y)" |
| 16 | + foo: bool = false, |
| 17 | +}; |
| 18 | + |
| 19 | +// TODO: set the correct "this" on Func object |
| 20 | +// see https://developer.mozilla.org/en-US/docs/Web/API/setTimeout#the_this_problem |
| 21 | +// should we use: |
| 22 | +// - the context globals? |
| 23 | +// - null? |
| 24 | +// - the calling function (info.getThis)? |
| 25 | + |
| 26 | +pub const FuncSync = struct { |
| 27 | + js_func: v8.Function, |
| 28 | + js_args: []v8.Value, |
| 29 | + isolate: v8.Isolate, |
| 30 | + |
| 31 | + pub fn init( |
| 32 | + alloc: std.mem.Allocator, |
| 33 | + comptime func: refl.Func, |
| 34 | + info: v8.FunctionCallbackInfo, |
| 35 | + isolate: v8.Isolate, |
| 36 | + ) !FuncSync { |
| 37 | + |
| 38 | + // retrieve callback arguments indexes |
| 39 | + // TODO: Should we do that at reflection? |
| 40 | + comptime var js_args_indexes: [func.args_callback_nb]usize = undefined; |
| 41 | + comptime var x: usize = 0; |
| 42 | + inline for (func.args) |arg, i| { |
| 43 | + if (arg.T == Arg) { |
| 44 | + js_args_indexes[x] = i; |
| 45 | + x += 1; |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + // retrieve callback arguments |
| 50 | + // var js_args: [func.args_callback_nb]v8.Value = undefined; |
| 51 | + var js_args = try alloc.alloc(v8.Value, func.args_callback_nb); |
| 52 | + for (js_args_indexes) |index, i| { |
| 53 | + js_args[i] = info.getArg(@intCast(u32, index - func.index_offset)); |
| 54 | + } |
| 55 | + |
| 56 | + // retrieve callback function |
| 57 | + const js_func_index = func.callback_index.? - func.index_offset - 1; // -1 because of self |
| 58 | + const js_func_val = info.getArg(js_func_index); |
| 59 | + if (!js_func_val.isFunction()) { |
| 60 | + return error.JSWrongType; |
| 61 | + } |
| 62 | + const js_func = js_func_val.castTo(v8.Function); |
| 63 | + |
| 64 | + return FuncSync{ |
| 65 | + .js_func = js_func, |
| 66 | + .js_args = js_args, |
| 67 | + .isolate = isolate, |
| 68 | + }; |
| 69 | + } |
| 70 | + |
| 71 | + pub fn call(self: FuncSync, alloc: std.mem.Allocator) void { |
| 72 | + |
| 73 | + // retrieve context |
| 74 | + // NOTE: match the Func.call implementation |
| 75 | + const ctx = self.isolate.getCurrentContext(); |
| 76 | + |
| 77 | + // retrieve JS this from persistent handle |
| 78 | + // TODO: see correct "this" comment above |
| 79 | + const this = ctx.getGlobal(); |
| 80 | + |
| 81 | + // execute function |
| 82 | + _ = self.js_func.call(ctx, this, self.js_args); |
| 83 | + |
| 84 | + // free heap |
| 85 | + alloc.free(self.js_args); |
| 86 | + } |
| 87 | +}; |
| 88 | + |
| 89 | +const PersistentFunction = v8.Persistent(v8.Function); |
| 90 | +const PersistentValue = v8.Persistent(v8.Value); |
| 91 | + |
| 92 | +pub const Func = struct { |
| 93 | + |
| 94 | + // NOTE: we use persistent handles here |
| 95 | + // to ensure the references are not garbage collected |
| 96 | + // at the end of the JS calling function execution. |
| 97 | + js_func_pers: *PersistentFunction, |
| 98 | + js_args_pers: []PersistentValue, |
| 99 | + |
| 100 | + isolate: v8.Isolate, |
| 101 | + |
| 102 | + pub fn init( |
| 103 | + // NOTE: we need to store the JS callback arguments on the heap |
| 104 | + // as the call method will be executed in another stack frame, |
| 105 | + // once the asynchronous operation will be fetched back from the kernel. |
| 106 | + alloc: std.mem.Allocator, |
| 107 | + comptime func: refl.Func, |
| 108 | + info: v8.FunctionCallbackInfo, |
| 109 | + isolate: v8.Isolate, |
| 110 | + ) !Func { |
| 111 | + |
| 112 | + // retrieve callback arguments indexes |
| 113 | + // TODO: Should we do that at reflection? |
| 114 | + comptime var js_args_indexes: [func.args_callback_nb]usize = undefined; |
| 115 | + comptime var x: usize = 0; |
| 116 | + inline for (func.args) |arg, i| { |
| 117 | + if (arg.T == Arg) { |
| 118 | + js_args_indexes[x] = i; |
| 119 | + x += 1; |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + // retrieve callback arguments |
| 124 | + var js_args_pers = try alloc.alloc(PersistentValue, func.args_callback_nb); |
| 125 | + for (js_args_indexes) |index, i| { |
| 126 | + const js_arg = info.getArg(@intCast(u32, index - func.index_offset)); |
| 127 | + const js_arg_pers = PersistentValue.init(isolate, js_arg); |
| 128 | + js_args_pers[i] = js_arg_pers; |
| 129 | + } |
| 130 | + |
| 131 | + // retrieve callback function |
| 132 | + const js_func_index = func.callback_index.? - func.index_offset - 1; // -1 because of self |
| 133 | + const js_func_val = info.getArg(js_func_index); |
| 134 | + if (!js_func_val.isFunction()) { |
| 135 | + return error.JSWrongType; |
| 136 | + } |
| 137 | + const js_func = js_func_val.castTo(v8.Function); |
| 138 | + |
| 139 | + // const js_func_pers = PersistentFunction.init(isolate, js_func); |
| 140 | + var js_func_pers = try alloc.create(PersistentFunction); |
| 141 | + js_func_pers.* = PersistentFunction.init(isolate, js_func); |
| 142 | + |
| 143 | + return Func{ |
| 144 | + .js_func_pers = js_func_pers, |
| 145 | + .js_args_pers = js_args_pers, |
| 146 | + .isolate = isolate, |
| 147 | + }; |
| 148 | + } |
| 149 | + |
| 150 | + fn deinit(self: Func, alloc: std.mem.Allocator) void { |
| 151 | + // cleanup persistent references in v8 |
| 152 | + var js_func_pers = self.js_func_pers; // TODO: why do we need var here? |
| 153 | + js_func_pers.deinit(); |
| 154 | + |
| 155 | + for (self.js_args_pers) |arg| { |
| 156 | + var arg_pers = arg; // TODO: why do we need var here? |
| 157 | + arg_pers.deinit(); |
| 158 | + } |
| 159 | + |
| 160 | + // free heap |
| 161 | + alloc.free(self.js_args_pers); |
| 162 | + alloc.destroy(self.js_func_pers); |
| 163 | + } |
| 164 | + |
| 165 | + pub fn call(self: Func, alloc: std.mem.Allocator) !void { |
| 166 | + defer self.deinit(alloc); |
| 167 | + |
| 168 | + // retrieve context |
| 169 | + // TODO: should we instead store the original context in the Func object? |
| 170 | + // in this case we need to have a permanent handle (Global ?) on it. |
| 171 | + const ctx = self.isolate.getCurrentContext(); |
| 172 | + |
| 173 | + // retrieve JS function from persistent handle |
| 174 | + const js_func = self.js_func_pers.castToFunction(); |
| 175 | + |
| 176 | + // retrieve JS arguments from persistent handle |
| 177 | + const js_args = try alloc.alloc(v8.Value, self.js_args_pers.len); |
| 178 | + defer alloc.free(js_args); |
| 179 | + for (self.js_args_pers) |arg, i| { |
| 180 | + js_args[i] = arg.toValue(); |
| 181 | + } |
| 182 | + |
| 183 | + // retrieve JS "this" from persistent handle |
| 184 | + // TODO: see correct "this" comment above |
| 185 | + const this = ctx.getGlobal(); |
| 186 | + |
| 187 | + // execute function |
| 188 | + const result = js_func.call(ctx, this, js_args); |
| 189 | + if (result == null) { |
| 190 | + return error.JSCallback; |
| 191 | + } |
| 192 | + } |
| 193 | +}; |
0 commit comments