Return std::string from cheerp::genericjs function

Got this working by peeking at the client::String definition, but would like to know if there is a cleaner alternative. Also, I don't quite get what is happening there. Is getLocal fully converted to JS? How is it able to manipulate a std::string? Thanks in advance :)
[[cheerp::genericjs]]
std::string getLocal(const std::string& key) {
std::string ret;
const client::String& str = client::jsGetLocal(key.c_str());
ret.resize(str.get_length());
for(int i=0; i<str.get_length(); i++) {
ret[i] = str.charCodeAt(i);
}
return ret;
}
[[cheerp::genericjs]]
std::string getLocal(const std::string& key) {
std::string ret;
const client::String& str = client::jsGetLocal(key.c_str());
ret.resize(str.get_length());
for(int i=0; i<str.get_length(); i++) {
ret[i] = str.charCodeAt(i);
}
return ret;
}
1 Reply
apignotti
apignotti6mo ago
If you are using a nightly build you should be able to use key.toUtf8() to get a properly converted std::string This code is indeed fully compiled to JavaScript. [[cheerp::genericjs]] has full access to linear memory types like std::string. On the other hand code compiled to WebAssembly cannot access JavaScript data. One of the biggest advantage of Cheerp is the ability of easily mixing JavaScript and WebAssembly code from a single C++ codebase.
Want results from more Discord servers?
Add your server