WebAssembly 调用 JavaScript 函数作为 C 中的函数指针

WebAssembly Call JavaScript Functions as Function Pointers From C

我想将 JavaScript 函数作为参数传递给从 WebAssembly 导出的函数,并使用函数指针作为参数。

考虑以下示例:

JavaScript代码:

function foo() {
    console.log("foo");
}

wasmInstance.exports.expects_funcptr(foo);

C代码:

typedef void(*funcptr_type)(void);

void expects_funcptr(funcptr_type my_funcptr)
{
    my_funcptr();
}

我没有使用 Emscripten,但他们的 "Interacting with code" 页面中有一个关于该主题的部分:https://emscripten.org/docs/porting/connecting_cpp_and_javascript/Interacting-with-code.html#interacting-with-code-call-function-pointers-from-c。为此,他们有一个名为 addFunction 的函数。

我在这里查看了它的实现:https://github.com/emscripten-core/emscripten/blob/incoming/src/support.js#L755

而且它看起来很……老套。看起来他们正在创建一个新的 wasm 模块,该模块将 javascript 函数作为导入并将其导出为 wasm 函数。只有这样他们才能将函数添加到 WebAssembly Table.

有更好的方法吗?

编辑:

以下是我目前的处理方式。通过使用以下函数将 JS 函数转换为 WASM,我可以像这样将 JS 函数传递给 WASM:

// How the above example would be called using the converter function.

wasmInstance.exports.expects_funcptr(convertFunction(foo, Types.VOID));

// The actual converter function (minus some details for simplicity)

function convertFunction(func, ret, params) {

    // Construct a .wasm binary by hand
    const bytes = new Uint8Array([
        0x00, 0x61, 0x73, 0x6d, // magic
        0x01, 0x00, 0x00, 0x00, // version
        // ... generate type, import, export sections as well
    ]);

    const module = new WebAssembly.Module(bytes);
    const instance = new WebAssembly.Instance(module, {
        a: {
            b: func
        }
    });

    const ret = table.length;

    table.grow(1);
    table.set(ret, instance.exports.f);

    return ret;

}

这是一个展示概念的粗略示例。一个实际的实现检查函数是否已经被转换,处理错误等。

函数表是 Wasm 中函数指针的原语。您将不得不使用一个函数指针。管理这个单独的地址 space 可能非常棘手,emscripten 中的所有 "hacky" 代码都是为了确保安全地完成此操作。在您自己的代码中,您不需要像 emscripten 那样强制执行许多不变量,因此您可能可以摆脱其中的大部分。也很高兴在评论中澄清这一点。

您可以使用 emscripten 导出的函数帮助查看这个简单的生成器

https://libamtrack.github.io/c2JsGenerator/