Crate javy

Crate javy 

Source
Expand description

Configurable JavaScript runtime for WebAssembly.

Example usage:

use anyhow::Result;
use javy::quickjs::{
    function::{MutFn, Rest},
    Ctx, Function, Value
};
use javy::{from_js_error, Runtime};

fn main() -> Result<()> {
    let runtime = Runtime::default();
    let context = runtime.context();

    context.with(|cx| {
        let globals = cx.globals();
        globals.set(
            "print_hello",
            Function::new(
                cx.clone(),
                MutFn::new(|_: Ctx<'_>, _: Rest<Value<'_>>| {
                    println!("Hello, world!");
                }),
            )?,
        )
    })?;

    context.with(|cx| {
        cx.eval_with_options("print_hello();", Default::default())
            .map_err(|e| from_js_error(cx.clone(), e))
            .map(|_: ()| ())
    })?;

    Ok(())
}

§Core concepts

  • Runtime - The entrypoint for using the JavaScript runtime. Use a Config to configure behavior.

§Features

  • json - functions for converting between [quickjs::JSValueRef] and JSON byte slices
  • messagepack - functions for converting between [quickjs::JSValueRef] and MessagePack byte slices

Re-exports§

pub use rquickjs as quickjs;

Macros§

hold
Alias for Args::hold
hold_and_release
Alias for [Args::hold(cx, args).release()]

Structs§

Args
A struct to hold the current Ctx and Values passed as arguments to Rust functions. A struct here is used to explicitly tie these values with a particular lifetime.
Config
A configuration for Runtime.
Runtime
A JavaScript Runtime.

Functions§

from_js_error
Handles a JavaScript error or exception and converts to anyhow::Error.
to_js_error
Converts an anyhow::Error to a JSError.
to_string_lossy
Converts the JavaScript value to a string, replacing any invalid UTF-8 sequences with the Unicode replacement character (U+FFFD).
val_to_string
Retrieves the string representation of a JavaScript value.