
Ask HN: Can JavaScript be treated like machine language? - mmanulis
I&#x27;m wondering if it&#x27;s possible to treat JS as if it&#x27;s a CPU instruction set and then building a compiler back-end for Clang or GCC for it.<p>Even if it&#x27;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.<p>I imagine the &quot;model&quot; would be centered around DOM manipulation and AJAX facilities.<p>I&#x27;m sure I&#x27;m not the first one to ask this, if people have relevant links to share, would love to see them.
======
acemarke
Uh... does this list of "languages that compile to Javascript" count? :)
[https://github.com/jashkenas/coffeescript/wiki/list-of-
langu...](https://github.com/jashkenas/coffeescript/wiki/list-of-languages-
that-compile-to-js)

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

~~~
mmanulis
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.

~~~
acemarke
"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.

~~~
mmanulis
Got it. Thanks for the links too, helps to get some context around this.

------
flavio81
See asm.js. This is exactly what you propose.

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

~~~
mmanulis
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.

~~~
flavio81
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.

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

