1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2025-01-04 05:18:59 -05:00
denoland-deno/test_plugin/src/lib.rs

61 lines
1.6 KiB
Rust
Raw Normal View History

#[macro_use]
extern crate deno_core;
extern crate futures;
use deno_core::Op;
use deno_core::PluginInitContext;
2020-01-24 15:10:49 -05:00
use deno_core::{Buf, ZeroCopyBuf};
use futures::future::FutureExt;
fn init(context: &mut dyn PluginInitContext) {
context.register_op("testSync", Box::new(op_test_sync));
context.register_op("testAsync", Box::new(op_test_async));
}
init_fn!(init);
pub fn op_test_sync(
_isolate: &mut deno_core::Isolate,
data: &[u8],
zero_copy: Option<ZeroCopyBuf>,
) -> Op {
if let Some(buf) = zero_copy {
let data_str = std::str::from_utf8(&data[..]).unwrap();
let buf_str = std::str::from_utf8(&buf[..]).unwrap();
println!(
"Hello from plugin. data: {} | zero_copy: {}",
data_str, buf_str
);
}
let result = b"test";
let result_box: Buf = Box::new(*result);
Op::Sync(result_box)
}
pub fn op_test_async(
_isolate: &mut deno_core::Isolate,
data: &[u8],
zero_copy: Option<ZeroCopyBuf>,
) -> Op {
let data_str = std::str::from_utf8(&data[..]).unwrap().to_string();
let fut = async move {
if let Some(buf) = zero_copy {
let buf_str = std::str::from_utf8(&buf[..]).unwrap();
println!(
"Hello from plugin. data: {} | zero_copy: {}",
data_str, buf_str
);
}
let (tx, rx) = futures::channel::oneshot::channel::<Result<(), ()>>();
std::thread::spawn(move || {
std::thread::sleep(std::time::Duration::from_secs(1));
tx.send(Ok(())).unwrap();
});
assert!(rx.await.is_ok());
let result = b"test";
let result_box: Buf = Box::new(*result);
2020-04-18 20:05:13 -04:00
result_box
};
Op::Async(fut.boxed())
}