Skip to content

Commit

Permalink
Merge pull request #220 from lightpanda-io/userctx
Browse files Browse the repository at this point in the history
Implement a UserContext
  • Loading branch information
krichprollsch committed May 22, 2024
2 parents d97847c + 655b9eb commit 700a41a
Show file tree
Hide file tree
Showing 13 changed files with 139 additions and 16 deletions.
2 changes: 2 additions & 0 deletions src/api.zig
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ pub const Variadic = types.Variadic;
pub const Loop = @import("loop.zig").SingleThreaded;
pub const Console = @import("console.zig").Console;

pub const UserContext = @import("user_context.zig").UserContext;

// JS engine
// ---------

Expand Down
4 changes: 3 additions & 1 deletion src/engine.zig
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,13 @@ const NativeContext = internal.NativeContext;
const public = @import("api.zig");
const Env = public.Env;
const Loop = public.Loop;
const UserContext = public.UserContext;

pub const ContextExecFn = (fn (std.mem.Allocator, *Env) anyerror!void);

pub fn loadEnv(
arena_alloc: *std.heap.ArenaAllocator,
userctx: ?UserContext,
comptime ctxExecFn: ContextExecFn,
) !void {
const alloc = arena_alloc.allocator();
Expand All @@ -40,7 +42,7 @@ pub fn loadEnv(
}
var loop = try Loop.init(alloc);
defer loop.deinit();
var js_env = try Env.init(alloc, &loop);
var js_env = try Env.init(alloc, &loop, userctx);
defer js_env.deinit();

// load APIs in JS env
Expand Down
2 changes: 2 additions & 0 deletions src/engines/v8/generate.zig
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ const NativeContext = internal.NativeContext;

const public = @import("../../api.zig");
const Loop = public.Loop;
const UserContext = public.UserContext;

const cbk = @import("callback.zig");
const nativeToJS = @import("types_primitives.zig").nativeToJS;
Expand Down Expand Up @@ -208,6 +209,7 @@ fn getArg(
value = switch (arg.T) {
std.mem.Allocator => alloc,
*Loop => nat_ctx.loop,
UserContext => nat_ctx.userctx orelse unreachable, // an arg requires a usercontext but it missing.
cbk.Func, cbk.FuncSync, cbk.Arg => unreachable,
JSObject => JSObject{ .nat_ctx = nat_ctx, .js_ctx = js_ctx, .js_obj = this },
JSObjectID => JSObjectID.set(js_val.?.castTo(v8.Object)),
Expand Down
12 changes: 10 additions & 2 deletions src/engines/v8/v8.zig
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,11 @@ pub const Env = struct {
return .v8;
}

pub fn init(alloc: std.mem.Allocator, loop: *public.Loop) anyerror!Env {
pub fn init(
alloc: std.mem.Allocator,
loop: *public.Loop,
userctx: ?public.UserContext,
) anyerror!Env {

// v8 values
// ---------
Expand All @@ -113,7 +117,7 @@ pub const Env = struct {
const globals = v8.FunctionTemplate.initDefault(isolate);

return Env{
.nat_ctx = try NativeContext.init(alloc, loop),
.nat_ctx = try NativeContext.init(alloc, loop, userctx),
.isolate_params = params,
.isolate = isolate,
.hscope = hscope,
Expand Down Expand Up @@ -145,6 +149,10 @@ pub const Env = struct {
self.* = undefined;
}

pub fn setUserContext(self: *Env, userctx: public.UserContext) anyerror!void {
self.nat_ctx.userctx = userctx;
}

// load user-defined Types into Javascript environement
pub fn load(self: Env, js_types: []usize) anyerror!void {
var tpls: [gen.Types.len]TPL = undefined;
Expand Down
7 changes: 6 additions & 1 deletion src/interfaces.zig
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ pub fn Env(
assertDecl(T, "engine", fn () public.engineType);

// init()
assertDecl(T, "init", fn (alloc: std.mem.Allocator, loop: *public.Loop) anyerror!T);
assertDecl(T, "init", fn (alloc: std.mem.Allocator, loop: *public.Loop, userctx: ?public.UserContext) anyerror!T);

// deinit()
assertDecl(T, "deinit", fn (self: *T) void);
Expand All @@ -64,6 +64,11 @@ pub fn Env(

assertDecl(T, "bindGlobal", fn (self: T, ob: anytype) anyerror!void);

assertDecl(T, "setUserContext", fn (
self: *T,
userctx: public.UserContext,
) anyerror!void);

// start()
assertDecl(T, "start", fn (
self: *T,
Expand Down
4 changes: 2 additions & 2 deletions src/main_bench.zig
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ fn benchWithIsolate(
) !bench.Result {
const duration = try bench.call(
public.loadEnv,
.{ arena_alloc, ctxExecFn },
.{ arena_alloc, null, ctxExecFn },
iter,
warmup,
);
Expand Down Expand Up @@ -72,7 +72,7 @@ fn benchWithoutIsolate(
duration_global = duration;
}
};
try public.loadEnv(arena_alloc, s.do);
try public.loadEnv(arena_alloc, null, s.do);
const alloc_stats = bench_alloc.stats();
return bench.Result{
.duration = duration_global,
Expand Down
5 changes: 4 additions & 1 deletion src/native_context.zig
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,13 @@
const std = @import("std");

const Loop = @import("api.zig").Loop;
const UserContext = @import("api.zig").UserContext;
const NatObjects = @import("internal_api.zig").refs.Map;

pub const NativeContext = struct {
alloc: std.mem.Allocator,
loop: *Loop,
userctx: ?UserContext,

js_objs: JSObjects,
nat_objs: NatObjects,
Expand All @@ -31,11 +33,12 @@ pub const NativeContext = struct {

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

pub fn init(alloc: std.mem.Allocator, loop: *Loop) !*NativeContext {
pub fn init(alloc: std.mem.Allocator, loop: *Loop, userctx: ?UserContext) !*NativeContext {
const self = try alloc.create(NativeContext);
self.* = .{
.alloc = alloc,
.loop = loop,
.userctx = userctx,
.js_objs = JSObjects{},
.nat_objs = NatObjects{},
};
Expand Down
7 changes: 7 additions & 0 deletions src/reflect.zig
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const Callback = public.Callback;
const CallbackSync = public.CallbackSync;
const CallbackArg = public.CallbackArg;
const JSObjectID = public.JSObjectID;
const UserContext = public.UserContext;

const JSObject = public.JSObject;

Expand Down Expand Up @@ -67,6 +68,7 @@ const internal_types = [_]type{
CallbackSync,
CallbackArg,
JSObjectID,
UserContext,
};

fn isInternalType(comptime T: type) bool {
Expand Down Expand Up @@ -562,6 +564,11 @@ pub const Func = struct {
index_offset += 1;
}

// user context
if (args_types[i].T == UserContext) {
index_offset += 1;
}

// JSObject
if (args_types[i].T == JSObject) {
// JSObject arg is not allowed in a constructor function
Expand Down
40 changes: 32 additions & 8 deletions src/run_tests.zig
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ const multiple_types = @import("tests/types_multiple_test.zig");
const object_types = @import("tests/types_object.zig");
const callback = @import("tests/cbk_test.zig");
const global = @import("tests/global_test.zig");
const userctx = @import("tests/userctx_test.zig");

// test to do
const do_proto = true;
Expand All @@ -43,6 +44,7 @@ const do_multi = true;
const do_obj = true;
const do_cbk = true;
const do_global = true;
const do_userctx = true;

// tests nb
const tests_nb = blk: {
Expand All @@ -55,6 +57,7 @@ const tests_nb = blk: {
if (do_obj) nb += 1;
if (do_cbk) nb += 1;
if (do_global) nb += 1;
if (do_userctx) nb += 1;
break :blk nb;
};

Expand All @@ -68,8 +71,11 @@ pub const Types = gen.reflect(gen.MergeTuple(.{
object_types.Types,
callback.Types,
global.Types,
userctx.Types,
}));

pub const UserContext = userctx.UserContext;

pub fn main() !void {
std.debug.print("\n", .{});

Expand All @@ -92,7 +98,7 @@ pub fn main() !void {
proto_alloc = bench.allocator(std.testing.allocator);
var proto_arena = std.heap.ArenaAllocator.init(proto_alloc.allocator());
defer proto_arena.deinit();
_ = try eng.loadEnv(&proto_arena, proto.exec);
_ = try eng.loadEnv(&proto_arena, null, proto.exec);
}

// primitive types tests
Expand All @@ -101,7 +107,7 @@ pub fn main() !void {
prim_alloc = bench.allocator(std.testing.allocator);
var prim_arena = std.heap.ArenaAllocator.init(prim_alloc.allocator());
defer prim_arena.deinit();
_ = try eng.loadEnv(&prim_arena, primitive_types.exec);
_ = try eng.loadEnv(&prim_arena, null, primitive_types.exec);
}

// native types tests
Expand All @@ -110,7 +116,7 @@ pub fn main() !void {
nat_alloc = bench.allocator(std.testing.allocator);
var nat_arena = std.heap.ArenaAllocator.init(nat_alloc.allocator());
defer nat_arena.deinit();
_ = try eng.loadEnv(&nat_arena, native_types.exec);
_ = try eng.loadEnv(&nat_arena, null, native_types.exec);
}

// complex types tests
Expand All @@ -119,7 +125,7 @@ pub fn main() !void {
complex_alloc = bench.allocator(std.testing.allocator);
var complex_arena = std.heap.ArenaAllocator.init(complex_alloc.allocator());
defer complex_arena.deinit();
_ = try eng.loadEnv(&complex_arena, complex_types.exec);
_ = try eng.loadEnv(&complex_arena, null, complex_types.exec);
}

// multiple types tests
Expand All @@ -128,7 +134,7 @@ pub fn main() !void {
multi_alloc = bench.allocator(std.testing.allocator);
var multi_arena = std.heap.ArenaAllocator.init(multi_alloc.allocator());
defer multi_arena.deinit();
_ = try eng.loadEnv(&multi_arena, multiple_types.exec);
_ = try eng.loadEnv(&multi_arena, null, multiple_types.exec);
}

// object types tests
Expand All @@ -137,7 +143,7 @@ pub fn main() !void {
obj_alloc = bench.allocator(std.testing.allocator);
var obj_arena = std.heap.ArenaAllocator.init(obj_alloc.allocator());
defer obj_arena.deinit();
_ = try eng.loadEnv(&obj_arena, object_types.exec);
_ = try eng.loadEnv(&obj_arena, null, object_types.exec);
}

// callback tests
Expand All @@ -146,7 +152,7 @@ pub fn main() !void {
cbk_alloc = bench.allocator(std.testing.allocator);
var cbk_arena = std.heap.ArenaAllocator.init(cbk_alloc.allocator());
defer cbk_arena.deinit();
_ = try eng.loadEnv(&cbk_arena, callback.exec);
_ = try eng.loadEnv(&cbk_arena, null, callback.exec);
}

// global tests
Expand All @@ -155,7 +161,16 @@ pub fn main() !void {
global_alloc = bench.allocator(std.testing.allocator);
var global_arena = std.heap.ArenaAllocator.init(global_alloc.allocator());
defer global_arena.deinit();
_ = try eng.loadEnv(&global_arena, global.exec);
_ = try eng.loadEnv(&global_arena, null, global.exec);
}

// user context tests
var userctx_alloc: bench.Allocator = undefined;
if (do_userctx) {
userctx_alloc = bench.allocator(std.testing.allocator);
var userctx_arena = std.heap.ArenaAllocator.init(userctx_alloc.allocator());
defer userctx_arena.deinit();
_ = try eng.loadEnv(&userctx_arena, null, userctx.exec);
}

if (tests_nb == 0) {
Expand Down Expand Up @@ -248,6 +263,15 @@ pub fn main() !void {
try t.addRow(.{ "Global", global_alloc.alloc_nb, global_alloc_size });
}

if (do_userctx) {
const userctx_alloc_stats = global_alloc.stats();
const userctx_alloc_size = pretty.Measure{
.unit = "b",
.value = userctx_alloc_stats.alloc_size,
};
try t.addRow(.{ "User context", userctx_alloc.alloc_nb, userctx_alloc_size });
}

const out = std.io.getStdErr().writer();
try t.render(out);
}
2 changes: 1 addition & 1 deletion src/shell.zig
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ pub fn shell(
if (ctxExecFn) |func| {
do_fn = func;
}
try public.loadEnv(arena_alloc, do_fn);
try public.loadEnv(arena_alloc, null, do_fn);
}

fn repl() !void {
Expand Down
1 change: 1 addition & 0 deletions src/test_runner.zig
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const std = @import("std");
const tests = @import("run_tests.zig");

pub const Types = tests.Types;
pub const UserContext = tests.UserContext;

pub fn main() !void {
try tests.main();
Expand Down
53 changes: 53 additions & 0 deletions src/tests/userctx_test.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
const std = @import("std");

const public = @import("../api.zig");
const tests = public.test_utils;

const Config = struct {
use_proxy: bool,
};

pub const UserContext = Config;

const Request = struct {
use_proxy: bool,

pub fn constructor(ctx: Config) Request {
return .{
.use_proxy = ctx.use_proxy,
};
}

pub fn get_proxy(self: *Request) bool {
return self.use_proxy;
}

pub fn _configProxy(_: *Request, ctx: Config) bool {
return ctx.use_proxy;
}
};

pub const Types = .{
Request,
};

// exec tests
pub fn exec(
alloc: std.mem.Allocator,
js_env: *public.Env,
) anyerror!void {
try js_env.setUserContext(Config{
.use_proxy = true,
});

// start JS env
try js_env.start(alloc);
defer js_env.stop();

var tc = [_]tests.Case{
.{ .src = "const req = new Request();", .ex = "undefined" },
.{ .src = "req.proxy", .ex = "true" },
.{ .src = "req.configProxy()", .ex = "true" },
};
try tests.checkCases(js_env, &tc);
}
16 changes: 16 additions & 0 deletions src/user_context.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const std = @import("std");

// UserContext is a type defined by the user optionally passed to the native
// API.
// The type is defined via a root declaration.
// Request a UserContext parameter in your native implementation to get the
// context.
pub const UserContext = blk: {
const root = @import("root");
if (@hasDecl(root, "UserContext")) {
break :blk root.UserContext;
}

// when no declaration is given, UserContext is define with an empty struct.
break :blk struct {};
};

0 comments on commit 700a41a

Please sign in to comment.