asm.js 和 WebAssembly 有什么区别?

What is the difference between asm.js and WebAssembly?

我最近一直在阅读 asm.js 和 WebAssembly:

http://ejohn.org/blog/asmjs-javascript-compile-target/

https://brendaneich.com/2015/06/from-asm-js-to-webassembly/

我仍然对一些事情感到困惑:

  1. asm.js代码编译及时运行?编译成什么?
  2. 除了 asm.js 是文本和 wasm(网络程序集)是二进制之外,两者之间还有什么区别?
  3. 这对其他脚本语言意味着什么,运行在浏览器中使用?以python为例,会不会是
    • python 代码编译成 wasm?或
    • python解释器(Cpython)编译成wasm并解释python?

Is asm.js code compiled in time and run? Compiled into what?

asm.js是常规的javascript代码,一如既往地被JS解释器编译成字节码。然而,具有 asm 支持的解释器应该进行提前编译,并且可能由于静态类型而生成更有效的代码表示。有关详细信息,请参阅 http://asmjs.org/

what are the differences between asm and wasm (other than text vs binary)?

None,现在。 wasm 应该是向后兼容的,compilable to asm (which again is executable as normal JS). It might however be extended with more features 将来随着对它的支持的增加。

What does this mean for other scripting languages, running in the browser?

后者,因为 Python 仍然需要解释。不需要解释器的脚本语言当然可以直接编译为 (w)asm,前提是有一个编译器(链)支持它作为目标。

Is asm.js code compiled in time and run? Compiled into what?

不同的浏览器以不同的方式编译 asm.js 代码。截至 2015 年 8 月:

  • Firefox 将 asm.js 编译为机器代码(并缓存机器代码以供将来加载相同的 asm.js)[1].
  • 在Windows10中作为实验性的flag,Edge也会对asm.js[2].
  • 做一些Ahead-of-Time验证和编译
  • Chrome 专门识别 asm.js 开头的 "use asm" 指令,以更积极地解析和分析代码并调整编译启发式。
  • Safari 没有对 asm.js.
  • 进行特殊处理

Other than asm.js being text and wasm (web assembly) being binary, what are the differences between the 2?

asm.js 只是 JavaScript,因此必须完全按照 JavaScript 规范运行。作为一个新标准,WebAssembly 能够修复一些 JavaScript 行为不理想的极端情况(从性能或编译的角度来看)[3]. In the future [4],WebAssembly 将能够添加原本不会的功能难以用 JavaScript.

表达

What does this mean for other scripting languages, running in the browser? Take python for example, is it going to be

  • python code compiled to wasm? or
  • python interpreter (Cpython) compiled into wasm and interpret python?

在 v.1 中,如您所说,在浏览器中 运行 Python 的最简单方法是将 Python 解释器编译为 wasm。这意味着,例如,Python GC 在 wasm 代码中 运行ning 并手动管理 wasm 线性内存。已经有一个实验项目向 PyPy 添加 asm.js 后端 [5] (which could work just as well for wasm). It currently runs into limitations of asm.js that could be addressed by the dynamic linking future feature of wasm. Going further, wasm seeks to provide both GC integration and JIT compilation support 这两者都将允许更有效和自然地与 Web 平台集成。

asm.js 是 JS 的一个子集,带有 "highly optimizable" 指令。基本上你可以声明类型(int,float)和 js 引擎(在浏览器中也是 node.js 之一)将更快地执行指令。如果与 WebGL 一起使用,如果您的应用程序进行大量计算或图形处理,它会有好处。

web assembly是JS的二进制格式,所有的JS,不仅asm.js。它不是字节码,它是解析器计算的 AST 的二进制编码。它有两大好处:

  • JS引擎可以跳过解析步骤
  • 它比 JS 原始源更紧凑

我们已经可以为非 JS 的浏览器编写代码:EMSCripten 可以在 JS 代码中编译 C++ 代码。其他转译器已经可以将您的代码编译成 JS。 使用 asm.js 该代码在计算时可以 运行 更快。 使用 webassembly 代码将更加紧凑,浏览器将能够更快地处理它(因为它将能够跳过解析)。 您不会像 DirectX、JavaApplets、Flash 或 Silverlight 那样加载新插件,因为所有内容都将 运行 在 JS 沙箱中。