emscripten-core / emscripten-core/emscripten
Embind doesn't accept string literals
- Dominant language
- C++
- Stars
- 27.6k
- Forks
- 3.6k
- Avg merge
- 1d 6h
- Merged PRs (30d)
- 109
Description
If you try to do something like
```cpp
globalProperty.call("foo", "abc");
```
on a `val` instance, Embind will fail to compile with an error like:
```
test_embind_with_pthreads (test_browser.browser) ... /usr/local/google/home/rreverser/emscripten/tests/embind_with_pthreads.cpp:17:8: error: variable has incomplete type 'void'
auto result = globalProperty.call("foo", "abc");
^
In file included from /usr/local/google/home/rreverser/emscripten/tests/embind_with_pthreads.cpp:6:
In file included from /usr/local/google/home/rreverser/emscripten/system/include/emscripten/val.h:15:
/usr/local/google/home/rreverser/emscripten/system/include/emscripten/wire.h:350:24: error: array 'new' cannot have initialization arguments
return new T(v);
^ ~
/usr/local/google/home/rreverser/emscripten/system/include/emscripten/val.h:231:62: note: in instantiation of member function 'emscripten::internal::GenericBindingType::toWireType' requested here
writeGenericWireType(cursor, BindingType::toWireType(std::forward(first)));
^
/usr/local/google/home/rreverser/emscripten/system/include/emscripten/val.h:239:17: note: in instantiation of function template specialization 'emscripten::internal::writeGenericWireTypes' requested here
writeGenericWireTypes(cursor, std::forward(args)...);
^
/usr/local/google/home/rreverser/emscripten/system/include/emscripten/val.h:273:39: note: in instantiation of member function 'emscripten::internal::WireTypePack::WireTypePack' requested here
WireTypePack argv(std::forward(args)...);
^
/usr/local/google/home/rreverser/emscripten/system/include/emscripten/val.h:485:56: note: in instantiation of member function 'emscripten::internal::MethodCaller::call' requested here
return MethodCaller::call(handle, name, std::forward(args)...);
^
/usr/local/google/home/rreverser/emscripten/tests/embind_with_pthreads.cpp:17:32: note: in instantiation of function template specialization 'emscripten::val::call' requested here
auto result = globalProperty.call("foo", "abc");
^
2 errors generated.
```
Same applies when trying to set a property to a constant string, e.g.:
```cpp
globalProperty.set("foo", "abc");
```
While, upon looking at documentation, this might be expected because Embind only claims to accept `std::string` and `std::wstring` instances, this feels counter-intuitive and would be better if Embind accepted string literals in the same way as any other.
What's worse, if you try to use a `const char*` constant instead (e.g. from C code), then Embind will happily compile the code, but later throw an obscure error at runtime (in browser) when such value is being converted:
```
exception thrown: BindingError: parameter 0 has unknown type PKc
```
cc @jgravelle-google in case you have time to take a look
Contributor guide
Assessment
This issue has not been assessed yet.