@@ -392,6 +392,8 @@ struct JSContext {
392
392
JSValue global_obj; /* global object */
393
393
JSValue global_var_obj; /* contains the global let/const definitions */
394
394
395
+ uint64_t time_origin;
396
+
395
397
uint64_t random_state;
396
398
bf_context_t *bf_ctx; /* points to rt->bf_ctx, shared by all contexts */
397
399
/* when the counter reaches zero, JSRutime.interrupt_handler is called */
@@ -1979,6 +1981,9 @@ JSContext *JS_NewContext(JSRuntime *rt)
1979
1981
JS_AddIntrinsicTypedArrays(ctx);
1980
1982
JS_AddIntrinsicPromise(ctx);
1981
1983
JS_AddIntrinsicBigInt(ctx);
1984
+
1985
+ JS_AddPerformance(ctx);
1986
+
1982
1987
return ctx;
1983
1988
}
1984
1989
@@ -50219,3 +50224,37 @@ void JS_AddIntrinsicTypedArrays(JSContext *ctx)
50219
50224
JS_AddIntrinsicAtomics(ctx);
50220
50225
#endif
50221
50226
}
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
+ }
0 commit comments