diff --git a/classlib/src/main/java/org/teavm/classlib/java/lang/TSystem.java b/classlib/src/main/java/org/teavm/classlib/java/lang/TSystem.java index 820140a30..beb54c301 100644 --- a/classlib/src/main/java/org/teavm/classlib/java/lang/TSystem.java +++ b/classlib/src/main/java/org/teavm/classlib/java/lang/TSystem.java @@ -28,6 +28,7 @@ import org.teavm.interop.DelegateTo; import org.teavm.interop.Import; import org.teavm.interop.NoSideEffects; import org.teavm.interop.Unmanaged; +import org.teavm.jso.browser.Performance; import org.teavm.runtime.Allocator; import org.teavm.runtime.GC; import org.teavm.runtime.RuntimeArray; @@ -222,9 +223,16 @@ public final class TSystem extends TObject { } public static long nanoTime() { - return currentTimeMillis() * 1000000; + if (PlatformDetector.isLowLevel()) { + return nanoTimeLowLevel(); + } else { + return (long) (Performance.now() * 1000000); + } } + @Import(name = "currentTimeNano") + private static native long nanoTimeLowLevel(); + public static int identityHashCode(Object x) { return ((TObject) x).identity(); } diff --git a/core/src/main/resources/org/teavm/backend/c/runtime.c b/core/src/main/resources/org/teavm/backend/c/runtime.c index db0e9606e..a4b9295d5 100644 --- a/core/src/main/resources/org/teavm/backend/c/runtime.c +++ b/core/src/main/resources/org/teavm/backend/c/runtime.c @@ -232,7 +232,13 @@ static int64_t currentTimeMillis() { struct timespec time; clock_gettime(CLOCK_REALTIME, &time); - return time.tv_sec * 1000 + (int64_t) round(time.tv_nsec / 1000000); + return time.tv_sec * INT64_C(1000) + (int64_t) round(time.tv_nsec / 1000000); +} +static int64_t currentTimeNano() { + struct timespec time; + clock_gettime(CLOCK_REALTIME, &time); + + return time.tv_sec * INT64_C(1000000000) + (int64_t) round(time.tv_nsec); } #endif