Skip to content

[lua] Implement Sys.time, Sys.getEnv, and Timer.stamp for lua-vanilla mode#12865

Open
jdonaldson wants to merge 1 commit intoHaxeFoundation:developmentfrom
jdonaldson:lua-vanilla-sys-fixes
Open

[lua] Implement Sys.time, Sys.getEnv, and Timer.stamp for lua-vanilla mode#12865
jdonaldson wants to merge 1 commit intoHaxeFoundation:developmentfrom
jdonaldson:lua-vanilla-sys-fixes

Conversation

@jdonaldson
Copy link
Copy Markdown
Member

Summary

  • Sys.time() — use os.time() instead of throwing. Note: integer-second precision only (vs microsecond precision with luv). Open question: should we emit a compiler warning here?
  • Sys.getEnv() — use os.getenv() instead of throwing
  • Timer.stamp() — use os.clock() directly on all Lua targets (subsecond precision, avoids the Sys.time() path entirely)

These are the remaining Haxe-side changes needed for #12843 (hxcoro with -D lua-vanilla). The bit ops part was already resolved by #12599.

Open question

os.time() only returns integer seconds. Should we warn at compile time when Sys.time() is used in lua-vanilla mode, since users might expect subsecond precision? Or is it fine as-is since the docs don't guarantee precision?

Test plan

  • CI passes on all targets
  • Verify Timer.stamp() returns subsecond values on Lua
  • Verify Sys.getEnv() works in lua-vanilla mode

… mode

Sys.time() and Sys.getEnv() were throwing NotImplementedException in
lua-vanilla mode despite having trivial vanilla Lua implementations
via os.time() and os.getenv(). Timer.stamp() now uses os.clock()
directly on all Lua targets for subsecond precision.

Addresses HaxeFoundation#12843 (hxcoro with -D lua-vanilla).
@tobil4sk
Copy link
Copy Markdown
Member

tobil4sk commented Mar 26, 2026

os.time() only returns integer seconds. Should we warn at compile time when Sys.time() is used in lua-vanilla mode, since users might expect subsecond precision? Or is it fine as-is since the docs don't guarantee precision?

I think we should use os.clock for both, unless we specify a behaviour that requires otherwise. That way haxe.Timer.stamp() can continue to call Sys.time().

EDIT: Lua docs say:

The os.clock function returns the number of seconds of CPU time for the program

We already use it for Sys.cpuTime(). So I guess the question is whether it is wrong for Sys.time() to give cpu time.

@jdonaldson
Copy link
Copy Markdown
Member Author

I looked at what every other target does for Sys.time():

Target Underlying call Type Precision
C++ QueryPerformanceCounter / mach_absolute_time / CLOCK_MONOTONIC Monotonic Nanosecond
JVM System.currentTimeMillis() / 1000.0 Wall-clock (epoch) Millisecond
Python time.time() Wall-clock (epoch) Microsecond
PHP microtime(true) Wall-clock (epoch) Microsecond
Neko sys_time native Wall-clock Float
HL sys_time native Wall-clock Float
Lua (luv) luv.Misc.gettimeofday() Wall-clock (epoch) Microsecond
Lua (vanilla) os.time() Wall-clock (epoch) Integer seconds

Every target returns wall-clock time (C++ is the outlier with monotonic). Using os.clock() for Sys.time() would make lua-vanilla the only target returning CPU time — which is semantically different. A sleeping/waiting program would show 0 elapsed time with os.clock(), while all other targets would show the actual wall time elapsed.

The precision is unfortunate, but vanilla Lua simply doesn't have a subsecond wall-clock API without external libraries. os.time() is the correct semantic match — it just has integer-second granularity.

For Timer.stamp() I think os.clock() is fine since it's already used for Sys.cpuTime() and the docs describe it as a monotonic timestamp for benchmarking.

@jdonaldson
Copy link
Copy Markdown
Member Author

I remember not using os.time() for this reason. I think it might be ok for lua-vanilla, maybe with some sort of warning? I wasn't using it for anything critical, so I'm all ears on what the right tradeoff is here.

@skial skial mentioned this pull request Mar 30, 2026
1 task
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants