私は、次のbashスクリプトを経由してコンパイルすると正常に動作する小さなWebAssemblyプログラムを持って破るために発生します削除「-s ONLY_MY_CODE = 1」コンパイル引数はWebAssemblyプログラムは
source_list="../../src/cpp/main/main.cc"
emcc -std=c++11 $source_list -o out/index.html -O1 -s WASM=1 -s ONLY_MY_CODE=1 -s EXPORTED_FUNCTIONS="['_go']"
cp ../../src/html/index.html out/
私は自分のブラウザにプログラムをロードするとJSコンソールにhi there
が印刷されています。
しかし、手の込んだコードを使用するための準備として、私は私のコンパイルスクリプトが-s ONLY_MY_CODE=1
エントリを削除することによって変更します。任意のファイルへの更なる変更なし
、私は、プログラムをコンパイルして実行したときに、私は、ブラウザに次のエラーメッセージを参照してください。
index.html:1 Uncaught (in promise) LinkError: WebAssembly Instantiation: Import #0 module="env" function="DYNAMICTOP_PTR" error: global import must be a number at <anonymous> Promise rejected (async) (anonymous) @ index.html:35
どのように私はこの問題を解決することができますか?
main.cc
extern "C"
{
extern void print(char *message);
void go()
{
print("hi there");
}
}
index.htmlを
<script>
var webSocket;
const memory = new WebAssembly.Memory({ initial: 256, maximum: 256 });
const buffer = new Uint8Array(memory.buffer);
var exports;
function toJsStr(offset){
var s="";
for(;;){
var b = buffer[offset++];
if(b == 0)
return s;
s += String.fromCharCode(b);
}
}
function print(offset){
console.log(toJsStr(offset));
}
fetch('index.wasm').then(response =>
response.arrayBuffer()
).then(bytes => {
var imports = {};
imports.env = {};
imports.env.memory = memory;
imports.env.memoryBase = 0;
imports.env.table = new WebAssembly.Table({ initial: 0, maximum: 0, element: 'anyfunc' });
imports.env.tableBase = 0;
imports.env._print = print;
return WebAssembly.instantiate(bytes, imports);
}
).then(module => {
exports = module.instance.exports;
exports._go();
}
);
</script>