diff --git a/client/space_lua/stdlib/os.ts b/client/space_lua/stdlib/os.ts index 2649a606..9af8f75b 100644 --- a/client/space_lua/stdlib/os.ts +++ b/client/space_lua/stdlib/os.ts @@ -365,4 +365,9 @@ export const osApi = new LuaTable({ return luaFormatTime(fmt, d, utc); }, ), + + // Returns an approximation of CPU time used by the program in seconds. + clock: new LuaBuiltinFunction((_sf): number => { + return performance.now() / 1000.0; + }), }); diff --git a/client/space_lua/stdlib/os_test.lua b/client/space_lua/stdlib/os_test.lua index 6d731b49..2fd48911 100644 --- a/client/space_lua/stdlib/os_test.lua +++ b/client/space_lua/stdlib/os_test.lua @@ -168,3 +168,15 @@ assert(type(err) == "string") -- Repeated specifiers assertEquals(os.date("!%Y%Y", ts), "20062006") + +-- os.clock +-- Returns a number (seconds) +assert(type(os.clock()) == "number") + +-- Must be non-negative +assert(os.clock() >= 0) + +-- Monotonically non-decreasing +local c1 = os.clock() +local c2 = os.clock() +assert(c2 >= c1)