|
| 1 | +const std = @import("std"); |
| 2 | +const napi = @import("napi-sys").napi_sys; |
| 3 | +const CallbackInfo = @import("./callback_info.zig").CallbackInfo; |
| 4 | +const napi_env = @import("../env.zig"); |
| 5 | +const Napi = @import("../util/napi.zig").Napi; |
| 6 | +const helper = @import("../util/helper.zig"); |
| 7 | +const GlobalAllocator = @import("../util/allocator.zig"); |
| 8 | + |
| 9 | +var class_constructors: std.StringHashMap(napi.napi_value) = std.StringHashMap(napi.napi_value).init(GlobalAllocator.globalAllocator()); |
| 10 | + |
| 11 | +pub fn ClassWrapper(comptime T: type, comptime HasInit: bool) type { |
| 12 | + const type_info = @typeInfo(T); |
| 13 | + if (type_info != .@"struct") { |
| 14 | + @compileError("Class() only support struct type"); |
| 15 | + } |
| 16 | + |
| 17 | + if (type_info.@"struct".is_tuple) { |
| 18 | + @compileError("Class() does not support tuple type"); |
| 19 | + } |
| 20 | + |
| 21 | + const fields = type_info.@"struct".fields; |
| 22 | + const decls = type_info.@"struct".decls; |
| 23 | + |
| 24 | + const class_name = comptime helper.shortTypeName(T); |
| 25 | + return struct { |
| 26 | + const WrappedType = T; |
| 27 | + |
| 28 | + env: napi.napi_env, |
| 29 | + raw: napi.napi_value, |
| 30 | + |
| 31 | + const Self = @This(); |
| 32 | + |
| 33 | + // 构造器回调 |
| 34 | + fn constructor_callback(env: napi.napi_env, callback_info: napi.napi_callback_info) callconv(.c) napi.napi_value { |
| 35 | + const infos = CallbackInfo.from_raw(env, callback_info); |
| 36 | + |
| 37 | + const data = GlobalAllocator.globalAllocator().create(T) catch return null; |
| 38 | + |
| 39 | + if (@hasDecl(T, "init")) { |
| 40 | + var tuple_args: std.meta.ArgsTuple(T.init) = undefined; |
| 41 | + inline for (@typeInfo(std.meta.ArgsTuple(T.init)).@"fn".params, 0..) |arg, i| { |
| 42 | + tuple_args[i] = Napi.from_napi_value(infos.env, infos.args[i].raw, arg.type.?); |
| 43 | + } |
| 44 | + |
| 45 | + data.* = @call(.auto, T.init, tuple_args) catch { |
| 46 | + GlobalAllocator.globalAllocator().destroy(data); |
| 47 | + return null; |
| 48 | + }; |
| 49 | + } else { |
| 50 | + // init with zero |
| 51 | + data.* = std.mem.zeroes(T); |
| 52 | + // if HasInit, init with args with order of fields |
| 53 | + if (comptime HasInit) { |
| 54 | + inline for (fields, 0..) |field, i| { |
| 55 | + @field(data.*, field.name) = Napi.from_napi_value(infos.env, infos.args[i].raw, field.type); |
| 56 | + } |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + const this_obj = infos.This(); |
| 61 | + |
| 62 | + var ref: napi.napi_ref = undefined; |
| 63 | + const status = napi.napi_wrap(env, this_obj, data, finalize_callback, null, &ref); |
| 64 | + if (status != napi.napi_ok) { |
| 65 | + GlobalAllocator.globalAllocator().destroy(data); |
| 66 | + return null; |
| 67 | + } |
| 68 | + |
| 69 | + var ref_count: u32 = undefined; |
| 70 | + _ = napi.napi_reference_unref(env, ref, &ref_count); |
| 71 | + |
| 72 | + return this_obj; |
| 73 | + } |
| 74 | + |
| 75 | + fn finalize_callback(env: napi.napi_env, data: ?*anyopaque, hint: ?*anyopaque) callconv(.c) void { |
| 76 | + _ = env; |
| 77 | + _ = hint; |
| 78 | + |
| 79 | + if (data) |ptr| { |
| 80 | + const typed_data: *T = @ptrCast(@alignCast(ptr)); |
| 81 | + if (@hasDecl(T, "deinit")) { |
| 82 | + typed_data.deinit(); |
| 83 | + } |
| 84 | + GlobalAllocator.globalAllocator().destroy(typed_data); |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + fn define_class(env: napi.napi_env) !napi.napi_value { |
| 89 | + comptime var property_count: usize = 0; |
| 90 | + inline for (fields) |_| { |
| 91 | + property_count += 1; |
| 92 | + } |
| 93 | + inline for (decls) |decl| { |
| 94 | + if (@typeInfo(@TypeOf(@field(T, decl.name))) == .Fn and |
| 95 | + !std.mem.eql(u8, decl.name, "init") and |
| 96 | + !std.mem.eql(u8, decl.name, "deinit")) |
| 97 | + { |
| 98 | + property_count += 1; |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + var properties: [property_count]napi.napi_property_descriptor = undefined; |
| 103 | + var prop_idx: usize = 0; |
| 104 | + |
| 105 | + inline for (fields) |field| { |
| 106 | + const FieldAccessor = struct { |
| 107 | + fn getter(getter_env: napi.napi_env, info: napi.napi_callback_info) callconv(.c) napi.napi_value { |
| 108 | + const cb_info = CallbackInfo.from_raw(getter_env, info); |
| 109 | + var data: ?*anyopaque = null; |
| 110 | + _ = napi.napi_unwrap(getter_env, cb_info.This(), &data); |
| 111 | + if (data == null) return null; |
| 112 | + |
| 113 | + const instance: *T = @ptrCast(@alignCast(data.?)); |
| 114 | + const field_value = @field(instance.*, field.name); |
| 115 | + return Napi.to_napi_value(getter_env, field_value, field.name) catch null; |
| 116 | + } |
| 117 | + |
| 118 | + fn setter(setter_env: napi.napi_env, info: napi.napi_callback_info) callconv(.c) napi.napi_value { |
| 119 | + const cb_info = CallbackInfo.from_raw(setter_env, info); |
| 120 | + var data: ?*anyopaque = null; |
| 121 | + _ = napi.napi_unwrap(setter_env, cb_info.This(), &data); |
| 122 | + if (data == null) return null; |
| 123 | + |
| 124 | + const instance: *T = @ptrCast(@alignCast(data.?)); |
| 125 | + const args = cb_info.args; |
| 126 | + if (args.len > 0) { |
| 127 | + const new_value = Napi.from_napi_value(setter_env, args[0].raw, field.type); |
| 128 | + @field(instance.*, field.name) = new_value; |
| 129 | + } |
| 130 | + |
| 131 | + return null; |
| 132 | + } |
| 133 | + }; |
| 134 | + |
| 135 | + properties[prop_idx] = napi.napi_property_descriptor{ |
| 136 | + .utf8name = @ptrCast(field.name.ptr), |
| 137 | + .name = null, |
| 138 | + .method = null, |
| 139 | + .getter = FieldAccessor.getter, |
| 140 | + .setter = FieldAccessor.setter, |
| 141 | + .value = null, |
| 142 | + .attributes = napi.napi_default, |
| 143 | + .data = null, |
| 144 | + }; |
| 145 | + prop_idx += 1; |
| 146 | + } |
| 147 | + |
| 148 | + inline for (decls) |decl| { |
| 149 | + if (@typeInfo(@TypeOf(@field(T, decl.name))) == .Fn and |
| 150 | + !std.mem.eql(u8, decl.name, "init") and |
| 151 | + !std.mem.eql(u8, decl.name, "deinit")) |
| 152 | + { |
| 153 | + const method = @field(T, decl.name); |
| 154 | + const method_info = @typeInfo(@TypeOf(method)); |
| 155 | + const params = method_info.Fn.params; |
| 156 | + const is_instance_method = params.len > 0 and params[0].type.? == *T; |
| 157 | + |
| 158 | + const MethodWrapper = struct { |
| 159 | + fn call(method_env: napi.napi_env, info: napi.napi_callback_info) callconv(.c) napi.napi_value { |
| 160 | + const cb_info = CallbackInfo.from_raw(method_env, info); |
| 161 | + |
| 162 | + if (is_instance_method) { |
| 163 | + var data: ?*anyopaque = null; |
| 164 | + _ = napi.napi_unwrap(method_env, cb_info.This(), &data); |
| 165 | + if (data == null) return null; |
| 166 | + |
| 167 | + const instance: *T = @ptrCast(@alignCast(data.?)); |
| 168 | + const result = method(instance); |
| 169 | + return Napi.to_napi_value(method_env, result, decl.name) catch null; |
| 170 | + } else { |
| 171 | + const result = method(); |
| 172 | + return Napi.to_napi_value(method_env, result, decl.name) catch null; |
| 173 | + } |
| 174 | + } |
| 175 | + }; |
| 176 | + |
| 177 | + properties[prop_idx] = napi.napi_property_descriptor{ |
| 178 | + .utf8name = @ptrCast(decl.name.ptr), |
| 179 | + .name = null, |
| 180 | + .method = MethodWrapper.call, |
| 181 | + .getter = null, |
| 182 | + .setter = null, |
| 183 | + .value = null, |
| 184 | + .attributes = if (is_instance_method) napi.napi_default else napi.napi_static, |
| 185 | + .data = null, |
| 186 | + }; |
| 187 | + prop_idx += 1; |
| 188 | + } |
| 189 | + } |
| 190 | + |
| 191 | + var constructor: napi.napi_value = undefined; |
| 192 | + _ = napi.napi_define_class(env, class_name.ptr, class_name.len, constructor_callback, null, prop_idx, &properties, &constructor); |
| 193 | + |
| 194 | + try class_constructors.put(class_name, constructor); |
| 195 | + return constructor; |
| 196 | + } |
| 197 | + |
| 198 | + fn define_custom_method(_: napi.napi_env, _: napi.napi_value) !void {} |
| 199 | + |
| 200 | + /// to_napi_value will create a class constructor and return it |
| 201 | + pub fn to_napi_value(env: napi_env.Env) !napi.napi_value { |
| 202 | + const constructor = try Self.define_class(env.raw); |
| 203 | + |
| 204 | + try Self.define_custom_method(env.raw, constructor); |
| 205 | + |
| 206 | + return constructor; |
| 207 | + } |
| 208 | + }; |
| 209 | +} |
| 210 | + |
| 211 | +/// Create a class with default constructor function |
| 212 | +pub fn Class(comptime T: type) type { |
| 213 | + return ClassWrapper(T, true); |
| 214 | +} |
| 215 | + |
| 216 | +/// Create a class without default constructor function |
| 217 | +pub fn ClassWithoutInit(comptime T: type) type { |
| 218 | + return ClassWrapper(T, false); |
| 219 | +} |
| 220 | + |
| 221 | +pub fn isClass(T: anytype) bool { |
| 222 | + const type_name = @typeName(T); |
| 223 | + |
| 224 | + return std.mem.indexOf(u8, type_name, "ClassWrapper") != null; |
| 225 | +} |
0 commit comments