Ask HN: Can JavaScript be treated like machine language?

1 points by mmanulis ↗ HN
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 ] thread
Uh... does this list of "languages that compile to Javascript" count? :) https://github.com/jashkenas/coffeescript/wiki/list-of-langu...

More specifically, Emscripten seems to be exactly what you're describing: https://github.com/kripken/emscripten

These are more like transpiling, right? That is, there is a direct translation from <language> into JS?

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.

"Transpiling" _is_ compiling. The term is just used to refer to output that is another high-level language, rather than some kind of assembly or bytecode.

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.

Got it. Thanks for the links too, helps to get some context around this.
See asm.js. This is exactly what you propose.

BTW, asm.js has been superseded by Webassembly which ditches Javascript​ completely.

As I understand Webassembly, it's meant to replace JS completely as a native implementation in a browser. That is, it's meant to replace the JS interpreter.

Not sure if I understood it correctly though.

Not exactly.

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.

I see, that makes sense. The bytecode part still has to be implemented in a browser though, right?