如何从Emscripten调用预先存在的WebAssembly代码

问题描述

我已经使用针对WebAssembly的编程语言(称为AEC)制作了compiler。但是,该编译器仅生成与WebAssembly Binary Toolkit(WABT)兼容的代码,因为我找不到与Emscripten兼容的程序集的足够文档。那么,如何从兼容Emscripten的C或C ++中以该语言调用函数
以前,我针对x86编写了针对该语言的编译器,使用C ++与以该语言编写的代码进行交互是rather easy。但是,使用WebAssembly时似乎并非如此。
假设我在AEC中有以下代码

Function plusOne(Integer32 integer) Which Returns Integer32 Does
    Return integer + 1;
EndFunction

我将其编译如下:

[teo.samarzija@teos-acer-laptop debug]$ ../AECforWebAssembly/aec *.aec
Running the tests...
All the tests passed.
Reading the file...
All characters read!
Tokenizing the program...
Finished tokenizing the program!
Parsing the program...
Finished parsing the program!
Compiling the program...
Compilation finished!
Saving the assembly in file "plusOne.wat"...
Assembly successfully saved,quitting Now.
[teo.samarzija@teos-acer-laptop debug]$ wat2wasm plusOne.wat

现在,假设我想从这样的C代码调用它:

#include <stdio.h>

extern int plusOne(int);

int main() {
    printf("plusOne(4)=%d\n",plusOne(4));
    return 0;
}

我如何编译C程序才能做到这一点?如果我以类似的方式尝试使用面向x86程序集的AEC编译器来执行此操作,则会收到此错误

[teo.samarzija@teos-acer-laptop debug]$ emcc -o test.html test.c plusOne.wasm
emcc: error: plusOne.wasm: Input file has an unkNown suffix,don't kNow what to do with it!

那我该怎么办?
以下NodeJS代码完成了我想要的:

const FileSystem = require('fs');
const wasmFileContent = FileSystem.readFileSync("plusOne.wasm");
let stack_pointer = new WebAssembly.Global({value : 'i32',mutable : true},0);
let memory = new WebAssembly.Memory({initial : 1,maximum : 1});
let importObject = {
  JavaScript : {stack_pointer : stack_pointer,memory : memory}
};
WebAssembly.instantiate(wasmFileContent,importObject).then((results) => {
  const exports = results.instance.exports;
  let plusOne = exports.plusOne;
  console.log("plusOne(4)=",plusOne(4));
});

那么,如何使用Emscripten在C或C ++中做到这一点?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)