From a290ab2d478088927953dbec601238dd67db895e Mon Sep 17 00:00:00 2001 From: Krzysztof Wolicki Date: Wed, 3 Jul 2024 09:46:52 +0200 Subject: [PATCH 1/2] Update stdlib to Zig 0.13 Update benchmark to Zig 0.13 Update build.zig --- build.zig | 40 +++++++++++++++----------------- src/benchmark/perf.zig | 8 +++---- src/benchmark/render.zig | 2 +- src/library/libraries/stdlib.zig | 8 +++---- 4 files changed, 28 insertions(+), 30 deletions(-) diff --git a/build.zig b/build.zig index 79d8fa4..a40ed61 100644 --- a/build.zig +++ b/build.zig @@ -16,15 +16,15 @@ const Example = struct { }; const examples = [_]Example{ - Example{ + .{ .name = "minimal-host", .path = "examples/host/minimal-host/main.zig", }, - Example{ + .{ .name = "multi-environment", .path = "examples/host/multi-environment/main.zig", }, - Example{ + .{ .name = "serialization", .path = "examples/host/serialization/main.zig", }, @@ -59,16 +59,14 @@ pub fn build(b: *Build) !void { }); exe.root_module.addImport("lola", mod_lola); exe.root_module.addImport("args", mod_args); - exe.root_module.addAnonymousImport("build_options", .{ - .root_source_file = build_options.getSource(), - }); + exe.root_module.addImport("build_options", build_options.createModule()); b.installArtifact(exe); const benchmark_renderer = b.addExecutable(.{ .name = "benchmark-render", .root_source_file = b.path("src/benchmark/render.zig"), .optimize = optimize, - .target = b.host, + .target = b.graph.host, }); b.installArtifact(benchmark_renderer); @@ -76,8 +74,8 @@ pub fn build(b: *Build) !void { const render_benchmark_step = b.step("render-benchmarks", "Runs the benchmark suite."); const only_render_benchmark = b.addRunArtifact(benchmark_renderer); - only_render_benchmark.addArg(b.pathFromRoot("benchmarks/data")); - only_render_benchmark.addArg(b.pathFromRoot("benchmarks/visualization")); + only_render_benchmark.addDirectoryArg(b.path("benchmarks/data")); + only_render_benchmark.addDirectoryArg(b.path("benchmarks/visualization")); render_benchmark_step.dependOn(&only_render_benchmark.step); } @@ -88,8 +86,8 @@ pub fn build(b: *Build) !void { const benchmark_step = b.step("benchmark", "Runs the benchmark suite."); const render_benchmark = b.addRunArtifact(benchmark_renderer); - render_benchmark.addArg(b.pathFromRoot("benchmarks/data")); - render_benchmark.addArg(b.pathFromRoot("benchmarks/visualization")); + render_benchmark.addDirectoryArg(b.path("benchmarks/data")); + render_benchmark.addDirectoryArg(b.path("benchmarks/visualization")); benchmark_step.dependOn(&render_benchmark.step); for (benchmark_modes) |benchmark_mode| { @@ -97,13 +95,13 @@ pub fn build(b: *Build) !void { .name = b.fmt("benchmark-{s}", .{@tagName(benchmark_mode)}), .root_source_file = b.path("src/benchmark/perf.zig"), .optimize = benchmark_mode, - .target = b.host, + .target = b.graph.host, }); benchmark.root_module.addImport("lola", mod_lola); const run_benchmark = b.addRunArtifact(benchmark); - run_benchmark.addArg(b.pathFromRoot("benchmarks/code")); - run_benchmark.addArg(b.pathFromRoot("benchmarks/data")); + run_benchmark.addDirectoryArg(b.path("benchmarks/code")); + run_benchmark.addDirectoryArg(b.path("benchmarks/data")); render_benchmark.step.dependOn(&run_benchmark.step); } @@ -138,7 +136,7 @@ pub fn build(b: *Build) !void { var main_tests = b.addTest(.{ .root_source_file = b.path("src/library/test.zig"), .optimize = optimize, - .target = b.host, + .target = b.graph.host, }); // main_tests.root_module.addImport("interface", mod_interface); main_tests.root_module.addImport("any-pointer", mod_any_pointer); @@ -155,13 +153,13 @@ pub fn build(b: *Build) !void { const behaviour_tests = b.addRunArtifact(exe); behaviour_tests.addArg("run"); behaviour_tests.addArg("--no-stdlib"); // we don't want the behaviour tests to be run with any stdlib functions - behaviour_tests.addArg(prefix ++ "behaviour.lola"); + behaviour_tests.addFileArg(b.path(prefix ++ "behaviour.lola")); behaviour_tests.expectStdOutEqual("Behaviour test suite passed.\n"); test_step.dependOn(&behaviour_tests.step); const stdib_test = b.addRunArtifact(exe); stdib_test.addArg("run"); - stdib_test.addArg(prefix ++ "stdlib.lola"); + stdib_test.addFileArg(b.path(prefix ++ "stdlib.lola")); stdib_test.expectStdOutEqual("Standard library test suite passed.\n"); test_step.dependOn(&stdib_test.step); @@ -201,26 +199,26 @@ pub fn build(b: *Build) !void { const emptyfile_test = b.addRunArtifact(exe); emptyfile_test.addArg("run"); - emptyfile_test.addArg(prefix ++ "empty.lola"); + emptyfile_test.addFileArg(b.path(prefix ++ "empty.lola")); emptyfile_test.expectStdOutEqual(""); test_step.dependOn(&emptyfile_test.step); const globreturn_test = b.addRunArtifact(exe); globreturn_test.addArg("run"); - globreturn_test.addArg(prefix ++ "global-return.lola"); + globreturn_test.addFileArg(b.path(prefix ++ "global-return.lola")); globreturn_test.expectStdOutEqual(""); test_step.dependOn(&globreturn_test.step); const extended_behaviour_test = b.addRunArtifact(exe); extended_behaviour_test.addArg("run"); - extended_behaviour_test.addArg(prefix ++ "behaviour-with-stdlib.lola"); + extended_behaviour_test.addFileArg(b.path(prefix ++ "behaviour-with-stdlib.lola")); extended_behaviour_test.expectStdOutEqual("Extended behaviour test suite passed.\n"); test_step.dependOn(&extended_behaviour_test.step); const compiler_test = b.addRunArtifact(exe); compiler_test.addArg("compile"); compiler_test.addArg("--verify"); // verify should not emit a compiled module - compiler_test.addArg(prefix ++ "compiler.lola"); + compiler_test.addFileArg(b.path(prefix ++ "compiler.lola")); compiler_test.expectStdOutEqual(""); test_step.dependOn(&compiler_test.step); } diff --git a/src/benchmark/perf.zig b/src/benchmark/perf.zig index 3aef464..ef6e83b 100644 --- a/src/benchmark/perf.zig +++ b/src/benchmark/perf.zig @@ -46,20 +46,20 @@ pub fn main() !u8 { }); { - var dir = try std.fs.cwd().openIterableDir(argv[1], .{}); + var dir = try std.fs.cwd().openDir(argv[1], .{ .iterate = true }); defer dir.close(); var iterator = dir.iterate(); while (try iterator.next()) |entry| { const name = try string_arena.allocator().dupe(u8, entry.name); - const source = try dir.dir.readFileAlloc(string_arena.allocator(), entry.name, size(1.5, .MeBi)); // 1 MB source + const source = try dir.readFileAlloc(string_arena.allocator(), entry.name, size(1.5, .MeBi)); // 1 MB source const target_file = try std.fmt.allocPrint(string_arena.allocator(), "{s}-{s}.csv", .{ name[0 .. name.len - std.fs.path.extension(name).len], @tagName(builtin.mode), }); - try files.append(Benchmark{ + try files.append(.{ .file_name = name, .source_code = source, .target_file = target_file, @@ -172,7 +172,7 @@ const Benchmark = struct { result.setup_time = @as(u128, @intCast(runtime_start - setup_start)); while (true) { - var res = try vm.execute(1_000_000); + const res = try vm.execute(1_000_000); pool.clearUsageCounters(); try pool.walkEnvironment(env); diff --git a/src/benchmark/render.zig b/src/benchmark/render.zig index 1ce171a..b2a437a 100644 --- a/src/benchmark/render.zig +++ b/src/benchmark/render.zig @@ -214,7 +214,7 @@ pub fn loadSeries(allocator: std.mem.Allocator, file: std.fs.File) ![]DataPoint if (line_or_eof) |line| { if (line.len == 0) continue; - var iter = std.mem.split(u8, line, ";"); + var iter = std.mem.splitScalar(u8, line, ';'); const time_str = iter.next() orelse return error.UnexpectedData; const compile_str = try std.fmt.parseInt(u64, iter.next() orelse return error.UnexpectedData, 10); const setup_str = try std.fmt.parseInt(u64, iter.next() orelse return error.UnexpectedData, 10); diff --git a/src/library/libraries/stdlib.zig b/src/library/libraries/stdlib.zig index 5150540..a443dcd 100644 --- a/src/library/libraries/stdlib.zig +++ b/src/library/libraries/stdlib.zig @@ -355,7 +355,7 @@ pub fn Split(env: *const lola.runtime.Environment, context: lola.runtime.Context items.deinit(); } - var iter = std.mem.split(u8, input, separator); + var iter = std.mem.splitAny(u8, input, separator); while (iter.next()) |slice| { if (!removeEmpty or slice.len > 0) { var val = try lola.runtime.Value.initString(env.allocator, slice); @@ -689,7 +689,7 @@ pub fn Random(env: *lola.runtime.Environment, context: lola.runtime.Context, arg defer random_mutex.unlock(); if (random == null) { - random = std.rand.DefaultPrng.init(@as(u64, @bitCast(@as(f64, @floatFromInt(milliTimestamp()))))); + random = std.Random.DefaultPrng.init(@as(u64, @bitCast(@as(f64, @floatFromInt(milliTimestamp()))))); } result = lower + (upper - lower) * random.?.random().float(f64); @@ -721,7 +721,7 @@ pub fn RandomInt(env: *lola.runtime.Environment, context: lola.runtime.Context, defer random_mutex.unlock(); if (random == null) { - random = std.rand.DefaultPrng.init(@as(u64, @bitCast(@as(f64, @floatFromInt(milliTimestamp()))))); + random = std.Random.DefaultPrng.init(@as(u64, @bitCast(@as(f64, @floatFromInt(milliTimestamp()))))); } result = random.?.random().intRangeLessThan(i32, lower, upper); @@ -731,4 +731,4 @@ pub fn RandomInt(env: *lola.runtime.Environment, context: lola.runtime.Context, } var random_mutex = std.Thread.Mutex{}; -var random: ?std.rand.DefaultPrng = null; +var random: ?std.Random.DefaultPrng = null; From 677b8e57d0ded7252a7c6a9fe1a8566d0686951a Mon Sep 17 00:00:00 2001 From: Krzysztof Wolicki Date: Wed, 3 Jul 2024 12:44:57 +0200 Subject: [PATCH 2/2] Fix the file IO test --- build.zig | 62 ++++++++++++++++++++++++++----------------------------- 1 file changed, 29 insertions(+), 33 deletions(-) diff --git a/build.zig b/build.zig index a40ed61..ae1fa42 100644 --- a/build.zig +++ b/build.zig @@ -163,39 +163,35 @@ pub fn build(b: *Build) !void { stdib_test.expectStdOutEqual("Standard library test suite passed.\n"); test_step.dependOn(&stdib_test.step); - // when the host is windows, this won't work :( - if (builtin.os.tag != .windows) { - std.fs.cwd().makeDir(".zig-cache/tmp") catch |err| switch (err) { - error.PathAlreadyExists => {}, // nice - else => |e| return e, - }; - - const runlib_test = b.addRunArtifact(exe); - - // execute in the zig-cache directory so we have a "safe" playfield - // for file I/O - runlib_test.cwd = b.path(".zig-cache/tmp"); - - // `Exit(123)` is the last call in the runtime suite - runlib_test.expectExitCode(123); - - runlib_test.expectStdOutEqual( - \\ - \\1 - \\1.2 - \\[ ] - \\[ 1, 2 ] - \\truefalse - \\hello - \\Runtime library test suite passed. - \\ - ); - - runlib_test.addArg("run"); - runlib_test.addArg("../../" ++ prefix ++ "runtime.lola"); - - test_step.dependOn(&runlib_test.step); - } + b.cache_root.handle.makeDir("tmp") catch |err| switch (err) { + error.PathAlreadyExists => {}, // nice + else => |e| return e, + }; + const runlib_test = b.addRunArtifact(exe); + + // execute in the .zig-cache directory so we have a "safe" playfield + // for file I/O + runlib_test.setCwd(.{ .cwd_relative = try b.cache_root.join(b.allocator, &.{"tmp"}) }); + + // `Exit(123)` is the last call in the runtime suite + runlib_test.expectExitCode(123); + + runlib_test.expectStdOutEqual( + \\ + \\1 + \\1.2 + \\[ ] + \\[ 1, 2 ] + \\truefalse + \\hello + \\Runtime library test suite passed. + \\ + ); + + runlib_test.addArg("run"); + runlib_test.addFileArg(b.path(prefix ++ "runtime.lola")); + + test_step.dependOn(&runlib_test.step); const emptyfile_test = b.addRunArtifact(exe); emptyfile_test.addArg("run");