简体   繁体   English

如何包含C ++文件中的WebAssembly模块?

[英]How can I include a WebAssembly module from a C++ file?

WebAssembly is a binary language that intends to, among other goals, break the barriers between programming languages. WebAssembly是一种二进制语言,旨在除其他目标外,突破编程语言之间的障碍。

Currently, it's easy enough to compile C++ code to WebAsm and include it into JavaScript code. 当前,将C ++代码编译为WebAsm并将其包含到JavaScript代码中已经足够容易了。 There's even a proposal and a Webpack polyfill to make it as easy as import { foo } from 'bar.wasm' . 甚至还有一个提案和一个Webpack polyfill ,使其像import { foo } from 'bar.wasm'一样容易。

Moreover, WebAssembly supports wasm files that list their own dependencies, in the form of import declarations. 此外,WebAssembly支持wasm文件,这些文件以导入声明的形式列出了它们自己的依赖项。

Is there some polyfill build tool that allows users to include a webasm module into a C++ compilation unit, in the process of compiling it to wasm? 是否有一些polyfill构建工具,允许用户在将其编译为wasm的过程中将webasm模块包含到C ++编译单元中? For example, let's say I have a Rust module that I want to use inside a C++ module, both of which I'm compiling to wasm. 例如,假设我有一个想要在C ++模块中使用的Rust模块,这两个模块我都将编译为wasm。 Is there a way to write code equivalent to this: 有没有办法编写与此等效的代码:

#include "node_modules/some_rust_utility/index.wasm"

int someCppFunction(const std::string& data) {
  return some_rust_utility::foobar(data.c_str());
}

and have it compile or not depending on whether foobar is defined with a matching type in some_rust_utility ? 是否根据在some_rust_utility是否使用匹配类型定义了foobar进行some_rust_utility


Note: I expect the answer to this question may change over time as WebAssembly support marches on. 注意:随着WebAssembly支持的发展,我希望这个问题的答案可能会随着时间而改变。 If you see this question years afterwards and the answer has changed, feel free to add an update. 如果多年后您看到此问题,并且答案已更改,请随时添加更新。

It's currently impossible. 目前不可能。

The situation might change at some point (which is why I'm not marking this question as solved), but so far the standard doesn't support wasm-to-wasm interoperability: 情况可能会在某个时候发生变化(这就是为什么我不将此问题标记为已解决)的原因,但是到目前为止,该标准还不支持wasm到wasm的互操作性:

Since the WebAssembly spec does not define how import names are interpreted [...] the host environment can interpret the module name as a file path, a URL, a key in a fixed set of builtin modules or the host environment may invoke a user-defined hook to resolve the module name to one of these; 由于WebAssembly规范没有定义导入名称的解释方式,因此主机环境可以将模块名称解释为文件路径,URL,固定的内置模块集中的密钥,或者主机环境可以调用用户-定义的挂钩将模块名称解析为其中之一;

(eg it's implementation-defined) (例如,它是实现定义的)

As far as I know, currently a wasm module cannot directly import from another wasm module, instead it must use JavaScript as an intermediary. 据我所知,当前wasm模块不能直接从另一个wasm模块导入,而是必须使用JavaScript作为中介。

Yes, the module defers this to the embedder (typically JS). 是的,该模块将其推迟到嵌入器(通常是JS)。 As far as the module is concerned, it just knows that something is providing the import, but it doesn't know where it came from. 就模块而言,它只知道提供导入的东西,但不知道它来自哪里。

It's not a design goal for Wasm to guarantee seamless interop between multiple languages, or even multiple compilers of the same language (whether with memory or GC). Wasm的设计目标不是保证多种语言甚至同一语言的多个编译器(无论是内存还是GC)之间的无缝互操作。 That is in line with its low-level approach and a very conscious decision: such universal interop has often been promised but never worked out well in practice. 这与它的低级方法和一个非常有意识的决定是一致的:这样的通用互操作常常被承诺,但是在实践中却没有很好地解决。 Languages are just too different. 语言太不同了。

If multiple compilers or languages want to be able to interoperate, then they are of course free to define a common ABI on top of Wasm. 如果多个编译器或语言希望能够进行互操作,那么它们当然可以自由地在Wasm之上定义通用的ABI。 That would be great! 那很好啊! But Wasm itself doesn't need to prescribe it, no more than a CPU architecture does. 但是Wasm本身不需要规定它,仅比CPU体系结构需要更多。

Given the above comment, it seems likely that language interoperability will remain a low priority for wasm contributors in the near future. 鉴于以上评论,在不久的将来,语言互操作性可能仍是wasm贡献者的低优先事项。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM