Hi,
maybe a stupid question :)
I wrote the test .cpp file, and try to make it tun in the javascript. This error occurred.
here are the test code.
test.h
using namespace emscripten;
extern "C" {
int mmm(int aaa);
int hhh(const char* aaa);
}
EMSCRIPTEN_BINDINGS(my_value_example) {
function("mmm", &mmm);
function("hhh", &hhh, allow_raw_pointers());
}
test.cpp
using namespace std;
int mmm(int aaa){
return 123;
}
int hhh(const char* aaa){
return 34;
}
test.html
<script type='text/javascript'>
**function sasa(){
//alert("Hello");
var mmmVal = Module.mmm(342);
alert(mmmVal);
var hhhVal = Module.hhh("sd");
alert(hhhVal);
}**
</script>
<script async type="text/javascript" src="test.js"></script>
so, the Module.mmm function can be conducted, but the Module.hhh function got this error.
i thought this is mainly related with the "allow_raw_pointers()" option. but still have no idea how to make it correct.
really appreciate.
ps:
if i write the cpp code as this:
int jjj(const std::string aaa);
function("jjj", &jjj);
int jjj(const std::string aaa){
return 1234;
}
that will be OK.
Hi,
This is because the char* type is not registered by embind. Using raw pointers directly is not always a good idea. If you want to be able to call your function with js string without changing your c interface, you can always use embind's optional_override :
#include <emscripten/bind.h>
using namespace emscripten;
int bla(const char* ptr) {
return 42;
}
EMSCRIPTEN_BINDINGS(bla) {
function("bla", optional_override(
[](const std::string s){
return bla(s.c_str());
}));
}
This, way you can call bla("hello") from js, but still have the const char* C interface.
@AlexPerrot, Thanks a lot for your help :)
@AlexPerrot
I want to use the pointer of a struct as a paramter of a function in class like:
#include <emscripten/bind.h>
using namespace emscripten;
typedef struct _RGEvent_t
{
int status;
//other property
}RGEvent_t;
class RGDataMgr
{
public:
RGDataMgr(){};
~RGDataMgr(){};
void setEvents(int eventCnt, RGEvent_t* events) {
//some code here
return ;
}
};
EMSCRIPTEN_BINDINGS(my_module) {
value_object<RGEvent_t>("RGEvent_t")
.field("status", &RGEvent_t::status)
;
class_<RGDataMgr>("RGDataMgr")
.constructor<>()
.function("setEvents", &RGDataMgr::setEvents, allow_raw_pointers())
;
}
And when I call the setEvents function at html, the same error 'UnboundTypeError: Cannot call RGDataMgr.setEvents due to unbound types: P10_RGEvent_t' occurred. I've read the optional_override documention here. Without more examples, I'm still confused.
This issue has been automatically marked as stale because there has been no activity in the past 2 years. It will be closed automatically if no further activity occurs in the next 7 days. Feel free to re-open at any time if this issue is still relevant.
Most helpful comment
Hi,
This is because the
char*type is not registered by embind. Using raw pointers directly is not always a good idea. If you want to be able to call your function with js string without changing your c interface, you can always use embind'soptional_override:This, way you can call
bla("hello")from js, but still have theconst char*C interface.