aboutsummaryrefslogtreecommitdiff
path: root/tests/embind
diff options
context:
space:
mode:
Diffstat (limited to 'tests/embind')
-rw-r--r--tests/embind/build_benchmark2
-rw-r--r--tests/embind/embind.test.js82
-rw-r--r--tests/embind/embind_test.cpp113
3 files changed, 167 insertions, 30 deletions
diff --git a/tests/embind/build_benchmark b/tests/embind/build_benchmark
index 3d5d816b..74239406 100644
--- a/tests/embind/build_benchmark
+++ b/tests/embind/build_benchmark
@@ -1,2 +1,2 @@
#!/bin/bash
-EMCC_LLVM_TARGET=le32-unknown-nacl ../../emcc --minify 0 --bind --post-js embind.benchmark.js -O2 --shell-file shell.html -o embind_benchmark.html embind_benchmark.cpp
+EMCC_LLVM_TARGET=asmjs-unknown-emscripten ../../emcc --minify 0 --bind --post-js embind.benchmark.js -O2 --shell-file shell.html -o embind_benchmark.html embind_benchmark.cpp
diff --git a/tests/embind/embind.test.js b/tests/embind/embind.test.js
index 5ca972be..6bba4de0 100644
--- a/tests/embind/embind.test.js
+++ b/tests/embind/embind.test.js
@@ -1408,7 +1408,7 @@ module({
var e = assert.throws(cm.BindingError, function() {
cm.passThroughCustomSmartPtr(o);
});
- assert.equal('Cannot convert argument of type NSt3__110shared_ptrI20HeldByCustomSmartPtrEE to parameter type 14CustomSmartPtrI20HeldByCustomSmartPtrE', e.message);
+ assert.equal('Cannot convert argument of type shared_ptr<HeldByCustomSmartPtr> to parameter type CustomSmartPtr<HeldByCustomSmartPtr>', e.message);
o.delete();
});
@@ -1646,6 +1646,10 @@ module({
if (typeof INVOKED_FROM_EMSCRIPTEN_TEST_RUNNER === "undefined") { // TODO: Enable this to work in Emscripten runner as well!
BaseFixture.extend("unbound types", function() {
+ if (!cm.hasUnboundTypeNames) {
+ return;
+ }
+
function assertMessage(fn, message) {
var e = assert.throws(cm.UnboundTypeError, fn);
assert.equal(message, e.message);
@@ -1938,10 +1942,84 @@ module({
derived.delete();
// Let the memory leak test superfixture check that no leaks occurred.
});
+
+ BaseFixture.extend("val::as", function() {
+ test("built-ins", function() {
+ assert.equal(true, cm.val_as_bool(true));
+ assert.equal(false, cm.val_as_bool(false));
+ assert.equal(127, cm.val_as_char(127));
+ assert.equal(32767, cm.val_as_short(32767));
+ assert.equal(65536, cm.val_as_int(65536));
+ assert.equal(65536, cm.val_as_long(65536));
+ assert.equal(10.5, cm.val_as_float(10.5));
+ assert.equal(10.5, cm.val_as_double(10.5));
+
+ assert.equal("foo", cm.val_as_string("foo"));
+ assert.equal("foo", cm.val_as_wstring("foo"));
+
+ var obj = {};
+ assert.equal(obj, cm.val_as_val(obj));
+
+ // JS->C++ memory view not implemented
+ //var ab = cm.val_as_memory_view(new ArrayBuffer(13));
+ //assert.equal(13, ab.byteLength);
+ });
+
+ test("value types", function() {
+ var tuple = [1, 2, 3, 4];
+ assert.deepEqual(tuple, cm.val_as_value_array(tuple));
+
+ var struct = {x: 1, y: 2, z: 3, w: 4};
+ assert.deepEqual(struct, cm.val_as_value_object(struct));
+ });
+
+ test("enums", function() {
+ assert.equal(cm.Enum.ONE, cm.val_as_enum(cm.Enum.ONE));
+ });
+ });
+
+ BaseFixture.extend("val::new_", function() {
+ test("variety of types", function() {
+ function factory() {
+ this.arguments = Array.prototype.slice.call(arguments, 0);
+ }
+ var instance = cm.construct_with_6_arguments(factory);
+ assert.deepEqual(
+ [6, -12.5, "a3", {x: 1, y: 2, z: 3, w: 4}, cm.EnumClass.TWO, [-1, -2, -3, -4]],
+ instance.arguments);
+ });
+
+ test("memory view", function() {
+ function factory(before, view, after) {
+ this.before = before;
+ this.view = view;
+ this.after = after;
+ }
+
+ var instance = cm.construct_with_memory_view(factory);
+ assert.equal("before", instance.before);
+ assert.equal(10, instance.view.byteLength);
+ assert.equal("after", instance.after);
+ });
+
+ test("ints_and_float", function() {
+ function factory(a, b, c) {
+ this.a = a;
+ this.b = b;
+ this.c = c;
+ }
+
+ var instance = cm.construct_with_ints_and_float(factory);
+ assert.equal(65537, instance.a);
+ assert.equal(4.0, instance.b);
+ assert.equal(65538, instance.c);
+ });
+ });
});
/* global run_all_tests */
// If running as part of the emscripten test runner suite, and not as part of the IMVU suite,
// we launch the test execution from here. IMVU suite uses its own dedicated mechanism instead of this.
-if (typeof run_all_tests !== "undefined")
+if (typeof run_all_tests !== "undefined") {
run_all_tests();
+}
diff --git a/tests/embind/embind_test.cpp b/tests/embind/embind_test.cpp
index 4efc4bd8..1b835751 100644
--- a/tests/embind/embind_test.cpp
+++ b/tests/embind/embind_test.cpp
@@ -842,7 +842,7 @@ Enum emval_test_take_and_return_Enum(Enum e) {
return e;
}
-enum class EnumClass { ONE, TWO };
+enum class EnumClass : char { ONE, TWO };
EnumClass emval_test_take_and_return_EnumClass(EnumClass e) {
return e;
@@ -1150,8 +1150,8 @@ void callDifferentArguments(AbstractClass& ac, int i, double d, unsigned char f,
EMSCRIPTEN_BINDINGS(interface_tests) {
class_<AbstractClass>("AbstractClass")
- .smart_ptr<std::shared_ptr<AbstractClass>>()
- .allow_subclass<AbstractClassWrapper>()
+ .smart_ptr<std::shared_ptr<AbstractClass>>("shared_ptr<AbstractClass>")
+ .allow_subclass<AbstractClassWrapper>("AbstractClassWrapper")
.function("abstractMethod", &AbstractClass::abstractMethod)
.function("optionalMethod", &AbstractClass::optionalMethod)
;
@@ -1483,7 +1483,7 @@ EMSCRIPTEN_BINDINGS(tests) {
function("emval_test_take_and_return_TupleInStruct", &emval_test_take_and_return_TupleInStruct);
class_<ValHolder>("ValHolder")
- .smart_ptr<std::shared_ptr<ValHolder>>()
+ .smart_ptr<std::shared_ptr<ValHolder>>("std::shared_ptr<ValHolder>")
.constructor<val>()
.function("getVal", &ValHolder::getVal)
.function("getValNonConst", &ValHolder::getValNonConst)
@@ -1522,7 +1522,7 @@ EMSCRIPTEN_BINDINGS(tests) {
function("emval_test_take_and_call_functor", &emval_test_take_and_call_functor);
class_<StringHolder>("StringHolder")
- .smart_ptr<std::shared_ptr<StringHolder>>()
+ .smart_ptr<std::shared_ptr<StringHolder>>("shared_ptr<StringHolder>")
.constructor<std::string>()
.function("set", &StringHolder::set)
.function("get", &StringHolder::get)
@@ -1566,7 +1566,7 @@ EMSCRIPTEN_BINDINGS(tests) {
// register Derived before Base as a test that it's possible to
// register base classes afterwards
class_<Derived, base<Base>>("Derived")
- .smart_ptr<std::shared_ptr<Derived>>()
+ .smart_ptr<std::shared_ptr<Derived>>("shared_ptr<Derived>")
.constructor<>()
.function("getClassName", &Derived::getClassName)
.function("getMember", &Derived::getMember)
@@ -1575,7 +1575,7 @@ EMSCRIPTEN_BINDINGS(tests) {
;
class_<Base>("Base")
- .smart_ptr<std::shared_ptr<Base>>()
+ .smart_ptr<std::shared_ptr<Base>>("shared_ptr<Base")
.constructor<>()
.function("getClassName", &Base::getClassName)
.function("getClassNameFromBase", &Base::getClassNameFromBase)
@@ -1589,7 +1589,7 @@ EMSCRIPTEN_BINDINGS(tests) {
;
class_<SecondBase>("SecondBase")
- .smart_ptr<std::shared_ptr<SecondBase>>()
+ .smart_ptr<std::shared_ptr<SecondBase>>("shared_ptr<SecondBase>")
.constructor<>()
.function("getClassName", &SecondBase::getClassName)
.function("getClassNameFromSecondBase", &SecondBase::getClassNameFromSecondBase)
@@ -1612,13 +1612,13 @@ EMSCRIPTEN_BINDINGS(tests) {
;
class_<SiblingDerived>("SiblingDerived")
- .smart_ptr<std::shared_ptr<SiblingDerived>>()
+ .smart_ptr<std::shared_ptr<SiblingDerived>>("shared_ptr<SiblingDerived>")
.constructor<>()
.function("getClassName", &SiblingDerived::getClassName)
;
class_<MultiplyDerived, base<Base>>("MultiplyDerived")
- .smart_ptr<std::shared_ptr<MultiplyDerived>>()
+ .smart_ptr<std::shared_ptr<MultiplyDerived>>("shared_ptr<MultiplyDerived>")
.constructor<>()
.function("getClassName", &MultiplyDerived::getClassName)
.class_function("getInstanceCount", &MultiplyDerived::getInstanceCount)
@@ -1630,26 +1630,26 @@ EMSCRIPTEN_BINDINGS(tests) {
;
class_<DerivedThrice, base<Derived> >("DerivedThrice")
- .smart_ptr<std::shared_ptr<DerivedThrice>>()
+ .smart_ptr<std::shared_ptr<DerivedThrice>>("shared_ptr<DerivedThrice>")
.constructor<>()
.function("getClassName", &DerivedThrice::getClassName)
;
class_<PolyBase>("PolyBase")
- .smart_ptr<std::shared_ptr<PolyBase>>()
+ .smart_ptr<std::shared_ptr<PolyBase>>("shared_ptr<PolyBase>")
.constructor<>()
.function("virtualGetClassName", &PolyBase::virtualGetClassName)
.function("getClassName", &PolyBase::getClassName)
;
class_<PolySecondBase>("PolySecondBase")
- .smart_ptr<std::shared_ptr<PolySecondBase>>()
+ .smart_ptr<std::shared_ptr<PolySecondBase>>("shared_ptr<PolySecondBase>")
.constructor<>()
.function("getClassName", &PolySecondBase::getClassName)
;
class_<PolyDerived, base<PolyBase>>("PolyDerived")
- .smart_ptr<std::shared_ptr<PolyDerived>>()
+ .smart_ptr<std::shared_ptr<PolyDerived>>("shared_ptr<PolyDerived>")
.constructor<>()
.function("virtualGetClassName", &PolyDerived::virtualGetClassName)
.function("getClassName", &PolyDerived::getClassName)
@@ -1671,43 +1671,43 @@ EMSCRIPTEN_BINDINGS(tests) {
// }
class_<PolySiblingDerived, base<PolyBase>>("PolySiblingDerived")
- .smart_ptr<std::shared_ptr<PolySiblingDerived>>()
+ .smart_ptr<std::shared_ptr<PolySiblingDerived>>("shared_ptr<PolySiblingDerived>")
.constructor<>()
.function("getClassName", &PolySiblingDerived::getClassName)
;
class_<PolyMultiplyDerived, base<PolyBase>>("PolyMultiplyDerived")
- .smart_ptr<std::shared_ptr<PolyMultiplyDerived>>()
+ .smart_ptr<std::shared_ptr<PolyMultiplyDerived>>("shared_ptr<PolyMultiplyDerived>")
.constructor<>()
.function("getClassName", &PolyMultiplyDerived::getClassName)
;
class_<PolyDerivedThrice, base<PolyDerived>>("PolyDerivedThrice")
- .smart_ptr<std::shared_ptr<PolyDerivedThrice>>()
+ .smart_ptr<std::shared_ptr<PolyDerivedThrice>>("shared_ptr<PolyDerivedThrice>")
.constructor<>()
.function("getClassName", &PolyDerivedThrice::getClassName)
;
class_<PolyDiamondBase>("PolyDiamondBase")
- .smart_ptr<std::shared_ptr<PolyDiamondBase>>()
+ .smart_ptr<std::shared_ptr<PolyDiamondBase>>("shared_ptr<PolyDiamondBase>")
.constructor<>()
.function("getClassName", &PolyDiamondBase::getClassName)
;
class_<PolyDiamondDerived>("PolyDiamondDerived")
- .smart_ptr<std::shared_ptr<PolyDiamondDerived>>()
+ .smart_ptr<std::shared_ptr<PolyDiamondDerived>>("shared_ptr<PolyDiamondDerived>")
.constructor<>()
.function("getClassName", &PolyDiamondDerived::getClassName)
;
class_<PolyDiamondSiblingDerived>("PolyDiamondSiblingDerived")
- .smart_ptr<std::shared_ptr<PolyDiamondSiblingDerived>>()
+ .smart_ptr<std::shared_ptr<PolyDiamondSiblingDerived>>("shared_ptr<PolyDiamondSiblingDerived>")
.constructor<>()
.function("getClassName", &PolyDiamondSiblingDerived::getClassName)
;
class_<PolyDiamondMultiplyDerived>("PolyDiamondMultiplyDerived")
- .smart_ptr<std::shared_ptr<PolyDiamondMultiplyDerived>>()
+ .smart_ptr<std::shared_ptr<PolyDiamondMultiplyDerived>>("shared_ptr<PolyDiamondMultiplyDerived>")
.constructor<>()
.function("getClassName", &PolyDiamondMultiplyDerived::getClassName)
;
@@ -1825,8 +1825,8 @@ EMSCRIPTEN_BINDINGS(tests) {
auto HeldBySmartPtr_class = class_<HeldBySmartPtr>("HeldBySmartPtr");
HeldBySmartPtr_class
- .smart_ptr<CustomSmartPtr<HeldBySmartPtr>>()
- .smart_ptr_constructor(&std::make_shared<HeldBySmartPtr, int, std::string>)
+ .smart_ptr<CustomSmartPtr<HeldBySmartPtr>>("CustomSmartPtr<HeldBySmartPtr>")
+ .smart_ptr_constructor("shared_ptr<HeldbySmartPtr>", &std::make_shared<HeldBySmartPtr, int, std::string>)
.class_function("newCustomPtr", HeldBySmartPtr::newCustomPtr)
.function("returnThis", &takesHeldBySmartPtrSharedPtr)
.property("i", &HeldBySmartPtr::i)
@@ -1836,8 +1836,8 @@ EMSCRIPTEN_BINDINGS(tests) {
function("takesHeldBySmartPtrSharedPtr", &takesHeldBySmartPtrSharedPtr);
class_<HeldByCustomSmartPtr>("HeldByCustomSmartPtr")
- .smart_ptr<std::shared_ptr<HeldByCustomSmartPtr>>()
- .smart_ptr_constructor(&HeldByCustomSmartPtr::create)
+ .smart_ptr<std::shared_ptr<HeldByCustomSmartPtr>>("shared_ptr<HeldByCustomSmartPtr>")
+ .smart_ptr_constructor("CustomSmartPtr<HeldByCustomSmartPtr>", &HeldByCustomSmartPtr::create)
.class_function("createSharedPtr", &HeldByCustomSmartPtr::createSharedPtr)
.property("i", &HeldByCustomSmartPtr::i)
.property("s", &HeldByCustomSmartPtr::s)
@@ -2022,7 +2022,7 @@ EMSCRIPTEN_BINDINGS(overloads) {
;
class_<MultipleSmartCtors>("MultipleSmartCtors")
- .smart_ptr<std::shared_ptr<MultipleSmartCtors>>()
+ .smart_ptr<std::shared_ptr<MultipleSmartCtors>>("shared_ptr<MultipleSmartCtors>")
.constructor(&std::make_shared<MultipleSmartCtors, int>)
.constructor(&std::make_shared<MultipleSmartCtors, int, int>)
.function("WhichCtorCalled", &MultipleSmartCtors::WhichCtorCalled)
@@ -2147,6 +2147,8 @@ struct BoundClass {
};
EMSCRIPTEN_BINDINGS(incomplete) {
+ constant("hasUnboundTypeNames", emscripten::has_unbound_type_names);
+
function("getUnboundClass", &passThrough<UnboundClass>);
class_<HasUnboundBase, base<UnboundClass>>("HasUnboundBase")
@@ -2247,7 +2249,7 @@ std::shared_ptr<Base> return_Base_from_DerivedWithOffset(std::shared_ptr<Derived
EMSCRIPTEN_BINDINGS(with_adjustment) {
class_<DerivedWithOffset, base<Base>>("DerivedWithOffset")
- .smart_ptr_constructor(&std::make_shared<DerivedWithOffset>)
+ .smart_ptr_constructor("shared_ptr<DerivedWithOffset>", &std::make_shared<DerivedWithOffset>)
;
function("return_Base_from_DerivedWithOffset", &return_Base_from_DerivedWithOffset);
@@ -2304,3 +2306,60 @@ EMSCRIPTEN_BINDINGS(mixins) {
.constructor<>()
);
}
+
+template<typename T>
+T val_as(const val& v) {
+ return v.as<T>();
+}
+
+EMSCRIPTEN_BINDINGS(val_as) {
+ function("val_as_bool", &val_as<bool>);
+ function("val_as_char", &val_as<char>);
+ function("val_as_short", &val_as<short>);
+ function("val_as_int", &val_as<int>);
+ function("val_as_long", &val_as<long>);
+
+ function("val_as_float", &val_as<float>);
+ function("val_as_double", &val_as<double>);
+
+ function("val_as_string", &val_as<std::string>);
+ function("val_as_wstring", &val_as<std::wstring>);
+ function("val_as_val", &val_as<val>);
+
+ function("val_as_value_object", &val_as<StructVector>);
+ function("val_as_value_array", &val_as<TupleVector>);
+
+ function("val_as_enum", &val_as<Enum>);
+
+ // memory_view is always JS -> C++
+ //function("val_as_memory_view", &val_as<memory_view>);
+}
+
+val construct_with_6(val factory) {
+ unsigned char a1 = 6;
+ double a2 = -12.5;
+ std::string a3("a3");
+ StructVector a4(1, 2, 3, 4);
+ EnumClass a5 = EnumClass::TWO;
+ TupleVector a6(-1, -2, -3, -4);
+ return factory.new_(a1, a2, a3, a4, a5, a6);
+}
+
+val construct_with_memory_view(val factory) {
+ static const char data[11] = "0123456789";
+ return factory.new_(
+ std::string("before"),
+ memory_view(10, data),
+ std::string("after"));
+}
+
+val construct_with_ints_and_float(val factory) {
+ static const char data[11] = "0123456789";
+ return factory.new_(65537, 4.0f, 65538);
+}
+
+EMSCRIPTEN_BINDINGS(val_new_) {
+ function("construct_with_6_arguments", &construct_with_6);
+ function("construct_with_memory_view", &construct_with_memory_view);
+ function("construct_with_ints_and_float", &construct_with_ints_and_float);
+}