Mozilla-Student-Projects / Projects-Tracker

Keep tracks of Firefox/Thunderbird/Firefox OS features that can/should/will be implemented as add-ons/apps.
66 stars 19 forks source link

Compile Rust to JavaScript with emscripten #33

Open brson opened 12 years ago

brson commented 12 years ago

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 commented 12 years ago

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 commented 11 years ago

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 commented 11 years ago

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 commented 11 years ago

@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 commented 10 years ago

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 commented 10 years ago

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 commented 10 years ago

Yes, they do compile and run fine.

brson commented 10 years ago

@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 commented 10 years ago

@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 commented 10 years ago

@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 commented 10 years ago

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

Arcnor commented 10 years ago

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 commented 10 years ago

See existing progress at http://www.reddit.com/r/rust/comments/20nnkk/rust_and_emscripten_a_small_success/

Arcnor commented 10 years ago

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 commented 10 years ago

@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 commented 10 years ago

@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 commented 9 years ago

It looks like https://github.com/rust-lang/rfcs/issues/604 is the most recent place for info about this