Crate javy

Source
Expand description

Configurable JavaScript runtime for WebAssembly.

Example usage:

use anyhow::anyhow;
use javy::{Runtime, from_js_error};
let runtime = Runtime::default();
let context = runtime.context();

context.with(|cx| {
    let globals = this.globals();
    globals.set(
        "print_hello",
        Function::new(
            this.clone(),
            MutFn::new(move |_, _| {
                println!("Hello, world!");
            }),
        )?,
    )?;
});

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

§Core concepts

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

§Features

  • export_alloc_fns - exports alloc::canonical_abi_realloc and alloc::canonical_abi_free from generated WebAssembly for allocating and freeing memory
  • 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;

Modules§

alloc

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.