-
Notifications
You must be signed in to change notification settings - Fork 0
/
binding.c
66 lines (49 loc) · 1.38 KB
/
binding.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include <assert.h>
#include <bare.h>
#include <js.h>
#include <uv.h>
static js_value_t *
bare_hrtime (js_env_t *env, js_callback_info_t *info) {
int err;
js_value_t *argv[2];
size_t argc = 2;
err = js_get_callback_info(env, info, &argc, argv, NULL, NULL);
assert(err == 0);
assert(argc == 2);
uint32_t *next;
err = js_get_typedarray_info(env, argv[0], NULL, (void **) &next, NULL, NULL, NULL);
assert(err == 0);
uint32_t *prev;
err = js_get_typedarray_info(env, argv[1], NULL, (void **) &prev, NULL, NULL, NULL);
assert(err == 0);
uint64_t then = prev[0] * 1e9 + prev[1];
uint64_t now = uv_hrtime() - then;
next[0] = now / ((uint32_t) 1e9);
next[1] = now % ((uint32_t) 1e9);
return argv[0];
}
static js_value_t *
bare_hrtime_bigint (js_env_t *env, js_callback_info_t *info) {
int err;
js_value_t *result;
err = js_create_bigint_uint64(env, uv_hrtime(), &result);
assert(err == 0);
return result;
}
static js_value_t *
bare_hrtime_exports (js_env_t *env, js_value_t *exports) {
int err;
#define V(name, fn) \
{ \
js_value_t *val; \
err = js_create_function(env, name, -1, fn, NULL, &val); \
assert(err == 0); \
err = js_set_named_property(env, exports, name, val); \
assert(err == 0); \
}
V("hrtime", bare_hrtime)
V("hrtimeBigint", bare_hrtime_bigint)
#undef V
return exports;
}
BARE_MODULE(bare_hrtime, bare_hrtime_exports)