Ask HN: Can JavaScript be treated like machine language?
I'm wondering if it's possible to treat JS as if it's a CPU instruction set and then building a compiler back-end for Clang or GCC for it.
Even if it's not possible for Clang or GCC, any reason(s) why JS cannot be treated as a machine language, potentially using a subset of it in the beginning.
I imagine the "model" would be centered around DOM manipulation and AJAX facilities.
I'm sure I'm not the first one to ask this, if people have relevant links to share, would love to see them.
8 comments
[ 4.6 ms ] story [ 32.5 ms ] threadMore specifically, Emscripten seems to be exactly what you're describing: https://github.com/kripken/emscripten
I was thinking more along the lines of how a compiler works, translating into some kind of AST and then converting into JS from the AST.
While I haven't looked at the implementations of any of those compilers, I would assume that most of them do actually parse their source language into an appropriate AST, and write out JS based on that AST.
BTW, asm.js has been superseded by Webassembly which ditches Javascript completely.
Not sure if I understood it correctly though.
The browser, instead of downloading javascript, downloads WASM bytecode, and this bytecode gets compiled ahead-of-time to machine language and ran by the browser.
The bytecode itself is the product of compiling a source code written in the language of your choice, for example Java or something more advanced like INTERCAL.