Skip to content

Commit 380dfe4

Browse files
brandontrugglesMylesBorins
authored andcommitted
src: removed unnecessary prototypes from Environment::SetProtoMethod
Added an optional parameter of type v8::ConstructorBehavior to Environment::NewFunctionTemplate, defaulting to v8::ConstructorBehavior::kAllow. Also modified Environment::SetProtoMethod to pass v8::ConstructorBehavior::kThrow to its call to Environment::NewFunctionTemplate. Fixes: #17668 Refs: #20321 PR-URL: #20321 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Tiancheng "Timothy" Gu <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Ben Noordhuis <[email protected]>
1 parent d55f90d commit 380dfe4

File tree

3 files changed

+27
-4
lines changed

3 files changed

+27
-4
lines changed

src/env-inl.h

+6-3
Original file line numberDiff line numberDiff line change
@@ -583,9 +583,11 @@ inline void Environment::ThrowUVException(int errorno,
583583

584584
inline v8::Local<v8::FunctionTemplate>
585585
Environment::NewFunctionTemplate(v8::FunctionCallback callback,
586-
v8::Local<v8::Signature> signature) {
586+
v8::Local<v8::Signature> signature,
587+
v8::ConstructorBehavior behavior) {
587588
v8::Local<v8::External> external = as_external();
588-
return v8::FunctionTemplate::New(isolate(), callback, external, signature);
589+
return v8::FunctionTemplate::New(isolate(), callback, external,
590+
signature, 0, behavior);
589591
}
590592

591593
inline void Environment::SetMethod(v8::Local<v8::Object> that,
@@ -605,7 +607,8 @@ inline void Environment::SetProtoMethod(v8::Local<v8::FunctionTemplate> that,
605607
const char* name,
606608
v8::FunctionCallback callback) {
607609
v8::Local<v8::Signature> signature = v8::Signature::New(isolate(), that);
608-
v8::Local<v8::FunctionTemplate> t = NewFunctionTemplate(callback, signature);
610+
v8::Local<v8::FunctionTemplate> t =
611+
NewFunctionTemplate(callback, signature, v8::ConstructorBehavior::kThrow);
609612
// kInternalized strings are created in the old space.
610613
const v8::NewStringType type = v8::NewStringType::kInternalized;
611614
v8::Local<v8::String> name_string =

src/env.h

+3-1
Original file line numberDiff line numberDiff line change
@@ -687,7 +687,9 @@ class Environment {
687687
inline v8::Local<v8::FunctionTemplate>
688688
NewFunctionTemplate(v8::FunctionCallback callback,
689689
v8::Local<v8::Signature> signature =
690-
v8::Local<v8::Signature>());
690+
v8::Local<v8::Signature>(),
691+
v8::ConstructorBehavior behavior =
692+
v8::ConstructorBehavior::kAllow);
691693

692694
// Convenience methods for NewFunctionTemplate().
693695
inline void SetMethod(v8::Local<v8::Object> that,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
'use strict';
2+
require('../common');
3+
4+
// This test ensures that unnecessary prototypes are no longer
5+
// being generated by Environment::NewFunctionTemplate.
6+
7+
const assert = require('assert');
8+
9+
[
10+
process.binding('udp_wrap').UDP.prototype.bind6,
11+
process.binding('tcp_wrap').TCP.prototype.bind6,
12+
process.binding('udp_wrap').UDP.prototype.send6,
13+
process.binding('tcp_wrap').TCP.prototype.bind,
14+
process.binding('udp_wrap').UDP.prototype.close,
15+
process.binding('tcp_wrap').TCP.prototype.open
16+
].forEach((binding, i) => {
17+
assert.strictEqual('prototype' in binding, false, `Test ${i} failed`);
18+
});

0 commit comments

Comments
 (0)