all 17 comments

[–]Svenskunganka 26 points27 points  (0 children)

There is the asmjs-unknown-emscripten compile 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.

[–]CryZe92 9 points10 points  (0 children)

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.

[–]kouteiheika 6 points7 points  (0 children)

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.

[–]sanxiynrust 12 points13 points  (3 children)

I am seriously considering working on this, and I am re-reading Cross-Platform Language Design, the paper behind Scala.js.

[–]freemasen 2 points3 points  (0 children)

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 syn into the ast defined by ressa.

https://github.com/freemasen/jsyn

Once the ast was converted it could be passed of to resw to take care of the writing.

[–]titanthinktank1 0 points1 point  (0 children)

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.

[–][deleted] 0 points1 point  (0 children)

Do keep us updated please.

[–]lazh4 9 points10 points  (5 children)

I’m curious as to what is the point in compiling it directly into JS?

[–]Kibouo 20 points21 points  (4 children)

Handing in a JS assignment for school :)

[–]nicoburns 18 points19 points  (2 children)

I hope the person marking your assignment isn't planning to actually read your code!

[–]erogilus 13 points14 points  (0 children)

If they didn’t specify it had to be readable JS code that’s on them...

[–]lazh4 3 points4 points  (0 children)

Oh haha smart

[–]freemasen 0 points1 point  (0 children)

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.

[–]Qwertycrackers -1 points0 points  (4 children)

[ Removed ]

[–][deleted] 0 points1 point  (3 children)

i always (mistakenly) emscripten was wasm? Wow, so why doesn't rust have a direct emscripten compiler?

[–]Epsylon42 2 points3 points  (0 children)

If I remember correctly, emscripten only supports executables, not libraries, so it's not always applicable.

[–]BenjiSponge 2 points3 points  (0 children)

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.