Skip to content

Commit 8946ea9

Browse files
committed
Add tests
1 parent 0cb3884 commit 8946ea9

File tree

2 files changed

+44
-19
lines changed

2 files changed

+44
-19
lines changed

tests/embind/embind.test.js

+11
Original file line numberDiff line numberDiff line change
@@ -1277,6 +1277,17 @@ module({
12771277
b.delete();
12781278
});
12791279

1280+
test("functions as class constructors", function() {
1281+
var a = new cm.ConstructFromFunction({optionA: true});
1282+
assert.equal(true, a.optionA);
1283+
1284+
var b = new cm.ConstructFromFunction();
1285+
assert.equal(false, b.optionA);
1286+
1287+
a.delete();
1288+
b.delete();
1289+
});
1290+
12801291
test("function objects as class methods", function() {
12811292
var b = cm.ValHolder.makeValHolder("foo");
12821293

tests/embind/embind_test.cpp

+33-19
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,7 @@ class SharedPtrHolder {
341341
std::shared_ptr<StringHolder> get() const {
342342
return ptr_;
343343
}
344-
344+
345345
void set(std::shared_ptr<StringHolder> p) {
346346
ptr_ = p;
347347
}
@@ -988,11 +988,11 @@ class UniquePtrToConstructor {
988988
UniquePtrToConstructor(std::unique_ptr<int> p)
989989
: value(*p)
990990
{}
991-
991+
992992
int getValue() const {
993993
return value;
994994
}
995-
995+
996996
private:
997997
int value;
998998
};
@@ -1412,7 +1412,7 @@ EMSCRIPTEN_BINDINGS(interface_tests) {
14121412
self.AbstractClass::passVal(v);
14131413
}))
14141414
;
1415-
1415+
14161416
function("getAbstractClass", &getAbstractClass);
14171417
function("callAbstractMethod", &callAbstractMethod);
14181418
function("callOptionalMethod", &callOptionalMethod);
@@ -1500,7 +1500,7 @@ class CustomSmartPtr {
15001500
~CustomSmartPtr() {
15011501
verify();
15021502
std::fill(d_, d_ + N_, Deleted);
1503-
1503+
15041504
if (ptr_ && --ptr_->refcount == 0) {
15051505
delete ptr_;
15061506
}
@@ -1751,6 +1751,16 @@ ConstructFromFunctor<I> construct_from_functor_mixin(const val& v, int i)
17511751
return {v, i};
17521752
}
17531753

1754+
struct ConstructFromFunction {
1755+
bool optionA;
1756+
ConstructFromFunction(bool optionA_): optionA{optionA_} {};
1757+
};
1758+
1759+
auto ConstructFromFunctionConstructor(emscripten::val value) {
1760+
bool optionA = (value.as<bool>() && value["optionA"].as<bool>());
1761+
return new ConstructFromFunction(optionA);
1762+
}
1763+
17541764
EMSCRIPTEN_BINDINGS(tests) {
17551765
register_vector<int>("IntegerVector");
17561766
register_vector<char>("CharVector");
@@ -1873,6 +1883,10 @@ EMSCRIPTEN_BINDINGS(tests) {
18731883
.function("getA", &ConstructFromFunctor<2>::getA)
18741884
;
18751885

1886+
class_<ConstructFromFunction>("ConstructFromFunction")
1887+
.constructor<emscripten::val>(&ConstructFromFunctionConstructor)
1888+
;
1889+
18761890
class_<ValHolder>("ValHolder")
18771891
.smart_ptr<std::shared_ptr<ValHolder>>("std::shared_ptr<ValHolder>")
18781892
.constructor<val>()
@@ -1929,7 +1943,7 @@ EMSCRIPTEN_BINDINGS(tests) {
19291943
.function("get", &StringHolder::get)
19301944
.function("get_const_ref", &StringHolder::get_const_ref)
19311945
;
1932-
1946+
19331947
class_<SharedPtrHolder>("SharedPtrHolder")
19341948
.constructor<>()
19351949
.function("get", &SharedPtrHolder::get)
@@ -2002,7 +2016,7 @@ EMSCRIPTEN_BINDINGS(tests) {
20022016
.property("member", &SecondBase::member)
20032017
.property("secondBaseMember", &SecondBase::secondBaseMember)
20042018
;
2005-
2019+
20062020

20072021
class_<DerivedHolder>("DerivedHolder")
20082022
.constructor<>()
@@ -2017,7 +2031,7 @@ EMSCRIPTEN_BINDINGS(tests) {
20172031
.constructor<>()
20182032
.function("getClassName", &SiblingDerived::getClassName)
20192033
;
2020-
2034+
20212035
class_<MultiplyDerived, base<Base>>("MultiplyDerived")
20222036
.smart_ptr<std::shared_ptr<MultiplyDerived>>("shared_ptr<MultiplyDerived>")
20232037
.constructor<>()
@@ -2094,7 +2108,7 @@ EMSCRIPTEN_BINDINGS(tests) {
20942108
.constructor<>()
20952109
.function("getClassName", &PolyDiamondBase::getClassName)
20962110
;
2097-
2111+
20982112
class_<PolyDiamondDerived>("PolyDiamondDerived")
20992113
.smart_ptr<std::shared_ptr<PolyDiamondDerived>>("shared_ptr<PolyDiamondDerived>")
21002114
.constructor<>()
@@ -2339,10 +2353,10 @@ class MultipleSmartCtors {
23392353
class MultipleOverloads {
23402354
public:
23412355
MultipleOverloads() {}
2342-
2356+
23432357
int value;
23442358
static int staticValue;
2345-
2359+
23462360
int Func(int i) {
23472361
assert(i == 10);
23482362
value = 1;
@@ -2358,7 +2372,7 @@ class MultipleOverloads {
23582372
int WhichFuncCalled() const {
23592373
return value;
23602374
}
2361-
2375+
23622376
static int StaticFunc(int i) {
23632377
assert(i == 10);
23642378
staticValue = 1;
@@ -2381,7 +2395,7 @@ int MultipleOverloads::staticValue = 0;
23812395
class MultipleOverloadsDerived : public MultipleOverloads {
23822396
public:
23832397
MultipleOverloadsDerived() {}
2384-
2398+
23852399
int Func(int i, int j, int k) {
23862400
assert(i == 30);
23872401
assert(j == 30);
@@ -2397,7 +2411,7 @@ class MultipleOverloadsDerived : public MultipleOverloads {
23972411
value = 4;
23982412
return 4;
23992413
}
2400-
2414+
24012415
static int StaticFunc(int i, int j, int k) {
24022416
assert(i == 30);
24032417
assert(j == 30);
@@ -2476,14 +2490,14 @@ EMSCRIPTEN_BINDINGS(overloads) {
24762490
.constructor<int, int, int>()
24772491
.function("WhichCtorCalled", &MultipleCtors::WhichCtorCalled)
24782492
;
2479-
2493+
24802494
class_<MultipleSmartCtors>("MultipleSmartCtors")
24812495
.smart_ptr<std::shared_ptr<MultipleSmartCtors>>("shared_ptr<MultipleSmartCtors>")
24822496
.constructor(&std::make_shared<MultipleSmartCtors, int>)
24832497
.constructor(&std::make_shared<MultipleSmartCtors, int, int>)
24842498
.function("WhichCtorCalled", &MultipleSmartCtors::WhichCtorCalled)
24852499
;
2486-
2500+
24872501
class_<MultipleOverloads>("MultipleOverloads")
24882502
.constructor<>()
24892503
.function("Func", select_overload<int(int)>(&MultipleOverloads::Func))
@@ -2560,7 +2574,7 @@ EMSCRIPTEN_BINDINGS(order) {
25602574
.field("first", &OrderedStruct::first)
25612575
.field("second", &OrderedStruct::second)
25622576
;
2563-
2577+
25642578
class_<SecondElement>("SecondElement")
25652579
;
25662580

@@ -2644,7 +2658,7 @@ EMSCRIPTEN_BINDINGS(incomplete) {
26442658
class Noncopyable {
26452659
Noncopyable(const Noncopyable&) = delete;
26462660
Noncopyable& operator=(const Noncopyable&) = delete;
2647-
2661+
26482662
public:
26492663
Noncopyable() {}
26502664
Noncopyable(Noncopyable&& other) {
@@ -2709,7 +2723,7 @@ EMSCRIPTEN_BINDINGS(constants) {
27092723
constant("VALUE_OBJECT_CONSTANT", sv);
27102724
}
27112725

2712-
class DerivedWithOffset : public DummyDataToTestPointerAdjustment, public Base {
2726+
class DerivedWithOffset : public DummyDataToTestPointerAdjustment, public Base {
27132727
};
27142728

27152729
std::shared_ptr<Base> return_Base_from_DerivedWithOffset(std::shared_ptr<DerivedWithOffset> ptr) {

0 commit comments

Comments
 (0)