diff --git a/system/include/emscripten/bind.h b/system/include/emscripten/bind.h index c13ae0cdf765..ca550b368530 100644 --- a/system/include/emscripten/bind.h +++ b/system/include/emscripten/bind.h @@ -1885,7 +1885,9 @@ void register_optional() { return; } hasRun = true; - internal::_embind_register_optional(internal::TypeID>::get(), internal::TypeID::get()); + internal::_embind_register_optional( + internal::TypeID>::get(), + internal::TypeID::type>::get()); } #endif diff --git a/test/embind/embind.test.js b/test/embind/embind.test.js index 2d9db9fd169d..822dbec64183 100644 --- a/test/embind/embind.test.js +++ b/test/embind/embind.test.js @@ -1160,6 +1160,14 @@ module({ assert.equal(20, vec.get(1)); vec.delete(); }); + + test("vectors can contain pointers", function() { + var vec = cm.emval_test_return_vector_pointers(); + var small = vec.get(0); + assert.equal(7, small.member); + small.delete(); + vec.delete(); + }); }); BaseFixture.extend("map", function() { @@ -1233,6 +1241,15 @@ module({ assert.equal(undefined, optional); }); + test("std::optional works with returning SmallClass pointer", function() { + var optional = cm.embind_test_return_optional_small_class_pointer(true); + assert.equal(7, optional.member); + optional.delete(); + + optional = cm.embind_test_return_optional_small_class(false); + assert.equal(undefined, optional); + }); + test("std::optional works with returning string", function() { var optional = cm.embind_test_return_optional_string(true); assert.equal("hello", optional); diff --git a/test/embind/embind_test.cpp b/test/embind/embind_test.cpp index b17725677b19..ca1472295439 100644 --- a/test/embind/embind_test.cpp +++ b/test/embind/embind_test.cpp @@ -1309,6 +1309,12 @@ std::vector> emval_test_return_shared_ptr_vector() return sharedStrVector; } +std::vector emval_test_return_vector_pointers() { + std::vector vec; + vec.push_back(new SmallClass()); + return vec; +} + void test_string_with_vec(const std::string& p1, std::vector& v1) { // THIS DOES NOT WORK -- need to get as val and then call vecFromJSArray printf("%s\n", p1.c_str()); @@ -1339,6 +1345,12 @@ std::optional embind_test_return_optional_small_class(bool create) { } return {}; } +std::optional embind_test_return_optional_small_class_pointer(bool create) { + if (create) { + return new SmallClass(); + } + return {}; +} int embind_test_optional_int_arg(std::optional arg) { if (arg) { @@ -1885,6 +1897,7 @@ EMSCRIPTEN_BINDINGS(tests) { register_vector("EmValVector"); register_vector("FloatVector"); register_vector>("IntegerVectorVector"); + register_vector("SmallClassPointerVector"); class_("DummyForPointer"); @@ -2352,6 +2365,7 @@ EMSCRIPTEN_BINDINGS(tests) { function("emval_test_return_vector", &emval_test_return_vector); function("emval_test_return_vector_of_vectors", &emval_test_return_vector_of_vectors); + function("emval_test_return_vector_pointers", &emval_test_return_vector_pointers); register_vector>("SharedPtrVector"); function("emval_test_return_shared_ptr_vector", &emval_test_return_shared_ptr_vector); @@ -2371,10 +2385,12 @@ EMSCRIPTEN_BINDINGS(tests) { register_optional(); register_optional(); register_optional(); + register_optional(); register_optional(); function("embind_test_return_optional_int", &embind_test_return_optional_int); function("embind_test_return_optional_float", &embind_test_return_optional_float); function("embind_test_return_optional_small_class", &embind_test_return_optional_small_class); + function("embind_test_return_optional_small_class_pointer", &embind_test_return_optional_small_class_pointer); function("embind_test_return_optional_string", &embind_test_return_optional_string); function("embind_test_optional_int_arg", &embind_test_optional_int_arg); function("embind_test_optional_float_arg", &embind_test_optional_float_arg);