4 ms·
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.
- acemarke 9y agoUh... does this list of "languages that compile to Javascript" count? :) https://github.com/jashkenas/coffeescript/wiki/list-of-languages-that-compile-to-js 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 https://github.com/kripken/emscripten
- mmanulis 9y agoThese 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 9y ago"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 9y agoGot it. Thanks for the links too, helps to get some context around this.
- flavio81 9y agoSee asm.js. This is exactly what you propose. BTW, asm.js has been superseded by Webassembly which ditches Javascript completely.
- mmanulis 9y agoAs 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 9y agoNot 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 9y agoI see, that makes sense. The bytecode part still has to be implemented in a browser though, right?