From 7c3b9b4f4f2f4ec8fdeb0e77bb853fd22ffaa476 Mon Sep 17 00:00:00 2001 From: Andy Finch Date: Thu, 5 Dec 2019 15:30:20 -0500 Subject: feat: first pass at native plugins (#3372) --- test_plugin/src/lib.rs | 53 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 test_plugin/src/lib.rs (limited to 'test_plugin/src/lib.rs') diff --git a/test_plugin/src/lib.rs b/test_plugin/src/lib.rs new file mode 100644 index 000000000..30df114b9 --- /dev/null +++ b/test_plugin/src/lib.rs @@ -0,0 +1,53 @@ +#[macro_use] +extern crate deno; +extern crate futures; + +use deno::CoreOp; +use deno::Op; +use deno::PluginInitContext; +use deno::{Buf, PinnedBuf}; +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(data: &[u8], zero_copy: Option) -> CoreOp { + 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(data: &[u8], zero_copy: Option) -> CoreOp { + 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::>(); + 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); + Ok(result_box) + }; + + Op::Async(fut.boxed()) +} -- cgit v1.2.3