Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 4 additions & 7 deletions src/engines/v8/callback.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
25 changes: 14 additions & 11 deletions src/engines/v8/v8.zig
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,7 @@ pub const Env = struct {
// ---------

// handle scope
var hscope = self.hscope;
hscope.deinit();
self.hscope.deinit();

// isolate
var isolate = self.isolate;
Expand Down Expand Up @@ -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.?);
}

Expand All @@ -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);
}

Expand All @@ -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.?);
Expand All @@ -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.?);
Expand All @@ -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;
}
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand Down
14 changes: 7 additions & 7 deletions src/interfaces.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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);
}

Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions src/loop.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
8 changes: 4 additions & 4 deletions src/tests/test_utils.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
Expand All @@ -119,15 +119,15 @@ 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);
}
continue;
};

// 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) {
Expand Down