bare-hrtime
Advanced tools
+28
-36
@@ -7,34 +7,5 @@ #include <assert.h> | ||
| static js_value_t * | ||
| bare_hrtime (js_env_t *env, js_callback_info_t *info) { | ||
| 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; | ||
@@ -47,11 +18,21 @@ err = js_create_bigint_uint64(env, uv_hrtime(), &result); | ||
| static int64_t | ||
| bare_hrtime_typed(js_value_t *receiver) { | ||
| return uv_hrtime(); | ||
| } | ||
| static js_value_t * | ||
| bare_hrtime_exports (js_env_t *env, js_value_t *exports) { | ||
| bare_hrtime_exports(js_env_t *env, js_value_t *exports) { | ||
| int err; | ||
| #define V(name, fn) \ | ||
| #define V(name, untyped, signature, typed) \ | ||
| { \ | ||
| js_value_t *val; \ | ||
| err = js_create_function(env, name, -1, fn, NULL, &val); \ | ||
| assert(err == 0); \ | ||
| if (signature) { \ | ||
| err = js_create_typed_function(env, name, -1, untyped, signature, typed, NULL, &val); \ | ||
| assert(err == 0); \ | ||
| } else { \ | ||
| err = js_create_function(env, name, -1, untyped, NULL, &val); \ | ||
| assert(err == 0); \ | ||
| } \ | ||
| err = js_set_named_property(env, exports, name, val); \ | ||
@@ -61,4 +42,15 @@ assert(err == 0); \ | ||
| V("hrtime", bare_hrtime) | ||
| V("hrtimeBigint", bare_hrtime_bigint) | ||
| V( | ||
| "hrtime", | ||
| bare_hrtime, | ||
| &((js_callback_signature_t) { | ||
| .version = 0, | ||
| .result = js_biguint64, | ||
| .args_len = 1, | ||
| .args = (int[]) { | ||
| js_object, | ||
| }, | ||
| }), | ||
| bare_hrtime_typed | ||
| ); | ||
| #undef V | ||
@@ -65,0 +57,0 @@ |
+7
-9
| const binding = require('./binding') | ||
| const EMPTY = Uint32Array.of(0, 0) | ||
| module.exports = exports = function hrtime(past) { | ||
| let now = binding.hrtime() | ||
| module.exports = exports = function hrtime(prev = EMPTY) { | ||
| if (prev instanceof Uint32Array === false) prev = Uint32Array.from(prev) | ||
| if (past) now -= BigInt(past[0]) * 1000000000n + BigInt(past[1]) | ||
| if (prev.length !== 2) { | ||
| throw new Error('Previous timestamp must have two components') | ||
| } | ||
| return [Number(now / 1000000000n), Number(now % 1000000000n)] | ||
| } | ||
| return binding.hrtime(new Uint32Array(2), prev) | ||
| exports.bigint = function hrtime() { | ||
| return binding.hrtime() | ||
| } | ||
| exports.bigint = binding.hrtimeBigint |
+1
-1
| { | ||
| "name": "bare-hrtime", | ||
| "version": "2.0.11", | ||
| "version": "2.1.0", | ||
| "description": "High-resolution timers for JavaScript", | ||
@@ -5,0 +5,0 @@ "exports": { |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
462056
-2.68%17
-5.56%