Skip to content

Commit cd034e9

Browse files
Renegade334targos
authored andcommitted
process: fix hrtime fast call signatures
PR-URL: #59600 Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com> Reviewed-By: Anna Henningsen <anna@addaleax.net>
1 parent a722f67 commit cd034e9

File tree

4 files changed

+55
-28
lines changed

4 files changed

+55
-28
lines changed

src/node_process.h

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
#if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
55

6+
#include "node_debug.h"
67
#include "node_snapshotable.h"
78
#include "v8-fast-api-calls.h"
89
#include "v8.h"
@@ -72,23 +73,23 @@ class BindingData : public SnapshotableObject {
7273
SET_SELF_SIZE(BindingData)
7374

7475
static BindingData* FromV8Value(v8::Local<v8::Value> receiver);
75-
static void NumberImpl(BindingData* receiver);
76+
static void HrtimeImpl(BindingData* receiver);
7677

77-
static void FastNumber(v8::Local<v8::Value> unused,
78-
v8::Local<v8::Value> receiver) {
79-
NumberImpl(FromV8Value(receiver));
78+
static void FastHrtime(v8::Local<v8::Value> receiver) {
79+
TRACK_V8_FAST_API_CALL("process.hrtime");
80+
HrtimeImpl(FromV8Value(receiver));
8081
}
8182

82-
static void SlowNumber(const v8::FunctionCallbackInfo<v8::Value>& args);
83+
static void SlowHrtime(const v8::FunctionCallbackInfo<v8::Value>& args);
8384

84-
static void BigIntImpl(BindingData* receiver);
85+
static void HrtimeBigIntImpl(BindingData* receiver);
8586

86-
static void FastBigInt(v8::Local<v8::Value> unused,
87-
v8::Local<v8::Value> receiver) {
88-
BigIntImpl(FromV8Value(receiver));
87+
static void FastHrtimeBigInt(v8::Local<v8::Value> receiver) {
88+
TRACK_V8_FAST_API_CALL("process.hrtimeBigInt");
89+
HrtimeBigIntImpl(FromV8Value(receiver));
8990
}
9091

91-
static void SlowBigInt(const v8::FunctionCallbackInfo<v8::Value>& args);
92+
static void SlowHrtimeBigInt(const v8::FunctionCallbackInfo<v8::Value>& args);
9293

9394
static void LoadEnvFile(const v8::FunctionCallbackInfo<v8::Value>& args);
9495

@@ -101,8 +102,8 @@ class BindingData : public SnapshotableObject {
101102
// These need to be static so that we have their addresses available to
102103
// register as external references in the snapshot at environment creation
103104
// time.
104-
static v8::CFunction fast_number_;
105-
static v8::CFunction fast_bigint_;
105+
static v8::CFunction fast_hrtime_;
106+
static v8::CFunction fast_hrtime_bigint_;
106107
};
107108

108109
} // namespace process

src/node_process_methods.cc

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -652,22 +652,22 @@ BindingData::BindingData(Realm* realm,
652652
hrtime_buffer_.MakeWeak();
653653
}
654654

655-
v8::CFunction BindingData::fast_number_(v8::CFunction::Make(FastNumber));
656-
v8::CFunction BindingData::fast_bigint_(v8::CFunction::Make(FastBigInt));
655+
CFunction BindingData::fast_hrtime_(CFunction::Make(FastHrtime));
656+
CFunction BindingData::fast_hrtime_bigint_(CFunction::Make(FastHrtimeBigInt));
657657

658658
void BindingData::AddMethods(Isolate* isolate, Local<ObjectTemplate> target) {
659659
SetFastMethodNoSideEffect(
660-
isolate, target, "hrtime", SlowNumber, &fast_number_);
660+
isolate, target, "hrtime", SlowHrtime, &fast_hrtime_);
661661
SetFastMethodNoSideEffect(
662-
isolate, target, "hrtimeBigInt", SlowBigInt, &fast_bigint_);
662+
isolate, target, "hrtimeBigInt", SlowHrtimeBigInt, &fast_hrtime_bigint_);
663663
}
664664

665665
void BindingData::RegisterExternalReferences(
666666
ExternalReferenceRegistry* registry) {
667-
registry->Register(SlowNumber);
668-
registry->Register(SlowBigInt);
669-
registry->Register(fast_number_);
670-
registry->Register(fast_bigint_);
667+
registry->Register(SlowHrtime);
668+
registry->Register(SlowHrtimeBigInt);
669+
registry->Register(fast_hrtime_);
670+
registry->Register(fast_hrtime_bigint_);
671671
}
672672

