diff --git a/src/node_process.h b/src/node_process.h index b88a2f99483..64393302d2c 100644 --- a/src/node_process.h +++ b/src/node_process.h @@ -3,6 +3,7 @@ #if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS +#include "node_debug.h" #include "node_snapshotable.h" #include "v8-fast-api-calls.h" #include "v8.h" @@ -72,23 +73,23 @@ class BindingData : public SnapshotableObject { SET_SELF_SIZE(BindingData) static BindingData* FromV8Value(v8::Local receiver); - static void NumberImpl(BindingData* receiver); + static void HrtimeImpl(BindingData* receiver); - static void FastNumber(v8::Local unused, - v8::Local receiver) { - NumberImpl(FromV8Value(receiver)); + static void FastHrtime(v8::Local receiver) { + TRACK_V8_FAST_API_CALL("process.hrtime"); + HrtimeImpl(FromV8Value(receiver)); } - static void SlowNumber(const v8::FunctionCallbackInfo& args); + static void SlowHrtime(const v8::FunctionCallbackInfo& args); - static void BigIntImpl(BindingData* receiver); + static void HrtimeBigIntImpl(BindingData* receiver); - static void FastBigInt(v8::Local unused, - v8::Local receiver) { - BigIntImpl(FromV8Value(receiver)); + static void FastHrtimeBigInt(v8::Local receiver) { + TRACK_V8_FAST_API_CALL("process.hrtimeBigInt"); + HrtimeBigIntImpl(FromV8Value(receiver)); } - static void SlowBigInt(const v8::FunctionCallbackInfo& args); + static void SlowHrtimeBigInt(const v8::FunctionCallbackInfo& args); static void LoadEnvFile(const v8::FunctionCallbackInfo& args); @@ -101,8 +102,8 @@ class BindingData : public SnapshotableObject { // These need to be static so that we have their addresses available to // register as external references in the snapshot at environment creation // time. - static v8::CFunction fast_number_; - static v8::CFunction fast_bigint_; + static v8::CFunction fast_hrtime_; + static v8::CFunction fast_hrtime_bigint_; }; } // namespace process diff --git a/src/node_process_methods.cc b/src/node_process_methods.cc index 51bfc633625..606425dac83 100644 --- a/src/node_process_methods.cc +++ b/src/node_process_methods.cc @@ -652,22 +652,22 @@ BindingData::BindingData(Realm* realm, hrtime_buffer_.MakeWeak(); } -v8::CFunction BindingData::fast_number_(v8::CFunction::Make(FastNumber)); -v8::CFunction BindingData::fast_bigint_(v8::CFunction::Make(FastBigInt)); +CFunction BindingData::fast_hrtime_(CFunction::Make(FastHrtime)); +CFunction BindingData::fast_hrtime_bigint_(CFunction::Make(FastHrtimeBigInt)); void BindingData::AddMethods(Isolate* isolate, Local target) { SetFastMethodNoSideEffect( - isolate, target, "hrtime", SlowNumber, &fast_number_); + isolate, target, "hrtime", SlowHrtime, &fast_hrtime_); SetFastMethodNoSideEffect( - isolate, target, "hrtimeBigInt", SlowBigInt, &fast_bigint_); + isolate, target, "hrtimeBigInt", SlowHrtimeBigInt, &fast_hrtime_bigint_); } void BindingData::RegisterExternalReferences( ExternalReferenceRegistry* registry) { - registry->Register(SlowNumber); - registry->Register(SlowBigInt); - registry->Register(fast_number_); - registry->Register(fast_bigint_); + registry->Register(SlowHrtime); + registry->Register(SlowHrtimeBigInt); + registry->Register(fast_hrtime_); + registry->Register(fast_hrtime_bigint_); } BindingData* BindingData::FromV8Value(Local value) { @@ -689,14 +689,14 @@ void BindingData::MemoryInfo(MemoryTracker* tracker) const { // broken into the upper/lower 32 bits to be converted back in JS, // because there is no Uint64Array in JS. // The third entry contains the remaining nanosecond part of the value. -void BindingData::NumberImpl(BindingData* receiver) { +void BindingData::HrtimeImpl(BindingData* receiver) { uint64_t t = uv_hrtime(); receiver->hrtime_buffer_[0] = (t / NANOS_PER_SEC) >> 32; receiver->hrtime_buffer_[1] = (t / NANOS_PER_SEC) & 0xffffffff; receiver->hrtime_buffer_[2] = t % NANOS_PER_SEC; } -void BindingData::BigIntImpl(BindingData* receiver) { +void BindingData::HrtimeBigIntImpl(BindingData* receiver) { uint64_t t = uv_hrtime(); // The buffer is a Uint32Array, so we need to reinterpret it as a // Uint64Array to write the value. The buffer is valid at this scope so we @@ -706,12 +706,12 @@ void BindingData::BigIntImpl(BindingData* receiver) { fields[0] = t; } -void BindingData::SlowBigInt(const FunctionCallbackInfo& args) { - BigIntImpl(FromJSObject(args.This())); +void BindingData::SlowHrtimeBigInt(const FunctionCallbackInfo& args) { + HrtimeBigIntImpl(FromJSObject(args.This())); } -void BindingData::SlowNumber(const v8::FunctionCallbackInfo& args) { - NumberImpl(FromJSObject(args.This())); +void BindingData::SlowHrtime(const FunctionCallbackInfo& args) { + HrtimeImpl(FromJSObject(args.This())); } bool BindingData::PrepareForSerialization(Local context, diff --git a/test/parallel/test-process-hrtime-bigint.js b/test/parallel/test-process-hrtime-bigint.js index e5ce40a994d..9d0e0e347b0 100644 --- a/test/parallel/test-process-hrtime-bigint.js +++ b/test/parallel/test-process-hrtime-bigint.js @@ -1,10 +1,13 @@ +// Flags: --allow-natives-syntax --expose-internals --no-warnings 'use strict'; // Tests that process.hrtime.bigint() works. -require('../common'); +const common = require('../common'); const assert = require('assert'); +const { internalBinding } = require('internal/test/binding'); + const start = process.hrtime.bigint(); assert.strictEqual(typeof start, 'bigint'); @@ -12,3 +15,13 @@ const end = process.hrtime.bigint(); assert.strictEqual(typeof end, 'bigint'); assert(end - start >= 0n); + +eval('%PrepareFunctionForOptimization(process.hrtime.bigint)'); +assert(process.hrtime.bigint()); +eval('%OptimizeFunctionOnNextCall(process.hrtime.bigint)'); +assert(process.hrtime.bigint()); + +if (common.isDebug) { + const { getV8FastApiCallCount } = internalBinding('debug'); + assert.strictEqual(getV8FastApiCallCount('process.hrtimeBigInt'), 1); +} diff --git a/test/parallel/test-process-hrtime.js b/test/parallel/test-process-hrtime.js index 34ef514aac3..e5815ebe10c 100644 --- a/test/parallel/test-process-hrtime.js +++ b/test/parallel/test-process-hrtime.js @@ -19,10 +19,13 @@ // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE // USE OR OTHER DEALINGS IN THE SOFTWARE. +// Flags: --allow-natives-syntax --expose-internals --no-warnings 'use strict'; -require('../common'); +const common = require('../common'); const assert = require('assert'); +const { internalBinding } = require('internal/test/binding'); + // The default behavior, return an Array "tuple" of numbers const tuple = process.hrtime(); @@ -72,3 +75,13 @@ function validateTuple(tuple) { const diff = process.hrtime([0, 1e9 - 1]); assert(diff[1] >= 0); // https://github.com/nodejs/node/issues/4751 + +eval('%PrepareFunctionForOptimization(process.hrtime)'); +assert(process.hrtime()); +eval('%OptimizeFunctionOnNextCall(process.hrtime)'); +assert(process.hrtime()); + +if (common.isDebug) { + const { getV8FastApiCallCount } = internalBinding('debug'); + assert.strictEqual(getV8FastApiCallCount('process.hrtime'), 1); +}