aboutsummaryrefslogtreecommitdiff
path: root/tests/embind/embind_test.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'tests/embind/embind_test.cpp')
-rw-r--r--tests/embind/embind_test.cpp1651
1 files changed, 1587 insertions, 64 deletions
diff --git a/tests/embind/embind_test.cpp b/tests/embind/embind_test.cpp
index 485cdfcd..e9feccfe 100644
--- a/tests/embind/embind_test.cpp
+++ b/tests/embind/embind_test.cpp
@@ -1,5 +1,6 @@
#include <string>
#include <malloc.h>
+#include <functional>
#include <emscripten/bind.h>
using namespace emscripten;
@@ -27,6 +28,10 @@ val emval_test_new_string() {
return val("Hello everyone");
}
+std::string emval_test_get_string_from_val(val v) {
+ return v["key"].as<std::string>();
+}
+
val emval_test_new_object() {
val rv(val::object());
rv.set("foo", val("bar"));
@@ -54,18 +59,22 @@ unsigned emval_test_as_unsigned(val v) {
}
unsigned emval_test_get_length(val v) {
- return v.get("length").as<unsigned>();
+ return v["length"].as<unsigned>();
}
double emval_test_add(char c, signed char sc, unsigned char uc, signed short ss, unsigned short us, signed int si, unsigned int ui, signed long sl, unsigned long ul, float f, double d) {
return c + sc + uc + ss + us + si + ui + sl + ul + f + d;
}
+float const_ref_adder(const int& i, const float& f) {
+ return i + f;
+}
+
unsigned emval_test_sum(val v) {
- unsigned length = v.get("length").as<unsigned>();
+ unsigned length = v["length"].as<unsigned>();
double rv = 0;
for (unsigned i = 0; i < length; ++i) {
- rv += v.get(i).as<double>();
+ rv += v[i].as<double>();
}
return rv;
}
@@ -82,30 +91,70 @@ std::string emval_test_take_and_return_std_string_const_ref(const std::string& s
return str;
}
+std::function<std::string (std::string)> emval_test_get_function_ptr() {
+ return emval_test_take_and_return_std_string;
+}
+
+std::string emval_test_take_and_call_functor(std::function<std::string(std::string)> func) {
+ return func("asdf");
+}
+
class ValHolder {
public:
ValHolder(val v)
- : v(v)
+ : v_(v)
{}
val getVal() const {
- return v;
+ return v_;
}
- void setVal(val v) {
- this->v = v;
+ val getValNonConst() {
+ return v_;
+ }
+
+ const val getConstVal() const {
+ return v_;
}
- int returnIntPlusFive( int x ) {
- return x + 5;
+ const val& getValConstRef() const {
+ return v_;
+ }
+
+ void setVal(val v) {
+ this->v_ = v;
}
static int some_class_method(int i) {
return i;
}
+ static const ValHolder* makeConst(val v) {
+ return new ValHolder(v);
+ }
+
+ static ValHolder makeValHolder(val v) {
+ return ValHolder(v);
+ }
+
+ static void set_via_raw_pointer(ValHolder* vh, val v) {
+ vh->setVal(v);
+ }
+
+ static val get_via_raw_pointer(const ValHolder* vh) {
+ return vh->getVal();
+ }
+
+ static void transfer_via_raw_pointer(ValHolder* target, const ValHolder* source) {
+ target->setVal(source->getVal());
+ }
+
+ static val getValNonMember(const ValHolder& target) {
+ return target.getVal();
+ }
+
private:
- val v;
+ val v_;
};
ValHolder emval_test_return_ValHolder() {
@@ -119,18 +168,556 @@ void emval_test_set_ValHolder_to_empty_object(ValHolder& vh) {
class StringHolder {
public:
StringHolder(const std::string& s)
- : str(s)
+ : str_(s)
{}
void set(const std::string& s) {
- str = s;
+ str_ = s;
}
+
std::string get() const {
- return str;
+ return str_;
+ }
+
+ std::string& get_ref() {
+ return str_;
+ }
+
+ const std::string& get_const_ref() const {
+ return str_;
+ }
+
+private:
+ std::string str_;
+};
+
+class SharedPtrHolder {
+public:
+ SharedPtrHolder()
+ : ptr_(new StringHolder("a string"))
+ {}
+
+ std::shared_ptr<StringHolder> get() const {
+ return ptr_;
+ }
+
+ void set(std::shared_ptr<StringHolder> p) {
+ ptr_ = p;
+ }
+private:
+ std::shared_ptr<StringHolder> ptr_;
+};
+
+class VectorHolder {
+public:
+ VectorHolder() {
+ v_.push_back(StringHolder("string #1"));
+ v_.push_back(StringHolder("string #2"));
+ }
+
+ std::vector<StringHolder> get() const {
+ return v_;
+ }
+
+ void set(std::vector<StringHolder> vec) {
+ v_ = vec;
}
private:
- std::string str;
+ std::vector<StringHolder> v_;
+};
+
+class SmallClass {
+public:
+ SmallClass(): member(7) {};
+ int member;
+};
+
+class BigClass {
+public:
+ BigClass(): member(11) {};
+ int member;
+ int otherMember;
+ int yetAnotherMember;
+
+ int getMember() {
+ return member;
+ }
+};
+
+class ParentClass {
+public:
+ ParentClass(): bigClass() {};
+
+ BigClass bigClass;
+
+ const BigClass& getBigClass() {
+ return bigClass;
+ };
+};
+
+template<typename T>
+class TemplateClass {
+public:
+ TemplateClass(T a, T b, T c) {
+ members[0] = a;
+ members[1] = b;
+ members[2] = c;
+ };
+
+ const T getMember(int n) {
+ return members[n];
+ }
+
+protected:
+ T members[3];
+};
+
+class ContainsTemplatedMemberClass {
+public:
+ ContainsTemplatedMemberClass(): testTemplate(86, 87, 88) {};
+
+ TemplateClass<int> testTemplate;
+
+ const TemplateClass<int>& getTestTemplate() {
+ return testTemplate;
+ };
+};
+
+// Begin Inheritance Hierarchy Class Definitions
+
+class Base {
+public:
+ Base(): name("Base"),
+ member(0),
+ baseMember(0)
+ {}
+
+ std::string getClassName() const {
+ return name;
+ }
+ std::string getClassNameFromBase() const {
+ return name;
+ }
+ std::string getClassNameNotAvailableInDerivedClasses() {
+ // but wait -- if you act now we will throw in a SECOND base class method ABSOLUTELY FREE!!
+ return name;
+ }
+ void setMember(int value) {
+ member = value;
+ }
+ int getMember() {
+ return member;
+ }
+ void setBaseMember(int value) {
+ baseMember = value;
+ }
+ int getBaseMember() {
+ return baseMember;
+ }
+ std::string name;
+ int member;
+ int baseMember;
+};
+
+class SecondBase {
+public:
+ SecondBase()
+ : name("SecondBase"),
+ member(0),
+ secondBaseMember(0)
+ {}
+
+ std::string getClassName() const {
+ return name;
+ }
+ std::string getClassNameNotAvailableInDerivedClasses() {
+ return name;
+ }
+ std::string getClassNameFromSecondBase() const {
+ return name;
+ }
+ void setMember(int value) {
+ member = value;
+ }
+ int getMember() {
+ return member;
+ }
+ void setSecondBaseMember(int value) {
+ secondBaseMember = value;
+ }
+ int getSecondBaseMember() {
+ return secondBaseMember;
+ }
+ std::string name;
+ int member;
+ int secondBaseMember;
+};
+
+class Derived : public Base{
+public:
+ Derived()
+ : Base()
+ , member(0)
+ , name_("Derived")
+ {}
+
+ std::string getClassName() const {
+ return name_;
+ }
+ void setMember(int value) {
+ member = value;
+ }
+ int getMember() {
+ return member;
+ }
+ int member;
+private:
+ std::string name_;
+};
+
+class DerivedHolder {
+public:
+ DerivedHolder() {
+ derived_.reset();
+ }
+ void newDerived() {
+ deleteDerived();
+ derived_ = std::shared_ptr<Derived>(new Derived());
+ }
+ void deleteDerived() {
+ derived_.reset();
+ }
+ std::shared_ptr<Derived> getDerived() {
+ return derived_;
+ }
+ std::string getDerivedClassName() {
+ return derived_->getClassName();
+ }
+private:
+ std::shared_ptr<Derived> derived_;
+};
+
+class SiblingDerived : public Base {
+public:
+ SiblingDerived()
+ : Base(),
+ name_("SiblingDerived")
+ {}
+
+ std::string getClassName() const {
+ return name_;
+ }
+
+private:
+ std::string name_;
+};
+
+class MultiplyDerived : public Base, public SecondBase {
+public:
+ MultiplyDerived()
+ : Base(), SecondBase(),
+ name_("MultiplyDerived")
+ { instanceCount_ ++; }
+
+ ~MultiplyDerived()
+ { instanceCount_ --; }
+
+ std::string getClassName() const {
+ return name_;
+ }
+
+ static int getInstanceCount() {
+ return instanceCount_;
+ }
+private:
+ std::string name_;
+ static int instanceCount_;
+};
+int MultiplyDerived::instanceCount_ = 0;
+
+class DerivedTwice : public Derived {
+public:
+ DerivedTwice()
+ : Derived(),
+ name_("DerivedTwice")
+ {}
+
+ std::string getClassName() const {
+ return name_;
+ }
+private:
+ std::string name_;
+};
+
+class DerivedTwiceNotBound : public Derived {
+public:
+ DerivedTwiceNotBound()
+ : Derived(),
+ name_("DerivedTwiceNotBound")
+ {}
+
+ std::string getClassName() const {
+ return name_;
+ }
+private:
+ std::string name_;
+};
+
+class DerivedThrice: public DerivedTwiceNotBound {
+public:
+ DerivedThrice()
+ : DerivedTwiceNotBound(),
+ name_("DerivedThrice")
+ {}
+
+ std::string getClassName() const {
+ return name_;
+ }
+private:
+ std::string name_;
+};
+
+class DerivedFourTimesNotBound: public DerivedThrice {
+public:
+ DerivedFourTimesNotBound()
+ : DerivedThrice(),
+ name_("DerivedFourTimesNotBound")
+ {}
+
+ std::string getClassName() const {
+ return name_;
+ }
+private:
+ std::string name_;
+};
+
+class PolyBase {
+public:
+ PolyBase(const std::string& s)
+ : str_(s),
+ name_("PolyBase")
+ {}
+
+ PolyBase(): name_("PolyBase") {}
+
+ virtual ~PolyBase() {}
+
+ virtual std::string virtualGetClassName() const {
+ return name_;
+ }
+
+ std::string getClassName() const {
+ return name_;
+ }
+
+private:
+ std::string str_;
+ std::string name_;
+};
+
+class PolySecondBase {
+public:
+ PolySecondBase(): name_("PolySecondBase")
+ {}
+
+ virtual ~PolySecondBase() {}
+
+ std::string getClassName() const {
+ return name_;
+ }
+private:
+ std::string name_;
+};
+
+class PolyDerived : public PolyBase{
+public:
+ PolyDerived()
+ : PolyBase("PolyDerived"),
+ name_("PolyDerived")
+ {}
+
+ std::string virtualGetClassName() const {
+ return name_;
+ }
+
+ std::string getClassName() const {
+ return name_;
+ }
+
+ static void setPtrDerived() {
+ ptr_ = std::shared_ptr<PolyDerived>(new PolyDerived());
+ }
+
+ static void releasePtr() {
+ ptr_.reset();
+ }
+
+ static std::string getPtrClassName() {
+ return ptr_->getClassName();
+ }
+
+ static std::shared_ptr<PolyBase> getPtr() {
+ return ptr_;
+ }
+
+private:
+ std::string name_;
+ static std::shared_ptr<PolyBase> ptr_;
+};
+std::shared_ptr<PolyBase> PolyDerived::ptr_;
+
+class PolySiblingDerived : public PolyBase {
+public:
+ PolySiblingDerived()
+ : PolyBase(),
+ name_("PolySiblingDerived")
+ {}
+
+ std::string getClassName() const {
+ return name_;
+ }
+private:
+ std::string name_;
+};
+
+class PolyMultiplyDerived : public PolyBase, public PolySecondBase {
+public:
+ PolyMultiplyDerived()
+ : PolyBase(), PolySecondBase(),
+ name_("PolyMultiplyDerived")
+ {}
+
+ std::string getClassName() const {
+ return name_;
+ }
+private:
+ std::string name_;
+};
+
+class PolyDerivedTwiceWithoutSmartPointer: public PolyDerived {
+public:
+ PolyDerivedTwiceWithoutSmartPointer()
+ : PolyDerived(),
+ name_("PolyDerivedTwiceWithoutSmartPointer")
+ {}
+
+ std::string getClassName() const {
+ return name_;
+ }
+private:
+ std::string name_;
+};
+
+class PolyDerivedTwiceNotBound : public PolyDerived {
+public:
+ PolyDerivedTwiceNotBound()
+ : PolyDerived(),
+ name_("PolyDerivedTwiceNotBound")
+ {}
+
+ std::string getClassName() const {
+ return name_;
+ }
+private:
+ std::string name_;
+};
+
+class PolyDerivedThrice: public PolyDerivedTwiceNotBound {
+public:
+ PolyDerivedThrice()
+ : PolyDerivedTwiceNotBound(),
+ name_("PolyDerivedThrice")
+ {}
+
+ std::string getClassName() const {
+ return name_;
+ }
+private:
+ std::string name_;
+};
+
+class PolyDerivedFourTimesNotBound: public PolyDerivedThrice {
+public:
+ PolyDerivedFourTimesNotBound()
+ : PolyDerivedThrice(),
+ name_("PolyDerivedFourTimesNotBound")
+ {}
+
+ std::string getClassName() const {
+ return name_;
+ }
+private:
+ std::string name_;
+};
+
+// todo: does it need to be polymorphic?
+// todo: virtual diamond pattern
+class PolyDiamondBase {
+public:
+ PolyDiamondBase():
+ name_("PolyBase")
+ {}
+ ~PolyDiamondBase() {}
+
+ std::string getClassName() const {
+ return name_;
+ }
+private:
+ std::string name_;
+};
+
+class PolyDiamondDerived: public PolyDiamondBase {
+public:
+ PolyDiamondDerived()
+ : PolyDiamondBase(),
+ name_("PolyDiamondDerived")
+ {}
+
+ std::string getClassName() const {
+ return name_;
+ }
+private:
+ std::string name_;
+};
+
+class PolyDiamondSiblingDerived: public PolyDiamondBase {
+public:
+ PolyDiamondSiblingDerived()
+ : PolyDiamondBase(),
+ name_("PolyDiamondSiblingDerived")
+ {}
+
+ std::string getClassName() const {
+ return name_;
+ }
+private:
+ std::string name_;
+};
+
+class PolyDiamondMultiplyDerived: public PolyDiamondDerived, public PolyDiamondSiblingDerived {
+public:
+ PolyDiamondMultiplyDerived()
+ : PolyDiamondDerived(), PolyDiamondSiblingDerived(),
+ name_("PolyDiamondMultiplyDerived")
+ {}
+
+ std::string getClassName() const {
+ return name_;
+ }
+private:
+ std::string name_;
+};
+
+// End Inheritance Hierarchy Class Definitions
+
+std::map<std::string, int> embind_test_get_string_int_map() {
+ std::map<std::string, int> m;
+
+ m["one"] = 1;
+ m["two"] = 2;
+
+ return m;
};
struct TupleVector {
@@ -187,6 +774,11 @@ struct CustomStruct {
CustomStruct()
: field(10)
{}
+
+ const int& getField() const {
+ return field;
+ }
+
int field;
};
@@ -210,52 +802,521 @@ EnumClass emval_test_take_and_return_EnumClass(EnumClass e) {
return e;
}
-class Interface {
+void emval_test_call_function(val v, int i, float f, TupleVector tv, StructVector sv) {
+ v(i, f, tv, sv);
+}
+
+std::unique_ptr<ValHolder> emval_test_return_unique_ptr() {
+ return std::unique_ptr<ValHolder>(new ValHolder(val::object()));
+}
+
+std::shared_ptr<ValHolder> emval_test_return_shared_ptr() {
+ return std::shared_ptr<ValHolder>(new ValHolder(val::object()));
+}
+
+std::shared_ptr<ValHolder> emval_test_return_empty_shared_ptr() {
+ return std::shared_ptr<ValHolder>();
+}
+
+bool emval_test_is_shared_ptr_null(std::shared_ptr<ValHolder> p) {
+ return !p;
+}
+
+static SmallClass smallClass;
+static BigClass bigClass;
+
+SmallClass embind_test_return_small_class_instance() {
+ return smallClass;
+}
+
+BigClass embind_test_return_big_class_instance() {
+ return bigClass;
+}
+
+int embind_test_accept_small_class_instance(SmallClass c) {
+ return c.member;
+}
+
+int embind_test_accept_big_class_instance(BigClass c) {
+ return c.member;
+}
+
+// Begin Inheritance Hierarchy Test Wrappers
+
+Base* embind_test_return_raw_base_ptr() {
+ return new Base();
+}
+
+Base* embind_test_return_raw_derived_ptr_as_base() {
+ return new Derived();
+}
+
+Base* embind_test_return_raw_sibling_derived_ptr_as_base() {
+ return new SiblingDerived();
+}
+
+PolyBase* embind_test_return_raw_polymorphic_derived_ptr_as_base() {
+ return new PolyDerived();
+}
+
+PolyBase* embind_test_return_raw_polymorphic_sibling_derived_ptr_as_base() {
+ return new PolySiblingDerived();
+}
+
+PolyBase* embind_test_return_raw_polymorphic_multiply_derived_ptr_as_base() {
+ return new PolyMultiplyDerived();
+}
+
+PolySecondBase* embind_test_return_raw_polymorphic_multiply_derived_ptr_as_second_base() {
+ return new PolyMultiplyDerived();
+}
+
+PolyBase* embind_test_return_raw_polymorphic_derived_four_times_not_bound_as_base() {
+ return new PolyDerivedFourTimesNotBound();
+}
+
+std::shared_ptr<Base> embind_test_return_smart_base_ptr() {
+ return std::shared_ptr<Base>(new Base());
+}
+
+std::shared_ptr<PolyBase> embind_test_return_smart_polymorphic_base_ptr() {
+ return std::shared_ptr<PolyBase>(new PolyBase("PolyBase"));
+}
+
+std::shared_ptr<Derived> embind_test_return_smart_derived_ptr() {
+ return std::shared_ptr<Derived>(new Derived());
+}
+
+std::shared_ptr<SiblingDerived> embind_test_return_smart_sibling_derived_ptr() {
+ return std::shared_ptr<SiblingDerived>(new SiblingDerived());
+}
+
+std::shared_ptr<MultiplyDerived> embind_test_return_smart_multiply_derived_ptr() {
+ return std::shared_ptr<MultiplyDerived>(new MultiplyDerived());
+}
+
+std::shared_ptr<DerivedThrice> embind_test_return_smart_derived_thrice_ptr() {
+ return std::shared_ptr<DerivedThrice>(new DerivedThrice());
+}
+
+std::shared_ptr<PolyDerived> embind_test_return_smart_polymorphic_derived_ptr() {
+ return std::shared_ptr<PolyDerived>(new PolyDerived());
+}
+
+std::shared_ptr<PolySiblingDerived> embind_test_return_smart_polymorphic_sibling_derived_ptr() {
+ return std::shared_ptr<PolySiblingDerived>(new PolySiblingDerived());
+}
+
+std::shared_ptr<PolyMultiplyDerived> embind_test_return_smart_polymorphic_multiply_derived_ptr() {
+ return std::shared_ptr<PolyMultiplyDerived>(new PolyMultiplyDerived());
+}
+
+std::shared_ptr<PolyBase> embind_test_return_poly_derived_twice_without_smart_pointer_as_poly_base() {
+ return std::shared_ptr<PolyBase>(new PolyDerivedTwiceWithoutSmartPointer());
+}
+
+std::shared_ptr<PolyDerivedThrice> embind_test_return_smart_poly_derived_thrice_ptr() {
+ return std::shared_ptr<PolyDerivedThrice>(new PolyDerivedThrice());
+}
+
+std::shared_ptr<PolyBase> embind_test_return_smart_derived_ptr_as_base() {
+ return std::shared_ptr<PolyBase>(new PolyDerived());
+}
+
+val embind_test_return_smart_derived_ptr_as_val() {
+ return val(std::shared_ptr<PolyBase>(new PolyDerived()));
+}
+
+std::shared_ptr<PolyBase> embind_test_return_smart_polymorphic_derived_ptr_as_base() {
+ return std::shared_ptr<PolyBase>(new PolyDerived());
+}
+
+std::shared_ptr<PolyBase> embind_test_return_smart_polymorphic_sibling_derived_ptr_as_base() {
+ return std::shared_ptr<PolyBase>(new PolySiblingDerived());
+}
+
+std::string embind_test_get_class_name_via_base_ptr(Base *p) {
+ return p->getClassName();
+}
+
+std::string embind_test_get_class_name_via_second_base_ptr(SecondBase *p) {
+ return p->getClassName();
+}
+
+std::string embind_test_get_class_name_via_polymorphic_base_ptr(PolyBase *p) {
+ return p->getClassName();
+}
+
+std::string embind_test_get_class_name_via_polymorphic_second_base_ptr(PolySecondBase *p) {
+ return p->getClassName();
+}
+
+std::string embind_test_get_class_name_via_smart_base_ptr(std::shared_ptr<Base> p) {
+ return p->getClassName();
+}
+
+std::string embind_test_get_class_name_via_reference_to_smart_base_ptr(std::shared_ptr<Base>& p) {
+ return p->getClassName();
+}
+
+std::string embind_test_get_class_name_via_smart_second_base_ptr(std::shared_ptr<SecondBase> p) {
+ return p->getClassName();
+}
+
+std::string embind_test_get_class_name_via_smart_polymorphic_base_ptr(std::shared_ptr<PolyBase> p) {
+ return p->getClassName();
+}
+
+std::string embind_test_get_virtual_class_name_via_smart_polymorphic_base_ptr(std::shared_ptr<PolyBase> p) {
+ return p->virtualGetClassName();
+}
+
+std::string embind_test_get_class_name_via_smart_polymorphic_second_base_ptr(std::shared_ptr<PolySecondBase> p) {
+ return p->getClassName();
+}
+
+void embind_modify_smart_pointer_passed_by_reference(std::shared_ptr<Base>& p) {
+ p = std::shared_ptr<Base>(new Base());
+ p->name = "Changed";
+}
+
+void embind_attempt_to_modify_smart_pointer_when_passed_by_value(std::shared_ptr<Base> p) {
+ p = std::shared_ptr<Base>(new Base());
+ p->name = "Changed";
+}
+
+static std::shared_ptr<Base> savedBasePointer;
+
+void embind_save_smart_base_pointer(std::shared_ptr<Base> p) {
+ savedBasePointer = p;
+}
+
+// End Inheritance Hierarchy Test Wrappers
+
+std::vector<int> emval_test_return_vector() {
+ int myints[] = { 10, 20, 30 };
+ return std::vector<int>(myints, myints + sizeof(myints) / sizeof(int));
+}
+
+std::vector<std::vector<int> > emval_test_return_vector_of_vectors() {
+ int myints1[] = { 10, 20, 30 };
+ int myints2[] = { 40, 50, 60 };
+ std::vector<int> vec1(myints1, myints1 + sizeof(myints1) / sizeof(int));
+ std::vector<int> vec2(myints2, myints2 + sizeof(myints2) / sizeof(int));
+ std::vector<std::vector<int>>vec3;
+ vec3.emplace_back(vec1);
+ vec3.emplace_back(vec2);
+ return vec3;
+}
+
+std::vector<std::shared_ptr<StringHolder>> emval_test_return_shared_ptr_vector() {
+ std::vector<std::shared_ptr<StringHolder>> sharedStrVector;
+ sharedStrVector.push_back(std::shared_ptr<StringHolder>(new StringHolder("string #1")));
+ sharedStrVector.push_back(std::shared_ptr<StringHolder>(new StringHolder("string #2")));
+
+ return sharedStrVector;
+}
+
+class JSInterfaceHolder {
public:
- virtual int method() = 0;
- virtual TupleInStruct method2(const TupleInStruct& arg1, float arg2) = 0;
- virtual void method3() = 0;
+ JSInterfaceHolder(JSInterface &jsobj) : jsobj_(jsobj) {
+ ptr_ = JSInterface::cloneToSharedPtr(jsobj_);
+ }
+
+ int callMethod(std::string method) { return jsobj_.call<int>(method.c_str()); }
+ int callMethodUsingSharedPtr(std::string method) { return ptr_->call<int>(method.c_str()); }
+
+private:
+ JSInterface jsobj_;
+ std::shared_ptr<JSInterface> ptr_;
};
-int emval_test_call_method(Interface& i) {
- return i.method();
+void test_string_with_vec(const std::string& p1, std::vector<std::string>& v1) {
+ // THIS DOES NOT WORK -- need to get as val and then call vecFromJSArray
+ printf("%s\n", p1.c_str());
}
-TupleInStruct emval_test_call_method2(Interface& i, const TupleInStruct& arg1, float arg2) {
- return i.method2(arg1, arg2);
+val embind_test_new_Object() {
+ return val::global("Object").new_();
}
-void emval_test_call_method3(Interface& i) {
- i.method3();
+val embind_test_new_factory(val factory, val argument) {
+ return factory.new_(10, std::string("hello"), argument);
}
-void emval_test_call_function(val v, int i, float f, TupleVector tv, StructVector sv) {
- v(i, f, tv, sv);
+class AbstractClass {
+public:
+ virtual ~AbstractClass() {}
+ virtual std::string abstractMethod() const = 0;
+};
+
+class AbstractClassWrapper : public wrapper<AbstractClass> {
+public:
+ EMSCRIPTEN_WRAPPER(AbstractClassWrapper);
+
+ std::string abstractMethod() const {
+ return call<std::string>("abstractMethod");
+ }
+};
+
+class ConcreteClass : public AbstractClass {
+ std::string abstractMethod() const {
+ return "from concrete";
+ }
+};
+
+std::shared_ptr<AbstractClass> getAbstractClass() {
+ return std::make_shared<ConcreteClass>();
}
-void optional_test_copy() {
- using emscripten::internal::optional;
+std::string callAbstractMethod(AbstractClass& ac) {
+ return ac.abstractMethod();
+}
- optional<int> foo = 22;
- optional<int> bar(foo);
+class HasExternalConstructor {
+public:
+ HasExternalConstructor(const std::string& str)
+ : m(str)
+ {}
+
+ std::string getString() const {
+ return m;
+ }
+
+ std::string m;
+};
- return bool(bar);
+HasExternalConstructor* createHasExternalConstructor(const std::string& str) {
+ return new HasExternalConstructor(str);
}
-void optional_test_copy2() {
- using emscripten::internal::optional;
+template<typename T>
+class CustomSmartPtr {
+public:
+ CustomSmartPtr()
+ : CustomSmartPtr(nullptr)
+ {
+ std::fill(d_, d_ + N_, Valid);
+ }
+
+ explicit CustomSmartPtr(T* t)
+ : ptr_(t)
+ {
+ std::fill(d_, d_ + N_, Valid);
+ }
+
+ CustomSmartPtr(const CustomSmartPtr& other)
+ : ptr_(other.ptr_)
+ {
+ other.verify();
+ std::fill(d_, d_ + N_, Valid);
+ if (ptr_) {
+ ++(ptr_->refcount);
+ }
+ }
- optional<int> foo;
- optional<int> bar(foo);
+ ~CustomSmartPtr() {
+ verify();
+ std::fill(d_, d_ + N_, Deleted);
+
+ if (ptr_ && --ptr_->refcount == 0) {
+ delete ptr_;
+ }
+ }
- return bool(bar);
+ T* get_raw() const {
+ return ptr_;
+ }
+
+private:
+ void verify() const {
+ for (size_t i = 0; i < N_; ++i) {
+ if (d_[i] != Valid) {
+ abort();
+ }
+ }
+ }
+
+ enum {
+ Valid = 255,
+ Deleted = 127,
+ };
+ static constexpr size_t N_ = 1000000;
+ unsigned char d_[N_];
+ T* ptr_;
+
+ CustomSmartPtr& operator=(const CustomSmartPtr&) = delete;
+};
+
+class HeldBySmartPtr {
+public:
+ HeldBySmartPtr(int i, const std::string& s)
+ : i(i)
+ , s(s)
+ {}
+
+ static CustomSmartPtr<HeldBySmartPtr> newCustomPtr(int i, const std::string& s) {
+ return CustomSmartPtr<HeldBySmartPtr>(new HeldBySmartPtr(i, s));
+ }
+
+ int refcount = 1;
+ int i;
+ std::string s;
+};
+
+HeldBySmartPtr takesHeldBySmartPtr(HeldBySmartPtr p) {
+ return p;
+}
+std::shared_ptr<HeldBySmartPtr> takesHeldBySmartPtrSharedPtr(std::shared_ptr<HeldBySmartPtr> p) {
+ return p;
}
-EMSCRIPTEN_BINDINGS(([]() {
- function("mallinfo", &emval_test_mallinfo);
+namespace emscripten {
+ template<typename T>
+ struct smart_ptr_trait<CustomSmartPtr<T>> {
+ typedef T element_type;
+
+ static sharing_policy get_sharing_policy() {
+ return sharing_policy::NONE;
+ }
+
+ static T* get(const CustomSmartPtr<T>& p) {
+ return p.get_raw();
+ }
+
+ static CustomSmartPtr<T> share(const CustomSmartPtr<T>& r, T* ptr) {
+ ++ptr->refcount; // implement an adopt API?
+ return CustomSmartPtr<T>(ptr);
+ }
+ };
+}
+
+typedef CustomSmartPtr<class HeldByCustomSmartPtr> HeldByCustomSmartPtrPtr;
+
+class HeldByCustomSmartPtr {
+public:
+ HeldByCustomSmartPtr(int i, const std::string& s)
+ : i(i)
+ , s(s)
+ {}
+
+ static HeldByCustomSmartPtrPtr create(int i, const std::string& s) {
+ return HeldByCustomSmartPtrPtr(new HeldByCustomSmartPtr(i, s));
+ }
+ static std::shared_ptr<HeldByCustomSmartPtr> createSharedPtr(int i, const std::string& s) {
+ return std::make_shared<HeldByCustomSmartPtr>(i, s);
+ };
+
+ int refcount = 1;
+ int i;
+ std::string s;
+};
+
+HeldByCustomSmartPtr* passThroughRawPtr(HeldByCustomSmartPtr* p) {
+ return p;
+}
+HeldByCustomSmartPtrPtr passThroughCustomSmartPtr(HeldByCustomSmartPtrPtr p) {
+ return p;
+}
+
+struct Base1 {
+public:
+ Base1(): field1("Base1") {}
+ std::string field1;
+
+ std::string getField() const {
+ return field1;
+ }
+};
+
+struct Base2 {
+public:
+ Base2(): field2("Base2") {}
+ std::string field2;
+
+ std::string getField() const {
+ return field2;
+ }
+};
+
+struct HasTwoBases : public Base1, public Base2 {
+};
+
+val get_module_property(const std::string& s) {
+ return val::module_property(s.c_str());
+}
+
+std::string char_to_string(char ch) {
+ char str[256];
+ sprintf(str, "%d", (int)ch);
+ return str;
+}
+
+std::string signed_char_to_string(signed char ch) {
+ char str[256];
+ sprintf(str, "%hhd", ch);
+ return str;
+}
+
+std::string unsigned_char_to_string(unsigned char ch) {
+ char str[256];
+ sprintf(str, "%hhu", ch);
+ return str;
+}
+
+std::string short_to_string(short val) {
+ char str[256];
+ sprintf(str, "%hd", val);
+ return str;
+}
+
+std::string unsigned_short_to_string(unsigned short val) {
+ char str[256];
+ sprintf(str, "%hu", val);
+ return str;
+}
+
+std::string int_to_string(int val) {
+ char str[256];
+ sprintf(str, "%d", val);
+ return str;
+}
+
+std::string unsigned_int_to_string(unsigned int val) {
+ char str[256];
+ sprintf(str, "%u", val);
+ return str;
+}
+
+std::string long_to_string(long val) {
+ char str[256];
+ sprintf(str, "%ld", val);
+ return str;
+}
+
+std::string unsigned_long_to_string(unsigned long val) {
+ char str[256];
+ sprintf(str, "%lu", val);
+ return str;
+}
+
+EMSCRIPTEN_BINDINGS(tests) {
+ register_js_interface();
+
+ register_vector<int>("IntegerVector");
+ register_vector<char>("CharVector");
+ register_vector<unsigned>("VectorUnsigned");
+ register_vector<unsigned char>("VectorUnsignedChar");
+ register_vector<std::string>("StringVector");
+ register_vector<emscripten::val>("EmValVector");
+ register_vector<float>("FloatVector");
+ register_vector<std::vector<int>>("IntegerVectorVector");
+
+ function("mallinfo", &emval_test_mallinfo);
function("emval_test_new_integer", &emval_test_new_integer);
function("emval_test_new_string", &emval_test_new_string);
+ function("emval_test_get_string_from_val", &emval_test_get_string_from_val);
function("emval_test_new_object", &emval_test_new_object);
function("emval_test_passthrough_unsigned", &emval_test_passthrough_unsigned);
function("emval_test_passthrough", &emval_test_passthrough);
@@ -265,6 +1326,7 @@ EMSCRIPTEN_BINDINGS(([]() {
function("emval_test_as_unsigned", &emval_test_as_unsigned);
function("emval_test_get_length", &emval_test_get_length);
function("emval_test_add", &emval_test_add);
+ function("const_ref_adder", &const_ref_adder