[我正在阅读这篇文章(https://www.smashingmagazine.com/2019/04/webassembly-speed-web-app/),其中解释了他们如何使用zlib来加速其Web项目:
为了支持zlib库,我们使用标志USE_ZLIB; zlib非常普遍,以至于已经被移植到WebAssembly,Emscripten会在我们的项目中为我们提供它]
我想在我自己的WASM模块中使用zlib。
在我的C代码(与emcc一起编译)中,我编写了此接口函数:
#include <zlib.h>
int pcf_decompress_zlib(unsigned char *input, int input_length, unsigned char *output, int output_length)
{
uLongf output_length_result = output_length;
int result = uncompress(output, &output_length_result, input, input_length);
if (result != Z_OK) {
return 0;
} else {
return output_length_result;
}
}
我这样编译它:
emcc decompress.c -O3 -s WASM=1 -s SIDE_MODULE=1 -s "EXPORTED_FUNCTIONS=['_pcf_decompress_zlib']" -s USE_ZLIB=1 -o decompress.wasm
当我这样做时,emcc自动下载到zlib库中,因此它似乎知道如何处理。
然后在浏览器中,我有这个课程:
export class Decompressor {
wasmOnLoad(obj) {
this.instance = obj.instance;
console.log("Loaded WASM");
console.log(obj.instance);
// Don't do anything else yet
}
constructor() {
this.memory = new WebAssembly.Memory({
initial: 1
});
this.heap = new Uint8Array(this.memory.buffer);
this.imports = {
env: {
__memory_base: 0,
memory: this.memory,
abort: function(err) {
throw new Error('abort ' + err);
},
}
};
}
start() {
console.log("startWasm");
WebAssembly.instantiateStreaming(fetch('decompress/decompress.wasm'), this.imports)
.then(this.wasmOnLoad.bind(this));
}
}
然后在从我的HTML加载的我的主要JS代码中进行此操作:
import { Decompressor } from "./decompress/decompress.js";
var l = new Decompressor();
l.start();
当我加载页面时,Firefox给我这个错误:
LinkError: import object field '_uncompress' is not a Function
似乎发出的wasm代码不包含zlib,并且浏览器中也不内置zlib。我曾考虑过将SIDE_MODULE
更改为MAIN_MODULE
,但这导致了数十个未定义的符号,使问题更加严重。
如果emcc没有自动使zlib可用,那么让emcc提供USE_ZLIB=1
选项是没有意义的。那我想念的是什么呢?如何让emcc将它已经具有的zlib代码静态包含到我正在编译的wasm模块中?
谢谢。
一种方法是在zlib构建期间包括emcc
源。我在下面测试。首先,创建此文件结构(包括您下载的zlib源文件夹)
$ tree -L 2 .
.
├── build.sh
├── dist
├── lib
│ └── zlib-1.2.11
└── src
└── decompress.c
build.sh
ZLIB="lib/zlib-1.2.11"
emcc \
-O3 \
-s WASM=1 \
-s EXPORTED_FUNCTIONS="[ \
'_free', '_malloc' \
, '_pcf_decompress_zlib' \
]" \
-I $ZLIB \
-o dist/decompress.wasm \
$ZLIB/*.c \
src/decompress.c
现在,配置zlib并构建!
$ lib/zlib-1.2.11/configure `# you only need to run this once`
$ ./build.sh