Skip to content

Commit 3ea5bcd

Browse files
Merge pull request #285 from karlseguin/env_by_ref
Take *const Env as argument instead of Env
2 parents 9122de4 + 1a73c8a commit 3ea5bcd

File tree

5 files changed

+31
-31
lines changed

5 files changed

+31
-31
lines changed

src/engines/v8/callback.zig

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -273,15 +273,12 @@ pub const Func = struct {
273273
) orelse return error.V8ObjectNotFound;
274274
}
275275

276-
pub fn deinit(self: Func, alloc: std.mem.Allocator) void {
276+
pub fn deinit(self: *Func, alloc: std.mem.Allocator) void {
277277

278278
// cleanup persistent references in v8
279-
var js_func_pers = self.js_func_pers; // TODO: why do we need var here?
280-
js_func_pers.deinit();
281-
282-
for (self.js_args_pers) |arg| {
283-
var arg_pers = arg; // TODO: why do we need var here?
284-
arg_pers.deinit();
279+
self.js_func_pers.deinit();
280+
for (self.js_args_pers) |*arg| {
281+
arg.deinit();
285282
}
286283

287284
// free heap

src/engines/v8/v8.zig

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -145,8 +145,7 @@ pub const Env = struct {
145145
// ---------
146146

147147
// handle scope
148-
var hscope = self.hscope;
149-
hscope.deinit();
148+
self.hscope.deinit();
150149

151150
// isolate
152151
var isolate = self.isolate;
@@ -565,7 +564,7 @@ pub const JSValue = struct {
565564
value: v8.Value,
566565

567566
// the caller needs to deinit the string returned
568-
pub fn toString(self: JSValue, alloc: std.mem.Allocator, env: Env) anyerror![]const u8 {
567+
pub fn toString(self: JSValue, alloc: std.mem.Allocator, env: *const Env) anyerror![]const u8 {
569568
return valueToUtf8(alloc, self.value, env.isolate, env.js_ctx.?);
570569
}
571570

@@ -585,7 +584,7 @@ pub const JSValue = struct {
585584
pub const TryCatch = struct {
586585
inner: v8.TryCatch,
587586

588-
pub fn init(self: *TryCatch, env: Env) void {
587+
pub fn init(self: *TryCatch, env: *const Env) void {
589588
self.inner.init(env.isolate);
590589
}
591590

@@ -594,8 +593,10 @@ pub const TryCatch = struct {
594593
}
595594

596595
// the caller needs to deinit the string returned
597-
pub fn exception(self: TryCatch, alloc: std.mem.Allocator, env: Env) anyerror!?[]const u8 {
598-
if (env.js_ctx == null) return error.EnvNotStarted;
596+
pub fn exception(self: TryCatch, alloc: std.mem.Allocator, env: *const Env) anyerror!?[]const u8 {
597+
if (env.js_ctx == null) {
598+
return error.EnvNotStarted;
599+
}
599600

600601
if (self.inner.getException()) |msg| {
601602
return try valueToUtf8(alloc, msg, env.isolate, env.js_ctx.?);
@@ -604,8 +605,10 @@ pub const TryCatch = struct {
604605
}
605606

606607
// the caller needs to deinit the string returned
607-
pub fn stack(self: TryCatch, alloc: std.mem.Allocator, env: Env) anyerror!?[]const u8 {
608-
if (env.js_ctx == null) return error.EnvNotStarted;
608+
pub fn stack(self: TryCatch, alloc: std.mem.Allocator, env: *const Env) anyerror!?[]const u8 {
609+
if (env.js_ctx == null) {
610+
return error.EnvNotStarted;
611+
}
609612

610613
const stck = self.inner.getStackTrace(env.js_ctx.?);
611614
if (stck) |s| return try valueToUtf8(alloc, s, env.isolate, env.js_ctx.?);
@@ -617,7 +620,7 @@ pub const TryCatch = struct {
617620
// - in Debug mode return the stack if available
618621
// - otherwhise return the exception if available
619622
// the caller needs to deinit the string returned
620-
pub fn err(self: TryCatch, alloc: std.mem.Allocator, env: Env) anyerror!?[]const u8 {
623+
pub fn err(self: TryCatch, alloc: std.mem.Allocator, env: *const Env) anyerror!?[]const u8 {
621624
if (builtin.mode == .Debug) {
622625
if (try self.stack(alloc, env)) |msg| return msg;
623626
}
@@ -653,7 +656,7 @@ pub const Inspector = struct {
653656

654657
pub fn init(
655658
alloc: std.mem.Allocator,
656-
env: Env,
659+
env: *const Env,
657660
ctx: *anyopaque,
658661
onResp: public.InspectorOnResponseFn,
659662
onEvent: public.InspectorOnEventFn,
@@ -680,7 +683,7 @@ pub const Inspector = struct {
680683
// {isDefault: boolean, type: 'default'|'isolated'|'worker', frameId: string}
681684
pub fn contextCreated(
682685
self: Inspector,
683-
env: Env,
686+
env: *const Env,
684687
name: []const u8,
685688
origin: []const u8,
686689
auxData: ?[]const u8,

src/interfaces.zig

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ pub fn Env(
119119
pub fn JSValue(comptime T: type, env: type) void {
120120

121121
// toString()
122-
assertDecl(T, "toString", fn (self: T, alloc: std.mem.Allocator, env: env) anyerror![]const u8);
122+
assertDecl(T, "toString", fn (self: T, alloc: std.mem.Allocator, env: *const env) anyerror![]const u8);
123123

124124
// typeOf()
125125
assertDecl(T, "typeOf", fn (self: T, env: env) anyerror!public.JSTypes);
@@ -134,7 +134,7 @@ pub fn JSObjectID(comptime T: type) void {
134134
pub fn TryCatch(comptime T: type, comptime env: type) void {
135135

136136
// init()
137-
assertDecl(T, "init", fn (self: *T, env: env) void);
137+
assertDecl(T, "init", fn (self: *T, env: *const env) void);
138138

139139
// deinit()
140140
assertDecl(T, "deinit", fn (self: *T) void);
@@ -146,21 +146,21 @@ pub fn TryCatch(comptime T: type, comptime env: type) void {
146146
assertDecl(T, "exception", fn (
147147
self: T,
148148
alloc: std.mem.Allocator,
149-
env: env,
149+
env: *const env,
150150
) anyerror!?[]const u8);
151151

152152
// err()
153153
assertDecl(T, "err", fn (
154154
self: T,
155155
alloc: std.mem.Allocator,
156-
env: env,
156+
env: *const env,
157157
) anyerror!?[]const u8);
158158

159159
// stack()
160160
assertDecl(T, "stack", fn (
161161
self: T,
162162
alloc: std.mem.Allocator,
163-
env: env,
163+
env: *const env,
164164
) anyerror!?[]const u8);
165165
}
166166

@@ -201,7 +201,7 @@ pub fn Inspector(comptime T: type, comptime Env_T: type) void {
201201
// init()
202202
assertDecl(T, "init", fn (
203203
alloc: std.mem.Allocator,
204-
env: Env_T,
204+
env: *const Env_T,
205205
ctx: *anyopaque,
206206
onResp: public.InspectorOnResponseFn,
207207
onEvent: public.InspectorOnEventFn,
@@ -213,7 +213,7 @@ pub fn Inspector(comptime T: type, comptime Env_T: type) void {
213213
// contextCreated()
214214
assertDecl(T, "contextCreated", fn (
215215
self: T,
216-
env: Env_T,
216+
env: *const Env_T,
217217
name: []const u8,
218218
origin: []const u8,
219219
auxData: ?[]const u8,

src/loop.zig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ pub const SingleThreaded = struct {
150150
};
151151

152152
// js callback
153-
if (ctx.js_cbk) |js_cbk| {
153+
if (ctx.js_cbk) |*js_cbk| {
154154
defer js_cbk.deinit(ctx.loop.alloc);
155155
js_cbk.call(null) catch {
156156
ctx.loop.cbk_error = true;
@@ -209,7 +209,7 @@ pub const SingleThreaded = struct {
209209
};
210210

211211
// js callback
212-
if (ctx.js_cbk) |js_cbk| {
212+
if (ctx.js_cbk) |*js_cbk| {
213213
defer js_cbk.deinit(ctx.loop.alloc);
214214
js_cbk.call(null) catch {
215215
ctx.loop.cbk_error = true;

src/tests/test_utils.zig

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ pub fn checkCasesAlloc(allocator: std.mem.Allocator, js_env: *public.Env, cases:
8787
var has_error = false;
8888

8989
var try_catch: public.TryCatch = undefined;
90-
try_catch.init(js_env.*);
90+
try_catch.init(js_env);
9191
defer try_catch.deinit();
9292

9393
// cases
@@ -103,7 +103,7 @@ pub fn checkCasesAlloc(allocator: std.mem.Allocator, js_env: *public.Env, cases:
103103
const res = js_env.execWait(case.src, name) catch |err| {
104104

105105
// is it an intended error?
106-
const except = try try_catch.exception(alloc, js_env.*);
106+
const except = try try_catch.exception(alloc, js_env);
107107
if (except) |msg| {
108108
defer alloc.free(msg);
109109
if (isTypeError(case.ex, msg)) continue;
@@ -119,15 +119,15 @@ pub fn checkCasesAlloc(allocator: std.mem.Allocator, js_env: *public.Env, cases:
119119
error.JSExecCallback => case.cbk_ex,
120120
else => return err,
121121
};
122-
if (try try_catch.stack(alloc, js_env.*)) |stack| {
122+
if (try try_catch.stack(alloc, js_env)) |stack| {
123123
defer alloc.free(stack);
124124
caseError(case.src, expected, except.?, stack);
125125
}
126126
continue;
127127
};
128128

129129
// check if result is expected
130-
const res_string = try res.toString(alloc, js_env.*);
130+
const res_string = try res.toString(alloc, js_env);
131131
defer alloc.free(res_string);
132132
const equal = std.mem.eql(u8, case.ex, res_string);
133133
if (!equal) {

0 commit comments

Comments
 (0)