Skip to content

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

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

[lua] Implement Sys.getEnv and Timer.stamp for lua-vanilla mode#12865
jdonaldson wants to merge 1 commit into
HaxeFoundation: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

@tobil4sk

tobil4sk commented Mar 26, 2026

Copy link
Copy Markdown
Member

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
@jdonaldson jdonaldson force-pushed the lua-vanilla-sys-fixes branch from 63c33e3 to 95e342b Compare May 7, 2026 00:11
@jdonaldson

Copy link
Copy Markdown
Member Author

Friendly ping on this one — wanted to check whether my Mar 27 reply changed your thinking on os.time() vs os.clock().

To recap the tradeoff: os.clock() returns CPU time (a sleeping program shows 0 elapsed), so using it for Sys.time() would make lua-vanilla the only target whose Sys.time() doesn't track wall-clock. os.time() matches every other target's semantics, just at integer-second precision.

Three options I see, happy to go with whichever you prefer:

  1. os.time() as-is — correct semantics, integer precision, no warning.
  2. os.time() + compile-time warning when Sys.time() is used in lua-vanilla mode, so users aren't surprised by the precision drop.
  3. Document the precision caveat in the API docs only, no warning.

Just rebased onto development to clear any merge-ref staleness.

@jdonaldson jdonaldson force-pushed the lua-vanilla-sys-fixes branch from 95e342b to 701cd14 Compare July 4, 2026 22:22
Sys.getEnv() was throwing NotImplementedException in lua-vanilla mode
despite having a trivial vanilla Lua implementation via os.getenv().
Timer.stamp() now uses os.clock() directly on all Lua targets for
subsecond precision.

Sys.time() is intentionally left as notImplemented() in lua-vanilla
mode: vanilla Lua's os.time() has only second-level precision, which
would silently disagree with the fractional-second wall-clock time
returned by Sys.time() on every other target. Subsecond wall-clock
time requires luv (non-vanilla mode).

Addresses HaxeFoundation#12843 (hxcoro with -D lua-vanilla).
@jdonaldson jdonaldson force-pushed the lua-vanilla-sys-fixes branch from 701cd14 to 7f7bcd7 Compare July 4, 2026 22:31
@jdonaldson

Copy link
Copy Markdown
Member Author

Dropped the Sys.time() implementation from this PR. Vanilla Lua's os.time() only has second-level precision, so it would silently disagree with the fractional-second wall-clock time Sys.time() returns on every other target — more confusing than helpful. I've left it as notImplemented() in lua-vanilla mode; subsecond wall-clock time genuinely requires luv (gettimeofday), which is what non-vanilla mode already uses.

The PR now just implements Sys.getEnv() (via os.getenv()) and Timer.stamp() (via os.clock()), both of which have exact vanilla equivalents. Force-pushed and rebased onto latest development.

@jdonaldson jdonaldson changed the title [lua] Implement Sys.time, Sys.getEnv, and Timer.stamp for lua-vanilla mode [lua] Implement Sys.getEnv and Timer.stamp for lua-vanilla mode Jul 4, 2026
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