Crate javy_plugin_api

Crate javy_plugin_api 

Source
Expand description

A crate for creating Javy plugins

Example usage for creating WASI preview2 plugins:

use javy_plugin_api::{
    javy::{quickjs::prelude::Func, Runtime},
    javy_plugin, Config,
};

wit_bindgen::generate!({ world: "my-javy-plugin-v1", generate_all });

fn config() -> Config {
    let mut config = Config::default();
    config
        .text_encoding(true)
        .javy_stream_io(true);
    config
}

fn modify_runtime(runtime: Runtime) -> Runtime {
    runtime.context().with(|ctx| {
        ctx.globals().set("plugin", true).unwrap();
    });
    runtime
}

struct Component;

// Dynamically linked modules will use `my_javy_plugin_v1` as the import
// namespace.
javy_plugin!("my-javy-plugin-v1", Component, config, modify_runtime);

export!(Component);

//! Example for creating WASI preview 1 plugins:

use javy_plugin_api::{
    import_namespace,
    javy::{quickjs::prelude::Func, Runtime},
    Config,
};

import_namespace!("test-plugin-wasip1");

#[link(wasm_import_module = "some_host")]
extern "C" {
    fn imported_function();
}

fn config() -> Config {
    Config::default()
}

fn modify_runtime(runtime: Runtime) -> Runtime {
    runtime.context().with(|ctx| {
        ctx.globals().set("plugin", true).unwrap();
        ctx.globals()
            .set(
                "func",
                Func::from(|| {
                    unsafe { crate::imported_function() };
                }),
            )
            .unwrap();
    });
    runtime
}

#[export_name = "initialize-runtime"]
fn initialize_runtime() {
    javy_plugin_api::initialize_runtime(config, modify_runtime).unwrap()
}

The crate will automatically add exports for a number of Wasm functions in your crate that Javy needs to work.

§Core concepts

  • javy - a re-export of the javy crate.
  • javy_plugin - Used for WASI preview 2 plugins and not WASI preview 1 plugins. Takes a namespace to use for the module name for imports, a struct to add function exports to, a config method, and a method for updating the Javy runtime.
  • import_namespace - Used for WASI preview 1 plugins. Takes a namespace to use for the module name for imports.
  • Config - to add behavior to the created javy::Runtime.

§Features

  • json - enables the json feature in the javy crate.
  • messagepack - enables the messagepack feature in the javy crate.

Re-exports§

pub use javy;

Macros§

import_namespace
Create a custom section named import_namespace with the contents of the string argument.
javy_plugin
Provides default implementations for required methods a Javy plugin.

Structs§

Config
A configuration for the Javy plugin API.

Functions§

compile_src
Compiles JS source code to QuickJS bytecode.
initialize_runtime
Initializes the Javy runtime.
invoke
Evaluates QuickJS bytecode and optionally invokes exported JS function with name.