From 8f207c0f3f3a43d77e0c88cfdc840b4b742b9708 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartek=20Iwa=C5=84czuk?= Date: Thu, 9 Mar 2023 10:56:19 -0400 Subject: refactor: Split extension registration for runtime and snapshotting (#18095) This commit splits "::init" functions into "init_ops" and "init_ops_and_esm". That way we don't have to construct list of ESM sources on each startup if we're running with a snapshot. In a follow up commit "deno_core" will be changed to not have a split between "extensions" and "extensions_with_js" - it will be embedders' responsibility to pass appropriately configured extensions. Prerequisite for https://github.com/denoland/deno/pull/18080 --- ext/cache/lib.rs | 54 ++++++++++++++++++++++++++++++++++++------------------ 1 file changed, 36 insertions(+), 18 deletions(-) (limited to 'ext/cache') diff --git a/ext/cache/lib.rs b/ext/cache/lib.rs index 477bbcb3e..a89296df9 100644 --- a/ext/cache/lib.rs +++ b/ext/cache/lib.rs @@ -13,6 +13,7 @@ use deno_core::serde::Deserialize; use deno_core::serde::Serialize; use deno_core::ByteString; use deno_core::Extension; +use deno_core::ExtensionBuilder; use deno_core::OpState; use deno_core::Resource; use deno_core::ResourceId; @@ -22,28 +23,45 @@ pub use sqlite::SqliteBackedCache; #[derive(Clone)] pub struct CreateCache(pub Arc C>); -pub fn init( - maybe_create_cache: Option>, -) -> Extension { +fn ext() -> ExtensionBuilder { Extension::builder_with_deps( env!("CARGO_PKG_NAME"), &["deno_webidl", "deno_web", "deno_url", "deno_fetch"], ) - .esm(include_js_files!("01_cache.js",)) - .ops(vec![ - op_cache_storage_open::decl::(), - op_cache_storage_has::decl::(), - op_cache_storage_delete::decl::(), - op_cache_put::decl::(), - op_cache_match::decl::(), - op_cache_delete::decl::(), - ]) - .state(move |state| { - if let Some(create_cache) = maybe_create_cache.clone() { - state.put(create_cache); - } - }) - .build() +} + +fn ops( + ext: &mut ExtensionBuilder, + maybe_create_cache: Option>, +) -> &mut ExtensionBuilder { + ext + .ops(vec![ + op_cache_storage_open::decl::(), + op_cache_storage_has::decl::(), + op_cache_storage_delete::decl::(), + op_cache_put::decl::(), + op_cache_match::decl::(), + op_cache_delete::decl::(), + ]) + .state(move |state| { + if let Some(create_cache) = maybe_create_cache.clone() { + state.put(create_cache); + } + }) +} + +pub fn init_ops_and_esm( + maybe_create_cache: Option>, +) -> Extension { + ops::(&mut ext(), maybe_create_cache) + .esm(include_js_files!("01_cache.js",)) + .build() +} + +pub fn init_ops( + maybe_create_cache: Option>, +) -> Extension { + ops::(&mut ext(), maybe_create_cache).build() } pub fn get_declaration() -> PathBuf { -- cgit v1.2.3