diff --git a/src/engines/v8/callback.zig b/src/engines/v8/callback.zig index 74cb6df..8f39927 100644 --- a/src/engines/v8/callback.zig +++ b/src/engines/v8/callback.zig @@ -273,15 +273,12 @@ pub const Func = struct { ) orelse return error.V8ObjectNotFound; } - pub fn deinit(self: Func, alloc: std.mem.Allocator) void { + pub fn deinit(self: *Func, alloc: std.mem.Allocator) void { // cleanup persistent references in v8 - var js_func_pers = self.js_func_pers; // TODO: why do we need var here? - js_func_pers.deinit(); - - for (self.js_args_pers) |arg| { - var arg_pers = arg; // TODO: why do we need var here? - arg_pers.deinit(); + self.js_func_pers.deinit(); + for (self.js_args_pers) |*arg| { + arg.deinit(); } // free heap diff --git a/src/engines/v8/v8.zig b/src/engines/v8/v8.zig index 6258f81..a1dbc52 100644 --- a/src/engines/v8/v8.zig +++ b/src/engines/v8/v8.zig @@ -145,8 +145,7 @@ pub const Env = struct { // --------- // handle scope - var hscope = self.hscope; - hscope.deinit(); + self.hscope.deinit(); // isolate var isolate = self.isolate; @@ -565,7 +564,7 @@ pub const JSValue = struct { value: v8.Value, // the caller needs to deinit the string returned - pub fn toString(self: JSValue, alloc: std.mem.Allocator, env: Env) anyerror![]const u8 { + pub fn toString(self: JSValue, alloc: std.mem.Allocator, env: *const Env) anyerror![]const u8 { return valueToUtf8(alloc, self.value, env.isolate, env.js_ctx.?); } @@ -585,7 +584,7 @@ pub const JSValue = struct { pub const TryCatch = struct { inner: v8.TryCatch, - pub fn init(self: *TryCatch, env: Env) void { + pub fn init(self: *TryCatch, env: *const Env) void { self.inner.init(env.isolate); } @@ -594,8 +593,10 @@ pub const TryCatch = struct { } // the caller needs to deinit the string returned - pub fn exception(self: TryCatch, alloc: std.mem.Allocator, env: Env) anyerror!?[]const u8 { - if (env.js_ctx == null) return error.EnvNotStarted; + pub fn exception(self: TryCatch, alloc: std.mem.Allocator, env: *const Env) anyerror!?[]const u8 { + if (env.js_ctx == null) { + return error.EnvNotStarted; + } if (self.inner.getException()) |msg| { return try valueToUtf8(alloc, msg, env.isolate, env.js_ctx.?); @@ -604,8 +605,10 @@ pub const TryCatch = struct { } // the caller needs to deinit the string returned - pub fn stack(self: TryCatch, alloc: std.mem.Allocator, env: Env) anyerror!?[]const u8 { - if (env.js_ctx == null) return error.EnvNotStarted; + pub fn stack(self: TryCatch, alloc: std.mem.Allocator, env: *const Env) anyerror!?[]const u8 { + if (env.js_ctx == null) { + return error.EnvNotStarted; + } const stck = self.inner.getStackTrace(env.js_ctx.?); if (stck) |s| return try valueToUtf8(alloc, s, env.isolate, env.js_ctx.?); @@ -617,7 +620,7 @@ pub const TryCatch = struct { // - in Debug mode return the stack if available // - otherwhise return the exception if available // the caller needs to deinit the string returned - pub fn err(self: TryCatch, alloc: std.mem.Allocator, env: Env) anyerror!?[]const u8 { + pub fn err(self: TryCatch, alloc: std.mem.Allocator, env: *const Env) anyerror!?[]const u8 { if (builtin.mode == .Debug) { if (try self.stack(alloc, env)) |msg| return msg; } @@ -653,7 +656,7 @@ pub const Inspector = struct { pub fn init( alloc: std.mem.Allocator, - env: Env, + env: *const Env, ctx: *anyopaque, onResp: public.InspectorOnResponseFn, onEvent: public.InspectorOnEventFn, @@ -680,7 +683,7 @@ pub const Inspector = struct { // {isDefault: boolean, type: 'default'|'isolated'|'worker', frameId: string} pub fn contextCreated( self: Inspector, - env: Env, + env: *const Env, name: []const u8, origin: []const u8, auxData: ?[]const u8, diff --git a/src/interfaces.zig b/src/interfaces.zig index ee82eeb..9151df8 100644 --- a/src/interfaces.zig +++ b/src/interfaces.zig @@ -119,7 +119,7 @@ pub fn Env( pub fn JSValue(comptime T: type, env: type) void { // toString() - assertDecl(T, "toString", fn (self: T, alloc: std.mem.Allocator, env: env) anyerror![]const u8); + assertDecl(T, "toString", fn (self: T, alloc: std.mem.Allocator, env: *const env) anyerror![]const u8); // typeOf() assertDecl(T, "typeOf", fn (self: T, env: env) anyerror!public.JSTypes); @@ -134,7 +134,7 @@ pub fn JSObjectID(comptime T: type) void { pub fn TryCatch(comptime T: type, comptime env: type) void { // init() - assertDecl(T, "init", fn (self: *T, env: env) void); + assertDecl(T, "init", fn (self: *T, env: *const env) void); // deinit() assertDecl(T, "deinit", fn (self: *T) void); @@ -146,21 +146,21 @@ pub fn TryCatch(comptime T: type, comptime env: type) void { assertDecl(T, "exception", fn ( self: T, alloc: std.mem.Allocator, - env: env, + env: *const env, ) anyerror!?[]const u8); // err() assertDecl(T, "err", fn ( self: T, alloc: std.mem.Allocator, - env: env, + env: *const env, ) anyerror!?[]const u8); // stack() assertDecl(T, "stack", fn ( self: T, alloc: std.mem.Allocator, - env: env, + env: *const env, ) anyerror!?[]const u8); } @@ -201,7 +201,7 @@ pub fn Inspector(comptime T: type, comptime Env_T: type) void { // init() assertDecl(T, "init", fn ( alloc: std.mem.Allocator, - env: Env_T, + env: *const Env_T, ctx: *anyopaque, onResp: public.InspectorOnResponseFn, onEvent: public.InspectorOnEventFn, @@ -213,7 +213,7 @@ pub fn Inspector(comptime T: type, comptime Env_T: type) void { // contextCreated() assertDecl(T, "contextCreated", fn ( self: T, - env: Env_T, + env: *const Env_T, name: []const u8, origin: []const u8, auxData: ?[]const u8, diff --git a/src/loop.zig b/src/loop.zig index 6d595fb..59f5820 100644 --- a/src/loop.zig +++ b/src/loop.zig @@ -150,7 +150,7 @@ pub const SingleThreaded = struct { }; // js callback - if (ctx.js_cbk) |js_cbk| { + if (ctx.js_cbk) |*js_cbk| { defer js_cbk.deinit(ctx.loop.alloc); js_cbk.call(null) catch { ctx.loop.cbk_error = true; @@ -209,7 +209,7 @@ pub const SingleThreaded = struct { }; // js callback - if (ctx.js_cbk) |js_cbk| { + if (ctx.js_cbk) |*js_cbk| { defer js_cbk.deinit(ctx.loop.alloc); js_cbk.call(null) catch { ctx.loop.cbk_error = true; diff --git a/src/tests/test_utils.zig b/src/tests/test_utils.zig index 3e5aacb..73e8358 100644 --- a/src/tests/test_utils.zig +++ b/src/tests/test_utils.zig @@ -87,7 +87,7 @@ pub fn checkCasesAlloc(allocator: std.mem.Allocator, js_env: *public.Env, cases: var has_error = false; var try_catch: public.TryCatch = undefined; - try_catch.init(js_env.*); + try_catch.init(js_env); defer try_catch.deinit(); // cases @@ -103,7 +103,7 @@ pub fn checkCasesAlloc(allocator: std.mem.Allocator, js_env: *public.Env, cases: const res = js_env.execWait(case.src, name) catch |err| { // is it an intended error? - const except = try try_catch.exception(alloc, js_env.*); + const except = try try_catch.exception(alloc, js_env); if (except) |msg| { defer alloc.free(msg); if (isTypeError(case.ex, msg)) continue; @@ -119,7 +119,7 @@ pub fn checkCasesAlloc(allocator: std.mem.Allocator, js_env: *public.Env, cases: error.JSExecCallback => case.cbk_ex, else => return err, }; - if (try try_catch.stack(alloc, js_env.*)) |stack| { + if (try try_catch.stack(alloc, js_env)) |stack| { defer alloc.free(stack); caseError(case.src, expected, except.?, stack); } @@ -127,7 +127,7 @@ pub fn checkCasesAlloc(allocator: std.mem.Allocator, js_env: *public.Env, cases: }; // check if result is expected - const res_string = try res.toString(alloc, js_env.*); + const res_string = try res.toString(alloc, js_env); defer alloc.free(res_string); const equal = std.mem.eql(u8, case.ex, res_string); if (!equal) {