Skip to content

Commit d491f04

Browse files
Merge pull request #216 from lightpanda-io/upgrade-zig
Upgrade zig 0.12.1
2 parents 27db56c + 9faf988 commit d491f04

17 files changed

+244
-152
lines changed

.github/workflows/benchmark.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ jobs:
4040

4141
runs-on: ubuntu-latest
4242
container:
43-
image: ghcr.io/lightpanda-io/zig-v8:0.12.0-dev.1773-8a8fd47d2
43+
image: ghcr.io/lightpanda-io/zig-v8:0.12.1
4444
credentials:
4545
username: ${{ github.actor }}
4646
password: ${{ secrets.GITHUB_TOKEN }}

.github/workflows/zig-fmt.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ jobs:
2828

2929
runs-on: ubuntu-latest
3030
container:
31-
image: ghcr.io/lightpanda-io/zig:0.12.0-dev.1773-8a8fd47d2
31+
image: ghcr.io/lightpanda-io/zig:0.12.1
3232
credentials:
3333
username: ${{ github.actor }}
3434
password: ${{ secrets.GITHUB_TOKEN }}

.github/workflows/zig-test.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ jobs:
4040

4141
runs-on: ubuntu-latest
4242
container:
43-
image: ghcr.io/lightpanda-io/zig-v8:0.12.0-dev.1773-8a8fd47d2
43+
image: ghcr.io/lightpanda-io/zig-v8:0.12.1
4444
credentials:
4545
username: ${{ github.actor }}
4646
password: ${{ secrets.GITHUB_TOKEN }}
@@ -71,7 +71,7 @@ jobs:
7171

7272
runs-on: ubuntu-latest
7373
container:
74-
image: ghcr.io/lightpanda-io/zig-v8:0.12.0-dev.1773-8a8fd47d2
74+
image: ghcr.io/lightpanda-io/zig-v8:0.12.1
7575
credentials:
7676
username: ${{ github.actor }}
7777
password: ${{ secrets.GITHUB_TOKEN }}
@@ -102,7 +102,7 @@ jobs:
102102

103103
runs-on: ubuntu-latest
104104
container:
105-
image: ghcr.io/lightpanda-io/zig-v8:0.12.0-dev.1773-8a8fd47d2
105+
image: ghcr.io/lightpanda-io/zig-v8:0.12.1
106106
credentials:
107107
username: ${{ github.actor }}
108108
password: ${{ secrets.GITHUB_TOKEN }}

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,14 +146,14 @@ $ make shell
146146
zig-js-runtime - Javascript Shell
147147
exit with Ctrl+D or "exit"
148148

