diff --git a/lib/lua/vm/stdlib/os.ex b/lib/lua/vm/stdlib/os.ex index c5ca5961..d77e04d3 100644 --- a/lib/lua/vm/stdlib/os.ex +++ b/lib/lua/vm/stdlib/os.ex @@ -10,6 +10,8 @@ defmodule Lua.VM.Stdlib.Os do ## Functions - `os.time([table])` - Epoch seconds, optionally from a date table. + - `os.time_ms()` - Current epoch in milliseconds (extension; not in PUC-Lua). + - `os.time_us()` - Current epoch in microseconds (extension; not in PUC-Lua). - `os.clock()` - Approximate CPU time used, in seconds. - `os.difftime(t2, t1)` - Difference in seconds between two times. - `os.date([format [, time]])` - Formats a time as a string or table. @@ -31,6 +33,10 @@ defmodule Lua.VM.Stdlib.Os do @impl true def install(state) do + # Seed the monotonic origin at install time so os.clock() measures from a + # stable startup point rather than from whenever it is first called. + boot_offset() + os_table = %{ "clock" => {:native_func, &os_clock/2}, "date" => {:native_func, &os_date/2}, @@ -39,6 +45,8 @@ defmodule Lua.VM.Stdlib.Os do "getenv" => {:native_func, &os_getenv/2}, "setlocale" => {:native_func, &os_setlocale/2}, "time" => {:native_func, &os_time/2}, + "time_ms" => {:native_func, &os_time_ms/2}, + "time_us" => {:native_func, &os_time_us/2}, "tmpname" => {:native_func, &os_tmpname/2} } @@ -110,6 +118,18 @@ defmodule Lua.VM.Stdlib.Os do raise ArgumentError.type_error("os.time", 1, "table", Util.typeof(arg)) end + # os.time_ms() — current epoch in milliseconds. Non-standard extension (not + # present in PUC-Lua); use os.time() for portable, whole-second time. + defp os_time_ms(_args, state) do + {[System.os_time(:millisecond)], state} + end + + # os.time_us() — current epoch in microseconds. Non-standard extension (not + # present in PUC-Lua); use os.time() for portable, whole-second time. + defp os_time_us(_args, state) do + {[System.os_time(:microsecond)], state} + end + # os.date([format [, time]]) — formats a time. defp os_date([], state), do: os_date(["%c"], state) defp os_date([nil | rest], state), do: os_date(["%c" | rest], state) @@ -266,8 +286,10 @@ defmodule Lua.VM.Stdlib.Os do defp pad2(n), do: n |> Integer.to_string() |> String.pad_leading(2, "0") defp pad3(n), do: n |> Integer.to_string() |> String.pad_leading(3, "0") - # Monotonic clock origin captured at module load so os.clock() reports - # elapsed seconds from a stable reference. + # Monotonic clock origin so os.clock() reports elapsed seconds from a stable + # reference. Seeded at library install (see install/1); the value lives in + # :persistent_term, which is BEAM-global, so the first VM to install wins and + # all later installs reuse that origin. defp boot_offset do case :persistent_term.get({__MODULE__, :boot}, nil) do nil -> diff --git a/test/lua/vm/stdlib/os_test.exs b/test/lua/vm/stdlib/os_test.exs index 0b650708..56a46410 100644 --- a/test/lua/vm/stdlib/os_test.exs +++ b/test/lua/vm/stdlib/os_test.exs @@ -25,6 +25,24 @@ defmodule Lua.VM.Stdlib.OsTest do assert t == 946_684_800 end + test "os.time_ms returns the current epoch in milliseconds" do + {[t], _} = Lua.eval!("return os.time_ms()") + assert is_integer(t) + assert t > 1_500_000_000_000 + end + + test "os.time_us returns the current epoch in microseconds" do + {[t], _} = Lua.eval!("return os.time_us()") + assert is_integer(t) + assert t > 1_500_000_000_000_000 + end + + test "os.time_ms and os.time_us share the same magnitude as os.time" do + {[secs, ms, us], _} = Lua.eval!("return os.time(), os.time_ms(), os.time_us()") + assert div(ms, 1000) in (secs - 2)..(secs + 2) + assert div(us, 1_000_000) in (secs - 2)..(secs + 2) + end + test "os.difftime returns the difference in seconds" do {[d], _} = Lua.eval!("return os.difftime(10, 3)") assert d == 7.0