emcc命令
emcc D:\cppcode\AutoFollow\hello1.cpp -s WASM=1 -o D:\cppcode\AutoFollow\hello\hello2.js
c++代码
#include <stdio.h>
#include <emscripten/emscripten.h>
extern "C"
{
int EMSCRIPTEN_KEEPALIVE aa(int x)
{
if (x <= 0)
return 0;
if (x <= 2)
return 1;
return x - 2;
}
int EMSCRIPTEN_KEEPALIVE my_function(int a, int b)
{
return a + b;
}
}
前端代码(vue框架)
loadWasm2() {
this.fetchAndInstantiate(this.$JBolt.API.cems_bigdata.server.downloadFile, this.importObject).then(function(instance) {
console.warn(instance.exports.aa(100));
});
},
fetchAndInstantiate(url, importObject) {
return fetch(url, {
method: "post",
body: JSON.stringify({ }),
headers: {
//根据项目来
}
}).then(response =>
response.arrayBuffer()
).then(bytes =>
WebAssembly.instantiate(bytes, importObject)
).then(results =>
results.instance
);
},