Just exploring how deno Rust plugins can be used. !!! warning the feature is still instable!!!
Clone the deno source & go to deno/test_plugin
The repo is structure as following:
test_plugin
βββ Cargo.toml
βββ src
βΒ Β βββ lib.rs
βββ tests
βββ ...
βββ test.js
It is a rust repo.
1- add deno_core
& specify your project as a crate of type cdylib
to make the lib dynamically available.
In the cargo manifest
[package]
name = "test_plugin"
...
[lib]
crate-type = ["cdylib"]
[dependencies]
...
deno_core = { path = "../core" }
...
// in lib.rs
fn op_test_sync(
_interface: &mut dyn Interface,
zero_copy: &mut [ZeroCopyBuf],
) -> Op {
...
Op::Sync(result_box)
}
// in lib.rs
#[no_mangle]
pub fn deno_plugin_init(interface: &mut dyn Interface) {
interface.register_op("testSync", op_test_sync);
...
}
$ cargo build -p test_plugin --release
// in test.js
const rid = Deno.openPlugin(<path the .dylib>);
// in test.js
const { testSync } = Deno.core.ops();
// in test.js
const response = Deno.core.dispatch(
testSync,
new Uint8Array([116, 101, 115, 116]),
new Uint8Array([49, 50, 51]),
new Uint8Array([99, 98, 97]),
);
async ops:
// in test.js
Deno.core.setAsyncHandler(testAsync, (response) => {
console.log(`Plugin Async Response: ${textDecoder.decode(response)}`);
});
const response = Deno.core.dispatch(
testAsync,
new Uint8Array([116, 101, 115, 116]),
new Uint8Array([49, 50, 51]),
);
// in test.js
Deno.close(rid);
$ deno run --allow-plugin --unstable tests/test.js release