Skip to content

Commit

Permalink
feat: support wrapped plugin ops
Browse files Browse the repository at this point in the history
  • Loading branch information
afinch7 committed Jun 9, 2020
1 parent 6c21ba0 commit e0604ad
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 5 deletions.
2 changes: 1 addition & 1 deletion cli/ops/plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ impl<'a> plugin_api::Interface for PluginInterface<'a> {
fn register_op(
&mut self,
name: &str,
dispatch_op_fn: plugin_api::DispatchOpFn,
dispatch_op_fn: Box<plugin_api::DispatchOpFn>,
) -> OpId {
let plugin_lib = self.plugin_lib.clone();
self.isolate_state.op_registry.register(
Expand Down
9 changes: 9 additions & 0 deletions core/core_isolate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,15 @@ pub struct CoreIsolateState {
waker: AtomicWaker,
}

impl Drop for CoreIsolateState {
fn drop(&mut self) {
// TODO(afinch7) this is a temporary fix for a segfault that occurus when
// dropping plugin ops. I know that the plugin Rc<Library> value gets dropped
// early for some reason, but still not quite sure why.
drop(std::mem::take(&mut self.op_registry));
}
}

// TODO(ry) The trait v8::InIsolate is superfluous. HandleScope::new should just
// take &mut v8::Isolate.
impl v8::InIsolate for CoreIsolate {
Expand Down
5 changes: 3 additions & 2 deletions core/plugin_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ pub use crate::ZeroCopyBuf;

pub type InitFn = fn(&mut dyn Interface);

pub type DispatchOpFn = fn(&mut dyn Interface, &[u8], &mut [ZeroCopyBuf]) -> Op;
pub type DispatchOpFn =
dyn Fn(&mut dyn Interface, &[u8], &mut [ZeroCopyBuf]) -> Op;

pub trait Interface {
fn register_op(&mut self, name: &str, dispatcher: DispatchOpFn) -> OpId;
fn register_op(&mut self, name: &str, dispatcher: Box<DispatchOpFn>) -> OpId;
}
4 changes: 2 additions & 2 deletions test_plugin/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ use futures::future::FutureExt;

#[no_mangle]
pub fn deno_plugin_init(interface: &mut dyn Interface) {
interface.register_op("testSync", op_test_sync);
interface.register_op("testAsync", op_test_async);
interface.register_op("testSync", Box::new(op_test_sync));
interface.register_op("testAsync", Box::new(op_test_async));
}

fn op_test_sync(
Expand Down

0 comments on commit e0604ad

Please sign in to comment.