@@ -37,10 +37,56 @@ function assert(x, msg) {
3737 if ( ! x ) throw new ERR_ASSERTION ( msg || 'assertion error' ) ;
3838}
3939
40+ function getFastAPIs ( binding ) {
41+ const {
42+ hrtime : _hrtime
43+ } = binding . getFastAPIs ( ) ;
44+
45+ // The 3 entries filled in by the original process.hrtime contains
46+ // the upper/lower 32 bits of the second part of the value,
47+ // and the remaining nanoseconds of the value.
48+ const hrValues = new Uint32Array ( _hrtime . buffer ) ;
49+
50+ function hrtime ( time ) {
51+ _hrtime . hrtime ( ) ;
52+
53+ if ( time !== undefined ) {
54+ if ( ! ArrayIsArray ( time ) ) {
55+ throw new ERR_INVALID_ARG_TYPE ( 'time' , 'Array' , time ) ;
56+ }
57+ if ( time . length !== 2 ) {
58+ throw new ERR_OUT_OF_RANGE ( 'time' , 2 , time . length ) ;
59+ }
60+
61+ const sec = ( hrValues [ 0 ] * 0x100000000 + hrValues [ 1 ] ) - time [ 0 ] ;
62+ const nsec = hrValues [ 2 ] - time [ 1 ] ;
63+ const needsBorrow = nsec < 0 ;
64+ return [ needsBorrow ? sec - 1 : sec , needsBorrow ? nsec + 1e9 : nsec ] ;
65+ }
66+
67+ return [
68+ hrValues [ 0 ] * 0x100000000 + hrValues [ 1 ] ,
69+ hrValues [ 2 ]
70+ ] ;
71+ }
72+
73+ // Use a BigUint64Array in the closure because this is actually a bit
74+ // faster than simply returning a BigInt from C++ in V8 7.1.
75+ const hrBigintValues = new BigUint64Array ( _hrtime . buffer , 0 , 1 ) ;
76+ function hrtimeBigInt ( ) {
77+ _hrtime . hrtimeBigInt ( ) ;
78+ return hrBigintValues [ 0 ] ;
79+ }
80+
81+ return {
82+ hrtime,
83+ hrtimeBigInt,
84+ } ;
85+ }
86+
4087// The execution of this function itself should not cause any side effects.
4188function wrapProcessMethods ( binding ) {
4289 const {
43- hrtime : _hrtime ,
4490 cpuUsage : _cpuUsage ,
4591 memoryUsage : _memoryUsage ,
4692 resourceUsage : _resourceUsage
@@ -109,42 +155,6 @@ function wrapProcessMethods(binding) {
109155 num >= 0 ;
110156 }
111157
112- // The 3 entries filled in by the original process.hrtime contains
113- // the upper/lower 32 bits of the second part of the value,
114- // and the remaining nanoseconds of the value.
115- const hrValues = new Uint32Array ( _hrtime . buffer ) ;
116-
117- function hrtime ( time ) {
118- _hrtime . hrtime ( ) ;
119-
120- if ( time !== undefined ) {
121- if ( ! ArrayIsArray ( time ) ) {
122- throw new ERR_INVALID_ARG_TYPE ( 'time' , 'Array' , time ) ;
123- }
124- if ( time . length !== 2 ) {
125- throw new ERR_OUT_OF_RANGE ( 'time' , 2 , time . length ) ;
126- }
127-
128- const sec = ( hrValues [ 0 ] * 0x100000000 + hrValues [ 1 ] ) - time [ 0 ] ;
129- const nsec = hrValues [ 2 ] - time [ 1 ] ;
130- const needsBorrow = nsec < 0 ;
131- return [ needsBorrow ? sec - 1 : sec , needsBorrow ? nsec + 1e9 : nsec ] ;
132- }
133-
134- return [
135- hrValues [ 0 ] * 0x100000000 + hrValues [ 1 ] ,
136- hrValues [ 2 ]
137- ] ;
138- }
139-
140- // Use a BigUint64Array in the closure because this is actually a bit
141- // faster than simply returning a BigInt from C++ in V8 7.1.
142- const hrBigintValues = new BigUint64Array ( _hrtime . buffer , 0 , 1 ) ;
143- function hrtimeBigInt ( ) {
144- _hrtime . hrtimeBigInt ( ) ;
145- return hrBigintValues [ 0 ] ;
146- }
147-
148158 const memValues = new Float64Array ( 5 ) ;
149159 function memoryUsage ( ) {
150160 _memoryUsage ( memValues ) ;
@@ -225,8 +235,6 @@ function wrapProcessMethods(binding) {
225235
226236 return {
227237 _rawDebug,
228- hrtime,
229- hrtimeBigInt,
230238 cpuUsage,
231239 resourceUsage,
232240 memoryUsage,
@@ -356,6 +364,7 @@ function toggleTraceCategoryState(asyncHooksEnabled) {
356364
357365module . exports = {
358366 toggleTraceCategoryState,
367+ getFastAPIs,
359368 assert,
360369 buildAllowedFlags,
361370 wrapProcessMethods
0 commit comments