rustwasm / team

A point of coordination for all things Rust and WebAssembly
MIT License
1.45k stars 59 forks source link

Enable experimentation with out-of-tree libstd for wasm #31

Open aidanhs opened 6 years ago

aidanhs commented 6 years ago

One of the points of contention in https://github.com/aturon/rust-wasm/issues/16 seems to be a disagreement about what should end up in the -unknown target.

If it was really easy to set up your own libstd to experiment with, it might lessen the urgency to get things into libstd and allow people to iterate out of tree (see my comment at https://github.com/rust-lang/rust/pull/47102#issuecomment-355589188).

First I'd like to document where I'm up to so far.

$ rustup default nightly-2018-01-15
$ git clone git@github.com:japaric/xargo.git && (cd xargs && cargo build)
$ git clone git@github.com:rust-lang/rust.git rust-wasm
$ cd rust-wasm
rust-wasm $ git checkout b71cbd
rust-wasm $ # some editing
rust-wasm $ git diff
diff --git a/src/etc/wasm32-shim.js b/src/etc/wasm32-shim.js
index d55083e..873b1de 100644
--- a/src/etc/wasm32-shim.js
+++ b/src/etc/wasm32-shim.js
@@ -113,3 +113,5 @@ for (var i = 0; i < module_imports.length; i++) {
 }

 let instance = new WebAssembly.Instance(m, imports);
+memory = instance.exports.memory;
+instance.exports.hi();
diff --git a/src/libstd/sys/wasm/mod.rs b/src/libstd/sys/wasm/mod.rs
index ba3d6a2..b57ce0e 100644
--- a/src/libstd/sys/wasm/mod.rs
+++ b/src/libstd/sys/wasm/mod.rs
@@ -36,7 +36,7 @@ use os::raw::c_char;
 // help receive debug output and see what's going on. In general this flag
 // currently controls "will we call out to our own defined shims in node.js",
 // and this flag should always be `false` for release builds.
-const DEBUG: bool = false;
+const DEBUG: bool = true;

 pub mod args;
 #[cfg(feature = "backtrace")]
rust-wasm $ cd ..
$ cargo new --bin mywasmtest
$ cd mywasmtest
mywasmtest $ vim src/main.rs
mywasmtest $ cat src/main.rs
fn main() {}

#[no_mangle]
pub fn hi() {
    println!("Hello, world!");
    let v = vec![1,2,3];
    println!("{:?}", v);
}
mywasmtest $ vim Xargo.toml
mywasmtest $ cat Xargo.toml
[dependencies]
std = { path = "../rust-wasm/src/libstd" }
mywasmtest $ XARGO_RUST_SRC=$(pwd)/../rust-wasm/src ../xargo/target/debug/xargo build --target wasm32-unknown-unknown --verbose --release
mywasmtest $ node ../rust-wasm/src/etc/wasm32-shim.js target/wasm32-unknown-unknown/release/mywasmtest.wasm                                                   
Hello, world!
[1, 2, 3]

i.e. it's possible to build your own working libstd wasm today with syscalls.

However, there are a few annoyances:

In my perfect world:

Which would then allow anyone to create their own repos of rustlibs and depend on them for applications.

aidanhs commented 6 years ago

Also related, the declaration that xargo is in maintenance mode and will probably be getting upstreamed at some point - https://github.com/japaric/xargo/issues/193