quickjs-tart

quickjs-based runtime for wallet-core logic
Log | Files | Refs | README | LICENSE

commit 419f7ba21f0be5981238cd2734f6201bc983ec43
parent 643dbdafff969628bb3e66ae124ba9e25472eab1
Author: Fabrice Bellard <fabrice@bellard.org>
Date:   Wed, 27 Dec 2023 19:09:29 +0100

added os.now()

Diffstat:
Mquickjs/doc/quickjs.texi | 5+++++
Mquickjs/quickjs-libc.c | 21+++++++++++++++++++++
Mquickjs/quickjs.c | 30------------------------------
Mquickjs/tests/microbench.js | 20++++++--------------
4 files changed, 32 insertions(+), 44 deletions(-)

diff --git a/quickjs/doc/quickjs.texi b/quickjs/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/quickjs-libc.c b/quickjs/quickjs-libc.c @@ -2082,6 +2082,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) @@ -2090,8 +2097,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) { @@ -4529,6 +4549,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 ), #ifndef NO_HTTP JS_CFUNC_DEF("fetchHttp", 2, js_os_fetchHttp ), diff --git a/quickjs/quickjs.c b/quickjs/quickjs.c @@ -43009,30 +43009,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) { @@ -49117,12 +49093,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/quickjs/tests/microbench.js b/quickjs/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;