diff options
Diffstat (limited to 'tests')
-rw-r--r-- | tests/embind/embind.test.js | 76 | ||||
-rw-r--r-- | tests/embind/embind_test.cpp | 59 |
2 files changed, 133 insertions, 2 deletions
diff --git a/tests/embind/embind.test.js b/tests/embind/embind.test.js index 5ca972be..e2160c33 100644 --- a/tests/embind/embind.test.js +++ b/tests/embind/embind.test.js @@ -1938,10 +1938,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..d299660a 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; @@ -2304,3 +2304,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); +} |