Skip to content

Commit 81dcc9d

Browse files
Merge pull request #258 from linusg/build-option-enum
Clean up engine selection
2 parents d115261 + 77e7393 commit 81dcc9d

File tree

5 files changed

+23
-52
lines changed

5 files changed

+23
-52
lines changed

build.zig

Lines changed: 12 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
const std = @import("std");
1616
const builtin = @import("builtin");
17+
const EngineType = @import("src/api.zig").EngineType;
1718

1819
const pkgs = packages("");
1920

@@ -40,7 +41,7 @@ pub fn build(b: *std.Build) !void {
4041

4142
// TODO: install only bench or shell with zig build <cmd>
4243

43-
const options = try buildOptions(b);
44+
const options = buildOptions(b);
4445

4546
// bench
4647
// -----
@@ -132,31 +133,16 @@ pub fn build(b: *std.Build) !void {
132133
test_step.dependOn(&run_tests.step);
133134
}
134135

135-
const Engine = enum {
136-
v8,
137-
};
138-
139136
pub const Options = struct {
140-
engine: Engine,
137+
engine: EngineType,
141138
opts: *std.Build.Step.Options,
142139
};
143140

144-
pub fn buildOptions(b: *std.Build) !Options {
141+
pub fn buildOptions(b: *std.Build) Options {
142+
const engine = b.option(EngineType, "engine", "JS engine (v8)") orelse .v8;
145143
const options = b.addOptions();
146-
const engine = b.option([]const u8, "engine", "JS engine (v8)");
147-
var eng: Engine = undefined;
148-
if (engine == null) {
149-
// default
150-
eng = .v8;
151-
} else {
152-
if (std.mem.eql(u8, engine.?, "v8")) {
153-
eng = .v8;
154-
} else {
155-
return error.EngineUnknown;
156-
}
157-
}
158-
options.addOption(?[]const u8, "engine", engine);
159-
return .{ .engine = eng, .opts = options };
144+
options.addOption(EngineType, "engine", engine);
145+
return .{ .engine = engine, .opts = options };
160146
}
161147

162148
fn common(
@@ -166,9 +152,11 @@ fn common(
166152
) !void {
167153
m.addOptions("jsruntime_build_options", options.opts);
168154
m.addImport("tigerbeetle-io", pkgs.tigerbeetle_io(b));
169-
if (options.engine == .v8) {
170-
try pkgs.v8(m);
171-
m.addImport("v8", pkgs.zig_v8(b));
155+
switch (options.engine) {
156+
.v8 => {
157+
try pkgs.v8(m);
158+
m.addImport("v8", pkgs.zig_v8(b));
159+
},
172160
}
173161
}
174162

src/api.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,6 @@ pub const Inspector = Engine.Inspector;
8787
pub const InspectorOnResponseFn = *const fn (ctx: *anyopaque, call_id: u32, msg: []const u8) void;
8888
pub const InspectorOnEventFn = *const fn (ctx: *anyopaque, msg: []const u8) void;
8989

90-
pub const engineType = enum {
90+
pub const EngineType = enum {
9191
v8,
9292
};

src/engines/v8/v8.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ pub const Env = struct {
9090

9191
js_ctx: ?v8.Context = null,
9292

93-
pub fn engine() public.engineType {
93+
pub fn engine() public.EngineType {
9494
return .v8;
9595
}
9696

src/interfaces.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ pub fn Env(
5252
) void {
5353

5454
// engine()
55-
assertDecl(T, "engine", fn () public.engineType);
55+
assertDecl(T, "engine", fn () public.EngineType);
5656

5757
// init()
5858
assertDecl(T, "init", fn (self: *T, alloc: std.mem.Allocator, loop: *public.Loop, userctx: ?public.UserContext) void);

src/private_api.zig

Lines changed: 8 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
const std = @import("std");
1616

17+
const build_opts = @import("jsruntime_build_options");
1718
const interfaces = @import("interfaces.zig");
1819

1920
fn checkInterfaces(engine: anytype) void {
@@ -44,31 +45,13 @@ fn checkInterfaces(engine: anytype) void {
4445
// private api
4546
}
4647

47-
pub const Engine = blk: {
48-
49-
// retrieve JS engine
50-
51-
// - as a build option
52-
const build_opts = @import("jsruntime_build_options");
53-
if (@hasDecl(build_opts, "engine")) {
54-
// use v8 by default.
55-
const eng = build_opts.engine orelse "v8";
56-
if (std.mem.eql(u8, eng, "v8")) {
57-
const engine = @import("engines/v8/v8.zig");
58-
checkInterfaces(engine);
59-
break :blk engine;
60-
}
61-
@compileError("unknwon -Dengine '" ++ eng ++ "'");
62-
}
63-
64-
// - as a root declaration
65-
const root = @import("root");
66-
if (@hasDecl(root, "JSEngine")) {
67-
checkInterfaces(root.JSEngine);
68-
break :blk root.JSEngine;
69-
}
70-
71-
@compileError("you need to specify a JS engine as a build option (-Dengine) or as a root file declaration (pub const JSEngine)");
48+
// retrieve JS engine
49+
pub const Engine = switch (build_opts.engine) {
50+
.v8 => blk: {
51+
const engine = @import("engines/v8/v8.zig");
52+
checkInterfaces(engine);
53+
break :blk engine;
54+
},
7255
};
7356

7457
pub const API = Engine.API;

0 commit comments

Comments
 (0)