10 releases

0.1.6 Mar 9, 2020
0.1.5 Mar 9, 2020
0.0.3 Mar 4, 2020

#923 in WebAssembly

42 downloads per month
Used in wcurses

MIT/Apache

12KB
322 lines

libw

docs.rs docs

This library is meant to be a more human wrapper around only the strict capabilities of wasi

  • only uses #[no_std] and alloc to encourage non-bloated wasm binaries
  • does not require rust be built with wasm32-wasi ( doing so increases file size )
  • high level operations independent of POSIX
  • great way to learn exactly how wasi works!.

hello world

[package]
name = "my_app"
version = "0.0.1"

[lib]
crate-type = ["cdylib"]

[profile.release]
lto = true

[dependencies]
libw = "0"
#![no_std]

#[no_mangle]
pub fn _start() {
    libw::print("hey!\n");
}
build:
	@RUSTFLAGS='-C link-arg=-s' cargo build --target wasm32-unknown-unknown --release
	@cp target/wasm32-unknown-unknown/release/wasp.wasm .
wasmer my_app.wasm

working with files

wasi modules are only able to work on folders they have been given explicit permission to access. By default wasi has 3 basic files:

  • 0 - application text input stream
  • 1 - application text output stream
  • 2 - application error text output stream

The file that you give explicit permission to will be given file descriptor number on startup. You must do this manually by specifying the directory.

In libw can get list of accessible directories with accessible_directores(). You must specify a full path that has a prefix of one of the accessible paths.

let path = "/foo/hello.txt";
let mut txt = libw::read_text(path);
txt.push_str("goodbye");
libw::write_text(path, txt);
wasmer my_app.wasm --dir=/foo 

API

data streams

  • read_line - read input characters until you hit enter
  • print - print characters
  • println - print characters ending with newline
  • error - print error

time

  • current_time - milliseconds since unix epoc
  • unix_time - seconds since unix epoc
  • high_precision_time - get the current realtime value of host clock

scheduling

  • exit - stop the current process
  • sleep - yield control of thread for n milliseconds

environment

  • accessible_directories - gets a vector of directories your wasi module has access to
  • environment_variables - get a vector of environment variables
  • command_arguments - get a vec of strings of command line arguments

file

  • read_text - read a text file into a string
  • write_text - write a text file from a string
  • read_bytes - read all bytes from file
  • write_bytes - write all bytes to a file
  • is_readable - can this file be read from
  • is_writeable - can this file be written to
  • is_directory - is the file a directory
  • is_stream - is this file a stream

math

  • random - get a random f32

License

This project is licensed under either of

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in libw by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

Dependencies

~1MB
~15K SLoC