Pedro
Pedro
LTLeaning Technologies
Created by Pedro on 5/31/2024 in #support
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;
}
2 replies
LTLeaning Technologies
Created by Pedro on 3/26/2024 in #support
Calling JS object member functions from Wasm
The pong tutorial (https://labs.leaningtech.com/cheerp/tutorials/pong) shows how to handle interactions between JS and Wasm using static functions and global instances. What is the recommended approach for accessing member functions of dynamically created JS objects from Wasm? Example code:
struct [[cheerp::genericjs]] Foo {
client::HTMLElement* span;
Foo() {
span = client::document.createElement("span");
span->set_textContent("Foo");
client::document.get_body()->appendChild(span);
}
void makeBar() {
span->set_textContent("Bar");
}
}

struct [[cheerp::wasm]] Bar {
Foo* foo; // obviously does not compile
Bar() {
foo = new Foo();
foo->makeBar();
}
}

[[cheerp::wasm]]
void webMain() {
Bar* bar = new Bar();
}
struct [[cheerp::genericjs]] Foo {
client::HTMLElement* span;
Foo() {
span = client::document.createElement("span");
span->set_textContent("Foo");
client::document.get_body()->appendChild(span);
}
void makeBar() {
span->set_textContent("Bar");
}
}

struct [[cheerp::wasm]] Bar {
Foo* foo; // obviously does not compile
Bar() {
foo = new Foo();
foo->makeBar();
}
}

[[cheerp::wasm]]
void webMain() {
Bar* bar = new Bar();
}
18 replies