Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove heap allocation for NativeContext #248

Merged
merged 1 commit into from
Oct 16, 2024
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
3 changes: 2 additions & 1 deletion src/engine.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
22 changes: 12 additions & 10 deletions src/engines/v8/v8.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
// ---------
Expand All @@ -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 {
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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();
Expand All @@ -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(),
Expand Down
6 changes: 3 additions & 3 deletions src/interfaces.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
6 changes: 2 additions & 4 deletions src/native_context.zig
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,14 @@ 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,
.userctx = userctx,
.js_objs = JSObjects{},
.nat_objs = NatObjects{},
};
return self;
}

pub fn stop(self: *NativeContext) void {
Expand All @@ -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));
Expand Down
Loading