Skip to content

Commit 112518f

Browse files
cola119danielleadams
authored andcommitted
perf_hooks: fix function wrapped by timerify to work correctly
PR-URL: #43330 Reviewed-By: Antoine du Hamel <[email protected]> Reviewed-By: Darshan Sen <[email protected]> Reviewed-By: Minwoo Jung <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent e1b8c85 commit 112518f

File tree

3 files changed

+22
-15
lines changed

3 files changed

+22
-15
lines changed

lib/internal/perf/timerify.js

+3-8
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,6 @@ const {
2020
isHistogram
2121
} = require('internal/histogram');
2222

23-
const {
24-
isConstructor,
25-
} = internalBinding('util');
26-
2723
const {
2824
codes: {
2925
ERR_INVALID_ARG_TYPE,
@@ -72,14 +68,13 @@ function timerify(fn, options = kEmptyObject) {
7268
histogram);
7369
}
7470

75-
const constructor = isConstructor(fn);
76-
7771
function timerified(...args) {
72+
const isConstructorCall = new.target !== undefined;
7873
const start = now();
79-
const result = constructor ?
74+
const result = isConstructorCall ?
8075
ReflectConstruct(fn, args, fn) :
8176
ReflectApply(fn, this, args);
82-
if (!constructor && typeof result?.finally === 'function') {
77+
if (!isConstructorCall && typeof result?.finally === 'function') {
8378
return result.finally(
8479
FunctionPrototypeBind(
8580
processComplete,

src/node_util.cc

-7
Original file line numberDiff line numberDiff line change
@@ -289,11 +289,6 @@ static void GuessHandleType(const FunctionCallbackInfo<Value>& args) {
289289
args.GetReturnValue().Set(OneByteString(env->isolate(), type));
290290
}
291291

292-
static void IsConstructor(const FunctionCallbackInfo<Value>& args) {
293-
CHECK(args[0]->IsFunction());
294-
args.GetReturnValue().Set(args[0].As<v8::Function>()->IsConstructor());
295-
}
296-
297292
static void ToUSVString(const FunctionCallbackInfo<Value>& args) {
298293
Environment* env = Environment::GetCurrent(args);
299294
CHECK_GE(args.Length(), 2);
@@ -344,7 +339,6 @@ void RegisterExternalReferences(ExternalReferenceRegistry* registry) {
344339
registry->Register(WeakReference::IncRef);
345340
registry->Register(WeakReference::DecRef);
346341
registry->Register(GuessHandleType);
347-
registry->Register(IsConstructor);
348342
registry->Register(ToUSVString);
349343
}
350344

@@ -384,7 +378,6 @@ void Initialize(Local<Object> target,
384378
env->SetMethodNoSideEffect(target, "getConstructorName", GetConstructorName);
385379
env->SetMethodNoSideEffect(target, "getExternalValue", GetExternalValue);
386380
env->SetMethod(target, "sleep", Sleep);
387-
env->SetMethodNoSideEffect(target, "isConstructor", IsConstructor);
388381

389382
env->SetMethod(target, "arrayBufferViewHasBuffer", ArrayBufferViewHasBuffer);
390383
Local<Object> constants = Object::New(env->isolate());

test/parallel/test-performance-function.js

+19
Original file line numberDiff line numberDiff line change
@@ -123,3 +123,22 @@ const {
123123
});
124124
});
125125
})().then(common.mustCall());
126+
127+
// Regression tests for https://github.com/nodejs/node/issues/40623
128+
{
129+
assert.strictEqual(performance.timerify(function func() {
130+
return 1;
131+
})(), 1);
132+
assert.strictEqual(performance.timerify(function() {
133+
return 1;
134+
})(), 1);
135+
assert.strictEqual(performance.timerify(() => {
136+
return 1;
137+
})(), 1);
138+
class C {}
139+
const wrap = performance.timerify(C);
140+
assert.ok(new wrap() instanceof C);
141+
assert.throws(() => wrap(), {
142+
name: 'TypeError',
143+
});
144+
}

0 commit comments

Comments
 (0)