2021-01-10 21:59:07 -05:00
|
|
|
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
|
2020-09-05 20:34:02 -04:00
|
|
|
|
2020-09-10 09:57:45 -04:00
|
|
|
use crate::metrics::metrics_op;
|
2020-09-19 19:17:35 -04:00
|
|
|
use crate::permissions::Permissions;
|
2020-09-14 12:48:57 -04:00
|
|
|
use deno_core::error::AnyError;
|
2020-09-21 12:36:37 -04:00
|
|
|
use deno_core::futures::prelude::*;
|
2020-05-11 14:20:14 -04:00
|
|
|
use deno_core::plugin_api;
|
2020-09-21 12:36:37 -04:00
|
|
|
use deno_core::serde_json;
|
|
|
|
use deno_core::serde_json::json;
|
|
|
|
use deno_core::serde_json::Value;
|
2020-09-05 20:34:02 -04:00
|
|
|
use deno_core::BufVec;
|
2020-09-10 09:57:45 -04:00
|
|
|
use deno_core::JsRuntime;
|
2020-05-11 14:20:14 -04:00
|
|
|
use deno_core::Op;
|
|
|
|
use deno_core::OpAsyncFuture;
|
|
|
|
use deno_core::OpId;
|
2020-09-10 09:57:45 -04:00
|
|
|
use deno_core::OpState;
|
2020-12-16 11:14:12 -05:00
|
|
|
use deno_core::Resource;
|
2020-05-11 14:20:14 -04:00
|
|
|
use deno_core::ZeroCopyBuf;
|
|
|
|
use dlopen::symbor::Library;
|
2020-09-16 12:43:08 -04:00
|
|
|
use serde::Deserialize;
|
2020-12-16 11:14:12 -05:00
|
|
|
use std::borrow::Cow;
|
2020-09-10 09:57:45 -04:00
|
|
|
use std::cell::RefCell;
|
2020-05-29 11:27:43 -04:00
|
|
|
use std::path::PathBuf;
|
2020-05-11 14:20:14 -04:00
|
|
|
use std::pin::Pin;
|
|
|
|
use std::rc::Rc;
|
|
|
|
use std::task::Context;
|
|
|
|
use std::task::Poll;
|
|
|
|
|
2020-09-10 09:57:45 -04:00
|
|
|
pub fn init(rt: &mut JsRuntime) {
|
|
|
|
super::reg_json_sync(rt, "op_open_plugin", op_open_plugin);
|
2020-05-11 14:20:14 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Deserialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
struct OpenPluginArgs {
|
|
|
|
filename: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn op_open_plugin(
|
2020-09-10 09:57:45 -04:00
|
|
|
state: &mut OpState,
|
2020-05-11 14:20:14 -04:00
|
|
|
args: Value,
|
2020-06-01 14:20:47 -04:00
|
|
|
_zero_copy: &mut [ZeroCopyBuf],
|
2020-09-14 12:48:57 -04:00
|
|
|
) -> Result<Value, AnyError> {
|
2020-09-10 09:57:45 -04:00
|
|
|
let args: OpenPluginArgs = serde_json::from_value(args)?;
|
2020-05-29 11:27:43 -04:00
|
|
|
let filename = PathBuf::from(&args.filename);
|
2020-05-11 14:20:14 -04:00
|
|
|
|
2020-09-26 14:26:51 -04:00
|
|
|
super::check_unstable(state, "Deno.openPlugin");
|
2020-09-19 19:17:35 -04:00
|
|
|
let permissions = state.borrow::<Permissions>();
|
|
|
|
permissions.check_plugin(&filename)?;
|
2020-05-11 14:20:14 -04:00
|
|
|
|
|
|
|
debug!("Loading Plugin: {:#?}", filename);
|
2020-08-25 18:22:15 -04:00
|
|
|
let plugin_lib = Library::open(filename).map(Rc::new)?;
|
2020-05-11 14:20:14 -04:00
|
|
|
let plugin_resource = PluginResource::new(&plugin_lib);
|
|
|
|
|
2020-09-05 20:34:02 -04:00
|
|
|
let rid;
|
|
|
|
let deno_plugin_init;
|
|
|
|
{
|
2020-12-16 11:14:12 -05:00
|
|
|
rid = state.resource_table.add(plugin_resource);
|
2020-09-05 20:34:02 -04:00
|
|
|
deno_plugin_init = *unsafe {
|
2020-09-10 09:57:45 -04:00
|
|
|
state
|
|
|
|
.resource_table
|
2020-09-05 20:34:02 -04:00
|
|
|
.get::<PluginResource>(rid)
|
|
|
|
.unwrap()
|
|
|
|
.lib
|
|
|
|
.symbol::<plugin_api::InitFn>("deno_plugin_init")
|
|
|
|
.unwrap()
|
|
|
|
};
|
2020-05-11 14:20:14 -04:00
|
|
|
}
|
|
|
|
|
2020-09-05 20:34:02 -04:00
|
|
|
let mut interface = PluginInterface::new(state, &plugin_lib);
|
2020-05-11 14:20:14 -04:00
|
|
|
deno_plugin_init(&mut interface);
|
|
|
|
|
2020-09-05 20:34:02 -04:00
|
|
|
Ok(json!(rid))
|
2020-05-11 14:20:14 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
struct PluginResource {
|
|
|
|
lib: Rc<Library>,
|
|
|
|
}
|
|
|
|
|
2020-12-16 11:14:12 -05:00
|
|
|
impl Resource for PluginResource {
|
|
|
|
fn name(&self) -> Cow<str> {
|
|
|
|
"plugin".into()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-11 14:20:14 -04:00
|
|
|
impl PluginResource {
|
|
|
|
fn new(lib: &Rc<Library>) -> Self {
|
|
|
|
Self { lib: lib.clone() }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct PluginInterface<'a> {
|
2020-09-10 09:57:45 -04:00
|
|
|
state: &'a mut OpState,
|
2020-05-11 14:20:14 -04:00
|
|
|
plugin_lib: &'a Rc<Library>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> PluginInterface<'a> {
|
2020-09-10 09:57:45 -04:00
|
|
|
fn new(state: &'a mut OpState, plugin_lib: &'a Rc<Library>) -> Self {
|
2020-09-05 20:34:02 -04:00
|
|
|
Self { state, plugin_lib }
|
2020-05-11 14:20:14 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> plugin_api::Interface for PluginInterface<'a> {
|
|
|
|
/// Does the same as `core::Isolate::register_op()`, but additionally makes
|
|
|
|
/// the registered op dispatcher, as well as the op futures created by it,
|
|
|
|
/// keep reference to the plugin `Library` object, so that the plugin doesn't
|
|
|
|
/// get unloaded before all its op registrations and the futures created by
|
|
|
|
/// them are dropped.
|
|
|
|
fn register_op(
|
|
|
|
&mut self,
|
|
|
|
name: &str,
|
|
|
|
dispatch_op_fn: plugin_api::DispatchOpFn,
|
|
|
|
) -> OpId {
|
|
|
|
let plugin_lib = self.plugin_lib.clone();
|
2020-09-10 09:57:45 -04:00
|
|
|
let plugin_op_fn = move |state_rc: Rc<RefCell<OpState>>,
|
|
|
|
mut zero_copy: BufVec| {
|
|
|
|
let mut state = state_rc.borrow_mut();
|
|
|
|
let mut interface = PluginInterface::new(&mut state, &plugin_lib);
|
|
|
|
let op = dispatch_op_fn(&mut interface, &mut zero_copy);
|
|
|
|
match op {
|
|
|
|
sync_op @ Op::Sync(..) => sync_op,
|
|
|
|
Op::Async(fut) => Op::Async(PluginOpAsyncFuture::new(&plugin_lib, fut)),
|
|
|
|
Op::AsyncUnref(fut) => {
|
|
|
|
Op::AsyncUnref(PluginOpAsyncFuture::new(&plugin_lib, fut))
|
2020-05-11 14:20:14 -04:00
|
|
|
}
|
2020-09-10 09:57:45 -04:00
|
|
|
_ => unreachable!(),
|
|
|
|
}
|
|
|
|
};
|
|
|
|
self
|
|
|
|
.state
|
|
|
|
.op_table
|
|
|
|
.register_op(name, metrics_op(Box::new(plugin_op_fn)))
|
2020-05-11 14:20:14 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct PluginOpAsyncFuture {
|
|
|
|
fut: Option<OpAsyncFuture>,
|
|
|
|
_plugin_lib: Rc<Library>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl PluginOpAsyncFuture {
|
|
|
|
fn new(plugin_lib: &Rc<Library>, fut: OpAsyncFuture) -> Pin<Box<Self>> {
|
|
|
|
let wrapped_fut = Self {
|
|
|
|
fut: Some(fut),
|
|
|
|
_plugin_lib: plugin_lib.clone(),
|
|
|
|
};
|
|
|
|
Box::pin(wrapped_fut)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Future for PluginOpAsyncFuture {
|
|
|
|
type Output = <OpAsyncFuture as Future>::Output;
|
|
|
|
fn poll(mut self: Pin<&mut Self>, ctx: &mut Context) -> Poll<Self::Output> {
|
|
|
|
self.fut.as_mut().unwrap().poll_unpin(ctx)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Drop for PluginOpAsyncFuture {
|
|
|
|
fn drop(&mut self) {
|
|
|
|
self.fut.take();
|
|
|
|
}
|
|
|
|
}
|