Skip to content

Commit be6ebd6

Browse files
Remove TPL global value and from Engine interface definition
And remove last v8 reference outside engines/v8 Signed-off-by: Francis Bouvier <[email protected]>
1 parent 251b2d3 commit be6ebd6

File tree

9 files changed

+71
-66
lines changed

9 files changed

+71
-66
lines changed

src/api.zig

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ pub const Console = @import("console.zig").Console;
4141
const Engine = @import("private_api.zig").Engine;
4242

4343
pub const API = Engine.API;
44-
pub const TPL = Engine.TPL;
4544

4645
pub const JSResult = Engine.JSResult;
4746
pub const JSObject = Engine.JSObject;

src/engine.zig

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ const NativeContext = internal.NativeContext;
99

1010
const public = @import("api.zig");
1111
const API = public.API;
12-
const TPL = public.TPL;
1312
const Env = public.Env;
1413
const Loop = public.Loop;
1514

@@ -37,8 +36,8 @@ pub fn loadEnv(
3736
if (builtin.is_test) {
3837
load_start = try std.time.Instant.now();
3938
}
40-
var tpls: [apis.len]TPL = undefined;
41-
try js_env.load(apis, &tpls);
39+
var types: [apis.len]usize = undefined;
40+
try js_env.load(apis, &types);
4241

4342
// execute JS function
4443
var exec_start: std.time.Instant = undefined;

src/engines/v8/generate.zig

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ fn throwBasicError(msg: []const u8, isolate: v8.Isolate) v8.Value {
3030

3131
fn throwError(
3232
alloc: std.mem.Allocator,
33-
objects: *NativeContext.Objects,
33+
nat_ctx: *NativeContext,
3434
comptime T_refl: refl.Struct,
3535
comptime all_T: []refl.Struct,
3636
comptime func: refl.Func,
@@ -60,7 +60,7 @@ fn throwError(
6060
const obj = except.?.T.init(alloc, err, func.js_name) catch unreachable; // TODO
6161
const js_obj = setNativeObject(
6262
alloc,
63-
objects,
63+
nat_ctx,
6464
except.?,
6565
@TypeOf(obj),
6666
obj,
@@ -486,13 +486,23 @@ fn bindObjectNativeAndJS(
486486
return js_obj_binded;
487487
}
488488

489-
inline fn initJSObject(index: usize, js_ctx: v8.Context) v8.Object {
490-
return gen.getTpl(index).tpl.getInstanceTemplate().initInstance(js_ctx);
489+
pub fn getTpl(nat_ctx: *NativeContext, index: usize) v8.FunctionTemplate {
490+
const handle = nat_ctx.getType(v8.C_FunctionTemplate, index);
491+
return v8.FunctionTemplate{ .handle = handle };
492+
}
493+
494+
inline fn initJSObject(
495+
nat_ctx: *NativeContext,
496+
index: usize,
497+
js_ctx: v8.Context,
498+
) v8.Object {
499+
const tpl = getTpl(nat_ctx, index);
500+
return tpl.getInstanceTemplate().initInstance(js_ctx);
491501
}
492502

493503
pub fn setNativeObject(
494504
alloc: std.mem.Allocator,
495-
objects: *NativeContext.Objects,
505+
nat_ctx: *NativeContext,
496506
comptime T_refl: refl.Struct,
497507
comptime T: type,
498508
nat_obj: anytype,
@@ -531,12 +541,12 @@ pub fn setNativeObject(
531541

532542
// Native object is a value, we need to return a new JS object
533543
// we can create it directly from its template
534-
js_obj_under = initJSObject(T_refl.index, js_ctx);
544+
js_obj_under = initJSObject(nat_ctx, T_refl.index, js_ctx);
535545
} else {
536546

537547
// JS object is not provided, check the objects map
538548
const nat_obj_ref = @intFromPtr(nat_obj_ptr);
539-
if (objects.get(nat_obj_ref)) |js_obj_ref| {
549+
if (nat_ctx.objects.get(nat_obj_ref)) |js_obj_ref| {
540550

541551
// a JS object is already linked to the current Native object
542552
// return it
@@ -547,14 +557,14 @@ pub fn setNativeObject(
547557

548558
// no JS object is linked to the current Native object
549559
// let's create one from its template
550-
js_obj_under = initJSObject(T_refl.index, js_ctx);
560+
js_obj_under = initJSObject(nat_ctx, T_refl.index, js_ctx);
551561
}
552562
}
553563

554564
// bind Native and JS objects together
555565
return try bindObjectNativeAndJS(
556566
alloc,
557-
objects,
567+
nat_ctx.objects,
558568
T_refl,
559569
nat_obj_ptr,
560570
js_obj_under,
@@ -565,7 +575,7 @@ pub fn setNativeObject(
565575

566576
fn setReturnType(
567577
alloc: std.mem.Allocator,
568-
objects: *NativeContext.Objects,
578+
nat_ctx: *NativeContext,
569579
comptime all_T: []refl.Struct,
570580
comptime ret: refl.Type,
571581
comptime func: refl.Func,
@@ -584,7 +594,7 @@ fn setReturnType(
584594
}
585595
return setReturnType(
586596
alloc,
587-
objects,
597+
nat_ctx,
588598
all_T,
589599
ret,
590600
func,
@@ -604,7 +614,7 @@ fn setReturnType(
604614
if (std.mem.eql(u8, activeTag, tt.name.?)) {
605615
return setReturnType(
606616
alloc,
607-
objects,
617+
nat_ctx,
608618
all_T,
609619
tt,
610620
func,
@@ -629,7 +639,7 @@ fn setReturnType(
629639
const name = field.name.?;
630640
const js_val = try setReturnType(
631641
alloc,
632-
objects,
642+
nat_ctx,
633643
all_T,
634644
field,
635645
func,
@@ -652,7 +662,7 @@ fn setReturnType(
652662

653663
const js_obj = try setNativeObject(
654664
alloc,
655-
objects,
665+
nat_ctx,
656666
all_T[index],
657667
ret.underT(),
658668
res,
@@ -808,7 +818,7 @@ fn callFunc(
808818
// TODO: how to handle internal errors vs user errors
809819
const js_err = throwError(
810820
nat_ctx.alloc,
811-
nat_ctx.objects,
821+
nat_ctx,
812822
T_refl,
813823
all_T,
814824
func,
@@ -827,7 +837,7 @@ fn callFunc(
827837
// bind native object to JS object this
828838
_ = setNativeObject(
829839
nat_ctx.alloc,
830-
nat_ctx.objects,
840+
nat_ctx,
831841
T_refl,
832842
func.return_type.underT(),
833843
res,
@@ -841,7 +851,7 @@ fn callFunc(
841851
// return to javascript the result
842852
const js_val = setReturnType(
843853
nat_ctx.alloc,
844-
nat_ctx.objects,
854+
nat_ctx,
845855
all_T,
846856
func.return_type,
847857
func,
@@ -852,7 +862,7 @@ fn callFunc(
852862
) catch |err| blk: {
853863
break :blk throwError(
854864
nat_ctx.alloc,
855-
nat_ctx.objects,
865+
nat_ctx,
856866
T_refl,
857867
all_T,
858868
func,

src/engines/v8/v8.zig

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ pub const CallbackArg = @import("callback.zig").Arg;
1717
pub const LoadFnType = @import("generate.zig").LoadFnType;
1818
pub const loadFn = @import("generate.zig").loadFn;
1919
const setNativeObject = @import("generate.zig").setNativeObject;
20+
const getTpl = @import("generate.zig").getTpl;
2021

2122
const nativeToJS = @import("types_primitives.zig").nativeToJS;
2223
const valueToUtf8 = @import("types_primitives.zig").valueToUtf8;
@@ -39,14 +40,6 @@ pub const TemplateType = v8.FunctionTemplate;
3940
pub const TPL = struct {
4041
tpl: v8.FunctionTemplate,
4142
index: usize,
42-
43-
pub inline fn template(self: TPL) v8.FunctionTemplate {
44-
return self.tpl;
45-
}
46-
47-
pub inline fn idx(self: TPL) usize {
48-
return self.index;
49-
}
5043
};
5144

5245
pub const Object = v8.Object;
@@ -162,8 +155,13 @@ pub const Env = struct {
162155
}
163156

164157
// load APIs into Javascript environement
165-
pub fn load(self: Env, comptime apis: []API, tpls: []TPL) anyerror!void {
166-
try gen.load(self.nat_ctx, self.isolate, self.globals, apis, tpls);
158+
pub fn load(self: Env, comptime apis: []API, js_types: []usize) anyerror!void {
159+
var tpls: [apis.len]TPL = undefined;
160+
try gen.load(self.nat_ctx, self.isolate, self.globals, apis, TPL, &tpls);
161+
for (tpls, 0..) |tpl, i| {
162+
js_types[i] = @intFromPtr(tpl.tpl.handle);
163+
}
164+
self.nat_ctx.loadTypes(js_types);
167165
}
168166

169167
// start a Javascript context
@@ -187,8 +185,8 @@ pub const Env = struct {
187185
// TODO: is there a better way to do it at the Template level?
188186
// see https://github.com/Browsercore/jsruntime-lib/issues/128
189187
if (api.T_refl.proto_index) |proto_index| {
190-
const cstr_tpl = gen.getTpl(i).tpl;
191-
const proto_tpl = gen.getTpl(proto_index).tpl;
188+
const cstr_tpl = getTpl(self.nat_ctx, i);
189+
const proto_tpl = getTpl(self.nat_ctx, proto_index);
192190
const cstr_obj = cstr_tpl.getFunction(js_ctx).toObject();
193191
const proto_obj = proto_tpl.getFunction(js_ctx).toObject();
194192
_ = cstr_obj.setPrototype(js_ctx, proto_obj);

src/generate.zig

Lines changed: 10 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,12 @@
11
const std = @import("std");
2-
const v8 = @import("v8");
32

43
const internal = @import("internal_api.zig");
54
const refl = internal.refl;
65
const NativeContext = internal.NativeContext;
76

8-
const public = @import("api.zig");
9-
const API = public.API;
10-
const TPL = public.TPL;
7+
const API = @import("api.zig").API;
118

12-
const private = @import("private_api.zig");
13-
const loadFn = private.loadFn;
9+
const loadFn = @import("private_api.zig").loadFn;
1410

1511
// Compile and loading mechanism
1612
// -----------------------------
@@ -46,33 +42,22 @@ pub fn compile(comptime types: anytype) []API {
4642
}
4743
}
4844

49-
// The list of APIs and TPLs holds corresponding data,
50-
// ie. TPLs[0] is generated by APIs[0].
51-
// This is assumed by the rest of the loading mechanism.
52-
// Therefore the content of thoses lists (and their order) should not be altered
53-
// afterwards.
54-
var TPLs: []TPL = undefined;
55-
56-
pub fn getTpl(index: usize) TPL {
57-
return TPLs[index];
58-
}
59-
60-
// Load native APIs into a JS isolate
45+
// Load native APIs into a JS sandbox
6146
// This function is called at runtime.
6247
pub fn load(
6348
nat_ctx: *NativeContext,
64-
isolate: v8.Isolate,
65-
globals: v8.ObjectTemplate,
49+
js_sandbox: anytype,
50+
js_globals: anytype,
6651
comptime apis: []API,
67-
tpls: []TPL,
52+
comptime js_T: type,
53+
js_types: []js_T,
6854
) !void {
6955
inline for (apis, 0..) |api, i| {
7056
if (api.T_refl.proto_index == null) {
71-
tpls[i] = try api.load(nat_ctx, isolate, globals, null);
57+
js_types[i] = try api.load(nat_ctx, js_sandbox, js_globals, null);
7258
} else {
73-
const proto_tpl = tpls[api.T_refl.proto_index.?]; // safe because apis are ordered from parent to child
74-
tpls[i] = try api.load(nat_ctx, isolate, globals, proto_tpl);
59+
const proto = js_types[api.T_refl.proto_index.?]; // safe because apis are ordered from parent to child
60+
js_types[i] = try api.load(nat_ctx, js_sandbox, js_globals, proto);
7561
}
7662
}
77-
TPLs = tpls;
7863
}

src/interfaces.zig

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@ pub fn API(comptime T: type, comptime LoadFnType: type) void {
2121
assertDecl(T, "loadFn", fn (self: T) callconv(.Inline) LoadFnType);
2222
}
2323

24-
pub fn TPL(comptime _: type) void {}
25-
2624
pub fn VM(comptime T: type) void {
2725

2826
// init()
@@ -35,7 +33,6 @@ pub fn VM(comptime T: type) void {
3533
pub fn Env(
3634
comptime T: type,
3735
comptime API_T: type,
38-
comptime TPL_T: type,
3936
comptime JSResult_T: type,
4037
comptime Object_T: type,
4138
) void {
@@ -53,7 +50,7 @@ pub fn Env(
5350
assertDecl(T, "load", fn (
5451
self: T,
5552
comptime apis: []API_T,
56-
tpls: []TPL_T,
53+
js_types: []usize,
5754
) anyerror!void);
5855

5956
// start()

src/native_context.zig

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,24 @@ pub const NativeContext = struct {
77
loop: *Loop,
88
objects: *Objects,
99

10+
// NOTE: DO NOT ACCESS DIRECTLY js_types
11+
// - use once loadTypes at startup to set them
12+
// - and then getType during execution to access them
13+
js_types: ?[]usize = null,
14+
1015
pub const Objects = std.AutoHashMapUnmanaged(usize, usize);
16+
17+
// loadTypes into the NativeContext
18+
// The caller holds the memory of the js_types slice,
19+
// no heap allocation is performed at the NativeContext level
20+
pub fn loadTypes(self: *NativeContext, js_types: []usize) void {
21+
std.debug.assert(self.js_types == null);
22+
self.js_types = js_types;
23+
}
24+
25+
pub fn getType(self: NativeContext, comptime T: type, index: usize) *T {
26+
std.debug.assert(self.js_types != null);
27+
const t = self.js_types.?[index];
28+
return @as(*T, @ptrFromInt(t));
29+
}
1130
};

src/private_api.zig

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ fn checkInterfaces(engine: anytype) void {
66

77
// public api
88
interfaces.API(engine.API, engine.LoadFnType);
9-
interfaces.TPL(engine.TPL);
109

1110
interfaces.Callback(engine.Callback);
1211
interfaces.CallbackSync(engine.CallbackSync);
@@ -19,7 +18,6 @@ fn checkInterfaces(engine: anytype) void {
1918
interfaces.Env(
2019
engine.Env,
2120
engine.API,
22-
engine.TPL,
2321
engine.JSResult,
2422
engine.Object,
2523
);

vendor/zig-v8

0 commit comments

Comments
 (0)