4 stable releases

2.2.0 Jan 31, 2024
2.1.0 Sep 11, 2023
2.0.0 Aug 17, 2023
1.0.0 May 17, 2023

#1009 in WebAssembly

Download history 7/week @ 2024-01-29 17/week @ 2024-02-19 60/week @ 2024-02-26 7/week @ 2024-03-04 25/week @ 2024-03-11 27/week @ 2024-04-01

53 downloads per month
Used in javy-apis

Apache-2.0 WITH LLVM-exception

145KB
3K SLoC

javy

A configurable JavaScript runtime for WebAssembly.

Uses QuickJS through the quickjs-wasm-rs crate to evalulate JavaScript source code or QuickJS bytecode.

Example usage

use anyhow::{anyhow, Result};
use javy::{quickjs::JSValue, Runtime};

fn main() -> Result<()> {
    let runtime = Runtime::default();
    let context = runtime.context();
    context.global_object()?.set_property(
        "print",
        context.wrap_callback(move |_ctx, _this, args| {
            let str = args
                .first()
                .ok_or(anyhow!("Need to pass an argument"))?
                .to_string();
            println!("{str}");
            Ok(JSValue::Undefined)
        })?,
    )?;
    context.eval_global("hello.js", "print('hello!');")?;
    Ok(())
}

Create a Runtime and use the reference returned by context() to add functions and evaluate source code.

Features

  • export_alloc_fns - exports canonical_abi_realloc and canonical_abi_free from generated WebAssembly for allocating and freeing memory
  • json - transcoding functions for converting between JSValueRef and JSON
  • messagepack - transcoding functions for converting between JSValueRef and MessagePack

Publishing to crates.io

To publish this crate to crates.io, run ./publish.sh.

Using a custom WASI SDK

This crate can be compiled using a custom WASI SDK. When building this crate, set the QUICKJS_WASM_SYS_WASI_SDK_PATH environment variable to the absolute path where you installed the SDK.

Dependencies

~3.5–8MB
~173K SLoC