Crate wasi_print

source ·
Expand description

This crate contains basic niceties for writing no_std modules for WASI. wasi-print provides:

  • An abort() function that raises a WASI exception.
  • A panic_handler that aborts after trying to print panic information.
  • A print_fd() function that prints an &str to a WASI fd.
  • Printing macros print!(), println!(), !eprint() and !eprintln().

Example

This is a full standalone Rust WASM program using wasi_print.

#![no_std]

use wasi_print::*;

#[no_mangle]
pub extern "C" fn math_add(x: i32, y: i32) -> i32 {
    eprint!("guest running math_add({}, {}) …", x, y);
    let result = x + y;
    eprintln!(" and returning {}", result);
    result
}

Features

  • print: Include printing code. This requires nightly for a variety of reasons.
  • panic_handler: Provide a panic handler.

Acknowledgments

Figuring out how to write this was made much easier by this excellent blog post by “James [Undefined]”.

License

This work is licensed under the “MIT License”. Please see the file LICENSE.txt in this distribution for license terms.

Macros

  • eprintprint
    Print to stdio without a trailing newline.
  • Print to stdio with a trailing newline.
  • formatprint
    Creates a String using interpolation of runtime expressions.
  • printprint
    Print to stdio without a trailing newline.
  • printlnprint
    Print to stdio with a trailing newline.

Functions

  • Attempt to terminate the current execution by raising a WASI ABRT exception. This function should not return: if it does, it will return an error indicating why it failed to terminate.
  • panic_handlerpanic-handler
    Handle a panic() in a WASI-compatible way.
  • Print the text of s to the WASI file descriptor fd.