Skip to content
This repository has been archived by the owner on May 27, 2020. It is now read-only.

Compile Rust to JavaScript with emscripten #33

Open
brson opened this issue Sep 24, 2012 · 17 comments
Open

Compile Rust to JavaScript with emscripten #33

brson opened this issue Sep 24, 2012 · 17 comments

Comments

@brson
Copy link

brson commented Sep 24, 2012

It would be nice to be able to run Rust code in the browser, particularly so we can create a 'try rust' website. We would like to do this using the emscripten LLVM-to-JavaScript compiler. The best approach is not exactly clear, but will probably entail porting more C++ runtime code to Rust and creating some sort of platform abstraction layer.

See also: 2235

@z0w0
Copy link

z0w0 commented Sep 24, 2012

It should be as "easy" as creating an intermediary library in JS for all runtime dependencies (libuv, etc.) like this. I say easy in the sense that I don't think you'll have to do much magic to get it working, just a lot of work to get the runtime wrapped with JS functionality.

@jsyeo
Copy link

jsyeo commented Oct 22, 2012

Hi, I will like to work on this. Are there any docs that I should consult before starting?

I have looked at https://github.com/kripken/emscripten/wiki and http://mozakai.blogspot.sg/2012/03/howto-port-cc-library-to-javascript.html.

Also, I've tried to compile it with emcc but I'm getting some errors. Should I post them to the emscripten mailing list?

@z0w0
Copy link

z0w0 commented Oct 22, 2012

You will need to look into how the runtime works, what sort of memory requirements it needs and how those memory requirements would best be wrapped in Javascript.

Assuming the error you get is something about braces, then I get that too. And yeah, I would post it to the mailing list, along with the bitcode file (rustc --emit-llvm) and the LLVM assembly file (llvm-dis file.bc). Or create an issue on emscripten's github repository.

@brson
Copy link
Author

brson commented Oct 22, 2012

@jsyeo by reading the emscripten docs you are a step ahead of me. You are fortunate that emscripten's author (@kripken / azakai) is on the Mozilla research team (and sits next to me) so you can feel free to bug him extensively. For things that you suspect are emscripten bugs, the best place to post them is the emscripten bug tracker on github, otherwise the emscripten mailing list is appropriate. #emscripten on irc.mozilla.org is a good place to ask questions too (as well as #rust).

@minhnhdo
Copy link

Any update on this?

I have tried with the following programs and they work fine.

#[start]
fn main(_:int, _: **u8) -> int { 0 }
#[start]
fn main(_: int, _: **u8) -> int {
    unsafe {
        std::libc::puts(0 as *i8);
    }
    0
}

@z0w0
Copy link

z0w0 commented Nov 21, 2013

I'd imagine with the work towards runtime-less rust this should be much easier to implement now. And by working, do you mean those programs compile fine in Emscripten?

@minhnhdo
Copy link

Yes, they do compile and run fine.

@brson
Copy link
Author

brson commented Nov 21, 2013

@mrordinaire I'd be very interested to see a blog post or other writeup about how you compiled Rust with emscripten. I've never heard of anybody doing that. The ultimate goal of this task would be to make emscripten a first-class platform in Rust so that you could e.g. rustc --target=i686-pc-emscripten (or whatever emcc is using as a triple), and that includes having a working standard library.

@minhnhdo
Copy link

@brson Yes, I will make a writeup of this when I can find the time (it's right in the middle of exams here on my side). About emscripten being a first class platform, how will rustc --target=i686-pc-emscripten's output different from the other targets? Does that mean emscripten will be bundled with rustc?

@brson
Copy link
Author

brson commented Nov 22, 2013

@mrordinaire rustc would probably need to take an argument specifically to provide the path to the emscripten toolchain, similar to our --android-cross-path argument.

@brson
Copy link
Author

brson commented May 15, 2014

Still a great project. Should be pretty easy these days since the libs are very modular now.

@Arcnor
Copy link

Arcnor commented Jun 7, 2014

Is there any progress with this? Do we need to modify Rust itself to make this work?

I was thinking the same, some sort of "Try Rust", as documentation is great but still no "user friendly"

@jdm
Copy link

jdm commented Jun 7, 2014

@Arcnor
Copy link

Arcnor commented Jun 7, 2014

Awesome! So sad that the std is not ported yet. I also found this: http://bilalhusain.com/rust-lexer/inference.html, not sure if that's using ECMAScripten to do whatever is doing

@brson
Copy link
Author

brson commented Jun 7, 2014

@Arcnor I just put a status update about how I think to proceed over on the main Rust issue tracker. You are very welcome to take a stab at it.

@Arcnor
Copy link

Arcnor commented Jun 7, 2014

@brson Just was looking at it, thank you very much :D. I'm still a super newbie at Rust, and that's why I was looking at some "to the web" version, to help or document my journey.

If I get any of your steps done, I'll comment in that issue. Thanks!

@jonnor
Copy link

jonnor commented Mar 28, 2015

It looks like rust-lang/rfcs#604 is the most recent place for info about this

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

7 participants