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

Update stdlib to Zig 0.13 #48

Merged
merged 2 commits into from
Jul 3, 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
92 changes: 43 additions & 49 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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",
},
Expand Down Expand Up @@ -59,25 +59,23 @@ 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);

{
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);
}
Expand All @@ -88,22 +86,22 @@ 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| {
const benchmark = b.addExecutable(.{
.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);
}
Expand Down Expand Up @@ -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);
Expand All @@ -155,72 +153,68 @@ 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);

// 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);
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.cwd = b.path(".zig-cache/tmp");
// 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);
// `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.expectStdOutEqual(
\\
\\1
\\1.2
\\[ ]
\\[ 1, 2 ]
\\truefalse
\\hello
\\Runtime library test suite passed.
\\
);

runlib_test.addArg("run");
runlib_test.addArg("../../" ++ prefix ++ "runtime.lola");
runlib_test.addArg("run");
runlib_test.addFileArg(b.path(prefix ++ "runtime.lola"));

test_step.dependOn(&runlib_test.step);
}
test_step.dependOn(&runlib_test.step);

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);
}
Expand Down
8 changes: 4 additions & 4 deletions src/benchmark/perf.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/benchmark/render.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
8 changes: 4 additions & 4 deletions src/library/libraries/stdlib.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand All @@ -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;
Loading