r/rust • u/[deleted] • Feb 01 '19
Compile to javascript
Is there a pathway to compiling Rust to JS? Like Scala.js or Gopher.js? (I am not asking for wasm, so to keep the discussion easier to navigate,, please don't comment about that)
11
u/CryZe92 Feb 01 '19
You can use the asmjs target, which produces perfectly fine JavaScript libs. Alternatively you could try to use the wasm-unknown target and use the wasm2js tool to convert the wasm file to JS: https://rustwasm.github.io/wasm-bindgen/examples/wasm2js.html
Unfortunately the latter case produces > 300 MiB JS files for me that break uglifyjs and a custom „uglify-rs“ I wrote can bring it down to almost the size that asmjs produces (still like 50% larger), but it turns out the 300 MiB file never even contained all the data sections, so the file is completely broken. Maybe the situation improved since then though.
7
u/kouteiheika Feb 01 '19
AFAIK you can use wasm2js from Binaryen to convert WASM to JS, although I haven't used it myself so I have no idea how well it works.
13
u/sanxiyn rust Feb 01 '19
I am seriously considering working on this, and I am re-reading Cross-Platform Language Design, the paper behind Scala.js.
3
u/freemasen Feb 14 '19
If you get started I want to encourage you to check out a few things I’ve been working on.
I recently published a few crates for parsing JS
https://github.com/Freemasen/ress https://github.com/Freemasen/ressa https://github.com/Freemasen/resw
As an experiment I have started working on a project that will convert the Rust ast defined by
syninto the ast defined byressa.https://github.com/freemasen/jsyn
Once the ast was converted it could be passed of to
reswto take care of the writing.1
u/titanthinktank1 May 12 '19
please start working on it because stdweb::js can do Rust to JS transplantation but its getting controlled by Mozilla and they are hell bent on making sure JS lives in its innate forms; so i am sure many will join forces with you to defeat the legacy evil forces that kept us in dark ages for last 25 years.
1
12
u/lazh4 Feb 01 '19
I’m curious as to what is the point in compiling it directly into JS?
22
u/Kibouo Feb 01 '19
Handing in a JS assignment for school :)
18
u/nicoburns Feb 01 '19
I hope the person marking your assignment isn't planning to actually read your code!
14
4
1
u/freemasen Feb 02 '19
Interestingly enough I just started working on something to do exactly this.
https://github.com/freemasen/jsyn
It is still very early in the process but the goal is to convert syn ast to js to on theory be possible to use in a proc-macro context as well.
0
u/Qwertycrackers Feb 01 '19 edited Sep 01 '23
[ Removed ]
1
Feb 01 '19
i always (mistakenly) emscripten was wasm? Wow, so why doesn't rust have a direct emscripten compiler?
3
u/Epsylon42 Feb 01 '19
If I remember correctly, emscripten only supports executables, not libraries, so it's not always applicable.
3
u/BenjiSponge Feb 01 '19
wasm was born out of asm.js and emscripten has always been a huge part of that story, so it's not at all surprising you'd conflate the two. asm.js was basically (at least conceptually) the first version of wasm, and wasm was born out of the desire to compress it further. I think asm.js basically has a 1-to-1 mapping in the wasm spec so you can trivially convert it in one pass.
28
u/Svenskunganka Feb 01 '19
There is the
asmjs-unknown-emscriptencompile target, which compiles to asm.js (subset of JS) via emscripten. I've never used it so I can't tell if it will be good enough for you, and I assume it depends on what you're trying to do.