Skip to content

Commit ea068d9

Browse files
committed
Add performance.{now,timeOrigin}
Ref: #16
1 parent f03ab48 commit ea068d9

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

quickjs.c

+39
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,8 @@ struct JSContext {
392392
JSValue global_obj; /* global object */
393393
JSValue global_var_obj; /* contains the global let/const definitions */
394394

395+
uint64_t time_origin;
396+
395397
uint64_t random_state;
396398
bf_context_t *bf_ctx; /* points to rt->bf_ctx, shared by all contexts */
397399
/* when the counter reaches zero, JSRutime.interrupt_handler is called */
@@ -1979,6 +1981,9 @@ JSContext *JS_NewContext(JSRuntime *rt)
19791981
JS_AddIntrinsicTypedArrays(ctx);
19801982
JS_AddIntrinsicPromise(ctx);
19811983
JS_AddIntrinsicBigInt(ctx);
1984+
1985+
JS_AddPerformance(ctx);
1986+
19821987
return ctx;
19831988
}
19841989

@@ -50219,3 +50224,37 @@ void JS_AddIntrinsicTypedArrays(JSContext *ctx)
5021950224
JS_AddIntrinsicAtomics(ctx);
5022050225
#endif
5022150226
}
50227+
50228+
/* Performance */
50229+
50230+
static uint64_t js__now_ms(void)
50231+
{
50232+
// TODO(saghul) Windows support.
50233+
struct timespec ts;
50234+
clock_gettime(CLOCK_MONOTONIC, &ts);
50235+
return (((uint64_t) ts.tv_sec) * 1000 + ts.tv_nsec / 1e6);
50236+
}
50237+
50238+
static JSValue js_perf_now(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv)
50239+
{
50240+
return JS_NewFloat64(ctx, js__now_ms() - ctx->time_origin);
50241+
}
50242+
50243+
static const JSCFunctionListEntry js_perf_proto_funcs[] = {
50244+
JS_CFUNC_DEF2("now", 0, js_perf_now, JS_PROP_ENUMERABLE),
50245+
};
50246+
50247+
void JS_AddPerformance(JSContext *ctx)
50248+
{
50249+
ctx->time_origin = js__now_ms();
50250+
50251+
JSValue performance = JS_NewObject(ctx);
50252+
JS_SetPropertyFunctionList(ctx, performance, js_perf_proto_funcs, countof(js_perf_proto_funcs));
50253+
JS_DefinePropertyValueStr(ctx, performance, "timeOrigin",
50254+
JS_NewFloat64(ctx, ctx->time_origin),
50255+
JS_PROP_ENUMERABLE);
50256+
JS_DefinePropertyValueStr(ctx, ctx->global_obj, "performance",
50257+
JS_DupValue(ctx, performance),
50258+
JS_PROP_ENUMERABLE);
50259+
JS_FreeValue(ctx, performance);
50260+
}

quickjs.h

+2
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,7 @@ void JS_AddIntrinsicMapSet(JSContext *ctx);
365365
void JS_AddIntrinsicTypedArrays(JSContext *ctx);
366366
void JS_AddIntrinsicPromise(JSContext *ctx);
367367
void JS_AddIntrinsicBigInt(JSContext *ctx);
368+
void JS_AddPerformance(JSContext *ctx);
368369

369370
JSValue js_string_codePointRange(JSContext *ctx, JSValueConst this_val,
370371
int argc, JSValueConst *argv);
@@ -960,6 +961,7 @@ typedef struct JSCFunctionListEntry {
960961

961962
/* Note: c++ does not like nested designators */
962963
#define JS_CFUNC_DEF(name, length, func1) { name, JS_PROP_WRITABLE | JS_PROP_CONFIGURABLE, JS_DEF_CFUNC, 0, .u = { .func = { length, JS_CFUNC_generic, { .generic = func1 } } } }
964+
#define JS_CFUNC_DEF2(name, length, func1, prop_flags) { name, prop_flags, JS_DEF_CFUNC, 0, .u = { .func = { length, JS_CFUNC_generic, { .generic = func1 } } } }
963965
#define JS_CFUNC_MAGIC_DEF(name, length, func1, magic) { name, JS_PROP_WRITABLE | JS_PROP_CONFIGURABLE, JS_DEF_CFUNC, magic, .u = { .func = { length, JS_CFUNC_generic_magic, { .generic_magic = func1 } } } }
964966
#define JS_CFUNC_SPECIAL_DEF(name, length, cproto, func1) { name, JS_PROP_WRITABLE | JS_PROP_CONFIGURABLE, JS_DEF_CFUNC, 0, .u = { .func = { length, JS_CFUNC_ ## cproto, { .cproto = func1 } } } }
965967
#define JS_ITERATOR_NEXT_DEF(name, length, func1, magic) { name, JS_PROP_WRITABLE | JS_PROP_CONFIGURABLE, JS_DEF_CFUNC, magic, .u = { .func = { length, JS_CFUNC_iterator_next, { .iterator_next = func1 } } } }

0 commit comments

Comments
 (0)