summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFabrice Bellard <fabrice@bellard.org>2023-12-27 19:09:29 +0100
committerFabrice Bellard <fabrice@bellard.org>2023-12-27 19:09:29 +0100
commit2ee6be705fde0eb68acec25915d2947de1207abb (patch)
tree05bb2acdbf538dad3603fc3bd1bbed377ac1fd50
parentffe81419fff5798b1983e30702c66d9ceb1f8a7a (diff)
downloadquickjs-2ee6be705fde0eb68acec25915d2947de1207abb.tar.gz
quickjs-2ee6be705fde0eb68acec25915d2947de1207abb.zip
added os.now()
-rw-r--r--doc/quickjs.texi5
-rw-r--r--quickjs-libc.c21
-rw-r--r--quickjs.c30
-rw-r--r--tests/microbench.js20
4 files changed, 32 insertions, 44 deletions
diff --git a/doc/quickjs.texi b/doc/quickjs.texi
index 9eb6354..5731f04 100644
--- a/doc/quickjs.texi
+++ b/doc/quickjs.texi
@@ -769,6 +769,11 @@ write_fd]} or null in case of error.
@item sleep(delay_ms)
Sleep during @code{delay_ms} milliseconds.
+@item now()
+Return a timestamp in milliseconds with more precision than
+@code{Date.now()}. The time origin is unspecified and is normally not
+impacted by system clock adjustments.
+
@item setTimeout(func, delay)
Call the function @code{func} after @code{delay} ms. Return a handle
to the timer.
diff --git a/quickjs-libc.c b/quickjs-libc.c
index f916314..d99bbf4 100644
--- a/quickjs-libc.c
+++ b/quickjs-libc.c
@@ -1970,6 +1970,13 @@ static int64_t get_time_ms(void)
clock_gettime(CLOCK_MONOTONIC, &ts);
return (uint64_t)ts.tv_sec * 1000 + (ts.tv_nsec / 1000000);
}
+
+static int64_t get_time_ns(void)
+{
+ struct timespec ts;
+ clock_gettime(CLOCK_MONOTONIC, &ts);
+ return (uint64_t)ts.tv_sec * 1000000000 + ts.tv_nsec;
+}
#else
/* more portable, but does not work if the date is updated */
static int64_t get_time_ms(void)
@@ -1978,8 +1985,21 @@ static int64_t get_time_ms(void)
gettimeofday(&tv, NULL);
return (int64_t)tv.tv_sec * 1000 + (tv.tv_usec / 1000);
}
+
+static int64_t get_time_ns(void)
+{
+ struct timeval tv;
+ gettimeofday(&tv, NULL);
+ return (int64_t)tv.tv_sec * 1000000000 + (tv.tv_usec * 1000);
+}
#endif
+static JSValue js_os_now(JSContext *ctx, JSValue this_val,
+ int argc, JSValue *argv)
+{
+ return JS_NewFloat64(ctx, (double)get_time_ns() / 1e6);
+}
+
static void unlink_timer(JSRuntime *rt, JSOSTimer *th)
{
if (th->link.prev) {
@@ -3625,6 +3645,7 @@ static const JSCFunctionListEntry js_os_funcs[] = {
OS_FLAG(SIGTTIN),
OS_FLAG(SIGTTOU),
#endif
+ JS_CFUNC_DEF("now", 0, js_os_now ),
JS_CFUNC_DEF("setTimeout", 2, js_os_setTimeout ),
JS_CFUNC_DEF("clearTimeout", 1, js_os_clearTimeout ),
JS_PROP_STRING_DEF("platform", OS_PLATFORM, 0 ),
diff --git a/quickjs.c b/quickjs.c
index a95340d..719fde1 100644
--- a/quickjs.c
+++ b/quickjs.c
@@ -42791,30 +42791,6 @@ static const JSCFunctionListEntry js_math_obj[] = {
/* Date */
-#if 0
-/* OS dependent: return the UTC time in ms since 1970. */
-static JSValue js___date_now(JSContext *ctx, JSValueConst this_val,
- int argc, JSValueConst *argv)
-{
- int64_t d;
- struct timeval tv;
- gettimeofday(&tv, NULL);
- d = (int64_t)tv.tv_sec * 1000 + (tv.tv_usec / 1000);
- return JS_NewInt64(ctx, d);
-}
-#endif
-
-/* OS dependent: return the UTC time in microseconds since 1970. */
-static JSValue js___date_clock(JSContext *ctx, JSValueConst this_val,
- int argc, JSValueConst *argv)
-{
- int64_t d;
- struct timeval tv;
- gettimeofday(&tv, NULL);
- d = (int64_t)tv.tv_sec * 1000000 + tv.tv_usec;
- return JS_NewInt64(ctx, d);
-}
-
/* OS dependent. d = argv[0] is in ms from 1970. Return the difference
between UTC time and local time 'd' in minutes */
static int getTimezoneOffset(int64_t time) {
@@ -48899,12 +48875,6 @@ static const JSCFunctionListEntry js_global_funcs[] = {
JS_PROP_DOUBLE_DEF("Infinity", 1.0 / 0.0, 0 ),
JS_PROP_DOUBLE_DEF("NaN", NAN, 0 ),
JS_PROP_UNDEFINED_DEF("undefined", 0 ),
-
- /* for the 'Date' implementation */
- JS_CFUNC_DEF("__date_clock", 0, js___date_clock ),
- //JS_CFUNC_DEF("__date_now", 0, js___date_now ),
- //JS_CFUNC_DEF("__date_getTimezoneOffset", 1, js___date_getTimezoneOffset ),
- //JS_CFUNC_DEF("__date_create", 3, js___date_create ),
};
/* Date */
diff --git a/tests/microbench.js b/tests/microbench.js
index 1c5f52d..c1b57bb 100644
--- a/tests/microbench.js
+++ b/tests/microbench.js
@@ -23,6 +23,7 @@
* THE SOFTWARE.
*/
import * as std from "std";
+import * as os from "os";
function pad(str, n) {
str += "";
@@ -93,21 +94,12 @@ function log_line() {
console.log(s);
}
-var clocks_per_sec = 1000000;
-var max_iterations = 100;
-var clock_threshold = 2000; /* favoring short measuring spans */
+var clocks_per_sec = 1000;
+var max_iterations = 10;
+var clock_threshold = 100; /* favoring short measuring spans */
var min_n_argument = 1;
-var get_clock;
-
-if (typeof globalThis.__date_clock != "function") {
- console.log("using fallback millisecond clock");
- clocks_per_sec = 1000;
- max_iterations = 10;
- clock_threshold = 100;
- get_clock = Date.now;
-} else {
- get_clock = globalThis.__date_clock;
-}
+//var get_clock = Date.now;
+var get_clock = os.now;
function log_one(text, n, ti) {
var ref;