0
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2025-03-03 17:34:47 -05:00

refactor(core): Extension::builder_with_deps (#18093)

Prerequisite for https://github.com/denoland/deno/pull/18080
This commit is contained in:
Bartek Iwańczuk 2023-03-09 08:10:54 -04:00 committed by GitHub
parent 521cb4ca9b
commit c3cba7f22c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
17 changed files with 309 additions and 289 deletions

View file

@ -77,7 +77,7 @@ pub struct Extension {
initialized: bool, initialized: bool,
enabled: bool, enabled: bool,
name: &'static str, name: &'static str,
deps: Option<Vec<&'static str>>, deps: Option<&'static [&'static str]>,
} }
// Note: this used to be a trait, but we "downgraded" it to a single concrete type // Note: this used to be a trait, but we "downgraded" it to a single concrete type
@ -90,11 +90,22 @@ impl Extension {
} }
} }
pub fn builder_with_deps(
name: &'static str,
deps: &'static [&'static str],
) -> ExtensionBuilder {
ExtensionBuilder {
name,
deps,
..Default::default()
}
}
/// Check if dependencies have been loaded, and errors if either: /// Check if dependencies have been loaded, and errors if either:
/// - The extension is depending on itself or an extension with the same name. /// - The extension is depending on itself or an extension with the same name.
/// - A dependency hasn't been loaded yet. /// - A dependency hasn't been loaded yet.
pub fn check_dependencies(&self, previous_exts: &[&mut Extension]) { pub fn check_dependencies(&self, previous_exts: &[&mut Extension]) {
if let Some(deps) = &self.deps { if let Some(deps) = self.deps {
'dep_loop: for dep in deps { 'dep_loop: for dep in deps {
if dep == &self.name { if dep == &self.name {
panic!("Extension '{}' is either depending on itself or there is another extension with the same name", self.name); panic!("Extension '{}' is either depending on itself or there is another extension with the same name", self.name);
@ -194,15 +205,10 @@ pub struct ExtensionBuilder {
middleware: Option<Box<OpMiddlewareFn>>, middleware: Option<Box<OpMiddlewareFn>>,
event_loop_middleware: Option<Box<OpEventLoopFn>>, event_loop_middleware: Option<Box<OpEventLoopFn>>,
name: &'static str, name: &'static str,
deps: Vec<&'static str>, deps: &'static [&'static str],
} }
impl ExtensionBuilder { impl ExtensionBuilder {
pub fn dependencies(&mut self, dependencies: Vec<&'static str>) -> &mut Self {
self.deps.extend(dependencies);
self
}
pub fn js(&mut self, js_files: Vec<ExtensionFileSource>) -> &mut Self { pub fn js(&mut self, js_files: Vec<ExtensionFileSource>) -> &mut Self {
let js_files = let js_files =
// TODO(bartlomieju): if we're automatically remapping here, then we should // TODO(bartlomieju): if we're automatically remapping here, then we should

View file

@ -110,8 +110,10 @@ pub fn init<BC: BroadcastChannel + 'static>(
bc: BC, bc: BC,
unstable: bool, unstable: bool,
) -> Extension { ) -> Extension {
Extension::builder(env!("CARGO_PKG_NAME")) Extension::builder_with_deps(
.dependencies(vec!["deno_webidl", "deno_web"]) env!("CARGO_PKG_NAME"),
&["deno_webidl", "deno_web"],
)
.esm(include_js_files!("01_broadcast_channel.js",)) .esm(include_js_files!("01_broadcast_channel.js",))
.ops(vec![ .ops(vec![
op_broadcast_subscribe::decl::<BC>(), op_broadcast_subscribe::decl::<BC>(),

6
ext/cache/lib.rs vendored
View file

@ -25,8 +25,10 @@ pub struct CreateCache<C: Cache + 'static>(pub Arc<dyn Fn() -> C>);
pub fn init<CA: Cache + 'static>( pub fn init<CA: Cache + 'static>(
maybe_create_cache: Option<CreateCache<CA>>, maybe_create_cache: Option<CreateCache<CA>>,
) -> Extension { ) -> Extension {
Extension::builder(env!("CARGO_PKG_NAME")) Extension::builder_with_deps(
.dependencies(vec!["deno_webidl", "deno_web", "deno_url", "deno_fetch"]) env!("CARGO_PKG_NAME"),
&["deno_webidl", "deno_web", "deno_url", "deno_fetch"],
)
.esm(include_js_files!("01_cache.js",)) .esm(include_js_files!("01_cache.js",))
.ops(vec![ .ops(vec![
op_cache_storage_open::decl::<CA>(), op_cache_storage_open::decl::<CA>(),

View file

@ -73,8 +73,10 @@ use crate::key::HkdfOutput;
use crate::shared::RawKeyData; use crate::shared::RawKeyData;
pub fn init(maybe_seed: Option<u64>) -> Extension { pub fn init(maybe_seed: Option<u64>) -> Extension {
Extension::builder(env!("CARGO_PKG_NAME")) Extension::builder_with_deps(
.dependencies(vec!["deno_webidl", "deno_web"]) env!("CARGO_PKG_NAME"),
&["deno_webidl", "deno_web"],
)
.esm(include_js_files!("00_crypto.js", "01_webidl.js",)) .esm(include_js_files!("00_crypto.js", "01_webidl.js",))
.ops(vec![ .ops(vec![
op_crypto_get_random_values::decl(), op_crypto_get_random_values::decl(),

View file

@ -95,8 +95,10 @@ pub fn init<FP>(options: Options) -> Extension
where where
FP: FetchPermissions + 'static, FP: FetchPermissions + 'static,
{ {
Extension::builder(env!("CARGO_PKG_NAME")) Extension::builder_with_deps(
.dependencies(vec!["deno_webidl", "deno_web", "deno_url", "deno_console"]) env!("CARGO_PKG_NAME"),
&["deno_webidl", "deno_web", "deno_url", "deno_console"],
)
.esm(include_js_files!( .esm(include_js_files!(
"20_headers.js", "20_headers.js",
"21_formdata.js", "21_formdata.js",

View file

@ -82,8 +82,7 @@ pub(crate) struct FfiState {
} }
pub fn init<P: FfiPermissions + 'static>(unstable: bool) -> Extension { pub fn init<P: FfiPermissions + 'static>(unstable: bool) -> Extension {
Extension::builder(env!("CARGO_PKG_NAME")) Extension::builder_with_deps(env!("CARGO_PKG_NAME"), &["deno_web"])
.dependencies(vec!["deno_web"])
.esm(include_js_files!("00_ffi.js",)) .esm(include_js_files!("00_ffi.js",))
.ops(vec![ .ops(vec![
op_ffi_load::decl::<P>(), op_ffi_load::decl::<P>(),

View file

@ -1527,14 +1527,16 @@ pub trait FlashPermissions {
} }
pub fn init<P: FlashPermissions + 'static>(unstable: bool) -> Extension { pub fn init<P: FlashPermissions + 'static>(unstable: bool) -> Extension {
Extension::builder(env!("CARGO_PKG_NAME")) Extension::builder_with_deps(
.dependencies(vec![ env!("CARGO_PKG_NAME"),
&[
"deno_web", "deno_web",
"deno_net", "deno_net",
"deno_fetch", "deno_fetch",
"deno_websocket", "deno_websocket",
"deno_http", "deno_http",
]) ],
)
.esm(deno_core::include_js_files!("01_http.js",)) .esm(deno_core::include_js_files!("01_http.js",))
.ops(vec![ .ops(vec![
op_flash_serve::decl::<P>(), op_flash_serve::decl::<P>(),

View file

@ -78,8 +78,10 @@ pub mod compressible;
mod reader_stream; mod reader_stream;
pub fn init() -> Extension { pub fn init() -> Extension {
Extension::builder(env!("CARGO_PKG_NAME")) Extension::builder_with_deps(
.dependencies(vec!["deno_web", "deno_net", "deno_fetch", "deno_websocket"]) env!("CARGO_PKG_NAME"),
&["deno_web", "deno_net", "deno_fetch", "deno_websocket"],
)
.esm(include_js_files!("01_http.js",)) .esm(include_js_files!("01_http.js",))
.ops(vec![ .ops(vec![
op_http_accept::decl(), op_http_accept::decl(),

View file

@ -82,9 +82,8 @@ pub fn init(stdio: Stdio) -> Extension {
// todo(dsheret): don't do this? Taking out the writers was necessary to prevent invalid handle panics // todo(dsheret): don't do this? Taking out the writers was necessary to prevent invalid handle panics
let stdio = Rc::new(RefCell::new(Some(stdio))); let stdio = Rc::new(RefCell::new(Some(stdio)));
Extension::builder("deno_io") Extension::builder_with_deps("deno_io", &["deno_web"])
.ops(vec![op_read_sync::decl(), op_write_sync::decl()]) .ops(vec![op_read_sync::decl(), op_write_sync::decl()])
.dependencies(vec!["deno_web"])
.esm(include_js_files!("12_io.js",)) .esm(include_js_files!("12_io.js",))
.middleware(|op| match op.name { .middleware(|op| match op.name {
"op_print" => op_print::decl(), "op_print" => op_print::decl(),

View file

@ -84,8 +84,7 @@ pub fn init<P: NetPermissions + 'static>(
) -> Extension { ) -> Extension {
let mut ops = ops::init::<P>(); let mut ops = ops::init::<P>();
ops.extend(ops_tls::init::<P>()); ops.extend(ops_tls::init::<P>());
Extension::builder(env!("CARGO_PKG_NAME")) Extension::builder_with_deps(env!("CARGO_PKG_NAME"), &["deno_web"])
.dependencies(vec!["deno_web"])
.esm(include_js_files!("01_net.js", "02_tls.js",)) .esm(include_js_files!("01_net.js", "02_tls.js",))
.ops(ops) .ops(ops)
.state(move |state| { .state(move |state| {

View file

@ -332,10 +332,9 @@ pub fn init_polyfill_ops_and_esm() -> Extension {
"zlib.ts", "zlib.ts",
); );
Extension::builder(env!("CARGO_PKG_NAME")) Extension::builder_with_deps(env!("CARGO_PKG_NAME"), &["deno_io", "deno_fs"])
.esm(esm_files) .esm(esm_files)
.esm_entry_point("ext:deno_node/module_all.ts") .esm_entry_point("ext:deno_node/module_all.ts")
.dependencies(vec!["deno_io", "deno_fs"])
.ops(vec![ .ops(vec![
crypto::op_node_create_hash::decl(), crypto::op_node_create_hash::decl(),
crypto::op_node_hash_update::decl(), crypto::op_node_hash_update::decl(),

View file

@ -18,8 +18,7 @@ use crate::urlpattern::op_urlpattern_parse;
use crate::urlpattern::op_urlpattern_process_match_input; use crate::urlpattern::op_urlpattern_process_match_input;
pub fn init() -> Extension { pub fn init() -> Extension {
Extension::builder(env!("CARGO_PKG_NAME")) Extension::builder_with_deps(env!("CARGO_PKG_NAME"), &["deno_webidl"])
.dependencies(vec!["deno_webidl"])
.esm(include_js_files!("00_url.js", "01_urlpattern.js",)) .esm(include_js_files!("00_url.js", "01_urlpattern.js",))
.ops(vec![ .ops(vec![
op_url_reparse::decl(), op_url_reparse::decl(),

View file

@ -62,8 +62,10 @@ pub fn init<P: TimersPermission + 'static>(
blob_store: BlobStore, blob_store: BlobStore,
maybe_location: Option<Url>, maybe_location: Option<Url>,
) -> Extension { ) -> Extension {
Extension::builder(env!("CARGO_PKG_NAME")) Extension::builder_with_deps(
.dependencies(vec!["deno_webidl", "deno_console", "deno_url"]) env!("CARGO_PKG_NAME"),
&["deno_webidl", "deno_console", "deno_url"],
)
.esm(include_js_files!( .esm(include_js_files!(
"00_infra.js", "00_infra.js",
"01_dom_exception.js", "01_dom_exception.js",

View file

@ -117,8 +117,10 @@ impl Resource for WebGpuQuerySet {
} }
pub fn init(unstable: bool) -> Extension { pub fn init(unstable: bool) -> Extension {
Extension::builder(env!("CARGO_PKG_NAME")) Extension::builder_with_deps(
.dependencies(vec!["deno_webidl", "deno_web"]) env!("CARGO_PKG_NAME"),
&["deno_webidl", "deno_web"],
)
.esm(include_js_files!("01_webgpu.js", "02_idl_types.js",)) .esm(include_js_files!("01_webgpu.js", "02_idl_types.js",))
.ops(declare_webgpu_ops()) .ops(declare_webgpu_ops())
.state(move |state| { .state(move |state| {

View file

@ -502,8 +502,10 @@ pub fn init<P: WebSocketPermissions + 'static>(
root_cert_store: Option<RootCertStore>, root_cert_store: Option<RootCertStore>,
unsafely_ignore_certificate_errors: Option<Vec<String>>, unsafely_ignore_certificate_errors: Option<Vec<String>>,
) -> Extension { ) -> Extension {
Extension::builder(env!("CARGO_PKG_NAME")) Extension::builder_with_deps(
.dependencies(vec!["deno_url", "deno_webidl"]) env!("CARGO_PKG_NAME"),
&["deno_url", "deno_webidl"],
)
.esm(include_js_files!( .esm(include_js_files!(
"01_websocket.js", "01_websocket.js",
"02_websocketstream.js", "02_websocketstream.js",

View file

@ -22,8 +22,7 @@ struct OriginStorageDir(PathBuf);
const MAX_STORAGE_BYTES: u32 = 10 * 1024 * 1024; const MAX_STORAGE_BYTES: u32 = 10 * 1024 * 1024;
pub fn init(origin_storage_dir: Option<PathBuf>) -> Extension { pub fn init(origin_storage_dir: Option<PathBuf>) -> Extension {
Extension::builder(env!("CARGO_PKG_NAME")) Extension::builder_with_deps(env!("CARGO_PKG_NAME"), &["deno_webidl"])
.dependencies(vec!["deno_webidl"])
.esm(include_js_files!("01_webstorage.js",)) .esm(include_js_files!("01_webstorage.js",))
.ops(vec![ .ops(vec![
op_webstorage_length::decl(), op_webstorage_length::decl(),

View file

@ -204,8 +204,9 @@ mod startup_snapshot {
snapshot_path: PathBuf, snapshot_path: PathBuf,
maybe_additional_extension: Option<Extension>, maybe_additional_extension: Option<Extension>,
) { ) {
let runtime_extension = Extension::builder("runtime") let runtime_extension = Extension::builder_with_deps(
.dependencies(vec![ "runtime",
&[
"deno_webidl", "deno_webidl",
"deno_console", "deno_console",
"deno_url", "deno_url",
@ -227,7 +228,8 @@ mod startup_snapshot {
"deno_flash", "deno_flash",
"deno_io", "deno_io",
"deno_fs", "deno_fs",
]) ],
)
.esm(include_js_files!( .esm(include_js_files!(
dir "js", dir "js",
"01_errors.js", "01_errors.js",