GithubHelp home page GithubHelp logo

sile / nowasm Goto Github PK

View Code? Open in Web Editor NEW
34.0 2.0 0.0 1.14 MB

No-std, no-unsafe and no-dependencies WebAssembly 1.0 runtime for Rust

License: MIT License

Rust 100.00%
nostd rust webassembly webassembly-runtime

nowasm's Introduction

nowasm

nowasm Documentation Actions Status License

nowasm is a WebAssembly 1.0 runtime that is implemented with no-std, no-unsafe and no-dependencies.

The goal is to provide a lightweight WebAssembly runtime that can be embedded wherever Rust is used, with a particular focus on Wasm-in-Wasm scenarios.

TODO list until v0.1.0

  • Add validation phase (TBD)
  • Add doc comments
  • Add more tests

Supported Extensions

nowasm supports the following extensions necessary to run WebAssembly binaries built with the latest stable Rust compiler.

Examples

Please execute the command $ cargo build --target wasm32-unknown-unknown --example hello to build the following "Hello World!" printing code (examples/wasm/hello.rs) into a WebAssembly binary:

extern "C" {
    fn print(s: *const u8, len: i32);
}

#[no_mangle]
pub fn hello() {
    let msg = "Hello, World!\n";
    unsafe {
        print(msg.as_ptr(), msg.len() as i32);
    }
}

Then, you can execute the hello() function via the following command:

$ cargo run --example call_hello
Hello, World!

The code of examples/call_hello.rs is as follows:

use nowasm::{Env, HostFunc, Module, Resolve, StdVectorFactory, Val};

pub fn main() {
    let wasm_bytes = include_bytes!("../target/wasm32-unknown-unknown/debug/examples/hello.wasm");

    let module = Module::<StdVectorFactory>::decode(wasm_bytes).expect("Failed to decode module");

    let mut instance = module
        .instantiate(Resolver)
        .expect("Failed to instantiate module");

    instance
        .invoke("hello", &[])
        .expect("Failed to invoke function");
}

struct Resolver;

impl Resolve for Resolver {
    type HostFunc = Print;

    fn resolve_func(&self, module: &str, name: &str) -> Option<Self::HostFunc> {
        assert_eq!(module, "env");
        assert_eq!(name, "print");
        Some(Print)
    }
}

struct Print;

impl HostFunc for Print {
    fn invoke(&mut self, args: &[Val], env: &mut Env) -> Option<Val> {
        let ptr = args[0].as_i32().expect("Not a i32") as usize;
        let len = args[1].as_i32().expect("Not a i32") as usize;
        let msg = std::str::from_utf8(&env.mem[ptr..ptr + len]).expect("Invalid utf8");
        print!("{msg}");
        None
    }
}

nowasm's People

Contributors

sile avatar

Stargazers

Dmitry Ledentsov avatar Takumasa Sakao avatar Syuparn avatar Ryo Hirayama avatar Adrian Zuber avatar SUZUKI Keiichi avatar Genkagaku.GPT avatar HeDui avatar cisen avatar Hirohisa Mitsuishi avatar hiromasa avatar Daido Shota avatar Akito avatar Masanori Tani avatar Tiago Carvalho avatar 赵雨愛 avatar Luckas avatar Yazalde Filimone avatar Jan Riemer avatar Rodrigo Navarro avatar SUZUKI Sosuke avatar Marc Espin avatar Dean Srebnik avatar Kotaro Suto avatar Sʜɪᴍᴜʀᴀ Yū avatar Shotaro Nakamura avatar TANIGUCHI Masaya avatar Mutsuha Asada avatar skanehira avatar saturday06 avatar oligami avatar Sosuke Hosokawa avatar uint256_t avatar V avatar

Watchers

Lucian avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.