What is the difference between asm.js and WebAssembly?

非 Y 不嫁゛ 提交于 2019-12-20 08:06:14

问题


I have been reading about asm.js and WebAssembly recently:

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

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? Compiled into what?
  2. Other than asm.js being text and wasm (web assembly) being binary, what are the differences between the 2?
  3. 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?

回答1:


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

asm.js is regular javascript code, and is compiled into bytecode by the JS interpreter as always. 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. See http://asmjs.org/ for details.

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

None, for now. wasm is supposed to be backwards-compatible, compilable to asm (which again is executable as normal 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. 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.




回答2:


asm.js is a subset of JS with "highly optimizable" instructions. 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. It has benefits if your app does a lot of calculation or graphic if used together with WebGL.

web assembly is a binary format for JS, all JS, not only asm.js. It's not a bytecode, it's a binary encoding of the AST that the parser calculates. It has 2 big benefits:

  • the JS engine can skip the parsing step
  • it's much more compact than the JS original source

We already can write code for browsers that isn't JS: EMSCripten can compile c++ code in JS code. Other transcompiler are already available to compile your code into JS. Using asm.js that code can run faster when it does math. 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). You won't have a new plugin to load like DirectX, JavaApplets, Flash or Silverlight because everything will run in the JS sandbox.




回答3:


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

Different browsers compile asm.js code in different ways. As of August 2015:

  • Firefox compiles asm.js to machine code (and caches the machine code for future loads of the same 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].
  • 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.
  • Safari does no special processing of asm.js.

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

asm.js is just JavaScript and thus must behave exactly according to the JavaScript spec. 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]. In the future [4], WebAssembly will be able to add features that would otherwise be difficult to express in 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?

In v.1, the simplest way to run Python in a browser will be to compile a Python interpreter to wasm, as you said. This means, e.g., the Python GC is running in wasm code and manually managing the wasm linear memory. There has already been an experimental projects to add an asm.js backend to PyPy [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 both of which would allow more efficient and natural integration with the Web platform.



来源:https://stackoverflow.com/questions/31502563/what-is-the-difference-between-asm-js-and-webassembly

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!