Skip to content
This repository was archived by the owner on Jul 25, 2019. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 0 additions & 7 deletions templates/nanCxxImpl.dot
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
2 changes: 2 additions & 0 deletions test/constructor/addon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<v8::Object> exports) {
NanMyClass::Init(exports);
NanMyClass2::Init(exports);

NanNoConstructorClass::Init(exports);

Expand Down
2 changes: 2 additions & 0 deletions test/constructor/binding.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -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": [
"<!(node -e \"require('nan')\")",
Expand Down
7 changes: 7 additions & 0 deletions test/constructor/constructor.widl
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,10 @@ interface ContainerClass {

NoConstructorClass createNoConstructorClassObject();
};

[
Constructor(long val)
]
interface MyClass2 {
readonly attribute long value;
};
24 changes: 24 additions & 0 deletions test/constructor/my_class2.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// To add your copyright and license header

#include "my_class2.h"

MyClass2::MyClass2(const int32_t& val) {
value_ = val;
}

MyClass2::MyClass2(const MyClass2& rhs) {
// TODO(widl-nan): copy from rhs if you want this behavior
// Or mark ctor = delete in my_class2.h
}

MyClass2::~MyClass2() {
// TODO(widl-nan): do cleanup if necessary
}

MyClass2& MyClass2::operator = (const MyClass2& rhs) {
if (&rhs != this) {
// TODO(widl-nan): copy members from rhs
}
return *this;
}

38 changes: 38 additions & 0 deletions test/constructor/my_class2.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// To add your copyright and license header

#ifndef _MY_CLASS2_H_
#define _MY_CLASS2_H_

#include <node.h>
#include <v8.h>

#include <string>

#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<v8::Object> obj) {
// Ignore this if you don't need it
// Typical usage: emit an event on `obj`
}

private:
int32_t value_;
};

#endif // _MY_CLASS2_H_
8 changes: 8 additions & 0 deletions test/test-constructor.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;
Expand All @@ -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');
Expand Down