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/webstorage/lib.rs | 58 +++++++++++++++++++-------------------------------- 1 file changed, 21 insertions(+), 37 deletions(-) (limited to 'ext/webstorage/lib.rs') diff --git a/ext/webstorage/lib.rs b/ext/webstorage/lib.rs index f3caddbaf..6cdc7bbff 100644 --- a/ext/webstorage/lib.rs +++ b/ext/webstorage/lib.rs @@ -6,10 +6,7 @@ use std::fmt; use std::path::PathBuf; use deno_core::error::AnyError; -use deno_core::include_js_files; use deno_core::op; -use deno_core::Extension; -use deno_core::ExtensionBuilder; use deno_core::OpState; use rusqlite::params; use rusqlite::Connection; @@ -22,40 +19,27 @@ struct OriginStorageDir(PathBuf); const MAX_STORAGE_BYTES: usize = 10 * 1024 * 1024; -fn ext() -> ExtensionBuilder { - Extension::builder_with_deps(env!("CARGO_PKG_NAME"), &["deno_webidl"]) -} - -fn ops( - ext: &mut ExtensionBuilder, - origin_storage_dir: Option, -) -> &mut ExtensionBuilder { - ext - .ops(vec![ - op_webstorage_length::decl(), - op_webstorage_key::decl(), - op_webstorage_set::decl(), - op_webstorage_get::decl(), - op_webstorage_remove::decl(), - op_webstorage_clear::decl(), - op_webstorage_iterate_keys::decl(), - ]) - .state(move |state| { - if let Some(origin_storage_dir) = &origin_storage_dir { - state.put(OriginStorageDir(origin_storage_dir.clone())); - } - }) -} - -pub fn init_ops_and_esm(origin_storage_dir: Option) -> Extension { - ops(&mut ext(), origin_storage_dir) - .esm(include_js_files!("01_webstorage.js",)) - .build() -} - -pub fn init_ops(origin_storage_dir: Option) -> Extension { - ops(&mut ext(), origin_storage_dir).build() -} +deno_core::extension!(deno_webstorage, + deps = [ deno_webidl ], + ops = [ + op_webstorage_length, + op_webstorage_key, + op_webstorage_set, + op_webstorage_get, + op_webstorage_remove, + op_webstorage_clear, + op_webstorage_iterate_keys, + ], + esm = [ "01_webstorage.js" ], + config = { + origin_storage_dir: Option + }, + state = |state, origin_storage_dir| { + if let Some(origin_storage_dir) = origin_storage_dir { + state.put(OriginStorageDir(origin_storage_dir)); + } + }, +); pub fn get_declaration() -> PathBuf { PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("lib.deno_webstorage.d.ts") -- cgit v1.2.3