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 --- cli/ops/mod.rs | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) (limited to 'cli/ops/mod.rs') diff --git a/cli/ops/mod.rs b/cli/ops/mod.rs index 53f74ad1f..562aa8649 100644 --- a/cli/ops/mod.rs +++ b/cli/ops/mod.rs @@ -10,17 +10,18 @@ pub mod bench; pub mod testing; pub fn cli_exts(ps: ProcState) -> Vec { - vec![init_proc_state(ps)] + vec![deno_cli::init_ops(ps)] } -fn init_proc_state(ps: ProcState) -> Extension { - Extension::builder("deno_cli") - .ops(vec![op_npm_process_state::decl()]) - .state(move |state| { - state.put(ps.clone()); - }) - .build() -} +deno_core::extension!(deno_cli, + ops = [op_npm_process_state], + config = { + ps: ProcState, + }, + state = |state, ps| { + state.put(ps); + }, +); #[op] fn op_npm_process_state(state: &mut OpState) -> Result { -- cgit v1.2.3