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