about summary refs log tree commit diff
path: root/src/libstd/sys/wasm/os.rs
diff options
context:
space:
mode:
authorAlex Crichton <alex@alexcrichton.com>2017-10-22 20:01:00 -0700
committerAlex Crichton <alex@alexcrichton.com>2017-11-19 21:07:41 -0800
commit80ff0f74b0c4a8d384160af81a1b21f53622d8af (patch)
tree0f9bd959bf437c8da2497433fd936d285f912e61 /src/libstd/sys/wasm/os.rs
parent481b42b50745d433c5bcfd90e7a03a4b9110a677 (diff)
downloadrust-80ff0f74b0c4a8d384160af81a1b21f53622d8af.tar.gz
rust-80ff0f74b0c4a8d384160af81a1b21f53622d8af.zip
std: Add a new wasm32-unknown-unknown target
This commit adds a new target to the compiler: wasm32-unknown-unknown. This
target is a reimagining of what it looks like to generate WebAssembly code from
Rust. Instead of using Emscripten which can bring with it a weighty runtime this
instead is a target which uses only the LLVM backend for WebAssembly and a
"custom linker" for now which will hopefully one day be direct calls to lld.

Notable features of this target include:

* There is zero runtime footprint. The target assumes nothing exists other than
  the wasm32 instruction set.
* There is zero toolchain footprint beyond adding the target. No custom linker
  is needed, rustc contains everything.
* Very small wasm modules can be generated directly from Rust code using this
  target.
* Most of the standard library is stubbed out to return an error, but anything
  related to allocation works (aka `HashMap`, `Vec`, etc).
* Naturally, any `#[no_std]` crate should be 100% compatible with this new
  target.

This target is currently somewhat janky due to how linking works. The "linking"
is currently unconditional whole program LTO (aka LLVM is being used as a
linker). Naturally that means compiling programs is pretty slow! Eventually
though this target should have a linker.

This target is also intended to be quite experimental. I'm hoping that this can
act as a catalyst for further experimentation in Rust with WebAssembly. Breaking
changes are very likely to land to this target, so it's not recommended to rely
on it in any critical capacity yet. We'll let you know when it's "production
ready".

---

Currently testing-wise this target is looking pretty good but isn't complete.
I've got almost the entire `run-pass` test suite working with this target (lots
of tests ignored, but many passing as well). The `core` test suite is still
getting LLVM bugs fixed to get that working and will take some time. Relatively
simple programs all seem to work though!

---

It's worth nothing that you may not immediately see the "smallest possible wasm
module" for the input you feed to rustc. For various reasons it's very difficult
to get rid of the final "bloat" in vanilla rustc (again, a real linker should
fix all this). For now what you'll have to do is:

    cargo install --git https://github.com/alexcrichton/wasm-gc
    wasm-gc foo.wasm bar.wasm

And then `bar.wasm` should be the smallest we can get it!

---

In any case for now I'd love feedback on this, particularly on the various
integration points if you've got better ideas of how to approach them!
Diffstat (limited to 'src/libstd/sys/wasm/os.rs')
-rw-r--r--src/libstd/sys/wasm/os.rs136
1 files changed, 136 insertions, 0 deletions
diff --git a/src/libstd/sys/wasm/os.rs b/src/libstd/sys/wasm/os.rs
new file mode 100644
index 00000000000..c98030f7ebf
--- /dev/null
+++ b/src/libstd/sys/wasm/os.rs
@@ -0,0 +1,136 @@
+// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
+// file at the top-level directory of this distribution and at
+// http://rust-lang.org/COPYRIGHT.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+use core::intrinsics;
+
+use error::Error as StdError;
+use ffi::{OsString, OsStr};
+use fmt;
+use io;
+use mem;
+use path::{self, PathBuf};
+use str;
+use sys::{unsupported, Void};
+
+pub fn errno() -> i32 {
+    0
+}
+
+pub fn error_string(_errno: i32) -> String {
+    format!("operation successful")
+}
+
+pub fn getcwd() -> io::Result<PathBuf> {
+    unsupported()
+}
+
+pub fn chdir(_: &path::Path) -> io::Result<()> {
+    unsupported()
+}
+
+pub struct SplitPaths<'a>(&'a Void);
+
+pub fn split_paths(_unparsed: &OsStr) -> SplitPaths {
+    panic!("unsupported")
+}
+
+impl<'a> Iterator for SplitPaths<'a> {
+    type Item = PathBuf;
+    fn next(&mut self) -> Option<PathBuf> {
+        match *self.0 {}
+    }
+}
+
+#[derive(Debug)]
+pub struct JoinPathsError;
+
+pub fn join_paths<I, T>(_paths: I) -> Result<OsString, JoinPathsError>
+    where I: Iterator<Item=T>, T: AsRef<OsStr>
+{
+    Err(JoinPathsError)
+}
+
+impl fmt::Display for JoinPathsError {
+    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+        "not supported on wasm yet".fmt(f)
+    }
+}
+
+impl StdError for JoinPathsError {
+    fn description(&self) -> &str {
+        "not supported on wasm yet"
+    }
+}
+
+pub fn current_exe() -> io::Result<PathBuf> {
+    unsupported()
+}
+
+pub struct Env(Void);
+
+impl Iterator for Env {
+    type Item = (OsString, OsString);
+    fn next(&mut self) -> Option<(OsString, OsString)> {
+        match self.0 {}
+    }
+}
+
+pub fn env() -> Env {
+    panic!("not supported on web assembly")
+}
+
+pub fn getenv(k: &OsStr) -> io::Result<Option<OsString>> {
+    // If we're debugging the runtime then we actually probe node.js to ask for
+    // the value of environment variables to help provide inputs to programs.
+    // The `extern` shims here are defined in `src/etc/wasm32-shim.js` and are
+    // intended for debugging only, you should not rely on them.
+    if !super::DEBUG {
+        return Ok(None)
+    }
+
+    extern {
+        fn rust_wasm_getenv_len(k: *const u8, kl: usize) -> isize;
+        fn rust_wasm_getenv_data(k: *const u8, kl: usize, v: *mut u8);
+    }
+    unsafe {
+        let k: &[u8] = mem::transmute(k);
+        let n = rust_wasm_getenv_len(k.as_ptr(), k.len());
+        if n == -1 {
+            return Ok(None)
+        }
+        let mut data = vec![0; n as usize];
+        rust_wasm_getenv_data(k.as_ptr(), k.len(), data.as_mut_ptr());
+        Ok(Some(mem::transmute(data)))
+    }
+}
+
+pub fn setenv(_k: &OsStr, _v: &OsStr) -> io::Result<()> {
+    unsupported()
+}
+
+pub fn unsetenv(_n: &OsStr) -> io::Result<()> {
+    unsupported()
+}
+
+pub fn temp_dir() -> PathBuf {
+    panic!("no filesystem on wasm")
+}
+
+pub fn home_dir() -> Option<PathBuf> {
+    None
+}
+
+pub fn exit(_code: i32) -> ! {
+    unsafe { intrinsics::abort() }
+}
+
+pub fn getpid() -> u32 {
+    panic!("no pids on wasm")
+}