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: https://github.com/nodejs/node/issues/17668
Refs: https://github.com/nodejs/node/pull/20321
PR-URL: https://github.com/nodejs/node/pull/20321
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Tiancheng "Timothy" Gu <timothygu99@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
This commit is contained in:
Brandon Ruggles 2018-04-25 21:51:55 -04:00 committed by Anna Henningsen
parent 67790962da
commit 64348f5ea5
No known key found for this signature in database
GPG Key ID: 9C63F3A6CD2AD8F9
3 changed files with 27 additions and 4 deletions

View File

@ -583,9 +583,11 @@ inline void Environment::ThrowUVException(int errorno,
inline v8::Local<v8::FunctionTemplate>
Environment::NewFunctionTemplate(v8::FunctionCallback callback,
v8::Local<v8::Signature> signature) {
v8::Local<v8::Signature> signature,
v8::ConstructorBehavior behavior) {
v8::Local<v8::External> external = as_external();
return v8::FunctionTemplate::New(isolate(), callback, external, signature);
return v8::FunctionTemplate::New(isolate(), callback, external,
signature, 0, behavior);
}
inline void Environment::SetMethod(v8::Local<v8::Object> that,
@ -605,7 +607,8 @@ inline void Environment::SetProtoMethod(v8::Local<v8::FunctionTemplate> that,
const char* name,
v8::FunctionCallback callback) {
v8::Local<v8::Signature> signature = v8::Signature::New(isolate(), that);
v8::Local<v8::FunctionTemplate> t = NewFunctionTemplate(callback, signature);
v8::Local<v8::FunctionTemplate> t =
NewFunctionTemplate(callback, signature, v8::ConstructorBehavior::kThrow);
// kInternalized strings are created in the old space.
const v8::NewStringType type = v8::NewStringType::kInternalized;
v8::Local<v8::String> name_string =

View File

@ -687,7 +687,9 @@ class Environment {
inline v8::Local<v8::FunctionTemplate>
NewFunctionTemplate(v8::FunctionCallback callback,
v8::Local<v8::Signature> signature =
v8::Local<v8::Signature>());
v8::Local<v8::Signature>(),
v8::ConstructorBehavior behavior =
v8::ConstructorBehavior::kAllow);
// Convenience methods for NewFunctionTemplate().
inline void SetMethod(v8::Local<v8::Object> that,

View File

@ -0,0 +1,18 @@
'use strict';
require('../common');
// This test ensures that unnecessary prototypes are no longer
// being generated by Environment::NewFunctionTemplate.
const assert = require('assert');
[
process.binding('udp_wrap').UDP.prototype.bind6,
process.binding('tcp_wrap').TCP.prototype.bind6,
process.binding('udp_wrap').UDP.prototype.send6,
process.binding('tcp_wrap').TCP.prototype.bind,
process.binding('udp_wrap').UDP.prototype.close,
process.binding('tcp_wrap').TCP.prototype.open
].forEach((binding, i) => {
assert.strictEqual('prototype' in binding, false, `Test ${i} failed`);
});