2023-01-02 16:00:42 -05:00
|
|
|
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
|
2021-11-16 09:02:28 -05:00
|
|
|
use crate::OpState;
|
|
|
|
use anyhow::Error;
|
2023-01-14 23:18:58 -05:00
|
|
|
use std::cell::RefCell;
|
|
|
|
use std::rc::Rc;
|
|
|
|
use std::task::Context;
|
2022-08-21 08:07:53 -04:00
|
|
|
use v8::fast_api::FastFunction;
|
2021-04-28 12:41:50 -04:00
|
|
|
|
2023-02-07 18:21:43 -05:00
|
|
|
pub struct ExtensionFileSource {
|
|
|
|
pub specifier: String,
|
|
|
|
pub code: &'static str,
|
|
|
|
}
|
2022-03-14 13:44:15 -04:00
|
|
|
pub type OpFnRef = v8::FunctionCallback;
|
2022-03-15 18:43:17 -04:00
|
|
|
pub type OpMiddlewareFn = dyn Fn(OpDecl) -> OpDecl;
|
2021-11-16 09:02:28 -05:00
|
|
|
pub type OpStateFn = dyn Fn(&mut OpState) -> Result<(), Error>;
|
2022-06-28 05:23:36 -04:00
|
|
|
pub type OpEventLoopFn = dyn Fn(Rc<RefCell<OpState>>, &mut Context) -> bool;
|
2021-04-28 12:41:50 -04:00
|
|
|
|
2022-03-15 18:43:17 -04:00
|
|
|
pub struct OpDecl {
|
|
|
|
pub name: &'static str,
|
|
|
|
pub v8_fn_ptr: OpFnRef,
|
2022-03-22 11:39:58 -04:00
|
|
|
pub enabled: bool,
|
2022-08-21 08:07:53 -04:00
|
|
|
pub is_async: bool,
|
2022-04-01 18:09:21 -04:00
|
|
|
pub is_unstable: bool,
|
2022-10-28 07:20:17 -04:00
|
|
|
/// V8 argument count. Used as an optimization
|
|
|
|
/// hint by `core.initalizeAsyncOps`.
|
|
|
|
pub argc: usize,
|
2022-05-12 13:06:42 -04:00
|
|
|
pub is_v8: bool,
|
2022-08-21 08:07:53 -04:00
|
|
|
pub fast_fn: Option<Box<dyn FastFunction>>,
|
2022-03-22 11:39:58 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
impl OpDecl {
|
|
|
|
pub fn enabled(self, enabled: bool) -> Self {
|
|
|
|
Self { enabled, ..self }
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn disable(self) -> Self {
|
|
|
|
self.enabled(false)
|
|
|
|
}
|
2022-03-15 18:43:17 -04:00
|
|
|
}
|
|
|
|
|
2021-04-28 12:41:50 -04:00
|
|
|
#[derive(Default)]
|
|
|
|
pub struct Extension {
|
2023-02-07 18:21:43 -05:00
|
|
|
js_files: Option<Vec<ExtensionFileSource>>,
|
|
|
|
esm_files: Option<Vec<ExtensionFileSource>>,
|
2022-07-22 09:36:32 -04:00
|
|
|
ops: Option<Vec<OpDecl>>,
|
2021-04-28 12:41:50 -04:00
|
|
|
opstate_fn: Option<Box<OpStateFn>>,
|
|
|
|
middleware_fn: Option<Box<OpMiddlewareFn>>,
|
2022-03-08 09:40:34 -05:00
|
|
|
event_loop_middleware: Option<Box<OpEventLoopFn>>,
|
2021-04-28 12:41:50 -04:00
|
|
|
initialized: bool,
|
2022-03-22 11:39:58 -04:00
|
|
|
enabled: bool,
|
2023-01-08 17:48:46 -05:00
|
|
|
name: &'static str,
|
|
|
|
deps: Option<Vec<&'static str>>,
|
2021-04-28 12:41:50 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Note: this used to be a trait, but we "downgraded" it to a single concrete type
|
|
|
|
// for the initial iteration, it will likely become a trait in the future
|
|
|
|
impl Extension {
|
2023-01-08 17:48:46 -05:00
|
|
|
pub fn builder(name: &'static str) -> ExtensionBuilder {
|
|
|
|
ExtensionBuilder {
|
|
|
|
name,
|
|
|
|
..Default::default()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Check if dependencies have been loaded, and errors if either:
|
|
|
|
/// - The extension is depending on itself or an extension with the same name.
|
|
|
|
/// - A dependency hasn't been loaded yet.
|
|
|
|
pub fn check_dependencies(&self, previous_exts: &[&mut Extension]) {
|
|
|
|
if let Some(deps) = &self.deps {
|
|
|
|
'dep_loop: for dep in deps {
|
|
|
|
if dep == &self.name {
|
|
|
|
panic!("Extension '{}' is either depending on itself or there is another extension with the same name", self.name);
|
|
|
|
}
|
|
|
|
|
|
|
|
for ext in previous_exts {
|
|
|
|
if dep == &ext.name {
|
|
|
|
continue 'dep_loop;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
panic!("Extension '{}' is missing dependency '{dep}'", self.name);
|
|
|
|
}
|
|
|
|
}
|
2021-04-28 18:16:45 -04:00
|
|
|
}
|
|
|
|
|
2021-04-28 12:41:50 -04:00
|
|
|
/// returns JS source code to be loaded into the isolate (either at snapshotting,
|
|
|
|
/// or at startup). as a vector of a tuple of the file name, and the source code.
|
2023-02-07 18:21:43 -05:00
|
|
|
pub fn get_js_sources(&self) -> &[ExtensionFileSource] {
|
2021-04-28 12:41:50 -04:00
|
|
|
match &self.js_files {
|
2021-05-29 10:20:52 -04:00
|
|
|
Some(files) => files,
|
|
|
|
None => &[],
|
2021-04-28 12:41:50 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-07 18:21:43 -05:00
|
|
|
pub fn get_esm_sources(&self) -> &[ExtensionFileSource] {
|
2023-02-07 14:22:46 -05:00
|
|
|
match &self.esm_files {
|
|
|
|
Some(files) => files,
|
|
|
|
None => &[],
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-28 12:41:50 -04:00
|
|
|
/// Called at JsRuntime startup to initialize ops in the isolate.
|
2022-03-15 18:43:17 -04:00
|
|
|
pub fn init_ops(&mut self) -> Option<Vec<OpDecl>> {
|
2021-04-28 12:41:50 -04:00
|
|
|
// TODO(@AaronO): maybe make op registration idempotent
|
|
|
|
if self.initialized {
|
|
|
|
panic!("init_ops called twice: not idempotent or correct");
|
|
|
|
}
|
|
|
|
self.initialized = true;
|
|
|
|
|
2022-03-22 11:39:58 -04:00
|
|
|
let mut ops = self.ops.take()?;
|
|
|
|
for op in ops.iter_mut() {
|
|
|
|
op.enabled = self.enabled && op.enabled;
|
|
|
|
}
|
|
|
|
Some(ops)
|
2021-04-28 12:41:50 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Allows setting up the initial op-state of an isolate at startup.
|
2021-11-16 09:02:28 -05:00
|
|
|
pub fn init_state(&self, state: &mut OpState) -> Result<(), Error> {
|
2021-04-28 12:41:50 -04:00
|
|
|
match &self.opstate_fn {
|
|
|
|
Some(ofn) => ofn(state),
|
|
|
|
None => Ok(()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// init_middleware lets us middleware op registrations, it's called before init_ops
|
2021-05-07 09:45:07 -04:00
|
|
|
pub fn init_middleware(&mut self) -> Option<Box<OpMiddlewareFn>> {
|
2021-04-28 12:41:50 -04:00
|
|
|
self.middleware_fn.take()
|
|
|
|
}
|
2022-03-08 09:40:34 -05:00
|
|
|
|
|
|
|
pub fn init_event_loop_middleware(&mut self) -> Option<Box<OpEventLoopFn>> {
|
|
|
|
self.event_loop_middleware.take()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn run_event_loop_middleware(
|
|
|
|
&self,
|
2022-06-28 05:23:36 -04:00
|
|
|
op_state_rc: Rc<RefCell<OpState>>,
|
2022-03-08 09:40:34 -05:00
|
|
|
cx: &mut Context,
|
|
|
|
) -> bool {
|
|
|
|
self
|
|
|
|
.event_loop_middleware
|
|
|
|
.as_ref()
|
2022-06-28 05:23:36 -04:00
|
|
|
.map(|f| f(op_state_rc, cx))
|
2022-03-08 09:40:34 -05:00
|
|
|
.unwrap_or(false)
|
|
|
|
}
|
2022-03-22 11:39:58 -04:00
|
|
|
|
|
|
|
pub fn enabled(self, enabled: bool) -> Self {
|
|
|
|
Self { enabled, ..self }
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn disable(self) -> Self {
|
|
|
|
self.enabled(false)
|
|
|
|
}
|
2021-04-28 12:41:50 -04:00
|
|
|
}
|
|
|
|
|
2021-04-28 18:16:45 -04:00
|
|
|
// Provides a convenient builder pattern to declare Extensions
|
|
|
|
#[derive(Default)]
|
|
|
|
pub struct ExtensionBuilder {
|
2023-02-07 18:21:43 -05:00
|
|
|
js: Vec<ExtensionFileSource>,
|
|
|
|
esm: Vec<ExtensionFileSource>,
|
2022-03-15 18:43:17 -04:00
|
|
|
ops: Vec<OpDecl>,
|
2021-04-28 18:16:45 -04:00
|
|
|
state: Option<Box<OpStateFn>>,
|
|
|
|
middleware: Option<Box<OpMiddlewareFn>>,
|
2022-03-08 09:40:34 -05:00
|
|
|
event_loop_middleware: Option<Box<OpEventLoopFn>>,
|
2023-01-08 17:48:46 -05:00
|
|
|
name: &'static str,
|
|
|
|
deps: Vec<&'static str>,
|
2021-04-28 18:16:45 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
impl ExtensionBuilder {
|
2023-01-08 17:48:46 -05:00
|
|
|
pub fn dependencies(&mut self, dependencies: Vec<&'static str>) -> &mut Self {
|
|
|
|
self.deps.extend(dependencies);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2023-02-07 18:21:43 -05:00
|
|
|
pub fn js(&mut self, js_files: Vec<ExtensionFileSource>) -> &mut Self {
|
|
|
|
let js_files =
|
|
|
|
js_files.into_iter().map(|file_source| ExtensionFileSource {
|
|
|
|
specifier: format!("internal:{}/{}", self.name, file_source.specifier),
|
|
|
|
code: file_source.code,
|
|
|
|
});
|
2021-04-28 18:16:45 -04:00
|
|
|
self.js.extend(js_files);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2023-02-07 18:21:43 -05:00
|
|
|
pub fn esm(&mut self, esm_files: Vec<ExtensionFileSource>) -> &mut Self {
|
|
|
|
let esm_files =
|
|
|
|
esm_files
|
|
|
|
.into_iter()
|
|
|
|
.map(|file_source| ExtensionFileSource {
|
|
|
|
specifier: format!(
|
|
|
|
"internal:{}/{}",
|
|
|
|
self.name, file_source.specifier
|
|
|
|
),
|
|
|
|
code: file_source.code,
|
|
|
|
});
|
2023-02-07 16:09:50 -05:00
|
|
|
self.esm.extend(esm_files);
|
2023-02-07 14:22:46 -05:00
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2022-03-15 18:43:17 -04:00
|
|
|
pub fn ops(&mut self, ops: Vec<OpDecl>) -> &mut Self {
|
2021-04-28 18:16:45 -04:00
|
|
|
self.ops.extend(ops);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn state<F>(&mut self, opstate_fn: F) -> &mut Self
|
|
|
|
where
|
2021-11-16 09:02:28 -05:00
|
|
|
F: Fn(&mut OpState) -> Result<(), Error> + 'static,
|
2021-04-28 18:16:45 -04:00
|
|
|
{
|
|
|
|
self.state = Some(Box::new(opstate_fn));
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn middleware<F>(&mut self, middleware_fn: F) -> &mut Self
|
|
|
|
where
|
2022-03-15 18:43:17 -04:00
|
|
|
F: Fn(OpDecl) -> OpDecl + 'static,
|
2021-04-28 18:16:45 -04:00
|
|
|
{
|
|
|
|
self.middleware = Some(Box::new(middleware_fn));
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2022-03-08 09:40:34 -05:00
|
|
|
pub fn event_loop_middleware<F>(&mut self, middleware_fn: F) -> &mut Self
|
|
|
|
where
|
2022-06-28 05:23:36 -04:00
|
|
|
F: Fn(Rc<RefCell<OpState>>, &mut Context) -> bool + 'static,
|
2022-03-08 09:40:34 -05:00
|
|
|
{
|
|
|
|
self.event_loop_middleware = Some(Box::new(middleware_fn));
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2021-04-28 18:16:45 -04:00
|
|
|
pub fn build(&mut self) -> Extension {
|
|
|
|
let js_files = Some(std::mem::take(&mut self.js));
|
2023-02-07 14:22:46 -05:00
|
|
|
let esm_files = Some(std::mem::take(&mut self.esm));
|
2021-04-28 18:16:45 -04:00
|
|
|
let ops = Some(std::mem::take(&mut self.ops));
|
2023-01-08 17:48:46 -05:00
|
|
|
let deps = Some(std::mem::take(&mut self.deps));
|
2021-04-28 18:16:45 -04:00
|
|
|
Extension {
|
|
|
|
js_files,
|
2023-02-07 14:22:46 -05:00
|
|
|
esm_files,
|
2021-04-28 18:16:45 -04:00
|
|
|
ops,
|
|
|
|
opstate_fn: self.state.take(),
|
|
|
|
middleware_fn: self.middleware.take(),
|
2022-03-08 09:40:34 -05:00
|
|
|
event_loop_middleware: self.event_loop_middleware.take(),
|
2021-04-28 18:16:45 -04:00
|
|
|
initialized: false,
|
2022-03-22 11:39:58 -04:00
|
|
|
enabled: true,
|
2023-01-08 17:48:46 -05:00
|
|
|
name: self.name,
|
|
|
|
deps,
|
2021-04-28 18:16:45 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-05-15 07:27:56 -04:00
|
|
|
/// Helps embed JS files in an extension. Returns Vec<(&'static str, &'static str)>
|
|
|
|
/// representing the filename and source code.
|
2021-04-28 12:41:50 -04:00
|
|
|
///
|
|
|
|
/// Example:
|
|
|
|
/// ```ignore
|
|
|
|
/// include_js_files!(
|
|
|
|
/// "01_hello.js",
|
|
|
|
/// "02_goodbye.js",
|
|
|
|
/// )
|
|
|
|
/// ```
|
|
|
|
#[macro_export]
|
|
|
|
macro_rules! include_js_files {
|
2023-02-07 16:09:50 -05:00
|
|
|
($($file:literal,)+) => {
|
2021-04-28 12:41:50 -04:00
|
|
|
vec![
|
2023-02-07 18:21:43 -05:00
|
|
|
$($crate::ExtensionFileSource {
|
|
|
|
specifier: $file.to_string(),
|
|
|
|
code: include_str!($file),
|
|
|
|
},)+
|
2021-04-28 12:41:50 -04:00
|
|
|
]
|
|
|
|
};
|
|
|
|
}
|