-
Notifications
You must be signed in to change notification settings - Fork 114
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Update docs for plugins #832
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,106 @@ | ||
# Extending | ||
|
||
If you want to use Javy for your own project, you may find that the existing | ||
code is not sufficient since you may want to offer custom APIs or use different | ||
branding for the CLI. The approach we'd recommend taking is to create your own | ||
version of the `javy-cli` and `javy-plugin` crates (you could fork these if you | ||
would like) and depend on the upstream version of the `javy` crate. You can add | ||
your own implementations of custom JS APIs in your fork of the `javy-plugin` crate | ||
or in a different crate that you depend on in your `javy-plugin` fork. If you find | ||
that something is missing in the `javy` crate that you require to implement | ||
something in your fork, we would appreciate it if you would open a GitHub issue | ||
and consider making the change upstream instead of in your fork so all users of | ||
the `javy` crate can benefit. | ||
code is not sufficient since you may want to offer custom JavaScript APIs. The | ||
approach we recommend taking is to create your own Javy plugin Wasm module. | ||
This plugin module can then be specified when using the Javy CLI when building | ||
Javy Wasm modules as a `-C plugin` flag when using `javy build`. | ||
|
||
To create your own Javy plugin Wasm module, create a new Rust project that | ||
will compile to a library (that is, `cargo init --lib`). | ||
|
||
Your `Cargo.toml` should look like: | ||
|
||
```toml | ||
[package] | ||
name = "my-plugin-name" | ||
version = "0.1.0" | ||
|
||
[lib] | ||
name = "my_plugin_name" | ||
crate-type = ["cdylib"] | ||
|
||
[dependencies] | ||
javy-plugin-api = "1.0.0" | ||
``` | ||
|
||
And `src/lib.rs` should look like: | ||
|
||
```rust | ||
use javy_plugin_api::import_namespace; | ||
use javy_plugin_api::javy::quickjs::prelude::Func; | ||
use javy_plugin_api::javy::Config; | ||
|
||
import_namespace!("my_plugin_name"); | ||
|
||
#[export_name = "initialize_runtime"] | ||
pub extern "C" fn initialize_runtime() { | ||
let mut config = Config::default(); | ||
config | ||
.text_encoding(true) | ||
.javy_stream_io(true); | ||
|
||
javy_plugin_api::initialize_runtime(config, |runtime| { | ||
runtime | ||
.context() | ||
.with(|ctx| { | ||
ctx.globals() | ||
.set("isThisAPlugin", Func::from(|| "yes it is")) | ||
}) | ||
.unwrap(); | ||
runtime | ||
}) | ||
.unwrap(); | ||
} | ||
``` | ||
|
||
You can then run `cargo build --target=wasm32-wasip1 --release` to create a | ||
Wasm module. Then you need to run | ||
|
||
``` | ||
javy init-plugin <path_to_plugin> -o <path_to_initialized_module>` | ||
``` | ||
|
||
which will validate and initialize the Javy runtime. This `javy init-plugin` | ||
step is required for the plugin to be useable by the Javy CLI. | ||
|
||
See our documentation on [using complex data types in Wasm | ||
functions](./contributing-complex-data-types.md) for how to support Wasm | ||
functions that need to use byte arrays, strings, or structured data. | ||
|
||
For a visual representation of how we expect forks to consume our crates: | ||
## The full plugin API | ||
|
||
```mermaid | ||
flowchart TD | ||
your-cli --> wasm | ||
subgraph wasm[your_plugin.wasm] | ||
your-plugin --> javy[upstream javy] | ||
javy --> rquickjs | ||
end | ||
``` | ||
This is the Wasm API the Javy CLI expects Javy plugins to expose. The | ||
`javy-plugin-api` crate will export implementations of all required exported | ||
functions except `initialize_runtime`. `import_namespace!` will define the | ||
`import_namespace` custom section. | ||
|
||
### Exported Wasm functions | ||
|
||
#### `initialize_runtime() -> ()` | ||
|
||
This will initialize a mutable global containing the Javy runtime for use by | ||
`compile_src` and `invoke`. | ||
|
||
#### `canonical_abi_realloc(orig_ptr: i32, orig_len: i32, new_ptr: i32, new_len: i32) -> ptr: i32` | ||
|
||
This is used to allocate memory in the plugin module. | ||
|
||
#### `compile_src(src_ptr: i32, src_len: i32) -> bytecode_wide_ptr: i32` | ||
|
||
This is used to compile JavaScript source code to QuickJS bytecode. The return | ||
pointer points to a tuple of `(bytecode_ptr: i32, bytecode_len: i32)` in the | ||
plugin instance's linear memory. | ||
|
||
#### `invoke(bytecode_ptr: i32, bytecode_len: i32, fn_name_ptr: i32, fn_name_len: i32) -> ()` | ||
|
||
This is used to evaluate the JavaScript code and optionally to call an exported | ||
JS function if `fn_name_ptr` is not `0`. | ||
|
||
### Custom sections | ||
|
||
#### `import_namespace` | ||
|
||
Contains a UTF-8 encoded string. This is used to determine the namespace that | ||
will be used for the Wasm imports in dynamically linked modules built with this | ||
plugin. |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🎉