|
1 | 1 | # Extending |
2 | 2 |
|
3 | 3 | If you want to use Javy for your own project, you may find that the existing |
4 | | -code is not sufficient since you may want to offer custom APIs or use different |
5 | | -branding for the CLI. The approach we'd recommend taking is to create your own |
6 | | -version of the `javy-cli` and `javy-plugin` crates (you could fork these if you |
7 | | -would like) and depend on the upstream version of the `javy` crate. You can add |
8 | | -your own implementations of custom JS APIs in your fork of the `javy-plugin` crate |
9 | | -or in a different crate that you depend on in your `javy-plugin` fork. If you find |
10 | | -that something is missing in the `javy` crate that you require to implement |
11 | | -something in your fork, we would appreciate it if you would open a GitHub issue |
12 | | -and consider making the change upstream instead of in your fork so all users of |
13 | | -the `javy` crate can benefit. |
| 4 | +code is not sufficient since you may want to offer custom JavaScript APIs. The |
| 5 | +approach we recommend taking is to create your own Javy plugin Wasm module. |
| 6 | +This plugin module can then be specified when using the Javy CLI when building |
| 7 | +Javy Wasm modules as a `-C plugin` flag when using `javy build`. |
| 8 | + |
| 9 | +To create your own Javy plugin Wasm module, create a new Rust project that |
| 10 | +will compile to a library (that is, `cargo init --lib`). |
| 11 | + |
| 12 | +Your `Cargo.toml` should look like: |
| 13 | + |
| 14 | +```toml |
| 15 | +[package] |
| 16 | +name = "my-plugin-name" |
| 17 | +version = "0.1.0" |
| 18 | + |
| 19 | +[lib] |
| 20 | +name = "my_plugin_name" |
| 21 | +crate-type = ["cdylib"] |
| 22 | + |
| 23 | +[dependencies] |
| 24 | +javy-plugin-api = "1.0.0" |
| 25 | +``` |
| 26 | + |
| 27 | +And `src/lib.rs` should look like: |
| 28 | + |
| 29 | +```rust |
| 30 | +use javy_plugin_api::import_namespace; |
| 31 | +use javy_plugin_api::javy::quickjs::prelude::Func; |
| 32 | +use javy_plugin_api::javy::Config; |
| 33 | + |
| 34 | +import_namespace!("my_plugin_name"); |
| 35 | + |
| 36 | +#[export_name = "initialize_runtime"] |
| 37 | +pub extern "C" fn initialize_runtime() { |
| 38 | + let mut config = Config::default(); |
| 39 | + config |
| 40 | + .text_encoding(true) |
| 41 | + .javy_stream_io(true); |
| 42 | + |
| 43 | + javy_plugin_api::initialize_runtime(config, |runtime| { |
| 44 | + runtime |
| 45 | + .context() |
| 46 | + .with(|ctx| { |
| 47 | + ctx.globals() |
| 48 | + .set("isThisAPlugin", Func::from(|| "yes it is")) |
| 49 | + }) |
| 50 | + .unwrap(); |
| 51 | + runtime |
| 52 | + }) |
| 53 | + .unwrap(); |
| 54 | +} |
| 55 | +``` |
| 56 | + |
| 57 | +You can then run `cargo build --target=wasm32-wasip1 --release` to create a |
| 58 | +Wasm module. Then you need to run |
| 59 | + |
| 60 | +``` |
| 61 | +javy init-plugin <path_to_plugin> -o <path_to_initialized_module>` |
| 62 | +``` |
| 63 | + |
| 64 | +which will validate and initialize the Javy runtime. This `javy init-plugin` |
| 65 | +step is required for the plugin to be useable by the Javy CLI. |
14 | 66 |
|
15 | 67 | See our documentation on [using complex data types in Wasm |
16 | 68 | functions](./contributing-complex-data-types.md) for how to support Wasm |
17 | 69 | functions that need to use byte arrays, strings, or structured data. |
18 | 70 |
|
19 | | -For a visual representation of how we expect forks to consume our crates: |
| 71 | +## The full plugin API |
20 | 72 |
|
21 | | -```mermaid |
22 | | -flowchart TD |
23 | | - your-cli --> wasm |
24 | | - subgraph wasm[your_plugin.wasm] |
25 | | - your-plugin --> javy[upstream javy] |
26 | | - javy --> rquickjs |
27 | | - end |
28 | | -``` |
| 73 | +This is the Wasm API the Javy CLI expects Javy plugins to expose. The |
| 74 | +`javy-plugin-api` crate will export implementations of all required exported |
| 75 | +functions except `initialize_runtime`. `import_namespace!` will define the |
| 76 | +`import_namespace` custom section. |
| 77 | + |
| 78 | +### Exported Wasm functions |
| 79 | + |
| 80 | +#### `initialize_runtime() -> ()` |
| 81 | + |
| 82 | +This will initialize a mutable global containing the Javy runtime for use by |
| 83 | +`compile_src` and `invoke`. |
| 84 | + |
| 85 | +#### `canonical_abi_realloc(orig_ptr: i32, orig_len: i32, new_ptr: i32, new_len: i32) -> ptr: i32` |
| 86 | + |
| 87 | +This is used to allocate memory in the plugin module. |
| 88 | + |
| 89 | +#### `compile_src(src_ptr: i32, src_len: i32) -> bytecode_wide_ptr: i32` |
| 90 | + |
| 91 | +This is used to compile JavaScript source code to QuickJS bytecode. The return |
| 92 | +pointer points to a tuple of `(bytecode_ptr: i32, bytecode_len: i32)` in the |
| 93 | +plugin instance's linear memory. |
| 94 | + |
| 95 | +#### `invoke(bytecode_ptr: i32, bytecode_len: i32, fn_name_ptr: i32, fn_name_len: i32) -> ()` |
| 96 | + |
| 97 | +This is used to evaluate the JavaScript code and optionally to call an exported |
| 98 | +JS function if `fn_name_ptr` is not `0`. |
| 99 | + |
| 100 | +### Custom sections |
| 101 | + |
| 102 | +#### `import_namespace` |
| 103 | + |
| 104 | +Contains a UTF-8 encoded string. This is used to determine the namespace that |
| 105 | +will be used for the Wasm imports in dynamically linked modules built with this |
| 106 | +plugin. |
0 commit comments