use crate::modules::ModuleCode;
use crate::OpState;
use anyhow::Context as _;
use anyhow::Error;
use std::borrow::Cow;
use std::cell::RefCell;
use std::rc::Rc;
use std::sync::Arc;
use std::task::Context;
use v8::fast_api::FastFunction;
use v8::ExternalReference;
#[derive(Clone, Debug)]
pub enum ExtensionFileSourceCode {
IncludedInBinary(&'static str),
LoadedFromFsDuringSnapshot(&'static str), Computed(Arc<str>),
}
#[derive(Clone, Debug)]
pub struct ExtensionFileSource {
pub specifier: &'static str,
pub code: ExtensionFileSourceCode,
}
impl ExtensionFileSource {
fn find_non_ascii(s: &str) -> String {
s.chars().filter(|c| !c.is_ascii()).collect::<String>()
}
pub fn load(&self) -> Result<ModuleCode, Error> {
match &self.code {
ExtensionFileSourceCode::IncludedInBinary(code) => {
debug_assert!(
code.is_ascii(),
"Extension code must be 7-bit ASCII: {} (found {})",
self.specifier,
Self::find_non_ascii(code)
);
Ok(ModuleCode::from_static(code))
}
ExtensionFileSourceCode::LoadedFromFsDuringSnapshot(path) => {
let msg = || format!("Failed to read \"{}\"", path);
let s = std::fs::read_to_string(path).with_context(msg)?;
debug_assert!(
s.is_ascii(),
"Extension code must be 7-bit ASCII: {} (found {})",
self.specifier,
Self::find_non_ascii(&s)
);
Ok(s.into())
}
ExtensionFileSourceCode::Computed(code) => {
debug_assert!(
code.is_ascii(),
"Extension code must be 7-bit ASCII: {} (found {})",
self.specifier,
Self::find_non_ascii(code)
);
Ok(ModuleCode::Arc(code.clone()))
}
}
}
}
pub type OpFnRef = v8::FunctionCallback;
pub type OpMiddlewareFn = dyn Fn(OpDecl) -> OpDecl;
pub type OpStateFn = dyn FnOnce(&mut OpState);
pub trait Op {
const NAME: &'static str;
const DECL: OpDecl;
}
pub type EventLoopMiddlewareFn = fn(Rc<RefCell<OpState>>, &mut Context) -> bool;
pub type GlobalTemplateMiddlewareFn =
for<'s> fn(
&mut v8::HandleScope<'s, ()>,
v8::Local<'s, v8::ObjectTemplate>,
) -> v8::Local<'s, v8::ObjectTemplate>;
pub type GlobalObjectMiddlewareFn =
for<'s> fn(&mut v8::HandleScope<'s>, v8::Local<'s, v8::Object>);
#[derive(Copy, Clone)]
pub struct OpDecl {
pub name: &'static str,
pub enabled: bool,
pub is_async: bool,
pub is_unstable: bool,
pub is_v8: bool,
pub arg_count: u8,
pub(crate) v8_fn_ptr: OpFnRef,
pub(crate) fast_fn: Option<FastFunction>,
}
impl OpDecl {
#[doc(hidden)]
pub const fn new_internal(
name: &'static str,
is_async: bool,
is_unstable: bool,
is_v8: bool,
arg_count: u8,
v8_fn_ptr: OpFnRef,
fast_fn: Option<FastFunction>,
) -> Self {
Self {
name,
enabled: true,
is_async,
is_unstable,
is_v8,
arg_count,
v8_fn_ptr,
fast_fn,
}
}
pub const fn enabled(self, enabled: bool) -> Self {
Self { enabled, ..self }
}
pub const fn disable(self) -> Self {
self.enabled(false)
}
pub const fn with_implementation_from(self, from: &Self) -> Self {
Self {
v8_fn_ptr: from.v8_fn_ptr,
fast_fn: from.fast_fn,
..self
}
}
}
#[macro_export]
macro_rules! ops {
($name:ident, parameters = [ $( $param:ident : $type:ident ),+ ], ops = [ $( $(#[$m:meta])* $( $op:ident )::+ $( < $op_param:ident > )? ),+ $(,)? ]) => {
pub(crate) fn $name < $( $param : $type + 'static ),+ > () -> Vec<$crate::OpDecl> {
vec![
$(
$( #[ $m ] )*
$( $op )::+ :: decl $( :: <$op_param> )? () ,
)+
]
}
};
($name:ident, [ $( $(#[$m:meta])* $( $op:ident )::+ ),+ $(,)? ] ) => {
pub(crate) fn $name() -> Vec<$crate::OpDecl> {
use $crate::Op;
vec![
$( $( #[ $m ] )* $( $op )::+ :: DECL, )+
]
}
}
}
#[macro_export]
macro_rules! or {
($e:expr, $fallback:expr) => {
$e
};
(, $fallback:expr) => {
$fallback
};
}
#[macro_export]
macro_rules! extension {
(
$name:ident
$(, deps = [ $( $dep:ident ),* ] )?
$(, parameters = [ $( $param:ident : $type:ident ),+ ] )?
$(, bounds = [ $( $bound:path : $bound_type:ident ),+ ] )?
$(, ops_fn = $ops_symbol:ident $( < $ops_param:ident > )? )?
$(, ops = [ $( $(#[$m:meta])* $( $op:ident )::+ $( < $( $op_param:ident ),* > )? ),+ $(,)? ] )?
$(, esm_entry_point = $esm_entry_point:expr )?
$(, esm = [ $( dir $dir_esm:expr , )? $( $esm:literal $( with_specifier $esm_specifier:expr )? ),* $(,)? ] )?
$(, js = [ $( dir $dir_js:expr , )? $( $js:literal ),* $(,)? ] )?
$(, options = { $( $options_id:ident : $options_type:ty ),* $(,)? } )?
$(, middleware = $middleware_fn:expr )?
$(, state = $state_fn:expr )?
$(, event_loop_middleware = $event_loop_middleware_fn:expr )?
$(, global_template_middleware = $global_template_middleware_fn:expr )?
$(, global_object_middleware = $global_object_middleware_fn:expr )?
$(, external_references = [ $( $external_reference:expr ),* $(,)? ] )?
$(, customizer = $customizer_fn:expr )?
$(,)?
) => {
#[doc = stringify!($name)]
#[allow(non_camel_case_types)]
pub struct $name {
}
impl $name {
fn ext $( < $( $param : $type + 'static ),+ > )?() -> $crate::Extension {
#[allow(unused_imports)]
use $crate::Op;
$crate::Extension {
name: stringify!($name),
deps: &[ $( $( stringify!($dep) ),* )? ],
js_files: {
const V: std::borrow::Cow<'static, [$crate::ExtensionFileSource]> = std::borrow::Cow::Borrowed(&$crate::or!($($crate::include_js_files!( $name $( dir $dir_js , )? $( $js , )* ))?, []));
V
},
esm_files: {
const V: std::borrow::Cow<'static, [$crate::ExtensionFileSource]> = std::borrow::Cow::Borrowed(&$crate::or!($($crate::include_js_files!( $name $( dir $dir_esm , )? $( $esm $( with_specifier $esm_specifier )? , )* ))?, []));
V
},
esm_entry_point: {
const V: Option<&'static str> = $crate::or!($(Some($esm_entry_point))?, None);
V
},
ops: std::borrow::Cow::Borrowed(&[$($(
$( #[ $m ] )*
$( $op )::+ $( :: < $($op_param),* > )? :: DECL
),+)?]),
external_references: std::borrow::Cow::Borrowed(&[ $( $external_reference ),* ]),
event_loop_middleware: None,
global_template_middleware: None,
global_object_middleware: None,
op_state_fn: None,
middleware_fn: None,
enabled: true,
}
}
#[inline(always)]
#[allow(unused_variables)]
fn with_ops_fn $( < $( $param : $type + 'static ),+ > )?(ext: &mut $crate::Extension)
$( where $( $bound : $bound_type ),+ )?
{
$crate::extension!(! __ops__ ext $( $ops_symbol $( < $ops_param > )? )? __eot__);
}
#[inline(always)]
#[allow(unused_variables)]
fn with_state_and_middleware$( < $( $param : $type + 'static ),+ > )?(ext: &mut $crate::Extension, $( $( $options_id : $options_type ),* )? )
$( where $( $bound : $bound_type ),+ )?
{
$crate::extension!(! __config__ ext $( parameters = [ $( $param : $type ),* ] )? $( config = { $( $options_id : $options_type ),* } )? $( state_fn = $state_fn )? );
$(
ext.event_loop_middleware = Some($event_loop_middleware_fn);
)?
$(
ext.global_template_middleware = Some($global_template_middleware_fn);
)?
$(
ext.global_object_middleware = Some($global_object_middleware_fn);
)?
$(
ext.middleware_fn = Some(Box::new($middleware_fn));
)?
}
#[inline(always)]
#[allow(unused_variables)]
#[allow(clippy::redundant_closure_call)]
fn with_customizer(ext: &mut $crate::Extension) {
$( ($customizer_fn)(ext); )?
}
#[allow(dead_code)]
pub fn init_js_only $( < $( $param : $type + 'static ),* > )? () -> $crate::Extension
$( where $( $bound : $bound_type ),+ )?
{
let mut ext = Self::ext $( ::< $( $param ),+ > )?();
Self::with_ops_fn $( ::< $( $param ),+ > )?(&mut ext);
Self::with_customizer(&mut ext);
ext
}
#[allow(dead_code)]
pub fn init_ops_and_esm $( < $( $param : $type + 'static ),+ > )? ( $( $( $options_id : $options_type ),* )? ) -> $crate::Extension
$( where $( $bound : $bound_type ),+ )?
{
let mut ext = Self::ext $( ::< $( $param ),+ > )?();
Self::with_ops_fn $( ::< $( $param ),+ > )?(&mut ext);
Self::with_state_and_middleware $( ::< $( $param ),+ > )?(&mut ext, $( $( $options_id , )* )? );
Self::with_customizer(&mut ext);
ext
}
#[allow(dead_code)]
pub fn init_ops $( < $( $param : $type + 'static ),+ > )? ( $( $( $options_id : $options_type ),* )? ) -> $crate::Extension
$( where $( $bound : $bound_type ),+ )?
{
let mut ext = Self::ext $( ::< $( $param ),+ > )?();
Self::with_ops_fn $( ::< $( $param ),+ > )?(&mut ext);
Self::with_state_and_middleware $( ::< $( $param ),+ > )?(&mut ext, $( $( $options_id , )* )? );
Self::with_customizer(&mut ext);
ext.js_files = std::borrow::Cow::Borrowed(&[]);
ext.esm_files = std::borrow::Cow::Borrowed(&[]);
ext.esm_entry_point = None;
ext
}
}
};
(! __config__ $ext:ident $( parameters = [ $( $param:ident : $type:ident ),+ ] )? config = { $( $options_id:ident : $options_type:ty ),* } $( state_fn = $state_fn:expr )? ) => {
{
#[doc(hidden)]
struct Config $( < $( $param : $type + 'static ),+ > )? {
$( pub $options_id : $options_type , )*
$( __phantom_data: ::std::marker::PhantomData<($( $param ),+)>, )?
}
let config = Config {
$( $options_id , )*
$( __phantom_data: ::std::marker::PhantomData::<($( $param ),+)>::default() )?
};
let state_fn: fn(&mut $crate::OpState, Config $( < $( $param ),+ > )? ) = $( $state_fn )?;
$ext.op_state_fn = Some(Box::new(move |state: &mut $crate::OpState| {
state_fn(state, config);
}));
}
};
(! __config__ $ext:ident $( parameters = [ $( $param:ident : $type:ident ),+ ] )? $( state_fn = $state_fn:expr )? ) => {
$( $ext.op_state_fn = Some(Box::new($state_fn)); )?
};
(! __ops__ $ext:ident __eot__) => {
};
(! __ops__ $ext:ident $ops_symbol:ident __eot__) => {
$ext.ops.to_mut().extend($ops_symbol())
};
(! __ops__ $ext:ident $ops_symbol:ident < $ops_param:ident > __eot__) => {
$ext.ops.to_mut().extend($ops_symbol::<$ops_param>())
};
}
pub struct Extension {
pub name: &'static str,
pub deps: &'static [&'static str],
pub js_files: Cow<'static, [ExtensionFileSource]>,
pub esm_files: Cow<'static, [ExtensionFileSource]>,
pub esm_entry_point: Option<&'static str>,
pub ops: Cow<'static, [OpDecl]>,
pub external_references: Cow<'static, [v8::ExternalReference<'static>]>,
pub event_loop_middleware: Option<EventLoopMiddlewareFn>,
pub global_template_middleware: Option<GlobalTemplateMiddlewareFn>,
pub global_object_middleware: Option<GlobalObjectMiddlewareFn>,
pub op_state_fn: Option<Box<OpStateFn>>,
pub middleware_fn: Option<Box<OpMiddlewareFn>>,
pub enabled: bool,
}
impl Default for Extension {
fn default() -> Self {
Self {
name: "DEFAULT",
deps: &[],
js_files: Cow::Borrowed(&[]),
esm_files: Cow::Borrowed(&[]),
esm_entry_point: None,
ops: Cow::Borrowed(&[]),
external_references: Cow::Borrowed(&[]),
event_loop_middleware: None,
global_template_middleware: None,
global_object_middleware: None,
op_state_fn: None,
middleware_fn: None,
enabled: true,
}
}
}
impl Extension {
#[deprecated(note = "Use Extension { ..., ..Default::default() }")]
#[allow(deprecated)]
pub fn builder(name: &'static str) -> ExtensionBuilder {
ExtensionBuilder {
name,
..Default::default()
}
}
#[deprecated(note = "Use Extension { ..., ..Default::default() }")]
#[allow(deprecated)]
pub fn builder_with_deps(
name: &'static str,
deps: &'static [&'static str],
) -> ExtensionBuilder {
ExtensionBuilder {
name,
deps,
..Default::default()
}
}
pub fn check_dependencies(&self, previous_exts: &[Extension]) {
'dep_loop: for dep in self.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);
}
}
pub fn get_js_sources(&self) -> &[ExtensionFileSource] {
self.js_files.as_ref()
}
pub fn get_esm_sources(&self) -> &[ExtensionFileSource] {
self.esm_files.as_ref()
}
pub fn get_esm_entry_point(&self) -> Option<&'static str> {
self.esm_entry_point
}
pub fn init_ops(&mut self) -> &[OpDecl] {
if !self.enabled {
for op in self.ops.to_mut() {
op.enabled = false;
}
}
self.ops.as_ref()
}
pub fn take_state(&mut self, state: &mut OpState) {
if let Some(op_fn) = self.op_state_fn.take() {
op_fn(state);
}
}
pub fn take_middleware(&mut self) -> Option<Box<OpMiddlewareFn>> {
self.middleware_fn.take()
}
pub fn get_event_loop_middleware(&mut self) -> Option<EventLoopMiddlewareFn> {
self.event_loop_middleware
}
pub fn get_global_template_middleware(
&mut self,
) -> Option<GlobalTemplateMiddlewareFn> {
self.global_template_middleware
}
pub fn get_global_object_middleware(
&mut self,
) -> Option<GlobalObjectMiddlewareFn> {
self.global_object_middleware
}
pub fn get_external_references(
&mut self,
) -> &[v8::ExternalReference<'static>] {
self.external_references.as_ref()
}
pub fn enabled(self, enabled: bool) -> Self {
Self { enabled, ..self }
}
pub fn disable(self) -> Self {
self.enabled(false)
}
}
#[derive(Default)]
pub struct ExtensionBuilder {
js: Vec<ExtensionFileSource>,
esm: Vec<ExtensionFileSource>,
esm_entry_point: Option<&'static str>,
ops: Vec<OpDecl>,
state: Option<Box<OpStateFn>>,
middleware: Option<Box<OpMiddlewareFn>>,
event_loop_middleware: Option<EventLoopMiddlewareFn>,
global_template_middleware: Option<GlobalTemplateMiddlewareFn>,
global_object_middleware: Option<GlobalObjectMiddlewareFn>,
external_references: Vec<ExternalReference<'static>>,
name: &'static str,
deps: &'static [&'static str],
}
impl ExtensionBuilder {
pub fn js(&mut self, js_files: Vec<ExtensionFileSource>) -> &mut Self {
self.js.extend(js_files);
self
}
pub fn esm(&mut self, esm_files: Vec<ExtensionFileSource>) -> &mut Self {
self.esm.extend(esm_files);
self
}
pub fn esm_entry_point(&mut self, entry_point: &'static str) -> &mut Self {
self.esm_entry_point = Some(entry_point);
self
}
pub fn ops(&mut self, ops: Vec<OpDecl>) -> &mut Self {
self.ops.extend(ops);
self
}
pub fn state<F>(&mut self, op_state_fn: F) -> &mut Self
where
F: FnOnce(&mut OpState) + 'static,
{
self.state = Some(Box::new(op_state_fn));
self
}
pub fn middleware<F>(&mut self, middleware_fn: F) -> &mut Self
where
F: Fn(OpDecl) -> OpDecl + 'static,
{
self.middleware = Some(Box::new(middleware_fn));
self
}
pub fn event_loop_middleware<F>(
&mut self,
middleware_fn: EventLoopMiddlewareFn,
) -> &mut Self {
self.event_loop_middleware = Some(middleware_fn);
self
}
pub fn global_template_middleware<F>(
&mut self,
middleware_fn: GlobalTemplateMiddlewareFn,
) -> &mut Self {
self.global_template_middleware = Some(middleware_fn);
self
}
pub fn global_object_middleware<F>(
&mut self,
middleware_fn: GlobalObjectMiddlewareFn,
) -> &mut Self {
self.global_object_middleware = Some(middleware_fn);
self
}
pub fn external_references(
&mut self,
external_references: Vec<ExternalReference<'static>>,
) -> &mut Self {
self.external_references.extend(external_references);
self
}
pub fn take(self) -> Extension {
Extension {
name: self.name,
deps: self.deps,
js_files: Cow::Owned(self.js),
esm_files: Cow::Owned(self.esm),
esm_entry_point: self.esm_entry_point,
ops: Cow::Owned(self.ops),
external_references: Cow::Owned(self.external_references),
event_loop_middleware: self.event_loop_middleware,
global_template_middleware: self.global_template_middleware,
global_object_middleware: self.global_object_middleware,
op_state_fn: self.state,
middleware_fn: self.middleware,
enabled: true,
}
}
pub fn build(&mut self) -> Extension {
Extension {
name: self.name,
deps: std::mem::take(&mut self.deps),
js_files: Cow::Owned(std::mem::take(&mut self.js)),
esm_files: Cow::Owned(std::mem::take(&mut self.esm)),
esm_entry_point: self.esm_entry_point.take(),
ops: Cow::Owned(std::mem::take(&mut self.ops)),
external_references: Cow::Owned(std::mem::take(
&mut self.external_references,
)),
event_loop_middleware: self.event_loop_middleware.take(),
global_template_middleware: self.global_template_middleware.take(),
global_object_middleware: self.global_object_middleware.take(),
op_state_fn: self.state.take(),
middleware_fn: self.middleware.take(),
enabled: true,
}
}
}
#[cfg(not(feature = "include_js_files_for_snapshotting"))]
#[macro_export]
macro_rules! include_js_files {
($name:ident dir $dir:expr, $($file:literal $(with_specifier $esm_specifier:expr)?,)+) => {
[
$($crate::ExtensionFileSource {
specifier: $crate::or!($($esm_specifier)?, concat!("ext:", stringify!($name), "/", $file)),
code: $crate::ExtensionFileSourceCode::IncludedInBinary(
include_str!(concat!(env!("CARGO_MANIFEST_DIR"), "/", $dir, "/", $file))
),
},)+
]
};
($name:ident $($file:literal $(with_specifier $esm_specifier:expr)?,)+) => {
[
$($crate::ExtensionFileSource {
specifier: $crate::or!($($esm_specifier)?, concat!("ext:", stringify!($name), "/", $file)),
code: $crate::ExtensionFileSourceCode::IncludedInBinary(
include_str!(concat!(env!("CARGO_MANIFEST_DIR"), "/", $file))
),
},)+
]
};
}
#[cfg(feature = "include_js_files_for_snapshotting")]
#[macro_export]
macro_rules! include_js_files {
($name:ident dir $dir:expr, $($file:literal $(with_specifier $esm_specifier:expr)?,)+) => {
[
$($crate::ExtensionFileSource {
specifier: $crate::or!($($esm_specifier)?, concat!("ext:", stringify!($name), "/", $file)),
code: $crate::ExtensionFileSourceCode::LoadedFromFsDuringSnapshot(
concat!(env!("CARGO_MANIFEST_DIR"), "/", $dir, "/", $file)
),
},)+
]
};
($name:ident $($file:literal $(with_specifier $esm_specifier:expr)?,)+) => {
[
$($crate::ExtensionFileSource {
specifier: $crate::or!($($esm_specifier)?, concat!("ext:", stringify!($name), "/", $file)),
code: $crate::ExtensionFileSourceCode::LoadedFromFsDuringSnapshot(
concat!(env!("CARGO_MANIFEST_DIR"), "/", $file)
),
},)+
]
};
}