From 18108b4f1fc5f2848d61f6e3fccfcb5316f8bb1c Mon Sep 17 00:00:00 2001 From: Brandon Ruggles Date: Wed, 25 Apr 2018 21:51:55 -0400 Subject: [PATCH] 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 --- src/env-inl.h | 9 ++++++--- src/env.h | 4 +++- ...rotomethod-remove-unnecessary-prototypes.js | 18 ++++++++++++++++++ 3 files changed, 27 insertions(+), 4 deletions(-) create mode 100644 test/parallel/test-env-newprotomethod-remove-unnecessary-prototypes.js diff --git a/src/env-inl.h b/src/env-inl.h index fa241f9706ec65..1aa62fc18ded52 100644 --- a/src/env-inl.h +++ b/src/env-inl.h @@ -583,9 +583,11 @@ inline void Environment::ThrowUVException(int errorno, inline v8::Local Environment::NewFunctionTemplate(v8::FunctionCallback callback, - v8::Local signature) { + v8::Local signature, + v8::ConstructorBehavior behavior) { v8::Local 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 that, @@ -605,7 +607,8 @@ inline void Environment::SetProtoMethod(v8::Local that, const char* name, v8::FunctionCallback callback) { v8::Local signature = v8::Signature::New(isolate(), that); - v8::Local t = NewFunctionTemplate(callback, signature); + v8::Local t = + NewFunctionTemplate(callback, signature, v8::ConstructorBehavior::kThrow); // kInternalized strings are created in the old space. const v8::NewStringType type = v8::NewStringType::kInternalized; v8::Local name_string = diff --git a/src/env.h b/src/env.h index af4470ad8632fe..252151028164d2 100644 --- a/src/env.h +++ b/src/env.h @@ -687,7 +687,9 @@ class Environment { inline v8::Local NewFunctionTemplate(v8::FunctionCallback callback, v8::Local signature = - v8::Local()); + v8::Local(), + v8::ConstructorBehavior behavior = + v8::ConstructorBehavior::kAllow); // Convenience methods for NewFunctionTemplate(). inline void SetMethod(v8::Local that, diff --git a/test/parallel/test-env-newprotomethod-remove-unnecessary-prototypes.js b/test/parallel/test-env-newprotomethod-remove-unnecessary-prototypes.js new file mode 100644 index 00000000000000..bcc522cd45ad50 --- /dev/null +++ b/test/parallel/test-env-newprotomethod-remove-unnecessary-prototypes.js @@ -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`); +});