diff --git a/src/engine.zig b/src/engine.zig index 901215d..76bbfc8 100644 --- a/src/engine.zig +++ b/src/engine.zig @@ -42,7 +42,8 @@ pub fn loadEnv( } var loop = try Loop.init(alloc); defer loop.deinit(); - var js_env = try Env.init(alloc, &loop, userctx); + var js_env: public.Env = undefined; + Env.init(&js_env, alloc, &loop, userctx); defer js_env.deinit(); // load APIs in JS env diff --git a/src/engines/v8/v8.zig b/src/engines/v8/v8.zig index 16f3c39..7cff259 100644 --- a/src/engines/v8/v8.zig +++ b/src/engines/v8/v8.zig @@ -80,7 +80,7 @@ pub const VM = struct { }; pub const Env = struct { - nat_ctx: *NativeContext, + nat_ctx: NativeContext, isolate: v8.Isolate, isolate_params: v8.CreateParams, @@ -94,10 +94,11 @@ pub const Env = struct { } pub fn init( + self: *Env, alloc: std.mem.Allocator, loop: *public.Loop, userctx: ?public.UserContext, - ) anyerror!Env { + ) void { // v8 values // --------- @@ -117,13 +118,14 @@ pub const Env = struct { // ObjectTemplate for the global namespace const globals = v8.FunctionTemplate.initDefault(isolate); - return Env{ - .nat_ctx = try NativeContext.init(alloc, loop, userctx), + self.* = Env{ + .nat_ctx = undefined, .isolate_params = params, .isolate = isolate, .hscope = hscope, .globals = globals, }; + NativeContext.init(&self.nat_ctx, alloc, loop, userctx); } pub fn deinit(self: *Env) void { @@ -155,9 +157,9 @@ pub const Env = struct { } // load user-defined Types into Javascript environement - pub fn load(self: Env, js_types: []usize) anyerror!void { + pub fn load(self: *Env, js_types: []usize) anyerror!void { var tpls: [gen.Types.len]TPL = undefined; - try gen.load(self.nat_ctx, self.isolate, self.globals, TPL, &tpls); + try gen.load(&self.nat_ctx, self.isolate, self.globals, TPL, &tpls); for (tpls, 0..) |tpl, i| { js_types[i] = @intFromPtr(tpl.tpl.handle); } @@ -185,8 +187,8 @@ pub const Env = struct { // TODO: is there a better way to do it at the Template level? // see https://github.com/Browsercore/jsruntime-lib/issues/128 if (T_refl.proto_index) |proto_index| { - const cstr_tpl = getTpl(self.nat_ctx, i); - const proto_tpl = getTpl(self.nat_ctx, proto_index); + const cstr_tpl = getTpl(&self.nat_ctx, i); + const proto_tpl = getTpl(&self.nat_ctx, proto_index); const cstr_obj = cstr_tpl.getFunction(js_ctx).toObject(); const proto_obj = proto_tpl.getFunction(js_ctx).toObject(); _ = cstr_obj.setPrototype(js_ctx, proto_obj); @@ -225,7 +227,7 @@ pub const Env = struct { return self.js_ctx.?.getGlobal(); } - pub fn bindGlobal(self: Env, obj: anytype) anyerror!void { + pub fn bindGlobal(self: *Env, obj: anytype) anyerror!void { const T_refl = comptime gen.getType(@TypeOf(obj)); if (!comptime refl.isGlobalType(T_refl.T)) return error.notGlobalType; const T = T_refl.Self(); @@ -252,7 +254,7 @@ pub const Env = struct { _ = try bindObjectNativeAndJS( self.nat_ctx.alloc, - self.nat_ctx, + &self.nat_ctx, T_refl, nat_obj_ptr, self.js_ctx.?.getGlobal(), diff --git a/src/interfaces.zig b/src/interfaces.zig index 1d7db3c..7de3a36 100644 --- a/src/interfaces.zig +++ b/src/interfaces.zig @@ -54,15 +54,15 @@ pub fn Env( assertDecl(T, "engine", fn () public.engineType); // init() - assertDecl(T, "init", fn (alloc: std.mem.Allocator, loop: *public.Loop, userctx: ?public.UserContext) anyerror!T); + assertDecl(T, "init", fn (self: *T, alloc: std.mem.Allocator, loop: *public.Loop, userctx: ?public.UserContext) void); // deinit() assertDecl(T, "deinit", fn (self: *T) void); // load() native apis into js templates - assertDecl(T, "load", fn (self: T, js_types: []usize) anyerror!void); + assertDecl(T, "load", fn (self: *T, js_types: []usize) anyerror!void); - assertDecl(T, "bindGlobal", fn (self: T, ob: anytype) anyerror!void); + assertDecl(T, "bindGlobal", fn (self: *T, ob: anytype) anyerror!void); assertDecl(T, "setUserContext", fn ( self: *T, diff --git a/src/native_context.zig b/src/native_context.zig index 8cb88b8..7fb7a71 100644 --- a/src/native_context.zig +++ b/src/native_context.zig @@ -33,8 +33,7 @@ pub const NativeContext = struct { pub const JSObjects = std.AutoHashMapUnmanaged(usize, usize); - pub fn init(alloc: std.mem.Allocator, loop: *Loop, userctx: ?UserContext) !*NativeContext { - const self = try alloc.create(NativeContext); + pub fn init(self: *NativeContext, alloc: std.mem.Allocator, loop: *Loop, userctx: ?UserContext) void { self.* = .{ .alloc = alloc, .loop = loop, @@ -42,7 +41,6 @@ pub const NativeContext = struct { .js_objs = JSObjects{}, .nat_objs = NatObjects{}, }; - return self; } pub fn stop(self: *NativeContext) void { @@ -58,7 +56,7 @@ pub const NativeContext = struct { self.js_types = js_types; } - pub fn getType(self: NativeContext, comptime T: type, index: usize) *T { + pub fn getType(self: *const NativeContext, comptime T: type, index: usize) *T { std.debug.assert(self.js_types != null); const t = self.js_types.?[index]; return @as(*T, @ptrFromInt(t));