diff --git a/crates/cli/tests/benchmarks/platform/host.zig b/crates/cli/tests/benchmarks/platform/host.zig index da5360ae26..3eaa623f7d 100644 --- a/crates/cli/tests/benchmarks/platform/host.zig +++ b/crates/cli/tests/benchmarks/platform/host.zig @@ -111,12 +111,15 @@ comptime { const Unit = extern struct {}; -pub fn main() !u8 { +pub export fn main() u8 { const stderr = std.io.getStdErr().writer(); // The size might be zero; if so, make it at least 8 so that we don't have a nullptr const size = @max(@as(usize, @intCast(roc__mainForHost_1_exposed_size())), 8); - const raw_output = roc_alloc(@as(usize, @intCast(size)), @alignOf(u64)).?; + const raw_output = roc_alloc(@as(usize, @intCast(size)), @alignOf(u64)) orelse { + std.log.err("Memory allocation failed", .{}); + return 1; + }; const output = @as([*]u8, @ptrCast(raw_output)); defer { diff --git a/crates/cli/tests/expects/zig-platform/host.zig b/crates/cli/tests/expects/zig-platform/host.zig index 6c18c87d1c..c194b22e4d 100644 --- a/crates/cli/tests/expects/zig-platform/host.zig +++ b/crates/cli/tests/expects/zig-platform/host.zig @@ -105,7 +105,7 @@ extern fn roc__mainForHost_1_exposed_generic(*RocStr) void; const Unit = extern struct {}; -pub fn main() u8 { +pub export fn main() u8 { const stdout = std.io.getStdOut().writer(); const stderr = std.io.getStdErr().writer(); diff --git a/crates/compiler/builtins/bitcode/benchmark/dec.zig b/crates/compiler/builtins/benchmark-dec.zig similarity index 91% rename from crates/compiler/builtins/bitcode/benchmark/dec.zig rename to crates/compiler/builtins/benchmark-dec.zig index 218c9ef5e4..d892b84dd6 100644 --- a/crates/compiler/builtins/bitcode/benchmark/dec.zig +++ b/crates/compiler/builtins/benchmark-dec.zig @@ -2,8 +2,8 @@ const std = @import("std"); const time = std.time; const Timer = time.Timer; -const RocStr = @import("../src/str.zig").RocStr; -const RocDec = @import("../src/dec.zig").RocDec; +const RocStr = @import("./bitcode/src/str.zig").RocStr; +const RocDec = @import("./bitcode/src/dec.zig").RocDec; fn roc_alloc(_: usize, _: u32) callconv(.C) ?*anyopaque { @panic("Not needed for dec benchmark"); @@ -23,7 +23,15 @@ comptime { var timer: Timer = undefined; -pub fn main() !void { +pub export fn main() u8 { + run_tests() catch |err| { + std.debug.print("Error: {}\n", .{err}); + return 1; + }; + return 0; +} + +fn run_tests() !void { const stdout = std.io.getStdOut().writer(); try stdout.print("Warning: Timer seems to step in units of 41ns\n\n", .{}); timer = try Timer.start(); @@ -35,11 +43,15 @@ pub fn main() !void { // This number are very close to 1 to avoid over and underflow. const f1 = 1.00123; - const dec1 = RocDec.fromF64(f1).?; + const dec1 = RocDec.fromF64(f1) orelse { + @panic("Failed to create RocDec from f64"); + }; // `asin` and `acos` have a limited range, so they will use this value. const f2 = 0.00130000847; - const dec2 = RocDec.fromF64(f2).?; + const dec2 = RocDec.fromF64(f2) orelse { + @panic("Failed to create RocDec from f64"); + }; try stdout.print("Dec:\n", .{}); try stdout.print("{} additions took ", .{add_sub_n}); @@ -129,8 +141,8 @@ fn avg_runs(comptime T: type, comptime n: usize, comptime op: fn (T, T) T, v: T) runs[i] = callWrapper(u64, .never_inline, run, .{ T, n, op, v }); } - var real_runs = runs[warmups..runs.len]; - std.sort.sort(u64, real_runs, {}, comptime std.sort.asc(u64)); + const real_runs = runs[warmups..runs.len]; + std.sort.insertion(u64, real_runs, {}, comptime std.sort.asc(u64)); const median = real_runs[real_runs.len / 2]; const highest = real_runs[real_runs.len - 1]; @@ -146,8 +158,8 @@ fn run(comptime T: type, comptime n: usize, comptime op: fn (T, T) T, v: T) u64 // Split into outer and inner loop to avoid breaking comptime. const max_inline = 100; - comptime var outer = n / max_inline; - comptime var inner = std.math.min(n, max_inline); + const outer = n / max_inline; + const inner = @min(n, max_inline); var i: usize = 0; while (i < outer) : (i += 1) { comptime var j = 0; @@ -156,7 +168,7 @@ fn run(comptime T: type, comptime n: usize, comptime op: fn (T, T) T, v: T) u64 } } const rem = n % max_inline; - comptime var j = 0; + const j = 0; inline while (j < rem) : (j += 1) { a = callWrapper(T, .always_inline, op, .{ a, v }); } @@ -172,7 +184,7 @@ fn run(comptime T: type, comptime n: usize, comptime op: fn (T, T) T, v: T) u64 // This is needed to work around a bug with using `@call` in loops. inline fn callWrapper(comptime T: type, call_modifier: anytype, comptime func: anytype, params: anytype) T { - return @call(.{ .modifier = call_modifier }, func, params); + return @call(call_modifier, func, params); } fn addF64(x: f64, y: f64) f64 { diff --git a/crates/compiler/builtins/bitcode/benchmark.sh b/crates/compiler/builtins/benchmark.sh similarity index 64% rename from crates/compiler/builtins/bitcode/benchmark.sh rename to crates/compiler/builtins/benchmark.sh index 410188d451..1c0ec10049 100755 --- a/crates/compiler/builtins/bitcode/benchmark.sh +++ b/crates/compiler/builtins/benchmark.sh @@ -3,5 +3,5 @@ # https://vaneyckt.io/posts/safer_bash_scripts_with_set_euxo_pipefail/ set -euxo pipefail -zig build-exe benchmark/dec.zig -O ReleaseFast --main-pkg-path . +zig build-exe benchmark-dec.zig -O ReleaseFast ./dec diff --git a/crates/compiler/test_gen/src/helpers/wasm_linking_test_host.zig b/crates/compiler/test_gen/src/helpers/wasm_linking_test_host.zig index 3f77fd7691..64cc0eed6f 100644 --- a/crates/compiler/test_gen/src/helpers/wasm_linking_test_host.zig +++ b/crates/compiler/test_gen/src/helpers/wasm_linking_test_host.zig @@ -33,7 +33,7 @@ export fn read_host_result() i32 { return host_result; } -pub fn main() !void { +pub export fn main() u8 { const host = host_called_directly_from_main(); const js = js_called_directly_from_main(); const app = roc__app_proc_1_exposed(); diff --git a/crates/valgrind/zig-platform/host.zig b/crates/valgrind/zig-platform/host.zig index f1158d93b5..26d432aa99 100644 --- a/crates/valgrind/zig-platform/host.zig +++ b/crates/valgrind/zig-platform/host.zig @@ -106,7 +106,7 @@ extern fn roc__mainForHost_1_exposed_generic(*RocStr) void; const Unit = extern struct {}; -pub fn main() u8 { +pub export fn main() u8 { const stdout = std.io.getStdOut().writer(); const stderr = std.io.getStdErr().writer(); diff --git a/examples/nodejs-interop/wasm/platform/host.zig b/examples/nodejs-interop/wasm/platform/host.zig index d6da990e7e..5f830fabdd 100644 --- a/examples/nodejs-interop/wasm/platform/host.zig +++ b/examples/nodejs-interop/wasm/platform/host.zig @@ -39,7 +39,7 @@ extern fn roc__mainForHost_1_exposed(*RocStr) void; extern fn js_display_roc_string(str_bytes: ?[*]u8, str_len: usize) void; -pub fn main() u8 { +pub export fn main() u8 { // actually call roc to populate the callresult var callresult = RocStr.empty(); roc__mainForHost_1_exposed(&callresult); diff --git a/examples/platform-switching/web-assembly-platform/host.zig b/examples/platform-switching/web-assembly-platform/host.zig index b6babf362f..04c8eb716b 100644 --- a/examples/platform-switching/web-assembly-platform/host.zig +++ b/examples/platform-switching/web-assembly-platform/host.zig @@ -39,7 +39,7 @@ extern fn roc__mainForHost_1_exposed(*RocStr) void; extern fn js_display_roc_string(str_bytes: ?[*]u8, str_len: usize) void; -pub fn main() u8 { +pub export fn main() u8 { // actually call roc to populate the callresult var callresult = RocStr.empty(); roc__mainForHost_1_exposed(&callresult); diff --git a/examples/platform-switching/zig-platform/host.zig b/examples/platform-switching/zig-platform/host.zig index f1158d93b5..26d432aa99 100644 --- a/examples/platform-switching/zig-platform/host.zig +++ b/examples/platform-switching/zig-platform/host.zig @@ -106,7 +106,7 @@ extern fn roc__mainForHost_1_exposed_generic(*RocStr) void; const Unit = extern struct {}; -pub fn main() u8 { +pub export fn main() u8 { const stdout = std.io.getStdOut().writer(); const stderr = std.io.getStdErr().writer(); diff --git a/examples/virtual-dom-wip/platform/src/server-side/host.zig b/examples/virtual-dom-wip/platform/src/server-side/host.zig index f2dcc9b756..4bef351ea2 100644 --- a/examples/virtual-dom-wip/platform/src/server-side/host.zig +++ b/examples/virtual-dom-wip/platform/src/server-side/host.zig @@ -56,7 +56,7 @@ extern fn roc__main_1_exposed(RocStr, RocStr) callconv(.C) ResultStrStr; const hostJavaScriptBytes = @embedFile("../client-side/host.js"); -pub fn main() u8 { +pub export fn main() u8 { const json = RocStr.fromSlice("42"); defer json.decref();