对于托管语言,是否需要将语言运行时嵌入到 Web 程序集可执行文件中?

For managed languages, will the language runtime need to be embedded into the web assembly executable?

例如,如果 Microsoft 为 .NET 创建了一个 WebAssembly 目标,他们是否需要将 .NET 运行时嵌入到已编译的二进制文件中?如果他们不这样做,垃圾收集器和基础 class 库将如何工作?

简单回答:是。

要 运行 WebAssembly 中的任何语言,你必须将它的 运行time 嵌入到你的二进制文件中。 Emscripten 如何处理 C++(它将 musl libc, libc++, libc++abi, compiler-rt, and pthread 嵌入到您的二进制文件中)以及其他语言也是如此。


更长,更细致,答案:

正确完成,一旦可用,GC would be part of WebAssembly itself, but you can always embed a Boehm GC 风险自负(在 WebAssembly 中扫描堆栈非常棘手,因为在 VM 中无法访问本地变量!)。

但并非所有语言的 VM 都有 GC。它们还有许多其他功能,有些甚至可能想要自己的 just-in-time compilation on top of WebAssembly.

基础 class 库通常只是关于对象布局的,它们很容易转换为 WebAssembly。请参阅 libc++abi 以了解这对 C++ 的意义。

也就是说 运行 时间不必在 编译的二进制文件中。两个选项:

  1. 改为在 JavaScript 中嵌入 运行 时间。您可以在 JavaScript 中实现重要代码,比如 chunks of a libc。语言 VM 的 运行 时间并没有什么不同。
  2. 将运行时间单独嵌入,dynamically-linked .wasm file. This means multiple different .wasm file can share the runtime! Multiple websites can even share that runtime.wasm and hope to all cache-hit on compiled code (assuming CORS算出来)。

事实上,要做任何有用的事情,您 必须 至少做一些 1.:WebAssembly 没有为嵌入器定义任何 API。在web embedding the only way to do anything at all is to be called from JavaScript, and / or call into JavaScript, through WebAssembly's import and export sections. In that sense, JavaScript is your application's microkernel, offering capabilities to .

当您的语言 运行 需要某些平台功能时,这会变得很棘手。例如,为网络重新创建一个平台的原生 UI 库是一项不小的任务。一种语言的大部分 运行 时间可以 "just work",但某些部分可能需要大量工作。