diff --git a/.gitignore b/.gitignore index 363a6ce..6f2b2ea 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,6 @@ zig-out .zig-cache +zig-pkg target lib diff --git a/.tool-versions b/.tool-versions new file mode 100644 index 0000000..f81adbc --- /dev/null +++ b/.tool-versions @@ -0,0 +1 @@ +zig master diff --git a/build.zig b/build.zig index 685abe3..a02217a 100644 --- a/build.zig +++ b/build.zig @@ -43,8 +43,8 @@ pub fn build(b: *std.Build) !void { const use_prebuilt = blk: { // Use the LazyPath to check if the library exists const lib_full_path = prebuilt_lib_file.getPath(b); - const lib_check_file = std.fs.openFileAbsolute(lib_full_path, .{}) catch break :blk false; - lib_check_file.close(); + const lib_check_file = std.Io.Dir.cwd().openFile(b.graph.io, lib_full_path, .{}) catch break :blk false; + lib_check_file.close(b.graph.io); break :blk true; }; @@ -136,7 +136,7 @@ pub fn build(b: *std.Build) !void { .mode = .simple, }, }); - mod_tests.linkLibC(); + mod_tests.root_module.link_libc = true; test_step.dependOn(&b.addRunArtifact(mod_tests).step); const exe_tests = b.addTest(.{ .root_module = exe.root_module }); test_step.dependOn(&b.addRunArtifact(exe_tests).step); @@ -159,7 +159,7 @@ pub fn build(b: *std.Build) !void { .mode = .simple, }, }); - test262_tests.linkLibC(); + test262_tests.root_module.link_libc = true; test262_step.dependOn(&b.addRunArtifact(test262_tests).step); } diff --git a/build.zig.zon b/build.zig.zon index 0bfe0c9..6b2413d 100644 --- a/build.zig.zon +++ b/build.zig.zon @@ -1,16 +1,16 @@ .{ .name = .temporalz, - .version = "0.0.0", + .version = "0.1.2", .fingerprint = 0xd8d79d59acc4faae, - .minimum_zig_version = "0.15.2", + .minimum_zig_version = "0.16.0", .dependencies = .{ .temporal_rs = .{ .url = "git+https://github.com/boa-dev/temporal#v0.1.2", .hash = "N-V-__8AANlkNAB77Z946lqp-lgp2qSsOBDADValZC86PhB-", }, .build_crab = .{ - .url = "git+https://github.com/linusg/build.crab#7c8ace3739023b88f1fb95151df4f5b65bd7a311", - .hash = "build_crab-0.2.1-U0id_33BAAD3l81W7CBvgLU3OJSw7WI6FVLBSEHTnjTX", + .url = "git+https://github.com/linusg/build.crab?ref=zig-dev#a4c247befef7c1642e7fdfdaf47d3a74dd96d46c", + .hash = "build_crab-0.2.1-U0id_yXEAADD2lzWEocoHFMzBXzSABdi0Z7lZPVxN-Ve", }, }, .paths = .{ diff --git a/example/src/main.zig b/example/src/main.zig index 834b7ae..286c264 100644 --- a/example/src/main.zig +++ b/example/src/main.zig @@ -1,8 +1,9 @@ const std = @import("std"); const Temporal = @import("temporalz"); -pub fn main() !void { - const allocator = std.heap.page_allocator; +pub fn main(init: std.process.Init) !void { + const allocator = init.arena.allocator(); + const io = init.io; // --- Instant --- // const instant = try Temporal.Instant.init(1_704_067_200_000_000_000); // 2024-01-01 00:00:00 UTC @@ -51,12 +52,12 @@ pub fn main() !void { }); // --- Now --- // - const now_instant = try Temporal.Now.instant(); + const now_instant = try Temporal.Now.instant(io); defer now_instant.deinit(); - const now_date = try Temporal.Now.plainDateISO(); + const now_date = try Temporal.Now.plainDateISO(io); defer now_date.deinit(); - const now_datetime = try Temporal.Now.plainDateTimeISO(); - const now_time = try Temporal.Now.plainTimeISO(); + const now_datetime = try Temporal.Now.plainDateTimeISO(io); + const now_time = try Temporal.Now.plainTimeISO(io); std.debug.print( \\Now \\ - instant: {s} diff --git a/src/Now.zig b/src/Now.zig index 38196ee..5745779 100644 --- a/src/Now.zig +++ b/src/Now.zig @@ -30,24 +30,24 @@ const Now = @This(); /// Returns the current time as a [`Temporal.Instant`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Temporal/Instant) object. /// /// See: [MDN Temporal.Now.instant](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Temporal/Now/instant) -pub fn instant() !Instant { - const ns: i128 = std.time.nanoTimestamp(); +pub fn instant(io: std.Io) !Instant { + const ns = std.Io.Timestamp.now(io, .real).nanoseconds; return Instant.fromEpochNanoseconds(ns); } /// Returns the current date as a [`Temporal.PlainDate`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Temporal/PlainDate) object, in the ISO 8601 calendar and the specified time zone. /// /// See: [MDN Temporal.Now.plainDateISO](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Temporal/Now/plainDateISO) -pub fn plainDateISO() !PlainDate { - const now = currentParts(); +pub fn plainDateISO(io: std.Io) !PlainDate { + const now = currentParts(io); return PlainDate.init(now.year, now.month, now.day); } /// Returns the current date and time as a [`Temporal.PlainDateTime`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Temporal/PlainDateTime) object, in the ISO 8601 calendar and the specified time zone. /// /// See: [MDN Temporal.Now.plainDateTimeISO](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Temporal/Now/plainDateTimeISO) -pub fn plainDateTimeISO() !PlainDateTime { - const now = currentParts(); +pub fn plainDateTimeISO(io: std.Io) !PlainDateTime { + const now = currentParts(io); return PlainDateTime.init( now.year, now.month, @@ -64,8 +64,8 @@ pub fn plainDateTimeISO() !PlainDateTime { /// Returns the current time as a [`Temporal.PlainTime`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Temporal/PlainTime) object, in the specified time zone. /// /// See: [MDN Temporal.Now.plainTimeISO](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Temporal/Now/plainTimeISO) -pub fn plainTimeISO() !PlainTime { - const now = currentParts(); +pub fn plainTimeISO(io: std.Io) !PlainTime { + const now = currentParts(io); return PlainTime.init( now.hour, now.minute, @@ -102,8 +102,8 @@ const CurrentParts = struct { nanosecond: u16, }; -fn currentParts() CurrentParts { - const ns: i128 = std.time.nanoTimestamp(); +fn currentParts(io: std.Io) CurrentParts { + const ns = std.Io.Timestamp.now(io, .real).nanoseconds; const seconds: i64 = @intCast(@divTrunc(ns, 1_000_000_000)); const subsec_nanos_u64: u64 = @intCast(@rem(ns, 1_000_000_000)); @@ -134,19 +134,22 @@ fn currentParts() CurrentParts { // ---------- Tests --------------------- test instant { - const inst = try instant(); + const io = std.testing.io; + const inst = try instant(io); defer inst.deinit(); try std.testing.expect(inst.epochNanoseconds() > 0); } test plainDateISO { - const date = try plainDateISO(); + const io = std.testing.io; + const date = try plainDateISO(io); try std.testing.expect(date.year() >= 1970); try std.testing.expect(date.month() >= 1 and date.month() <= 12); try std.testing.expect(date.day() >= 1 and date.day() <= 31); } test plainDateTimeISO { - const dt = try plainDateTimeISO(); + const io = std.testing.io; + const dt = try plainDateTimeISO(io); try std.testing.expect(dt.year() >= 1970); try std.testing.expect(dt.month() >= 1 and dt.month() <= 12); try std.testing.expect(dt.day() >= 1 and dt.day() <= 31); @@ -154,7 +157,8 @@ test plainDateTimeISO { try std.testing.expect(dt.minute() < 60); } test plainTimeISO { - const t = try plainTimeISO(); + const io = std.testing.io; + const t = try plainTimeISO(io); try std.testing.expect(t.hour() < 24); try std.testing.expect(t.minute() < 60); try std.testing.expect(t.second() < 60); diff --git a/test/runner.zig b/test/runner.zig index 1d3c333..e8af9f1 100644 --- a/test/runner.zig +++ b/test/runner.zig @@ -25,16 +25,16 @@ const BORDER = "=" ** 80; // use in custom panic handler var current_test: ?[]const u8 = null; -pub fn main() !void { - var mem: [8192]u8 = undefined; +pub fn main(init: std.process.Init.Minimal) !void { + var mem: [64 * 1024]u8 = undefined; var fba = std.heap.FixedBufferAllocator.init(&mem); - const allocator = fba.allocator(); + const io = std.testing.io; - const env = Env.init(allocator); + const env = Env.init(allocator, init.environ); defer env.deinit(allocator); - var slowest = SlowTracker.init(allocator, 15); + var slowest = SlowTracker.init(allocator, io, 15); defer slowest.deinit(); var pass: usize = 0; @@ -151,7 +151,7 @@ pub fn main() !void { Printer.status(.fail, "\n{s}\n\"{s}\" - {s}\n{s}\n", .{ BORDER, friendly_name, @errorName(err), BORDER }); } if (@errorReturnTrace()) |trace| { - std.debug.dumpStackTrace(trace.*); + std.debug.dumpStackTrace(trace); } if (env.fail_first) { break; @@ -222,7 +222,7 @@ pub fn main() !void { Printer.fmt("\n", .{}); try slowest.display(); Printer.fmt("\n", .{}); - std.posix.exit(if (fail == 0) 0 else 1); + std.process.exit(if (fail == 0) 0 else 1); } const Printer = struct { @@ -254,15 +254,16 @@ const SlowTracker = struct { const SlowestQueue = std.PriorityDequeue(TestInfo, void, compareTiming); max: usize, slowest: SlowestQueue, - timer: std.time.Timer, + io: std.Io, + start_ts: std.Io.Timestamp, - fn init(allocator: Allocator, count: u32) SlowTracker { - const timer = std.time.Timer.start() catch @panic("failed to start timer"); + fn init(allocator: Allocator, io: std.Io, count: u32) SlowTracker { var slowest = SlowestQueue.init(allocator, {}); slowest.ensureTotalCapacity(count) catch @panic("OOM"); return .{ .max = count, - .timer = timer, + .io = io, + .start_ts = .zero, .slowest = slowest, }; } @@ -278,12 +279,12 @@ const SlowTracker = struct { } fn startTiming(self: *SlowTracker) void { - self.timer.reset(); + self.start_ts = std.Io.Timestamp.now(self.io, .awake); } fn endTiming(self: *SlowTracker, scope_name: []const u8, test_name: []const u8) u64 { - var timer = self.timer; - const ns = timer.lap(); + const now = std.Io.Timestamp.now(self.io, .awake); + const ns: u64 = @intCast(now.nanoseconds -| self.start_ts.nanoseconds); var slowest = &self.slowest; @@ -335,13 +336,15 @@ const Env = struct { fail_first: bool, filter: ?[]const u8, flaky_retries: u32, + environ: std.process.Environ, - fn init(allocator: Allocator) Env { + fn init(allocator: Allocator, environ: std.process.Environ) Env { return .{ - .verbose = readEnvBool(allocator, "TEST_VERBOSE", true), - .fail_first = readEnvBool(allocator, "TEST_FAIL_FIRST", false), - .filter = readEnv(allocator, "TEST_FILTER"), - .flaky_retries = readEnvInt(allocator, "TEST_FLAKY_RETRIES", 3), + .verbose = readEnvBool(environ, allocator, "TEST_VERBOSE", true), + .fail_first = readEnvBool(environ, allocator, "TEST_FAIL_FIRST", false), + .filter = readEnv(environ, allocator, "TEST_FILTER"), + .flaky_retries = readEnvInt(environ, allocator, "TEST_FLAKY_RETRIES", 3), + .environ = environ, }; } @@ -351,8 +354,8 @@ const Env = struct { } } - fn readEnv(allocator: Allocator, key: []const u8) ?[]const u8 { - const v = std.process.getEnvVarOwned(allocator, key) catch |err| { + fn readEnv(environ: std.process.Environ, allocator: Allocator, key: []const u8) ?[]const u8 { + const v = environ.getAlloc(allocator, key) catch |err| { if (err == error.EnvironmentVariableNotFound) { return null; } @@ -362,14 +365,14 @@ const Env = struct { return v; } - fn readEnvBool(allocator: Allocator, key: []const u8, deflt: bool) bool { - const value = readEnv(allocator, key) orelse return deflt; + fn readEnvBool(environ: std.process.Environ, allocator: Allocator, key: []const u8, deflt: bool) bool { + const value = readEnv(environ, allocator, key) orelse return deflt; defer allocator.free(value); return std.ascii.eqlIgnoreCase(value, "true"); } - fn readEnvInt(allocator: Allocator, key: []const u8, deflt: u32) u32 { - const value = readEnv(allocator, key) orelse return deflt; + fn readEnvInt(environ: std.process.Environ, allocator: Allocator, key: []const u8, deflt: u32) u32 { + const value = readEnv(environ, allocator, key) orelse return deflt; defer allocator.free(value); return std.fmt.parseInt(u32, value, 10) catch deflt; }