HelloWorld
编写C++代码:helloworld.cpp
#include <stdio.h>
int main(int argc, char ** argv) {
printf("Hello World!!!\n");
}
使用Docker将C++代码,编译成wasm,生成调用的js和html:
docker run --rm -v $(pwd):/src emscripten/emsdk emcc helloworld.cpp -o helloworld.html
可以把helloworld.html
放web服务器中,或者在IDEA中打开。
当然也可以只编译出wasm文件:
docker run --rm -v $(pwd):/src emscripten/emsdk emcc helloworld.cpp -o helloworld.wasm
或者只编译出wasm文件,以及调用的js文件:
docker run --rm -v $(pwd):/src emscripten/emsdk emcc helloworld.cpp -o helloworld.js
Export C/C++ Function
JS调用C函数,一定要加KEEPALIVE导出函数,参考MDN:
// helloworld.c
#include <stdio.h>
#include <emscripten/emscripten.h>
void EMSCRIPTEN_KEEPALIVE myFunction(int argc, char ** argv) {
printf("MyFunction Called\n");
}
或者C++函数,一定要加extern "C"导出符号:
// helloworld.cpp
#include <stdio.h>
#include <emscripten/emscripten.h>
#ifdef __cplusplus
extern "C" {
#endif
void EMSCRIPTEN_KEEPALIVE myFunction(int argc, char ** argv) {
printf("MyFunction Called\n");
}
#ifdef __cplusplus
}
#endif
编译,一定要加ccall参数:
docker run --rm -v $(pwd):/src emscripten/emsdk emcc helloworld.cpp -o helloworld.js \
-s "EXTRA_EXPORTED_RUNTIME_METHODS=['ccall']"
调用页面,一定要等WASM初始化完成后才能使用,比如用个按钮点击时调用:
<button id="mybutton">Run myFunction</button>
<script>
document.getElementById('mybutton').addEventListener('click', function() {
Module.ccall('myFunction', null, null, null);
});
</script>
<script src="helloworld.js"></script>
点击按钮后,在Console中会打印出字符串
MyFunciton Called
。
Function Args
调用函数时指定参数:
// helloworld.c
#include <stdio.h>
float generate_float(int iteration, double seed1, float seed2) {
float ret;
printf ("calling into WASM function: %s\n", __FUNCTION__);
for (int i=0; i<iteration; i++){
ret += 1.0f/seed1 + seed2;
}
return ret;
}
编译时,指定ccal,并且指定导出函数(前面加下划线),指定内存和堆栈(可选):
docker run --rm -v $(pwd):/src emscripten/emsdk emcc helloworld.c -o helloworld.js \
-s "EXTRA_EXPORTED_RUNTIME_METHODS=['ccall']" \
-s TOTAL_MEMORY=65536 -s TOTAL_STACK=4096 -s "EXPORTED_FUNCTIONS=['_generate_float']"
前面用的宏定义
EMSCRIPTEN_KEEPALIVE
,是WASM的SDK中的定义,这里我们在编译时指定导出的符号。
在JS调用时,指定参数:
<button id="mybutton">Run myFunction</button>
<script>
document.getElementById('mybutton').addEventListener('click', function() {
var r0 = Module.ccall('generate_float', 'float', ['int','double','float'], [10,1.1,2]);
console.log('r0 is', r0);
});
</script>
<script src="helloworld.js"></script>
Runtime: wasm-micro-runtime
可以在C/C++中调用WASM中的函数,比如未来SRS会支持WASM,这就需要用到runtime来解释WASM文件了。
有几个Runtime,参考#2153:
- wavm,Envoy中支持这个,为非web环境设计的,效率高但加载速度慢。
- wasmer,这个Star也比较多。
- wasm-micro-runtime,调研时用这个做的例子。
- wasmtime,这个是JIT解释器,修改代码就生效的那种。
编译解释器:
git clone https://github.com/winlinvip/wasm-micro-runtime.git &&
cd wasm-micro-runtime/samples/basic && (cmake . && make)
生成wasm文件:
docker run --rm -v $(pwd):/src emscripten/emsdk emcc wasm-apps/testapp.c -o t.wasm \
-s TOTAL_MEMORY=65536 -s TOTAL_STACK=4096 -s ERROR_ON_UNDEFINED_SYMBOLS=0 --no-entry \
-s "EXPORTED_FUNCTIONS=['_generate_float','_float_to_string','_calculate']"
注意需要指定堆栈大小,参考Wiki,否则会报错
Instantiate wasm module failed. error: WASM module instantiate failed: allocate memory failed
。
启动解释器,执行t.wasm
:
./basic -f t.wasm
网友评论