Skip to content

Commit

Permalink
Add performance.{now,timeOrigin}
Browse files Browse the repository at this point in the history
Ref: #16
  • Loading branch information
saghul committed Nov 18, 2023
1 parent f03ab48 commit ea068d9
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
39 changes: 39 additions & 0 deletions quickjs.c
Expand Up @@ -392,6 +392,8 @@ struct JSContext {
JSValue global_obj; /* global object */
JSValue global_var_obj; /* contains the global let/const definitions */

uint64_t time_origin;

uint64_t random_state;
bf_context_t *bf_ctx; /* points to rt->bf_ctx, shared by all contexts */
/* when the counter reaches zero, JSRutime.interrupt_handler is called */
Expand Down Expand Up @@ -1979,6 +1981,9 @@ JSContext *JS_NewContext(JSRuntime *rt)
JS_AddIntrinsicTypedArrays(ctx);
JS_AddIntrinsicPromise(ctx);
JS_AddIntrinsicBigInt(ctx);

JS_AddPerformance(ctx);

return ctx;
}

Expand Down Expand Up @@ -50219,3 +50224,37 @@ void JS_AddIntrinsicTypedArrays(JSContext *ctx)
JS_AddIntrinsicAtomics(ctx);
#endif
}

/* Performance */

static uint64_t js__now_ms(void)
{
// TODO(saghul) Windows support.
struct timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts);
return (((uint64_t) ts.tv_sec) * 1000 + ts.tv_nsec / 1e6);
}

static JSValue js_perf_now(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv)
{
return JS_NewFloat64(ctx, js__now_ms() - ctx->time_origin);
}

static const JSCFunctionListEntry js_perf_proto_funcs[] = {
JS_CFUNC_DEF2("now", 0, js_perf_now, JS_PROP_ENUMERABLE),
};

void JS_AddPerformance(JSContext *ctx)
{
ctx->time_origin = js__now_ms();

JSValue performance = JS_NewObject(ctx);
JS_SetPropertyFunctionList(ctx, performance, js_perf_proto_funcs, countof(js_perf_proto_funcs));
JS_DefinePropertyValueStr(ctx, performance, "timeOrigin",
JS_NewFloat64(ctx, ctx->time_origin),
JS_PROP_ENUMERABLE);
JS_DefinePropertyValueStr(ctx, ctx->global_obj, "performance",
JS_DupValue(ctx, performance),
JS_PROP_ENUMERABLE);
JS_FreeValue(ctx, performance);
}
2 changes: 2 additions & 0 deletions quickjs.h
Expand Up @@ -365,6 +365,7 @@ void JS_AddIntrinsicMapSet(JSContext *ctx);
void JS_AddIntrinsicTypedArrays(JSContext *ctx);
void JS_AddIntrinsicPromise(JSContext *ctx);
void JS_AddIntrinsicBigInt(JSContext *ctx);
void JS_AddPerformance(JSContext *ctx);

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

/* Note: c++ does not like nested designators */
#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 } } } }
#define JS_CFUNC_DEF2(name, length, func1, prop_flags) { name, prop_flags, JS_DEF_CFUNC, 0, .u = { .func = { length, JS_CFUNC_generic, { .generic = func1 } } } }
#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 } } } }
#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 } } } }
#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 } } } }
Expand Down

0 comments on commit ea068d9

Please sign in to comment.