Skip to content

Commit 6287339

Browse files
Merge pull request #248 from lightpanda-io/nat_ctx_stack
Remove heap allocation for NativeContext
2 parents da15ee8 + 4eaacec commit 6287339

File tree

4 files changed

+19
-18
lines changed

4 files changed

+19
-18
lines changed

src/engine.zig

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ pub fn loadEnv(
4242
}
4343
var loop = try Loop.init(alloc);
4444
defer loop.deinit();
45-
var js_env = try Env.init(alloc, &loop, userctx);
45+
var js_env: public.Env = undefined;
46+
Env.init(&js_env, alloc, &loop, userctx);
4647
defer js_env.deinit();
4748

4849
// load APIs in JS env

src/engines/v8/v8.zig

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ pub const VM = struct {
8080
};
8181

8282
pub const Env = struct {
83-
nat_ctx: *NativeContext,
83+
nat_ctx: NativeContext,
8484

8585
isolate: v8.Isolate,
8686
isolate_params: v8.CreateParams,
@@ -94,10 +94,11 @@ pub const Env = struct {
9494
}
9595

9696
pub fn init(
97+
self: *Env,
9798
alloc: std.mem.Allocator,
9899
loop: *public.Loop,
99100
userctx: ?public.UserContext,
100-
) anyerror!Env {
101+
) void {
101102

102103
// v8 values
103104
// ---------
@@ -117,13 +118,14 @@ pub const Env = struct {
117118
// ObjectTemplate for the global namespace
118119
const globals = v8.FunctionTemplate.initDefault(isolate);
119120

120-
return Env{
121-
.nat_ctx = try NativeContext.init(alloc, loop, userctx),
121+
self.* = Env{
122+
.nat_ctx = undefined,
122123
.isolate_params = params,
123124
.isolate = isolate,
124125
.hscope = hscope,
125126
.globals = globals,
126127
};
128+
NativeContext.init(&self.nat_ctx, alloc, loop, userctx);
127129
}
128130

129131
pub fn deinit(self: *Env) void {
@@ -155,9 +157,9 @@ pub const Env = struct {
155157
}
156158

157159
// load user-defined Types into Javascript environement
158-
pub fn load(self: Env, js_types: []usize) anyerror!void {
160+
pub fn load(self: *Env, js_types: []usize) anyerror!void {
159161
var tpls: [gen.Types.len]TPL = undefined;
160-
try gen.load(self.nat_ctx, self.isolate, self.globals, TPL, &tpls);
162+
try gen.load(&self.nat_ctx, self.isolate, self.globals, TPL, &tpls);
161163
for (tpls, 0..) |tpl, i| {
162164
js_types[i] = @intFromPtr(tpl.tpl.handle);
163165
}
@@ -185,8 +187,8 @@ pub const Env = struct {
185187
// TODO: is there a better way to do it at the Template level?
186188
// see https://github.com/Browsercore/jsruntime-lib/issues/128
187189
if (T_refl.proto_index) |proto_index| {
188-
const cstr_tpl = getTpl(self.nat_ctx, i);
189-
const proto_tpl = getTpl(self.nat_ctx, proto_index);
190+
const cstr_tpl = getTpl(&self.nat_ctx, i);
191+
const proto_tpl = getTpl(&self.nat_ctx, proto_index);
190192
const cstr_obj = cstr_tpl.getFunction(js_ctx).toObject();
191193
const proto_obj = proto_tpl.getFunction(js_ctx).toObject();
192194
_ = cstr_obj.setPrototype(js_ctx, proto_obj);
@@ -225,7 +227,7 @@ pub const Env = struct {
225227
return self.js_ctx.?.getGlobal();
226228
}
227229

228-
pub fn bindGlobal(self: Env, obj: anytype) anyerror!void {
230+
pub fn bindGlobal(self: *Env, obj: anytype) anyerror!void {
229231
const T_refl = comptime gen.getType(@TypeOf(obj));
230232
if (!comptime refl.isGlobalType(T_refl.T)) return error.notGlobalType;
231233
const T = T_refl.Self();
@@ -252,7 +254,7 @@ pub const Env = struct {
252254

253255
_ = try bindObjectNativeAndJS(
254256
self.nat_ctx.alloc,
255-
self.nat_ctx,
257+
&self.nat_ctx,
256258
T_refl,
257259
nat_obj_ptr,
258260
self.js_ctx.?.getGlobal(),

src/interfaces.zig

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,15 +54,15 @@ pub fn Env(
5454
assertDecl(T, "engine", fn () public.engineType);
5555

5656
// init()
57-
assertDecl(T, "init", fn (alloc: std.mem.Allocator, loop: *public.Loop, userctx: ?public.UserContext) anyerror!T);
57+
assertDecl(T, "init", fn (self: *T, alloc: std.mem.Allocator, loop: *public.Loop, userctx: ?public.UserContext) void);
5858

5959
// deinit()
6060
assertDecl(T, "deinit", fn (self: *T) void);
6161

6262
// load() native apis into js templates
63-
assertDecl(T, "load", fn (self: T, js_types: []usize) anyerror!void);
63+
assertDecl(T, "load", fn (self: *T, js_types: []usize) anyerror!void);
6464

65-
assertDecl(T, "bindGlobal", fn (self: T, ob: anytype) anyerror!void);
65+
assertDecl(T, "bindGlobal", fn (self: *T, ob: anytype) anyerror!void);
6666

6767
assertDecl(T, "setUserContext", fn (
6868
self: *T,

src/native_context.zig

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,16 +33,14 @@ pub const NativeContext = struct {
3333

3434
pub const JSObjects = std.AutoHashMapUnmanaged(usize, usize);
3535

36-
pub fn init(alloc: std.mem.Allocator, loop: *Loop, userctx: ?UserContext) !*NativeContext {
37-
const self = try alloc.create(NativeContext);
36+
pub fn init(self: *NativeContext, alloc: std.mem.Allocator, loop: *Loop, userctx: ?UserContext) void {
3837
self.* = .{
3938
.alloc = alloc,
4039
.loop = loop,
4140
.userctx = userctx,
4241
.js_objs = JSObjects{},
4342
.nat_objs = NatObjects{},
4443
};
45-
return self;
4644
}
4745

4846
pub fn stop(self: *NativeContext) void {
@@ -58,7 +56,7 @@ pub const NativeContext = struct {
5856
self.js_types = js_types;
5957
}
6058

61-
pub fn getType(self: NativeContext, comptime T: type, index: usize) *T {
59+
pub fn getType(self: *const NativeContext, comptime T: type, index: usize) *T {
6260
std.debug.assert(self.js_types != null);
6361
const t = self.js_types.?[index];
6462
return @as(*T, @ptrFromInt(t));

0 commit comments

Comments
 (0)