From b10dd5f56171a1cc0195cd1f2890e069626e9a99 Mon Sep 17 00:00:00 2001 From: "Shao,Ting" Date: Thu, 20 Oct 2016 15:29:24 +0800 Subject: [PATCH] Fix issue #68 remove unnecessary constructor calling --- templates/nanCxxImpl.dot | 7 ------ test/constructor/addon.cpp | 2 ++ test/constructor/binding.gyp | 2 ++ test/constructor/constructor.widl | 7 ++++++ test/constructor/my_class2.cpp | 24 +++++++++++++++++++ test/constructor/my_class2.h | 38 +++++++++++++++++++++++++++++++ test/test-constructor.js | 8 +++++++ 7 files changed, 81 insertions(+), 7 deletions(-) create mode 100644 test/constructor/my_class2.cpp create mode 100644 test/constructor/my_class2.h diff --git a/templates/nanCxxImpl.dot b/templates/nanCxxImpl.dot index 6a31b34..f794c77 100644 --- a/templates/nanCxxImpl.dot +++ b/templates/nanCxxImpl.dot @@ -136,14 +136,7 @@ NAN_METHOD(Nan{{=it.name}}::New) { } {{~}} else { - if ( internal_ctor_call_once_ ) { -{{? useImpl}} - obj->impl_ = new {{=it.name}}(); -{{?}} - internal_ctor_call_once_ = false; - } else { Nan::ThrowTypeError("Illegal constructor"); - } } obj->Wrap(info.This()); diff --git a/test/constructor/addon.cpp b/test/constructor/addon.cpp index 456f9a1..7566fd2 100644 --- a/test/constructor/addon.cpp +++ b/test/constructor/addon.cpp @@ -4,10 +4,12 @@ #include "gen/nan__container_class.h" #include "gen/nan__my_class.h" +#include "gen/nan__my_class2.h" #include "gen/nan__no_constructor_class.h" void initModule(v8::Local exports) { NanMyClass::Init(exports); + NanMyClass2::Init(exports); NanNoConstructorClass::Init(exports); diff --git a/test/constructor/binding.gyp b/test/constructor/binding.gyp index 1966910..5152423 100644 --- a/test/constructor/binding.gyp +++ b/test/constructor/binding.gyp @@ -12,8 +12,10 @@ "no_constructor_class.cpp", "gen/nan__container_class.cpp", "gen/nan__my_class.cpp", + "gen/nan__my_class2.cpp", "gen/nan__no_constructor_class.cpp", "my_class.cpp", + "my_class2.cpp", ], "include_dirs": [ " +#include + +#include + +#include "gen/generator_helper.h" +#include "gen/array_helper.h" + +class MyClass2 { + public: + explicit MyClass2(const int32_t& val); + + MyClass2(const MyClass2& rhs); + + ~MyClass2(); + + MyClass2& operator = (const MyClass2& rhs); + + public: + int32_t get_value() const { + return this->value_; + } + + void SetJavaScriptThis(v8::Local obj) { + // Ignore this if you don't need it + // Typical usage: emit an event on `obj` + } + + private: + int32_t value_; +}; + +#endif // _MY_CLASS2_H_ diff --git a/test/test-constructor.js b/test/test-constructor.js index 4e105f9..d0c5d1a 100644 --- a/test/test-constructor.js +++ b/test/test-constructor.js @@ -11,6 +11,7 @@ var compile = require('../lib/compile.js').compile; var path = require('path'); var MyClass = null; +var MyClass2 = null; var NoConstructorClass = null; var ContainerClass = null; @@ -32,6 +33,7 @@ describe('widl-nan Unit Test - Constructor', function() { // eslint-disable-next-line camelcase {bindings: 'widlNanAddon', module_root: addonDir}); MyClass = addon.MyClass; + MyClass2 = addon.MyClass2; assert.equal(typeof MyClass, 'function'); NoConstructorClass = addon.NoConstructorClass; @@ -55,6 +57,12 @@ describe('widl-nan Unit Test - Constructor', function() { done(); }); + it('Test only has one constructor with 1 parameter', done => { + var x = new MyClass2(100); + assert.equal(x.value, 100); + done(); + }); + it('Test constructor with 2 parameters', done => { var x = new MyClass('https', 1989); assert.equal(x.url, 'https');