2021-01-10 21:59:07 -05:00
|
|
|
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
|
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;
|
2021-05-02 19:22:57 -04:00
|
|
|
use deno_core::op_sync;
|
|
|
|
use deno_core::Extension;
|
2020-09-10 09:57:45 -04:00
|
|
|
use deno_core::OpState;
|
2020-12-16 11:14:12 -05:00
|
|
|
use deno_core::Resource;
|
2021-04-05 12:40:24 -04:00
|
|
|
use deno_core::ResourceId;
|
2020-05-11 14:20:14 -04:00
|
|
|
use dlopen::symbor::Library;
|
2021-03-26 12:34:25 -04:00
|
|
|
use log::debug;
|
2020-12-16 11:14:12 -05:00
|
|
|
use std::borrow::Cow;
|
2021-05-07 09:45:07 -04:00
|
|
|
use std::mem;
|
2020-05-29 11:27:43 -04:00
|
|
|
use std::path::PathBuf;
|
2020-05-11 14:20:14 -04:00
|
|
|
use std::rc::Rc;
|
2021-05-07 09:45:07 -04:00
|
|
|
|
|
|
|
/// A default `init` function for plugins which mimics the way the internal
|
|
|
|
/// extensions are initalized. Plugins currently do not support all extension
|
|
|
|
/// features and are most likely not going to in the future. Currently only
|
|
|
|
/// `init_state` and `init_ops` are supported while `init_middleware` and `init_js`
|
|
|
|
/// are not. Currently the `PluginResource` does not support being closed due to
|
|
|
|
/// certain risks in unloading the dynamic library without unloading dependent
|
|
|
|
/// functions and resources.
|
|
|
|
pub type InitFn = fn() -> Extension;
|
2020-05-11 14:20:14 -04:00
|
|
|
|
2021-05-02 19:22:57 -04:00
|
|
|
pub fn init() -> Extension {
|
|
|
|
Extension::builder()
|
|
|
|
.ops(vec![("op_open_plugin", op_sync(op_open_plugin))])
|
|
|
|
.build()
|
2020-05-11 14:20:14 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn op_open_plugin(
|
2020-09-10 09:57:45 -04:00
|
|
|
state: &mut OpState,
|
2021-04-05 12:40:24 -04:00
|
|
|
filename: String,
|
2021-05-08 08:37:42 -04:00
|
|
|
_: (),
|
2021-04-05 12:40:24 -04:00
|
|
|
) -> Result<ResourceId, AnyError> {
|
|
|
|
let filename = PathBuf::from(&filename);
|
2020-05-11 14:20:14 -04:00
|
|
|
|
2020-09-26 14:26:51 -04:00
|
|
|
super::check_unstable(state, "Deno.openPlugin");
|
2021-04-11 22:15:43 -04:00
|
|
|
let permissions = state.borrow_mut::<Permissions>();
|
2021-03-17 17:45:12 -04:00
|
|
|
permissions.plugin.check()?;
|
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);
|
|
|
|
|
2021-05-07 09:45:07 -04:00
|
|
|
// Forgets the plugin_lib value to prevent segfaults when the process exits
|
|
|
|
mem::forget(plugin_lib);
|
2020-05-11 14:20:14 -04:00
|
|
|
|
2021-05-07 09:45:07 -04:00
|
|
|
let init = *unsafe { plugin_resource.0.symbol::<InitFn>("init") }?;
|
|
|
|
let rid = state.resource_table.add(plugin_resource);
|
|
|
|
let mut extension = init();
|
2020-05-11 14:20:14 -04:00
|
|
|
|
2021-05-07 09:45:07 -04:00
|
|
|
if !extension.init_js().is_empty() {
|
|
|
|
panic!("Plugins do not support loading js");
|
2020-12-16 11:14:12 -05:00
|
|
|
}
|
|
|
|
|
2021-05-07 09:45:07 -04:00
|
|
|
if extension.init_middleware().is_some() {
|
|
|
|
panic!("Plugins do not support middleware");
|
2020-05-11 14:20:14 -04:00
|
|
|
}
|
|
|
|
|
2021-05-07 09:45:07 -04:00
|
|
|
extension.init_state(state)?;
|
|
|
|
let ops = extension.init_ops().unwrap_or_default();
|
|
|
|
for (name, opfn) in ops {
|
|
|
|
state.op_table.register_op(name, opfn);
|
2020-05-11 14:20:14 -04:00
|
|
|
}
|
|
|
|
|
2021-05-07 09:45:07 -04:00
|
|
|
Ok(rid)
|
2020-05-11 14:20:14 -04:00
|
|
|
}
|
|
|
|
|
2021-05-07 09:45:07 -04:00
|
|
|
struct PluginResource(Rc<Library>);
|
2020-05-11 14:20:14 -04:00
|
|
|
|
2021-05-07 09:45:07 -04:00
|
|
|
impl Resource for PluginResource {
|
|
|
|
fn name(&self) -> Cow<str> {
|
|
|
|
"plugin".into()
|
2020-05-11 14:20:14 -04:00
|
|
|
}
|
|
|
|
|
2021-05-07 09:45:07 -04:00
|
|
|
fn close(self: Rc<Self>) {
|
|
|
|
unimplemented!();
|
2020-05-11 14:20:14 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-07 09:45:07 -04:00
|
|
|
impl PluginResource {
|
|
|
|
fn new(lib: &Rc<Library>) -> Self {
|
|
|
|
Self(lib.clone())
|
2020-05-11 14:20:14 -04:00
|
|
|
}
|
|
|
|
}
|