From 77e7393acbc1c78a931e723137068f7cec9c6481 Mon Sep 17 00:00:00 2001 From: Linus Groh Date: Tue, 12 Nov 2024 19:39:42 +0000 Subject: [PATCH] Clean up engine selection --- build.zig | 36 ++++++++++++------------------------ src/api.zig | 2 +- src/engines/v8/v8.zig | 2 +- src/interfaces.zig | 2 +- src/private_api.zig | 33 ++++++++------------------------- 5 files changed, 23 insertions(+), 52 deletions(-) diff --git a/build.zig b/build.zig index c844e7c..015e3cd 100644 --- a/build.zig +++ b/build.zig @@ -14,6 +14,7 @@ const std = @import("std"); const builtin = @import("builtin"); +const EngineType = @import("src/api.zig").EngineType; const pkgs = packages(""); @@ -40,7 +41,7 @@ pub fn build(b: *std.Build) !void { // TODO: install only bench or shell with zig build - const options = try buildOptions(b); + const options = buildOptions(b); // bench // ----- @@ -132,31 +133,16 @@ pub fn build(b: *std.Build) !void { test_step.dependOn(&run_tests.step); } -const Engine = enum { - v8, -}; - pub const Options = struct { - engine: Engine, + engine: EngineType, opts: *std.Build.Step.Options, }; -pub fn buildOptions(b: *std.Build) !Options { +pub fn buildOptions(b: *std.Build) Options { + const engine = b.option(EngineType, "engine", "JS engine (v8)") orelse .v8; const options = b.addOptions(); - const engine = b.option([]const u8, "engine", "JS engine (v8)"); - var eng: Engine = undefined; - if (engine == null) { - // default - eng = .v8; - } else { - if (std.mem.eql(u8, engine.?, "v8")) { - eng = .v8; - } else { - return error.EngineUnknown; - } - } - options.addOption(?[]const u8, "engine", engine); - return .{ .engine = eng, .opts = options }; + options.addOption(EngineType, "engine", engine); + return .{ .engine = engine, .opts = options }; } fn common( @@ -166,9 +152,11 @@ fn common( ) !void { m.addOptions("jsruntime_build_options", options.opts); m.addImport("tigerbeetle-io", pkgs.tigerbeetle_io(b)); - if (options.engine == .v8) { - try pkgs.v8(m); - m.addImport("v8", pkgs.zig_v8(b)); + switch (options.engine) { + .v8 => { + try pkgs.v8(m); + m.addImport("v8", pkgs.zig_v8(b)); + }, } } diff --git a/src/api.zig b/src/api.zig index 70c3357..efc42a5 100644 --- a/src/api.zig +++ b/src/api.zig @@ -87,6 +87,6 @@ pub const Inspector = Engine.Inspector; pub const InspectorOnResponseFn = *const fn (ctx: *anyopaque, call_id: u32, msg: []const u8) void; pub const InspectorOnEventFn = *const fn (ctx: *anyopaque, msg: []const u8) void; -pub const engineType = enum { +pub const EngineType = enum { v8, }; diff --git a/src/engines/v8/v8.zig b/src/engines/v8/v8.zig index aea8dce..2901d3a 100644 --- a/src/engines/v8/v8.zig +++ b/src/engines/v8/v8.zig @@ -90,7 +90,7 @@ pub const Env = struct { js_ctx: ?v8.Context = null, - pub fn engine() public.engineType { + pub fn engine() public.EngineType { return .v8; } diff --git a/src/interfaces.zig b/src/interfaces.zig index 3a0ecc7..ee82eeb 100644 --- a/src/interfaces.zig +++ b/src/interfaces.zig @@ -52,7 +52,7 @@ pub fn Env( ) void { // engine() - assertDecl(T, "engine", fn () public.engineType); + assertDecl(T, "engine", fn () public.EngineType); // init() assertDecl(T, "init", fn (self: *T, alloc: std.mem.Allocator, loop: *public.Loop, userctx: ?public.UserContext) void); diff --git a/src/private_api.zig b/src/private_api.zig index fe72e5e..0758f6c 100644 --- a/src/private_api.zig +++ b/src/private_api.zig @@ -14,6 +14,7 @@ const std = @import("std"); +const build_opts = @import("jsruntime_build_options"); const interfaces = @import("interfaces.zig"); fn checkInterfaces(engine: anytype) void { @@ -44,31 +45,13 @@ fn checkInterfaces(engine: anytype) void { // private api } -pub const Engine = blk: { - - // retrieve JS engine - - // - as a build option - const build_opts = @import("jsruntime_build_options"); - if (@hasDecl(build_opts, "engine")) { - // use v8 by default. - const eng = build_opts.engine orelse "v8"; - if (std.mem.eql(u8, eng, "v8")) { - const engine = @import("engines/v8/v8.zig"); - checkInterfaces(engine); - break :blk engine; - } - @compileError("unknwon -Dengine '" ++ eng ++ "'"); - } - - // - as a root declaration - const root = @import("root"); - if (@hasDecl(root, "JSEngine")) { - checkInterfaces(root.JSEngine); - break :blk root.JSEngine; - } - - @compileError("you need to specify a JS engine as a build option (-Dengine) or as a root file declaration (pub const JSEngine)"); +// retrieve JS engine +pub const Engine = switch (build_opts.engine) { + .v8 => blk: { + const engine = @import("engines/v8/v8.zig"); + checkInterfaces(engine); + break :blk engine; + }, }; pub const API = Engine.API;