From e3c7c4fdc073273259224874269d83cff5d70453 Mon Sep 17 00:00:00 2001 From: cola119 Date: Mon, 6 Jun 2022 23:21:15 +0900 Subject: [PATCH 1/2] perf_hooks: fix function wrapped by timerify to work correctly --- lib/internal/perf/timerify.js | 11 +++-------- src/node_util.cc | 7 ------- test/parallel/test-performance-function.js | 19 +++++++++++++++++++ 3 files changed, 22 insertions(+), 15 deletions(-) diff --git a/lib/internal/perf/timerify.js b/lib/internal/perf/timerify.js index 38f6b3d37acdd2..dc16b4c6c405ef 100644 --- a/lib/internal/perf/timerify.js +++ b/lib/internal/perf/timerify.js @@ -20,10 +20,6 @@ const { isHistogram } = require('internal/histogram'); -const { - isConstructor, -} = internalBinding('util'); - const { codes: { ERR_INVALID_ARG_TYPE, @@ -68,14 +64,13 @@ function timerify(fn, options = {}) { histogram); } - const constructor = isConstructor(fn); - function timerified(...args) { + const isConstructorCall = !!new.target; const start = now(); - const result = constructor ? + const result = isConstructorCall ? ReflectConstruct(fn, args, fn) : ReflectApply(fn, this, args); - if (!constructor && typeof result?.finally === 'function') { + if (!isConstructorCall && typeof result?.finally === 'function') { return result.finally( FunctionPrototypeBind( processComplete, diff --git a/src/node_util.cc b/src/node_util.cc index 2db45bd1fb40af..5b5dab36f08fbf 100644 --- a/src/node_util.cc +++ b/src/node_util.cc @@ -289,11 +289,6 @@ static void GuessHandleType(const FunctionCallbackInfo& args) { args.GetReturnValue().Set(OneByteString(env->isolate(), type)); } -static void IsConstructor(const FunctionCallbackInfo& args) { - CHECK(args[0]->IsFunction()); - args.GetReturnValue().Set(args[0].As()->IsConstructor()); -} - static void ToUSVString(const FunctionCallbackInfo& args) { Environment* env = Environment::GetCurrent(args); CHECK_GE(args.Length(), 2); @@ -344,7 +339,6 @@ void RegisterExternalReferences(ExternalReferenceRegistry* registry) { registry->Register(WeakReference::IncRef); registry->Register(WeakReference::DecRef); registry->Register(GuessHandleType); - registry->Register(IsConstructor); registry->Register(ToUSVString); } @@ -384,7 +378,6 @@ void Initialize(Local target, env->SetMethodNoSideEffect(target, "getConstructorName", GetConstructorName); env->SetMethodNoSideEffect(target, "getExternalValue", GetExternalValue); env->SetMethod(target, "sleep", Sleep); - env->SetMethodNoSideEffect(target, "isConstructor", IsConstructor); env->SetMethod(target, "arrayBufferViewHasBuffer", ArrayBufferViewHasBuffer); Local constants = Object::New(env->isolate()); diff --git a/test/parallel/test-performance-function.js b/test/parallel/test-performance-function.js index fcc3004d02884a..5f774d6c2adcf5 100644 --- a/test/parallel/test-performance-function.js +++ b/test/parallel/test-performance-function.js @@ -123,3 +123,22 @@ const { }); }); })().then(common.mustCall()); + +// Regression tests for https://github.com/nodejs/node/issues/40623 +{ + assert.strictEqual(performance.timerify(function func() { + return 1; + })(), 1); + assert.strictEqual(performance.timerify(function() { + return 1; + })(), 1); + assert.strictEqual(performance.timerify(() => { + return 1; + })(), 1); + class C {} + const wrap = performance.timerify(C); + assert.ok(new wrap() instanceof C); + assert.throws(() => wrap(), { + name: 'TypeError', + }); +} From 2e521c0140a97db1a49ba237862dd5f3f17169cd Mon Sep 17 00:00:00 2001 From: Kohei Ueno Date: Wed, 8 Jun 2022 06:41:52 +0900 Subject: [PATCH 2/2] fixup! perf_hooks: fix function wrapped by timerify to work correctly Co-authored-by: Antoine du Hamel --- lib/internal/perf/timerify.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/internal/perf/timerify.js b/lib/internal/perf/timerify.js index dc16b4c6c405ef..35024d8cf0cae1 100644 --- a/lib/internal/perf/timerify.js +++ b/lib/internal/perf/timerify.js @@ -65,7 +65,7 @@ function timerify(fn, options = {}) { } function timerified(...args) { - const isConstructorCall = !!new.target; + const isConstructorCall = new.target !== undefined; const start = now(); const result = isConstructorCall ? ReflectConstruct(fn, args, fn) :