summaryrefslogtreecommitdiff
path: root/ext/webstorage/lib.rs
diff options
context:
space:
mode:
authorMatt Mastracci <matthew@mastracci.com>2023-03-17 12:22:15 -0600
committerGitHub <noreply@github.com>2023-03-17 18:22:15 +0000
commite55b448730160a6e4df9815a268d4049ac89deab (patch)
tree35d80fd60f2f1d1d06903caff256484a7d703d76 /ext/webstorage/lib.rs
parent0bc6bf5d33b8198253954d7f04558270de45c925 (diff)
feat(core) deno_core::extension! macro to simplify extension registration (#18210)
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 <biwanczuk@gmail.com>
Diffstat (limited to 'ext/webstorage/lib.rs')
-rw-r--r--ext/webstorage/lib.rs58
1 files changed, 21 insertions, 37 deletions
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<PathBuf>,
-) -> &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<PathBuf>) -> Extension {
- ops(&mut ext(), origin_storage_dir)
- .esm(include_js_files!("01_webstorage.js",))
- .build()
-}
-
-pub fn init_ops(origin_storage_dir: Option<PathBuf>) -> 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<PathBuf>
+ },
+ 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")