MIRROR: javascript for 馃悳's, a tiny runtime with big ambitions
1#include <stdio.h>
2#include <stdlib.h>
3#include <time.h>
4#ifdef _WIN32
5#define WIN32_LEAN_AND_MEAN
6#include <windows.h>
7#else
8#include <sys/time.h>
9#endif
10
11#include "ant.h"
12#include "runtime.h"
13#include "modules/symbol.h"
14#include "modules/performance.h"
15
16static double time_origin_ms = 0;
17
18static double get_current_time_ms(void) {
19#ifdef _WIN32
20 LARGE_INTEGER freq, count;
21 QueryPerformanceFrequency(&freq);
22 QueryPerformanceCounter(&count);
23 return (double)count.QuadPart * 1000.0 / (double)freq.QuadPart;
24#else
25 struct timeval tv;
26 gettimeofday(&tv, NULL);
27 return (double)tv.tv_sec * 1000.0 + (double)tv.tv_usec / 1000.0;
28#endif
29}
30
31// performance.now()
32static ant_value_t js_performance_now(ant_t *js, ant_value_t *args, int nargs) {
33 (void) args; (void) nargs;
34 double now = get_current_time_ms() - time_origin_ms;
35 return js_mknum(now);
36}
37
38// performance.timeOrigin
39static ant_value_t js_performance_time_origin(ant_t *js, ant_value_t *args, int nargs) {
40 (void) args; (void) nargs;
41 return js_mknum(time_origin_ms);
42}
43
44ant_value_t perf_hooks_library(ant_t *js) {
45 ant_value_t lib = js_mkobj(js);
46 js_set(js, lib, "performance", js_get(js, js_glob(js), "performance"));
47 js_set_sym(js, lib, get_toStringTag_sym(), ANT_STRING("perf_hooks"));
48 return lib;
49}
50
51void init_performance_module() {
52 ant_t *js = rt->js;
53
54 ant_value_t glob = js_glob(js);
55 ant_value_t perf_obj = js_mkobj(js);
56
57 time_origin_ms = get_current_time_ms();
58
59 js_set(js, perf_obj, "now", js_mkfun(js_performance_now));
60 js_set(js, perf_obj, "timeOrigin", js_mknum(time_origin_ms));
61
62 js_set_sym(js, perf_obj, get_toStringTag_sym(), ANT_STRING("Performance"));
63 js_set(js, glob, "performance", perf_obj);
64}