Мне удалось создать очень простые примеры.
Теперь я бы хотел бы попробовать использовать объекты JS в своем коде на C++. В частности,
Я хотел бы попробовать распечатать элементы, полученные из окон, например местоположение в коде C++.
Это мой код C++:
Код: Выделить всё
#include
#include
#include
using namespace emscripten;
EMSCRIPTEN_KEEPALIVE
std::string decode_request(val window) {
val location = window["location"];
std::string search = location["search"].as();
size_t pos = search.find('?');
if (pos != std::string::npos) {
return search.substr(pos + 1);
} else {
return "";
}
}
extern "C" {
EMSCRIPTEN_KEEPALIVE
const char* myFunction() {
val window = val::global("window");
std::string query = decode_request(window);
return query.c_str();
}
}
Код: Выделить всё
emcc -o module.js module_2.cpp -s EXPORTED_RUNTIME_METHODS="['ccall']" -lembind
Код: Выделить всё
module_2.cpp:24:16: warning: address of stack memory associated with local variable 'query' returned [-Wreturn-stack-address]
24 | return query.c_str();
| ^~~~~
1 warning generated.
cache:INFO: generating system asset: symbol_lists/83809947191162c2d512457e4d02c5266930142b.json... (this will be cached in "/Users/myuser/Documents/Emscripten/emsdk/upstream/emscripten/cache/symbol_lists/83809947191162c2d512457e4d02c5266930142b.json" for subsequent builds)
cache:INFO: - ok
Код: Выделить всё
WebAssembly Example
WebAssembly Example
Result:
Trigger
function run() {
WebAssembly.instantiateStreaming(
fetch("wrapping.wasm"), {}
).then(result => {
if (!result || !result.instance || !result.instance.exports) {
console.error("Failed to instantiate WebAssembly module.");
return;
}
var sum = result.instance.exports.myFunction();
console.log("Result:", sum);
document.getElementById("result").textContent = "Result: " + sum;
}).catch(error => {
console.error("Error loading WebAssembly module:", error);
});
}
Код: Выделить всё
Error loading WebAssembly module: TypeError: WebAssembly.instantiate(): Import #0 module="env": module is not an object or function
(anonymous) @ (index):32
Promise.catch (async)
run @ (index):31
onclick @ (index):12
Подробнее здесь: https://stackoverflow.com/questions/782 ... -an-object