Skip to content

Commit

Permalink
Remove TPL global value and from Engine interface definition
Browse files Browse the repository at this point in the history
And remove last v8 reference outside engines/v8

Signed-off-by: Francis Bouvier <[email protected]>
  • Loading branch information
francisbouvier committed Dec 18, 2023
1 parent 251b2d3 commit be6ebd6
Show file tree
Hide file tree
Showing 9 changed files with 71 additions and 66 deletions.
1 change: 0 additions & 1 deletion src/api.zig
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ pub const Console = @import("console.zig").Console;
const Engine = @import("private_api.zig").Engine;

pub const API = Engine.API;
pub const TPL = Engine.TPL;

pub const JSResult = Engine.JSResult;
pub const JSObject = Engine.JSObject;
Expand Down
5 changes: 2 additions & 3 deletions src/engine.zig
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ const NativeContext = internal.NativeContext;

const public = @import("api.zig");
const API = public.API;
const TPL = public.TPL;
const Env = public.Env;
const Loop = public.Loop;

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

// execute JS function
var exec_start: std.time.Instant = undefined;
Expand Down
46 changes: 28 additions & 18 deletions src/engines/v8/generate.zig
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ fn throwBasicError(msg: []const u8, isolate: v8.Isolate) v8.Value {

fn throwError(
alloc: std.mem.Allocator,
objects: *NativeContext.Objects,
nat_ctx: *NativeContext,
comptime T_refl: refl.Struct,
comptime all_T: []refl.Struct,
comptime func: refl.Func,
Expand Down Expand Up @@ -60,7 +60,7 @@ fn throwError(
const obj = except.?.T.init(alloc, err, func.js_name) catch unreachable; // TODO
const js_obj = setNativeObject(
alloc,
objects,
nat_ctx,
except.?,
@TypeOf(obj),
obj,
Expand Down Expand Up @@ -486,13 +486,23 @@ fn bindObjectNativeAndJS(
return js_obj_binded;
}

inline fn initJSObject(index: usize, js_ctx: v8.Context) v8.Object {
return gen.getTpl(index).tpl.getInstanceTemplate().initInstance(js_ctx);
pub fn getTpl(nat_ctx: *NativeContext, index: usize) v8.FunctionTemplate {
const handle = nat_ctx.getType(v8.C_FunctionTemplate, index);
return v8.FunctionTemplate{ .handle = handle };
}

inline fn initJSObject(
nat_ctx: *NativeContext,
index: usize,
js_ctx: v8.Context,
) v8.Object {
const tpl = getTpl(nat_ctx, index);
return tpl.getInstanceTemplate().initInstance(js_ctx);
}

pub fn setNativeObject(
alloc: std.mem.Allocator,
objects: *NativeContext.Objects,
nat_ctx: *NativeContext,
comptime T_refl: refl.Struct,
comptime T: type,
nat_obj: anytype,
Expand Down Expand Up @@ -531,12 +541,12 @@ pub fn setNativeObject(

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

// JS object is not provided, check the objects map
const nat_obj_ref = @intFromPtr(nat_obj_ptr);
if (objects.get(nat_obj_ref)) |js_obj_ref| {
if (nat_ctx.objects.get(nat_obj_ref)) |js_obj_ref| {

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

// no JS object is linked to the current Native object
// let's create one from its template
js_obj_under = initJSObject(T_refl.index, js_ctx);
js_obj_under = initJSObject(nat_ctx, T_refl.index, js_ctx);
}
}

// bind Native and JS objects together
return try bindObjectNativeAndJS(
alloc,
objects,
nat_ctx.objects,
T_refl,
nat_obj_ptr,
js_obj_under,
Expand All @@ -565,7 +575,7 @@ pub fn setNativeObject(

fn setReturnType(
alloc: std.mem.Allocator,
objects: *NativeContext.Objects,
nat_ctx: *NativeContext,
comptime all_T: []refl.Struct,
comptime ret: refl.Type,
comptime func: refl.Func,
Expand All @@ -584,7 +594,7 @@ fn setReturnType(
}
return setReturnType(
alloc,
objects,
nat_ctx,
all_T,
ret,
func,
Expand All @@ -604,7 +614,7 @@ fn setReturnType(
if (std.mem.eql(u8, activeTag, tt.name.?)) {
return setReturnType(
alloc,
objects,
nat_ctx,
all_T,
tt,
func,
Expand All @@ -629,7 +639,7 @@ fn setReturnType(
const name = field.name.?;
const js_val = try setReturnType(
alloc,
objects,
nat_ctx,
all_T,
field,
func,
Expand All @@ -652,7 +662,7 @@ fn setReturnType(

const js_obj = try setNativeObject(
alloc,
objects,
nat_ctx,
all_T[index],
ret.underT(),
res,
Expand Down Expand Up @@ -808,7 +818,7 @@ fn callFunc(
// TODO: how to handle internal errors vs user errors
const js_err = throwError(
nat_ctx.alloc,
nat_ctx.objects,
nat_ctx,
T_refl,
all_T,
func,
Expand All @@ -827,7 +837,7 @@ fn callFunc(
// bind native object to JS object this
_ = setNativeObject(
nat_ctx.alloc,
nat_ctx.objects,
nat_ctx,
T_refl,
func.return_type.underT(),
res,
Expand All @@ -841,7 +851,7 @@ fn callFunc(
// return to javascript the result
const js_val = setReturnType(
nat_ctx.alloc,
nat_ctx.objects,
nat_ctx,
all_T,
func.return_type,
func,
Expand All @@ -852,7 +862,7 @@ fn callFunc(
) catch |err| blk: {
break :blk throwError(
nat_ctx.alloc,
nat_ctx.objects,
nat_ctx,
T_refl,
all_T,
func,
Expand Down
22 changes: 10 additions & 12 deletions src/engines/v8/v8.zig
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ pub const CallbackArg = @import("callback.zig").Arg;
pub const LoadFnType = @import("generate.zig").LoadFnType;
pub const loadFn = @import("generate.zig").loadFn;
const setNativeObject = @import("generate.zig").setNativeObject;
const getTpl = @import("generate.zig").getTpl;

const nativeToJS = @import("types_primitives.zig").nativeToJS;
const valueToUtf8 = @import("types_primitives.zig").valueToUtf8;
Expand All @@ -39,14 +40,6 @@ pub const TemplateType = v8.FunctionTemplate;
pub const TPL = struct {
tpl: v8.FunctionTemplate,
index: usize,

pub inline fn template(self: TPL) v8.FunctionTemplate {
return self.tpl;
}

pub inline fn idx(self: TPL) usize {
return self.index;
}
};

pub const Object = v8.Object;
Expand Down Expand Up @@ -162,8 +155,13 @@ pub const Env = struct {
}

// load APIs into Javascript environement
pub fn load(self: Env, comptime apis: []API, tpls: []TPL) anyerror!void {
try gen.load(self.nat_ctx, self.isolate, self.globals, apis, tpls);
pub fn load(self: Env, comptime apis: []API, js_types: []usize) anyerror!void {
var tpls: [apis.len]TPL = undefined;
try gen.load(self.nat_ctx, self.isolate, self.globals, apis, TPL, &tpls);
for (tpls, 0..) |tpl, i| {
js_types[i] = @intFromPtr(tpl.tpl.handle);
}
self.nat_ctx.loadTypes(js_types);
}

// start a Javascript context
Expand All @@ -187,8 +185,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 (api.T_refl.proto_index) |proto_index| {
const cstr_tpl = gen.getTpl(i).tpl;
const proto_tpl = gen.getTpl(proto_index).tpl;
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
35 changes: 10 additions & 25 deletions src/generate.zig
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
const std = @import("std");
const v8 = @import("v8");

const internal = @import("internal_api.zig");
const refl = internal.refl;
const NativeContext = internal.NativeContext;

const public = @import("api.zig");
const API = public.API;
const TPL = public.TPL;
const API = @import("api.zig").API;

const private = @import("private_api.zig");
const loadFn = private.loadFn;
const loadFn = @import("private_api.zig").loadFn;

// Compile and loading mechanism
// -----------------------------
Expand Down Expand Up @@ -46,33 +42,22 @@ pub fn compile(comptime types: anytype) []API {
}
}

// The list of APIs and TPLs holds corresponding data,
// ie. TPLs[0] is generated by APIs[0].
// This is assumed by the rest of the loading mechanism.
// Therefore the content of thoses lists (and their order) should not be altered
// afterwards.
var TPLs: []TPL = undefined;

pub fn getTpl(index: usize) TPL {
return TPLs[index];
}

// Load native APIs into a JS isolate
// Load native APIs into a JS sandbox
// This function is called at runtime.
pub fn load(
nat_ctx: *NativeContext,
isolate: v8.Isolate,
globals: v8.ObjectTemplate,
js_sandbox: anytype,
js_globals: anytype,
comptime apis: []API,
tpls: []TPL,
comptime js_T: type,
js_types: []js_T,
) !void {
inline for (apis, 0..) |api, i| {
if (api.T_refl.proto_index == null) {
tpls[i] = try api.load(nat_ctx, isolate, globals, null);
js_types[i] = try api.load(nat_ctx, js_sandbox, js_globals, null);
} else {
const proto_tpl = tpls[api.T_refl.proto_index.?]; // safe because apis are ordered from parent to child
tpls[i] = try api.load(nat_ctx, isolate, globals, proto_tpl);
const proto = js_types[api.T_refl.proto_index.?]; // safe because apis are ordered from parent to child
js_types[i] = try api.load(nat_ctx, js_sandbox, js_globals, proto);
}
}
TPLs = tpls;
}
5 changes: 1 addition & 4 deletions src/interfaces.zig
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ pub fn API(comptime T: type, comptime LoadFnType: type) void {
assertDecl(T, "loadFn", fn (self: T) callconv(.Inline) LoadFnType);
}

pub fn TPL(comptime _: type) void {}

pub fn VM(comptime T: type) void {

// init()
Expand All @@ -35,7 +33,6 @@ pub fn VM(comptime T: type) void {
pub fn Env(
comptime T: type,
comptime API_T: type,
comptime TPL_T: type,
comptime JSResult_T: type,
comptime Object_T: type,
) void {
Expand All @@ -53,7 +50,7 @@ pub fn Env(
assertDecl(T, "load", fn (
self: T,
comptime apis: []API_T,
tpls: []TPL_T,
js_types: []usize,
) anyerror!void);

// start()
Expand Down
19 changes: 19 additions & 0 deletions src/native_context.zig
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,24 @@ pub const NativeContext = struct {
loop: *Loop,
objects: *Objects,

// NOTE: DO NOT ACCESS DIRECTLY js_types
// - use once loadTypes at startup to set them
// - and then getType during execution to access them
js_types: ?[]usize = null,

pub const Objects = std.AutoHashMapUnmanaged(usize, usize);

// loadTypes into the NativeContext
// The caller holds the memory of the js_types slice,
// no heap allocation is performed at the NativeContext level
pub fn loadTypes(self: *NativeContext, js_types: []usize) void {
std.debug.assert(self.js_types == null);
self.js_types = js_types;
}

pub fn getType(self: 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));
}
};
2 changes: 0 additions & 2 deletions src/private_api.zig
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ fn checkInterfaces(engine: anytype) void {

// public api
interfaces.API(engine.API, engine.LoadFnType);
interfaces.TPL(engine.TPL);

interfaces.Callback(engine.Callback);
interfaces.CallbackSync(engine.CallbackSync);
Expand All @@ -19,7 +18,6 @@ fn checkInterfaces(engine: anytype) void {
interfaces.Env(
engine.Env,
engine.API,
engine.TPL,
engine.JSResult,
engine.Object,
);
Expand Down
2 changes: 1 addition & 1 deletion vendor/zig-v8

0 comments on commit be6ebd6

Please sign in to comment.