Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clean up engine selection #258

Merged
merged 1 commit into from
Nov 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 12 additions & 24 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

const std = @import("std");
const builtin = @import("builtin");
const EngineType = @import("src/api.zig").EngineType;

const pkgs = packages("");

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

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

const options = try buildOptions(b);
const options = buildOptions(b);

// bench
// -----
Expand Down Expand Up @@ -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(
Expand All @@ -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));
},
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/api.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
2 changes: 1 addition & 1 deletion src/engines/v8/v8.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
2 changes: 1 addition & 1 deletion src/interfaces.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
33 changes: 8 additions & 25 deletions src/private_api.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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;
Expand Down