简体   繁体   English

asm.js和WebAssembly有什么区别?

[英]What is the difference between asm.js and WebAssembly?

I have been reading about asm.js and WebAssembly recently: 我最近一直在阅读有关asm.js和WebAssembly的内容:

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

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

I am still confused about a few things: 我仍然对一些事情感到困惑:

  1. Is asm.js code compiled in time and run? asm.js代码是否可以及时编译并运行? Compiled into what? 编译成什么?
  2. Other than asm.js being text and wasm (web assembly) being binary, what are the differences between the 2? 除了asm.js为文本和wasm(Web程序集)为二进制以外,两者之间有什么区别?
  3. What does this mean for other scripting languages, running in the browser? 这对于在浏览器中运行的其他脚本语言意味着什么? Take python for example, is it going to be 以python为例,它将是
    • python code compiled to wasm? python代码编译为wasm? or 要么
    • python interpreter (Cpython) compiled into wasm and interpret python? python解释器(Cpython)编译成wasm并解释python?

asm.js is a subset of JS with "highly optimizable" instructions. asm.js是带有“高度优化”指令的JS子集。 Basically you can declare the type (int, float) and the js engine (in the browsers but also the node.js one) will execute the instructions faster. 基本上,您可以声明类型(int,float),并且js引擎(在浏览器中也可以在node.js中)将更快地执行指令。 It has benefits if your app does a lot of calculation or graphic if used together with WebGL. 如果与WebGL一起使用,您的应用程序会进行大量的计算或图形处理,则将具有很多好处。

web assembly is a binary format for JS, all JS, not only asm.js. Web程序集是JS的二进制格式,所有JS不仅是asm.js。 It's not a bytecode, it's a binary encoding of the AST that the parser calculates. 它不是字节码,而是解析器计算的AST的二进制编码。 It has 2 big benefits: 它有2大好处:

  • the JS engine can skip the parsing step JS引擎可以跳过解析步骤
  • it's much more compact than the JS original source 它比JS原始源代码紧凑得多

We already can write code for browsers that isn't JS: EMSCripten can compile c++ code in JS code. 我们已经可以为非JS的浏览器编写代码:EMSCripten可以在JS代码中编译c ++代码。 Other transcompiler are already available to compile your code into JS. 其他反编译器已经可以将您的代码编译为JS。 Using asm.js that code can run faster when it does math. 使用asm.js进行数学运算时,代码可以更快地运行。 Using web assembly that code will be more compact and the browser will be able to process it faster (because it will be able to skip the parsing). 使用Web汇编,代码将更加紧凑,浏览器将能够更快地处理它(因为它将能够跳过解析)。 You won't have a new plugin to load like DirectX, JavaApplets, Flash or Silverlight because everything will run in the JS sandbox. 您将不需要像DirectX,JavaApplets,Flash或Silverlight这样的新插件来加载,因为所有插件都将在JS沙箱中运行。

Is asm.js code compiled in time and run? asm.js代码是否可以及时编译并运行? Compiled into what? 编译成什么?

asm.js is regular javascript code, and is compiled into bytecode by the JS interpreter as always. asm.js是常规的javascript代码,通常会被JS解释器编译为字节码。 However, an interpreter with asm support is supposed to do ahead-of-time compilation, and possibly to generate more efficient code representation because of the static typing. 但是,具有asm支持的解释器应该进行提前编译,并且由于静态类型的原因,可能会生成更有效的代码表示形式。 See http://asmjs.org/ for details. 有关详细信息,请参见http://asmjs.org/

what are the differences between asm and wasm (other than text vs binary)? asm和wasm之间有什么区别(文本和二进制除外)?

None, for now. 暂时没有。 wasm is supposed to be backwards-compatible, compilable to asm (which again is executable as normal JS). wasm应该是向后兼容的,可以编译为asm (它也可以像普通JS一样执行)。 It might however be extended with more features in the future as support for it grows. 但是,随着对它的支持的增加,将来可能会扩展它以提供更多功能

What does this mean for other scripting languages, running in the browser? 这对于在浏览器中运行的其他脚本语言意味着什么?

The latter, rather, as Python still needs to be interpreted. 相反,后者仍然需要解释Python。 Scripting languages that don't need an interpreter can of course be directly compiled to (w)asm, given that there is a compiler (chain) that supports it as a target. 假设有一个支持它的编译器(链),那么不需要解释器的脚本语言当然可以直接编译为(w)asm。

Is asm.js code compiled in time and run? asm.js代码是否可以及时编译并运行? Compiled into what? 编译成什么?

Different browsers compile asm.js code in different ways. 不同的浏览器以不同的方式编译asm.js代码。 As of August 2015: 截至2015年8月:

  • Firefox compiles asm.js to machine code (and caches the machine code for future loads of the same asm.js) [ 1 ]. Firefox将asm.js编译为机器代码(并为以后的相同asm.js加载而缓存机器代码)[ 1 ]。
  • In Windows 10 as an experimental flag, Edge will also do some Ahead-of-Time validation and compilation of asm.js [ 2 ]. 在Windows 10中,作为实验性标志,Edge还将进行asm.js的提前验证和编译[ 2 ]。
  • Chrome specially recognizes the "use asm" directive at the beginning of asm.js to parse and analyze it the code more eagerly and tweak compilation heuristics. Chrome会在asm.js的开头特别识别“ use asm”指令,以便更急切地分析和分析代码并调整编译启发式方法。
  • Safari does no special processing of asm.js. Safari没有对asm.js进行特殊处理。

Other than asm.js being text and wasm (web assembly) being binary, what are the differences between the 2? 除了asm.js为文本和wasm(Web程序集)为二进制以外,两者之间有什么区别?

asm.js is just JavaScript and thus must behave exactly according to the JavaScript spec. asm.js只是JavaScript,因此必须严格按照JavaScript规范运行。 As a new standard, WebAssembly is able to fix some corner cases where JavaScript behavior is not the ideal (from a performance or compilation perspective) [ 3 ]. 作为一种新标准,WebAssembly能够解决JavaScript行为不理想的一些极端情况(从性能或编译角度来看)[ 3 ]。 In the future [ 4 ], WebAssembly will be able to add features that would otherwise be difficult to express in JavaScript. 将来[ 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为例,它将是

  • python code compiled to wasm? python代码编译为wasm? or 要么
  • python interpreter (Cpython) compiled into wasm and interpret python? python解释器(Cpython)编译成wasm并解释python?

In v.1, the simplest way to run Python in a browser will be to compile a Python interpreter to wasm, as you said. 如您所说,在v.1中,在浏览器中运行Python的最简单方法是将Python解释器编译为wasm。 This means, eg, the Python GC is running in wasm code and manually managing the wasm linear memory. 这意味着,例如,Python GC正在wasm代码中运行并手动管理wasm线性内存。 There has already been an experimental projects to add an asm.js backend to PyPy [ 5 ] (which could work just as well for wasm). 已经有一个实验项目将asm.js后端添加到PyPy [ 5 ](对于wasm也可以工作)。 It currently runs into limitations of asm.js that could be addressed by the dynamic linking future feature of wasm. 当前,它遇到了asm.js的局限性,可以通过wasm的动态链接未来功能来解决。 Going further, wasm seeks to provide both GC integration and JIT compilation support both of which would allow more efficient and natural integration with the Web platform. 更进一步,wasm寻求同时提供GC集成JIT编译支持 ,这将使与Web平台的集成更加高效自然。

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

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