673673
BindingData* BindingData::FromV8Value(Local<Value> value) {
@@ -689,14 +689,14 @@ void BindingData::MemoryInfo(MemoryTracker* tracker) const {
689689
// broken into the upper/lower 32 bits to be converted back in JS,
690690
// because there is no Uint64Array in JS.
691691
// The third entry contains the remaining nanosecond part of the value.
692-
void BindingData::NumberImpl(BindingData* receiver) {
692+
void BindingData::HrtimeImpl(BindingData* receiver) {
693693
uint64_t t = uv_hrtime();
694694
receiver->hrtime_buffer_[0] = (t / NANOS_PER_SEC) >> 32;
695695
receiver->hrtime_buffer_[1] = (t / NANOS_PER_SEC) & 0xffffffff;
696696
receiver->hrtime_buffer_[2] = t % NANOS_PER_SEC;
697697
}
698698

699-
void BindingData::BigIntImpl(BindingData* receiver) {
699+
void BindingData::HrtimeBigIntImpl(BindingData* receiver) {
700700
uint64_t t = uv_hrtime();
701701
// The buffer is a Uint32Array, so we need to reinterpret it as a
702702
// Uint64Array to write the value. The buffer is valid at this scope so we
@@ -706,12 +706,12 @@ void BindingData::BigIntImpl(BindingData* receiver) {
706706
fields[0] = t;
707707
}
708708

709-
void BindingData::SlowBigInt(const FunctionCallbackInfo<Value>& args) {
710-
BigIntImpl(FromJSObject<BindingData>(args.This()));
709+
void BindingData::SlowHrtimeBigInt(const FunctionCallbackInfo<Value>& args) {
710+
HrtimeBigIntImpl(FromJSObject<BindingData>(args.This()));
711711
}
712712

713-
void BindingData::SlowNumber(const v8::FunctionCallbackInfo<v8::Value>& args) {
714-
NumberImpl(FromJSObject<BindingData>(args.This()));
713+
void BindingData::SlowHrtime(const FunctionCallbackInfo<Value>& args) {
714+
HrtimeImpl(FromJSObject<BindingData>(args.This()));
715715
}
716716

717717
bool BindingData::PrepareForSerialization(Local<Context> context,
Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,27 @@
1+
// Flags: --allow-natives-syntax --expose-internals --no-warnings
12
'use strict';
23

34
// Tests that process.hrtime.bigint() works.
45

5-
require('../common');
6+
const common = require('../common');
67
const assert = require('assert');
78

9+
const { internalBinding } = require('internal/test/binding');
10+
811
const start = process.hrtime.bigint();
912
assert.strictEqual(typeof start, 'bigint');
1013

1114
const end = process.hrtime.bigint();
1215
assert.strictEqual(typeof end, 'bigint');
1316

1417
assert(end - start >= 0n);
18+
19+
eval('%PrepareFunctionForOptimization(process.hrtime.bigint)');
20+
assert(process.hrtime.bigint());
21+
eval('%OptimizeFunctionOnNextCall(process.hrtime.bigint)');
22+
assert(process.hrtime.bigint());
23+
24+
if (common.isDebug) {
25+
const { getV8FastApiCallCount } = internalBinding('debug');
26+
assert.strictEqual(getV8FastApiCallCount('process.hrtimeBigInt'), 1);
27+
}

test/parallel/test-process-hrtime.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,13 @@
1919
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
2020
// USE OR OTHER DEALINGS IN THE SOFTWARE.
2121

22+
// Flags: --allow-natives-syntax --expose-internals --no-warnings
2223
'use strict';
23-
require('../common');
24+
const common = require('../common');
2425
const assert = require('assert');
2526

27+
const { internalBinding } = require('internal/test/binding');
28+
2629
// The default behavior, return an Array "tuple" of numbers
2730
const tuple = process.hrtime();
2831

@@ -72,3 +75,13 @@ function validateTuple(tuple) {
7275

7376
const diff = process.hrtime([0, 1e9 - 1]);
7477
assert(diff[1] >= 0); // https://github.com/nodejs/node/issues/4751
78+
79+
eval('%PrepareFunctionForOptimization(process.hrtime)');
80+
assert(process.hrtime());
81+
eval('%OptimizeFunctionOnNextCall(process.hrtime)');
82+
assert(process.hrtime());
83+
84+
if (common.isDebug) {
85+
const { getV8FastApiCallCount } = internalBinding('debug');
86+
assert.strictEqual(getV8FastApiCallCount('process.hrtime'), 1);
87+
}

0 commit comments

Comments
 (0)