Skip to content

Commit c95f089

Browse files
authored
[embind] Allow optional types to contain pointers. (#21769)
Previously, register_vector and register_map didn't use std::optional, but allowed pointers. After changing them to use std::optional, pointers were no longer were allowed. This restores the previous behavior. Fixes #21768
1 parent 8b85e96 commit c95f089

File tree

3 files changed

+36
-1
lines changed

3 files changed

+36
-1
lines changed

system/include/emscripten/bind.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1885,7 +1885,9 @@ void register_optional() {
18851885
return;
18861886
}
18871887
hasRun = true;
1888-
internal::_embind_register_optional(internal::TypeID<std::optional<T>>::get(), internal::TypeID<T>::get());
1888+
internal::_embind_register_optional(
1889+
internal::TypeID<std::optional<T>>::get(),
1890+
internal::TypeID<typename std::remove_pointer<T>::type>::get());
18891891
}
18901892
#endif
18911893

test/embind/embind.test.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1160,6 +1160,14 @@ module({
11601160
assert.equal(20, vec.get(1));
11611161
vec.delete();
11621162
});
1163+
1164+
test("vectors can contain pointers", function() {
1165+
var vec = cm.emval_test_return_vector_pointers();
1166+
var small = vec.get(0);
1167+
assert.equal(7, small.member);
1168+
small.delete();
1169+
vec.delete();
1170+
});
11631171
});
11641172

11651173
BaseFixture.extend("map", function() {
@@ -1233,6 +1241,15 @@ module({
12331241
assert.equal(undefined, optional);
12341242
});
12351243

1244+
test("std::optional works with returning SmallClass pointer", function() {
1245+
var optional = cm.embind_test_return_optional_small_class_pointer(true);
1246+
assert.equal(7, optional.member);
1247+
optional.delete();
1248+
1249+
optional = cm.embind_test_return_optional_small_class(false);
1250+
assert.equal(undefined, optional);
1251+
});
1252+
12361253
test("std::optional works with returning string", function() {
12371254
var optional = cm.embind_test_return_optional_string(true);
12381255
assert.equal("hello", optional);

test/embind/embind_test.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1309,6 +1309,12 @@ std::vector<std::shared_ptr<StringHolder>> emval_test_return_shared_ptr_vector()
13091309
return sharedStrVector;
13101310
}
13111311

1312+
std::vector<SmallClass*> emval_test_return_vector_pointers() {
1313+
std::vector<SmallClass*> vec;
1314+
vec.push_back(new SmallClass());
1315+
return vec;
1316+
}
1317+
13121318
void test_string_with_vec(const std::string& p1, std::vector<std::string>& v1) {
13131319
// THIS DOES NOT WORK -- need to get as val and then call vecFromJSArray
13141320
printf("%s\n", p1.c_str());
@@ -1339,6 +1345,12 @@ std::optional<SmallClass> embind_test_return_optional_small_class(bool create) {
13391345
}
13401346
return {};
13411347
}
1348+
std::optional<SmallClass*> embind_test_return_optional_small_class_pointer(bool create) {
1349+
if (create) {
1350+
return new SmallClass();
1351+
}
1352+
return {};
1353+
}
13421354

13431355
int embind_test_optional_int_arg(std::optional<int> arg) {
13441356
if (arg) {
@@ -1885,6 +1897,7 @@ EMSCRIPTEN_BINDINGS(tests) {
18851897
register_vector<emscripten::val>("EmValVector");
18861898
register_vector<float>("FloatVector");
18871899
register_vector<std::vector<int>>("IntegerVectorVector");
1900+
register_vector<SmallClass*>("SmallClassPointerVector");
18881901

18891902
class_<DummyForPointer>("DummyForPointer");
18901903

@@ -2352,6 +2365,7 @@ EMSCRIPTEN_BINDINGS(tests) {
23522365

23532366
function("emval_test_return_vector", &emval_test_return_vector);
23542367
function("emval_test_return_vector_of_vectors", &emval_test_return_vector_of_vectors);
2368+
function("emval_test_return_vector_pointers", &emval_test_return_vector_pointers);
23552369

23562370
register_vector<std::shared_ptr<StringHolder>>("SharedPtrVector");
23572371
function("emval_test_return_shared_ptr_vector", &emval_test_return_shared_ptr_vector);
@@ -2371,10 +2385,12 @@ EMSCRIPTEN_BINDINGS(tests) {
23712385
register_optional<int>();
23722386
register_optional<float>();
23732387
register_optional<SmallClass>();
2388+
register_optional<SmallClass*>();
23742389
register_optional<std::string>();
23752390
function("embind_test_return_optional_int", &embind_test_return_optional_int);
23762391
function("embind_test_return_optional_float", &embind_test_return_optional_float);
23772392
function("embind_test_return_optional_small_class", &embind_test_return_optional_small_class);
2393+
function("embind_test_return_optional_small_class_pointer", &embind_test_return_optional_small_class_pointer);
23782394
function("embind_test_return_optional_string", &embind_test_return_optional_string);
23792395
function("embind_test_optional_int_arg", &embind_test_optional_int_arg);
23802396
function("embind_test_optional_float_arg", &embind_test_optional_float_arg);

0 commit comments

Comments
 (0)