From e55b448730160a6e4df9815a268d4049ac89deab Mon Sep 17 00:00:00 2001 From: Matt Mastracci Date: Fri, 17 Mar 2023 12:22:15 -0600 Subject: feat(core) deno_core::extension! macro to simplify extension registration (#18210) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This implements two macros to simplify extension registration and centralize a lot of the boilerplate as a base for future improvements: * `deno_core::ops!` registers a block of `#[op]`s, optionally with type parameters, useful for places where we share lists of ops * `deno_core::extension!` is used to register an extension, and creates two methods that can be used at runtime/snapshot generation time: `init_ops` and `init_ops_and_esm`. --------- Co-authored-by: Bartek IwaƄczuk --- ext/cache/lib.rs | 64 +++++++++++++++++++------------------------------------- 1 file changed, 21 insertions(+), 43 deletions(-) (limited to 'ext/cache') diff --git a/ext/cache/lib.rs b/ext/cache/lib.rs index a89296df9..eaf56c8b7 100644 --- a/ext/cache/lib.rs +++ b/ext/cache/lib.rs @@ -7,13 +7,10 @@ use std::sync::Arc; use async_trait::async_trait; use deno_core::error::AnyError; -use deno_core::include_js_files; use deno_core::op; 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; @@ -23,46 +20,27 @@ pub use sqlite::SqliteBackedCache; #[derive(Clone)] pub struct CreateCache(pub Arc C>); -fn ext() -> ExtensionBuilder { - Extension::builder_with_deps( - env!("CARGO_PKG_NAME"), - &["deno_webidl", "deno_web", "deno_url", "deno_fetch"], - ) -} - -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() -} +deno_core::extension!(deno_cache, + deps = [ deno_webidl, deno_web, deno_url, deno_fetch ], + parameters=[CA: Cache], + ops = [ + op_cache_storage_open, + op_cache_storage_has, + op_cache_storage_delete, + op_cache_put, + op_cache_match, + op_cache_delete, + ], + esm = [ "01_cache.js" ], + config = { + maybe_create_cache: Option>, + }, + state = |state, maybe_create_cache| { + if let Some(create_cache) = maybe_create_cache { + state.put(create_cache); + } + }, +); pub fn get_declaration() -> PathBuf { PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("lib.deno_cache.d.ts") -- cgit v1.2.3