From 5b01f13a9f1a9e8868bc7ec9c09d282fa3412214 Mon Sep 17 00:00:00 2001 From: Yagiz Nizipli Date: Mon, 18 Nov 2024 10:37:42 -0500 Subject: [PATCH] test: add fast api tests for getLibuvNow() --- src/timers.cc | 6 ++++-- src/timers.h | 3 +-- test/parallel/test-timers-now.js | 24 +++++++++++++++++++++--- 3 files changed, 26 insertions(+), 7 deletions(-) diff --git a/src/timers.cc b/src/timers.cc index 49b508e4e2edaa..2c7b1813602d6f 100644 --- a/src/timers.cc +++ b/src/timers.cc @@ -1,5 +1,7 @@ #include "timers.h" + #include "env-inl.h" +#include "node_debug.h" #include "node_external_reference.h" #include "util-inl.h" #include "v8.h" @@ -33,8 +35,8 @@ void BindingData::SlowGetLibuvNow(const FunctionCallbackInfo& args) { args.GetReturnValue().Set(Number::New(args.GetIsolate(), now)); } -double BindingData::FastGetLibuvNow(Local unused, - Local receiver) { +double BindingData::FastGetLibuvNow(Local receiver) { + TRACK_V8_FAST_API_CALL("timers.getLibuvNow"); return GetLibuvNowImpl(FromJSObject(receiver)); } diff --git a/src/timers.h b/src/timers.h index fd3cbf66f7c516..5148ea5bc7bd82 100644 --- a/src/timers.h +++ b/src/timers.h @@ -26,8 +26,7 @@ class BindingData : public SnapshotableObject { static void SetupTimers(const v8::FunctionCallbackInfo& args); static void SlowGetLibuvNow(const v8::FunctionCallbackInfo& args); - static double FastGetLibuvNow(v8::Local unused, - v8::Local receiver); + static double FastGetLibuvNow(v8::Local receiver); static double GetLibuvNowImpl(BindingData* data); static void SlowScheduleTimer( diff --git a/test/parallel/test-timers-now.js b/test/parallel/test-timers-now.js index e5e97521f1b18f..738f9a17313c77 100644 --- a/test/parallel/test-timers-now.js +++ b/test/parallel/test-timers-now.js @@ -1,11 +1,29 @@ +// Flags: --expose-internals --no-warnings --allow-natives-syntax 'use strict'; -// Flags: --expose-internals -require('../common'); -const assert = require('assert'); +const common = require('../common'); +const assert = require('node:assert'); const { internalBinding } = require('internal/test/binding'); const binding = internalBinding('timers'); // Return value of getLibuvNow() should easily fit in a SMI after start-up. // We need to use the binding as the receiver for fast API calls. assert(binding.getLibuvNow() < 0x3ffffff); + +{ + // Only javascript methods can be optimized through %OptimizeFunctionOnNextCall + // This is why we surround the C++ method we want to optimize with a JS function. + function getLibuvNow() { + return binding.getLibuvNow(); + } + + eval('%PrepareFunctionForOptimization(getLibuvNow)'); + getLibuvNow(); + eval('%OptimizeFunctionOnNextCall(getLibuvNow)'); + assert(getLibuvNow() < 0x3ffffff); + + if (common.isDebug) { + const { getV8FastApiCallCount } = internalBinding('debug'); + assert.strictEqual(getV8FastApiCallCount('timers.getLibuvNow'), 1); + } +}