2019-12-05 15:30:20 -05:00
|
|
|
use super::dispatch_json::{Deserialize, JsonOp, Value};
|
|
|
|
use crate::fs as deno_fs;
|
2020-02-23 14:51:29 -05:00
|
|
|
use crate::op_error::OpError;
|
2019-12-05 15:30:20 -05:00
|
|
|
use crate::ops::json_op;
|
2020-02-08 14:34:31 -05:00
|
|
|
use crate::state::State;
|
2020-01-05 11:56:18 -05:00
|
|
|
use deno_core::*;
|
2019-12-05 15:30:20 -05:00
|
|
|
use dlopen::symbor::Library;
|
|
|
|
use std::collections::HashMap;
|
|
|
|
use std::ffi::OsStr;
|
2020-01-20 09:45:44 -05:00
|
|
|
use std::path::Path;
|
2020-02-14 19:18:36 -05:00
|
|
|
use std::rc::Rc;
|
2019-12-05 15:30:20 -05:00
|
|
|
|
2020-02-14 19:18:36 -05:00
|
|
|
pub fn init(i: &mut Isolate, s: &State, r: Rc<deno_core::OpRegistry>) {
|
2019-12-23 09:59:44 -05:00
|
|
|
let r_ = r;
|
2019-12-05 15:30:20 -05:00
|
|
|
i.register_op(
|
2020-02-25 09:14:27 -05:00
|
|
|
"op_open_plugin",
|
2019-12-05 15:30:20 -05:00
|
|
|
s.core_op(json_op(s.stateful_op(move |state, args, zero_copy| {
|
|
|
|
op_open_plugin(&r_, state, args, zero_copy)
|
|
|
|
}))),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-02-23 14:51:29 -05:00
|
|
|
fn open_plugin<P: AsRef<OsStr>>(lib_path: P) -> Result<Library, OpError> {
|
2019-12-05 15:30:20 -05:00
|
|
|
debug!("Loading Plugin: {:#?}", lib_path.as_ref());
|
|
|
|
|
2020-02-23 14:51:29 -05:00
|
|
|
Library::open(lib_path).map_err(OpError::from)
|
2019-12-05 15:30:20 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
struct PluginResource {
|
|
|
|
lib: Library,
|
|
|
|
ops: HashMap<String, OpId>,
|
|
|
|
}
|
|
|
|
|
|
|
|
struct InitContext {
|
|
|
|
ops: HashMap<String, Box<OpDispatcher>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl PluginInitContext for InitContext {
|
|
|
|
fn register_op(&mut self, name: &str, op: Box<OpDispatcher>) {
|
|
|
|
let existing = self.ops.insert(name.to_string(), op);
|
|
|
|
assert!(
|
|
|
|
existing.is_none(),
|
|
|
|
format!("Op already registered: {}", name)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Deserialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
struct OpenPluginArgs {
|
|
|
|
filename: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn op_open_plugin(
|
2020-02-14 19:18:36 -05:00
|
|
|
registry: &Rc<deno_core::OpRegistry>,
|
2020-02-08 14:34:31 -05:00
|
|
|
state: &State,
|
2019-12-05 15:30:20 -05:00
|
|
|
args: Value,
|
2020-01-24 15:10:49 -05:00
|
|
|
_zero_copy: Option<ZeroCopyBuf>,
|
2020-02-23 14:51:29 -05:00
|
|
|
) -> Result<JsonOp, OpError> {
|
2019-12-05 15:30:20 -05:00
|
|
|
let args: OpenPluginArgs = serde_json::from_value(args)?;
|
2020-01-20 09:45:44 -05:00
|
|
|
let filename = deno_fs::resolve_from_cwd(Path::new(&args.filename))?;
|
2019-12-05 15:30:20 -05:00
|
|
|
|
2020-01-20 09:45:44 -05:00
|
|
|
state.check_plugin(&filename)?;
|
2019-12-05 15:30:20 -05:00
|
|
|
|
|
|
|
let lib = open_plugin(filename)?;
|
|
|
|
let plugin_resource = PluginResource {
|
|
|
|
lib,
|
|
|
|
ops: HashMap::new(),
|
|
|
|
};
|
2020-02-08 14:34:31 -05:00
|
|
|
let mut state_ = state.borrow_mut();
|
|
|
|
let rid = state_
|
|
|
|
.resource_table
|
|
|
|
.add("plugin", Box::new(plugin_resource));
|
|
|
|
let plugin_resource = state_
|
|
|
|
.resource_table
|
|
|
|
.get_mut::<PluginResource>(rid)
|
|
|
|
.unwrap();
|
2019-12-05 15:30:20 -05:00
|
|
|
|
|
|
|
let init_fn = *unsafe {
|
|
|
|
plugin_resource
|
|
|
|
.lib
|
|
|
|
.symbol::<PluginInitFn>("deno_plugin_init")
|
|
|
|
}?;
|
|
|
|
let mut init_context = InitContext {
|
|
|
|
ops: HashMap::new(),
|
|
|
|
};
|
|
|
|
init_fn(&mut init_context);
|
|
|
|
for op in init_context.ops {
|
|
|
|
// Register each plugin op in the `OpRegistry` with the name
|
|
|
|
// formated like this `plugin_{plugin_rid}_{name}`.
|
|
|
|
// The inclusion of prefix and rid is designed to avoid any
|
|
|
|
// op name collision beyond the bound of a single loaded
|
|
|
|
// plugin instance.
|
2019-12-07 15:09:09 -05:00
|
|
|
let op_id = registry
|
|
|
|
.register(&format!("plugin_{}_{}", rid, op.0), state.core_op(op.1));
|
2019-12-05 15:30:20 -05:00
|
|
|
plugin_resource.ops.insert(op.0, op_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(JsonOp::Sync(
|
|
|
|
json!({ "rid": rid, "ops": plugin_resource.ops }),
|
|
|
|
))
|
|
|
|
}
|