将 C++ 代码转换为 webassembly 的脚本不会终止
Script to transform C++ code to webassembly is not terminating
我想将 CPP 文件编译为 Web 程序集。我在以下 link
中使用脚本
https://github.com/wasdk/wasmexplorer-service/tree/master/scripts
我在 Linux OS
中使用以下命令
../scripts/compile2.sh ../test.cpp "-fno-verbose-asm -03 -std=c++98" 2>&1
命令永远不会终止。我已经尝试了所有选项组合。任何人都可以在这方面指导我吗?或者任何其他方式将 C++ 代码转换为相应的 Web 汇编代码(帮我设计一个独立的应用程序)。
文件内容如下:
double fact(int i) {
long long n = 1;
for (;i > 0; i--) {
n *= i;
}
return (double)n;
}
我的预期输出如下(来自https://mbebenita.github.io/WasmExplorer/)
(module
(table 0 anyfunc)
(memory [=11=] 1)
(export "memory" (memory [=11=]))
(export "_Z4facti" (func $_Z4facti))
(func $_Z4facti (; 0 ;) (param [=11=] i32) (result f64)
(local i64)
(local i64)
(block $label[=11=]
(br_if $label[=11=]
(i32.lt_s
(get_local [=11=])
(i32.const 1)
)
)
(set_local
(i64.add
(i64.extend_s/i32
(get_local [=11=])
)
(i64.const 1)
)
)
(set_local
(i64.const 1)
)
(loop $label
(set_local
(i64.mul
(get_local )
(tee_local
(i64.add
(get_local )
(i64.const -1)
)
)
)
)
(br_if $label
(i64.gt_s
(get_local )
(i64.const 1)
)
)
)
(return
(f64.convert_s/i64
(get_local )
)
)
)
(f64.const 1)
)
)
运行 下面的代码适合我
/*
# run me with 'bash test.cpp'
clang++ [=10=] --target=wasm32-unknown-unknown-wasm -nostartfiles -nostdlib -Wl,--no-entry -Wl,--export-all -o test.wasm
exit 1
*/
extern "C" double fact(int i) {
long long n = 1;
for (;i > 0; i--) {
n *= i;
}
return (double)n;
}
测试
<!DOCTYPE html>
<script type="module">
async function init() {
const { instance } = await WebAssembly.instantiateStreaming(fetch("./test.wasm"));
console.log(instance.exports.fact(6));
}
init();
</script>
我想将 CPP 文件编译为 Web 程序集。我在以下 link
中使用脚本https://github.com/wasdk/wasmexplorer-service/tree/master/scripts
我在 Linux OS
中使用以下命令../scripts/compile2.sh ../test.cpp "-fno-verbose-asm -03 -std=c++98" 2>&1
命令永远不会终止。我已经尝试了所有选项组合。任何人都可以在这方面指导我吗?或者任何其他方式将 C++ 代码转换为相应的 Web 汇编代码(帮我设计一个独立的应用程序)。
文件内容如下:
double fact(int i) {
long long n = 1;
for (;i > 0; i--) {
n *= i;
}
return (double)n;
}
我的预期输出如下(来自https://mbebenita.github.io/WasmExplorer/)
(module
(table 0 anyfunc)
(memory [=11=] 1)
(export "memory" (memory [=11=]))
(export "_Z4facti" (func $_Z4facti))
(func $_Z4facti (; 0 ;) (param [=11=] i32) (result f64)
(local i64)
(local i64)
(block $label[=11=]
(br_if $label[=11=]
(i32.lt_s
(get_local [=11=])
(i32.const 1)
)
)
(set_local
(i64.add
(i64.extend_s/i32
(get_local [=11=])
)
(i64.const 1)
)
)
(set_local
(i64.const 1)
)
(loop $label
(set_local
(i64.mul
(get_local )
(tee_local
(i64.add
(get_local )
(i64.const -1)
)
)
)
)
(br_if $label
(i64.gt_s
(get_local )
(i64.const 1)
)
)
)
(return
(f64.convert_s/i64
(get_local )
)
)
)
(f64.const 1)
)
)
运行 下面的代码适合我
/*
# run me with 'bash test.cpp'
clang++ [=10=] --target=wasm32-unknown-unknown-wasm -nostartfiles -nostdlib -Wl,--no-entry -Wl,--export-all -o test.wasm
exit 1
*/
extern "C" double fact(int i) {
long long n = 1;
for (;i > 0; i--) {
n *= i;
}
return (double)n;
}
测试
<!DOCTYPE html>
<script type="module">
async function init() {
const { instance } = await WebAssembly.instantiateStreaming(fetch("./test.wasm"));
console.log(instance.exports.fact(6));
}
init();
</script>