added os.now()

This commit is contained in:
Fabrice Bellard 2023-12-27 19:09:29 +01:00
parent ffe81419ff
commit 2ee6be705f
4 changed files with 32 additions and 44 deletions

View file

@ -769,6 +769,11 @@ write_fd]} or null in case of error.
@item sleep(delay_ms) @item sleep(delay_ms)
Sleep during @code{delay_ms} milliseconds. 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) @item setTimeout(func, delay)
Call the function @code{func} after @code{delay} ms. Return a handle Call the function @code{func} after @code{delay} ms. Return a handle
to the timer. to the timer.

View file

@ -1970,6 +1970,13 @@ static int64_t get_time_ms(void)
clock_gettime(CLOCK_MONOTONIC, &ts); clock_gettime(CLOCK_MONOTONIC, &ts);
return (uint64_t)ts.tv_sec * 1000 + (ts.tv_nsec / 1000000); 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 #else
/* more portable, but does not work if the date is updated */ /* more portable, but does not work if the date is updated */
static int64_t get_time_ms(void) static int64_t get_time_ms(void)
@ -1978,8 +1985,21 @@ static int64_t get_time_ms(void)
gettimeofday(&tv, NULL); gettimeofday(&tv, NULL);
return (int64_t)tv.tv_sec * 1000 + (tv.tv_usec / 1000); 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 #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) static void unlink_timer(JSRuntime *rt, JSOSTimer *th)
{ {
if (th->link.prev) { if (th->link.prev) {
@ -3625,6 +3645,7 @@ static const JSCFunctionListEntry js_os_funcs[] = {
OS_FLAG(SIGTTIN), OS_FLAG(SIGTTIN),
OS_FLAG(SIGTTOU), OS_FLAG(SIGTTOU),
#endif #endif
JS_CFUNC_DEF("now", 0, js_os_now ),
JS_CFUNC_DEF("setTimeout", 2, js_os_setTimeout ), JS_CFUNC_DEF("setTimeout", 2, js_os_setTimeout ),
JS_CFUNC_DEF("clearTimeout", 1, js_os_clearTimeout ), JS_CFUNC_DEF("clearTimeout", 1, js_os_clearTimeout ),
JS_PROP_STRING_DEF("platform", OS_PLATFORM, 0 ), JS_PROP_STRING_DEF("platform", OS_PLATFORM, 0 ),

View file

@ -42791,30 +42791,6 @@ static const JSCFunctionListEntry js_math_obj[] = {
/* Date */ /* 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 /* OS dependent. d = argv[0] is in ms from 1970. Return the difference
between UTC time and local time 'd' in minutes */ between UTC time and local time 'd' in minutes */
static int getTimezoneOffset(int64_t time) { 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("Infinity", 1.0 / 0.0, 0 ),
JS_PROP_DOUBLE_DEF("NaN", NAN, 0 ), JS_PROP_DOUBLE_DEF("NaN", NAN, 0 ),
JS_PROP_UNDEFINED_DEF("undefined", 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 */ /* Date */

View file

@ -23,6 +23,7 @@
* THE SOFTWARE. * THE SOFTWARE.
*/ */
import * as std from "std"; import * as std from "std";
import * as os from "os";
function pad(str, n) { function pad(str, n) {
str += ""; str += "";
@ -93,21 +94,12 @@ function log_line() {
console.log(s); console.log(s);
} }
var clocks_per_sec = 1000000; var clocks_per_sec = 1000;
var max_iterations = 100; var max_iterations = 10;
var clock_threshold = 2000; /* favoring short measuring spans */ var clock_threshold = 100; /* favoring short measuring spans */
var min_n_argument = 1; var min_n_argument = 1;
var get_clock; //var get_clock = Date.now;
var get_clock = os.now;
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;
}
function log_one(text, n, ti) { function log_one(text, n, ti) {
var ref; var ref;