diff options
| author | Matt Mastracci <matthew@mastracci.com> | 2023-03-17 12:22:15 -0600 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-03-17 18:22:15 +0000 |
| commit | e55b448730160a6e4df9815a268d4049ac89deab (patch) | |
| tree | 35d80fd60f2f1d1d06903caff256484a7d703d76 /ext/url | |
| parent | 0bc6bf5d33b8198253954d7f04558270de45c925 (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/url')
| -rw-r--r-- | ext/url/benches/url_ops.rs | 4 | ||||
| -rw-r--r-- | ext/url/lib.rs | 44 |
2 files changed, 17 insertions, 31 deletions
diff --git a/ext/url/benches/url_ops.rs b/ext/url/benches/url_ops.rs index 7dc2651b2..4096a6a92 100644 --- a/ext/url/benches/url_ops.rs +++ b/ext/url/benches/url_ops.rs @@ -11,8 +11,8 @@ use deno_core::ExtensionFileSourceCode; fn setup() -> Vec<Extension> { vec![ - deno_webidl::init_esm(), - deno_url::init_ops_and_esm(), + deno_webidl::deno_webidl::init_ops_and_esm(), + deno_url::deno_url::init_ops_and_esm(), Extension::builder("bench_setup") .esm(vec![ExtensionFileSource { specifier: "ext:setup".to_string(), diff --git a/ext/url/lib.rs b/ext/url/lib.rs index 8a20c9bc6..cfb6497c3 100644 --- a/ext/url/lib.rs +++ b/ext/url/lib.rs @@ -4,13 +4,10 @@ mod urlpattern; use deno_core::error::type_error; use deno_core::error::AnyError; -use deno_core::include_js_files; use deno_core::op; use deno_core::url::form_urlencoded; use deno_core::url::quirks; use deno_core::url::Url; -use deno_core::Extension; -use deno_core::ExtensionBuilder; use deno_core::OpState; use deno_core::ZeroCopyBuf; use std::path::PathBuf; @@ -18,32 +15,21 @@ use std::path::PathBuf; use crate::urlpattern::op_urlpattern_parse; use crate::urlpattern::op_urlpattern_process_match_input; -fn ext() -> ExtensionBuilder { - Extension::builder_with_deps(env!("CARGO_PKG_NAME"), &["deno_webidl"]) -} - -fn ops(ext: &mut ExtensionBuilder) -> &mut ExtensionBuilder { - ext.ops(vec![ - op_url_reparse::decl(), - op_url_parse::decl(), - op_url_get_serialization::decl(), - op_url_parse_with_base::decl(), - op_url_parse_search_params::decl(), - op_url_stringify_search_params::decl(), - op_urlpattern_parse::decl(), - op_urlpattern_process_match_input::decl(), - ]) -} - -pub fn init_ops_and_esm() -> Extension { - ops(&mut ext()) - .esm(include_js_files!("00_url.js", "01_urlpattern.js",)) - .build() -} - -pub fn init_ops() -> Extension { - ops(&mut ext()).build() -} +deno_core::extension!( + deno_url, + deps = [deno_webidl], + ops = [ + op_url_reparse, + op_url_parse, + op_url_get_serialization, + op_url_parse_with_base, + op_url_parse_search_params, + op_url_stringify_search_params, + op_urlpattern_parse, + op_urlpattern_process_match_input + ], + esm = ["00_url.js", "01_urlpattern.js"], +); /// Parse `href` with a `base_href`. Fills the out `buf` with URL components. #[op] |