149-
>
149+
>
150150
```
151151

152152
## Build
153153

154154
### Prerequisites
155155

156-
zig-js-runtime is written with [Zig](https://ziglang.org/) `0.12`. You have to
156+
zig-js-runtime is written with [Zig](https://ziglang.org/) `0.12.1`. You have to
157157
install it with the right version in order to build the project.
158158

159159
To be able to build the v8 engine, you have to install some libs:

build.zig

Lines changed: 54 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ const pkgs = packages("");
1919

2020
/// Do not rename this constant. It is scanned by some scripts to determine
2121
/// which zig version to install.
22-
pub const recommended_zig_version = "0.12.0-dev.1773+8a8fd47d2";
22+
pub const recommended_zig_version = "0.12.1";
2323

2424
pub fn build(b: *std.Build) !void {
2525
switch (comptime builtin.zig_version.order(std.SemanticVersion.parse(recommended_zig_version) catch unreachable)) {
@@ -48,17 +48,17 @@ pub fn build(b: *std.Build) !void {
4848
// compile and install
4949
const bench = b.addExecutable(.{
5050
.name = "zig-js-runtime-bench",
51-
.root_source_file = .{ .path = "src/main_bench.zig" },
51+
.root_source_file = b.path("src/main_bench.zig"),
5252
.single_threaded = true,
5353
.target = target,
5454
.optimize = mode,
5555
});
5656

57-
try common(bench, mode, options);
57+
try common(b, &bench.root_module, options);
5858
if (mode == .ReleaseSafe) {
5959
// remove debug info
6060
// TODO: check if mandatory in release-safe
61-
bench.strip = true;
61+
bench.root_module.strip = true;
6262
}
6363
b.installArtifact(bench);
6464

@@ -78,16 +78,16 @@ pub fn build(b: *std.Build) !void {
7878
// compile and install
7979
const shell = b.addExecutable(.{
8080
.name = "zig-js-runtime-shell",
81-
.root_source_file = .{ .path = "src/main_shell.zig" },
81+
.root_source_file = b.path("src/main_shell.zig"),
8282
.target = target,
8383
.optimize = mode,
8484
});
85-
try common(shell, mode, options);
85+
try common(b, &shell.root_module, options);
8686
try pkgs.add_shell(shell);
8787
if (mode == .ReleaseSafe) {
8888
// remove debug info
8989
// TODO: check if mandatory in release-safe
90-
shell.strip = true;
90+
shell.root_module.strip = true;
9191
}
9292
// do not install shell binary
9393
// b.installArtifact(shell);
@@ -107,13 +107,13 @@ pub fn build(b: *std.Build) !void {
107107

108108
// compile
109109
const tests = b.addTest(.{
110-
.root_source_file = .{ .path = "src/run_tests.zig" },
110+
.root_source_file = b.path("src/run_tests.zig"),
111111
.target = target,
112112
.optimize = mode,
113113
});
114-
try common(tests, mode, options);
115-
tests.single_threaded = true;
116-
tests.test_runner = "src/test_runner.zig";
114+
try common(b, &tests.root_module, options);
115+
tests.root_module.single_threaded = true;
116+
tests.test_runner = b.path("src/test_runner.zig");
117117
const run_tests = b.addRunArtifact(tests);
118118

119119
// step
@@ -149,15 +149,15 @@ pub fn buildOptions(b: *std.Build) !Options {
149149
}
150150

151151
fn common(
152-
step: *std.Build.Step.Compile,
153-
mode: std.builtin.Mode,
152+
b: *std.Build,
153+
m: *std.Build.Module,
154154
options: Options,
155155
) !void {
156-
step.addOptions("jsruntime_build_options", options.opts);
157-
step.addModule("tigerbeetle-io", pkgs.tigerbeetle_io(step));
156+
m.addOptions("jsruntime_build_options", options.opts);
157+
m.addImport("tigerbeetle-io", pkgs.tigerbeetle_io(b));
158158
if (options.engine == .v8) {
159-
try pkgs.v8(step, mode);
160-
step.addModule("v8", pkgs.zig_v8(step));
159+
try pkgs.v8(m);
160+
m.addImport("v8", pkgs.zig_v8(b));
161161
}
162162
}
163163

@@ -167,33 +167,35 @@ pub fn packages(comptime vendor_path: []const u8) type {
167167

168168
const vendor = vendor_path ++ "vendor";
169169

170-
fn tigerbeetle_io(step: *std.Build.Step.Compile) *std.Build.Module {
171-
return step.step.owner.createModule(.{
172-
.source_file = .{ .path = vendor ++ "/tigerbeetle-io/io.zig" },
170+
fn tigerbeetle_io(b: *std.Build) *std.Build.Module {
171+
return b.createModule(.{
172+
.root_source_file = b.path(vendor ++ "/tigerbeetle-io/io.zig"),
173173
});
174174
}
175175

176-
fn zig_v8(step: *std.Build.Step.Compile) *std.Build.Module {
177-
step.addIncludePath(.{ .path = vendor ++ "/zig-v8/src" });
178-
179-
return step.step.owner.createModule(.{
180-
.source_file = .{ .path = vendor ++ "/zig-v8/src/v8.zig" },
176+
fn zig_v8(b: *std.Build) *std.Build.Module {
177+
const mod = b.createModule(.{
178+
.root_source_file = b.path(vendor ++ "/zig-v8/src/v8.zig"),
179+
.link_libc = false,
180+
.link_libcpp = false,
181181
});
182-
}
183182

184-
fn v8(step: *std.Build.Step.Compile, mode: std.builtin.Mode) !void {
185-
const mode_str: []const u8 = if (mode == .Debug) "debug" else "release";
186-
// step.linkLibC(); // TODO: do we need to link libc?
183+
mod.addIncludePath(b.path(vendor ++ "/zig-v8/src"));
184+
185+
return mod;
186+
}
187187

188+
fn v8(mod: *std.Build.Module) !void {
189+
const mode_str: []const u8 = if (mod.optimize.? == .Debug) "debug" else "release";
188190
// FIXME: we are tied to native v8 builds, currently:
189191
// - aarch64-macos
190192
// - x86_64-linux
191-
const os = step.target.getOsTag();
192-
const arch = step.target.getCpuArch();
193+
const os = mod.resolved_target.?.result.os.tag;
194+
const arch = mod.resolved_target.?.result.cpu.arch;
193195
switch (os) {
194196
.linux => blk: {
195197
// TODO: why do we need it? It should be linked already when we built v8
196-
step.linkLibCpp();
198+
mod.link_libcpp = true;
197199
break :blk;
198200
},
199201
.macos => blk: {
@@ -207,45 +209,49 @@ pub fn packages(comptime vendor_path: []const u8) type {
207209
}
208210

209211
const lib_path = try std.fmt.allocPrint(
210-
step.step.owner.allocator,
212+
mod.owner.allocator,
211213
"{s}vendor/v8/{s}-{s}/{s}/libc_v8.a",
212214
.{ vendor_path, @tagName(arch), @tagName(os), mode_str },
213215
);
214-
step.addObjectFile(.{ .path = lib_path });
216+
mod.addObjectFile(mod.owner.path(lib_path));
215217
}
216218

217219
pub fn add_shell(step: *std.Build.Step.Compile) !void {
218-
step.addIncludePath(.{ .path = vendor ++ "/linenoise-mob" });
220+
step.addIncludePath(step.root_module.owner.path(vendor ++ "/linenoise-mob"));
219221
const lib = step.step.owner.addStaticLibrary(.{
220222
.name = "linenoise",
221-
.target = step.target,
222-
.optimize = step.optimize,
223+
.target = step.root_module.resolved_target.?,
224+
.optimize = step.root_module.optimize.?,
223225
.link_libc = true,
224226
});
225227
// TODO: use mode to add debug/release flags
226228
const cflags = &.{};
227229
lib.addCSourceFile(.{
228-
.file = .{ .path = vendor ++ "/linenoise-mob/linenoise.c" },
230+
.file = step.root_module.owner.path(vendor ++ "/linenoise-mob/linenoise.c"),
229231
.flags = cflags,
230232
});
231233
step.linkLibrary(lib);
232234
}
233235

234-
pub fn add(
235-
step: *std.build.Step.Compile,
236+
pub fn module(
237+
b: *std.Build,
236238
options: Options,
237-
) !void {
238-
const jsruntime_mod = step.step.owner.createModule(.{
239-
.source_file = .{ .path = vendor_path ++ "/src/api.zig" },
240-
.dependencies = &[_]std.build.ModuleDependency{
239+
mode: std.builtin.Mode,
240+
target: std.Build.ResolvedTarget,
241+
) !*std.Build.Module {
242+
const mod = b.createModule(.{
243+
.root_source_file = b.path(vendor_path ++ "/src/api.zig"),
244+
.optimize = mode,
245+
.target = target,
246+
.imports = &[_]std.Build.Module.Import{
241247
.{ .name = "jsruntime_build_options", .module = options.opts.createModule() },
242-
.{ .name = "tigerbeetle-io", .module = Self.tigerbeetle_io(step) },
243-
.{ .name = "v8", .module = Self.zig_v8(step) },
248+
.{ .name = "tigerbeetle-io", .module = Self.tigerbeetle_io(b) },
249+
.{ .name = "v8", .module = Self.zig_v8(b) },
244250
},
245251
});
246-
try Self.v8(step, step.optimize);
252+
try Self.v8(mod);
247253

248-
step.addModule("jsruntime", jsruntime_mod);
254+
return mod;
249255
}
250256
};
251257
}

src/engines/v8/generate.zig

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ fn getNativeArg(
168168

169169
// JS object
170170
const ptr = try getNativeObject(nat_ctx, T_refl, js_value.castTo(v8.Object));
171-
if (arg_T.underPtr() != null) {
171+
if (comptime arg_T.underPtr() != null) {
172172
value = ptr;
173173
} else {
174174
value = ptr.*;
@@ -389,13 +389,14 @@ fn getArgs(
389389

390390
fn freeArgs(alloc: std.mem.Allocator, comptime func: refl.Func, obj: anytype) !void {
391391
inline for (func.args) |arg_T| {
392+
const underT = comptime arg_T.underT();
392393

393394
// free char slices
394395
// the API functions will be responsible of copying the slice
395396
// in their implementations if they want to keep it afterwards
396-
if (arg_T.underT() == []u8 or arg_T.underT() == []const u8) {
397+
if (underT == []u8 or underT == []const u8) {
397398
const val = @field(obj, arg_T.name.?);
398-
if (arg_T.underOpt() != null) {
399+
if (comptime arg_T.underOpt() != null) {
399400
// free only if val is non-null
400401
if (val) |v| {
401402
alloc.free(v);
@@ -406,7 +407,7 @@ fn freeArgs(alloc: std.mem.Allocator, comptime func: refl.Func, obj: anytype) !v
406407
}
407408

408409
// free varidadic slices
409-
if (try refl.Type.variadic(arg_T.underT(), null) != null) {
410+
if (try refl.Type.variadic(underT, null) != null) {
410411
const val = @field(obj, arg_T.name.?).?;
411412
// NOTE: variadic are optional by design
412413
alloc.free(@field(val, "slice"));
@@ -913,7 +914,7 @@ fn callFunc(
913914

914915
// call native func
915916
const function = @field(T_refl.T, func.name);
916-
const res_T = func.return_type.underErr() orelse func.return_type.T;
917+
const res_T = comptime func.return_type.underErr() orelse func.return_type.T;
917918
var res: res_T = undefined;
918919
if (comptime @typeInfo(func.return_type.T) == .ErrorUnion) {
919920
res = @call(.auto, function, args) catch |err| {
@@ -940,7 +941,7 @@ fn callFunc(
940941
nat_ctx.alloc,
941942
nat_ctx,
942943
T_refl,
943-
func.return_type.underT(),
944+
comptime func.return_type.underT(),
944945
res,
945946
cbk_info.getThis(),
946947
isolate,

src/generate.zig

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,16 @@ const loadFn = @import("private_api.zig").loadFn;
3838
// reflect the user-defined types to obtain type information (T_refl)
3939
// This function must be called at comptime by the root file of the project
4040
// and stored in a constant named `Types`
41-
pub fn reflect(comptime types: anytype) []refl.Struct {
41+
pub fn reflect(comptime types: anytype) []const refl.Struct {
4242
std.debug.assert(@inComptime());
4343

4444
// call types reflection
45-
return refl.do(types) catch unreachable;
45+
const structs: []const refl.Struct = refl.do(types) catch |e| @compileError(@errorName(e));
46+
return structs;
4647
}
4748

4849
// Import user-defined types
49-
pub const Types: []refl.Struct = @import("root").Types;
50+
pub const Types: []const refl.Struct = @import("root").Types;
5051

5152
// retrieved the reflected type of a user-defined native type
5253
pub fn getType(comptime T: type) refl.Struct {
@@ -61,7 +62,7 @@ pub fn getType(comptime T: type) refl.Struct {
6162

6263
// generate APIs from reflected types
6364
// which can be later loaded in JS.
64-
fn generate(comptime types: []refl.Struct) []API {
65+
fn generate(comptime types: []const refl.Struct) []API {
6566
std.debug.assert(@inComptime());
6667

6768
var apis: [types.len]API = undefined;
@@ -146,7 +147,11 @@ fn MergeTupleT(comptime value: anytype) type {
146147
var i = 0;
147148
while (i < fields_nb) {
148149
fields[i] = .{
149-
.name = itoa(i),
150+
// StructField.name expect a null terminated string.
151+
// concatenate the `[]const u8` string with an empty string
152+
// literal (`name ++ ""`) to explicitly coerce it to `[:0]const
153+
// u8`.
154+
.name = itoa(i) ++ "",
150155
.type = type,
151156
.default_value = null,
152157
.is_comptime = false,
@@ -156,7 +161,7 @@ fn MergeTupleT(comptime value: anytype) type {
156161
}
157162
const decls: [0]std.builtin.Type.Declaration = undefined;
158163
const info = std.builtin.Type.Struct{
159-
.layout = .Auto,
164+
.layout = .auto,
160165
.fields = &fields,
161166
.decls = &decls,
162167
.is_tuple = true,

0 commit comments

Comments
 (0)