Skip to content

Commit e3c7c4f

Browse files
committed
perf_hooks: fix function wrapped by timerify to work correctly
1 parent dfa896f commit e3c7c4f

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,
@@ -68,14 +64,13 @@ function timerify(fn, options = {}) {
6864
histogram);
6965
}
7066

71-
const constructor = isConstructor(fn);
72-
7367
function timerified(...args) {
68+
const isConstructorCall = !!new.target;
7469
const start = now();
75-
const result = constructor ?
70+
const result = isConstructorCall ?
7671
ReflectConstruct(fn, args, fn) :
7772
ReflectApply(fn, this, args);
78-
if (!constructor && typeof result?.finally === 'function') {
73+
if (!isConstructorCall && typeof result?.finally === 'function') {
7974
return result.finally(
8075
FunctionPrototypeBind(
8176
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)