From 06ce040347bcaeccced5cba5802ab446a1d920cd Mon Sep 17 00:00:00 2001 From: "Nurul Huda (Apon)" Date: Thu, 16 Jul 2026 15:01:12 +0600 Subject: [PATCH] refactor: cleanup to have single entrypoints --- test/build.zig | 3 ++- test/src/main.zig | 21 ++++++++++++++++++--- test/src/root.zig | 15 --------------- test/src/wasm.zig | 17 ----------------- 4 files changed, 20 insertions(+), 36 deletions(-) delete mode 100644 test/src/wasm.zig diff --git a/test/build.zig b/test/build.zig index 6a91165..69f175f 100644 --- a/test/build.zig +++ b/test/build.zig @@ -12,7 +12,7 @@ pub fn build(b: *std.Build) void { const exe = b.addExecutable(.{ .name = "temporalz", .root_module = b.createModule(.{ - .root_source_file = b.path(if (is_freestanding) "src/wasm.zig" else "src/main.zig"), + .root_source_file = b.path("src/main.zig"), .target = target, .optimize = optimize, .link_libc = !is_freestanding, @@ -21,6 +21,7 @@ pub fn build(b: *std.Build) void { }); exe.root_module.addImport("temporalz", temporalz.module("temporalz")); exe.rdynamic = is_freestanding; + if (is_freestanding) exe.entry = .disabled; b.installArtifact(exe); b.enable_wasmtime = true; // This doesn't work since Zig 0.17.0 release diff --git a/test/src/main.zig b/test/src/main.zig index 7b318c4..217ee52 100644 --- a/test/src/main.zig +++ b/test/src/main.zig @@ -1,9 +1,24 @@ const std = @import("std"); +const builtin = @import("builtin"); const program = @import("root.zig"); -pub fn main(init: std.process.Init) !void { - const allocator = init.arena.allocator(); - const io = init.io; +const freestanding = builtin.os.tag == .freestanding; + +pub fn main(init: Init) !void { + const allocator = std.heap.page_allocator; + const io = if (freestanding) null else init.io; try program.run(allocator, io); } + +// -- // +const Init = if (freestanding) std.process.Init.Minimal else std.process.Init; +pub const std_options: std.Options = .{ + .logFn = if (freestanding) logFn else std.log.defaultLog, +}; + +extern fn console(ptr: [*]u8, len: u32) void; +fn logFn(comptime _: anytype, comptime _: anytype, comptime format: []const u8, args: anytype) void { + const formatted = std.fmt.allocPrint(std.heap.wasm_allocator, format, args) catch return; + console(formatted.ptr, formatted.len); +} diff --git a/test/src/root.zig b/test/src/root.zig index 54119ad..285fade 100644 --- a/test/src/root.zig +++ b/test/src/root.zig @@ -911,18 +911,3 @@ pub fn runTimeZoneExamples( }); } } - -extern fn consoleLog(ptr: [*]u8, len: u32) void; -pub fn logFn(comptime message_level: std.log.Level, comptime scope: @TypeOf(.enum_literal), comptime format: []const u8, args: anytype) void { - if (builtin.os.tag == .freestanding) { - const prefix = if (scope == .default) "" else "(" ++ @tagName(scope) ++ ") "; - const formatted = std.fmt.allocPrint(std.heap.wasm_allocator, prefix ++ format, args) catch return; - consoleLog(formatted.ptr, formatted.len); - } - - std.log.defaultLog(message_level, scope, format, args); -} - -pub const std_options: std.Options = .{ - .logFn = logFn, -}; diff --git a/test/src/wasm.zig b/test/src/wasm.zig deleted file mode 100644 index be27e88..0000000 --- a/test/src/wasm.zig +++ /dev/null @@ -1,17 +0,0 @@ -const std = @import("std"); -const program = @import("root.zig"); -const Temporal = @import("temporalz"); - -export fn _start() void { - const allocator = std.heap.page_allocator; - program.run(allocator, null) catch {}; -} - -extern fn console(ptr: [*]u8, len: u32) void; - -fn logFn(comptime _: anytype, comptime _: anytype, comptime format: []const u8, args: anytype) void { - const formatted = std.fmt.allocPrint(std.heap.wasm_allocator, format, args) catch return; - console(formatted.ptr, formatted.len); -} - -pub const std_options: std.Options = .{ .logFn = logFn };