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/flash/lib.rs | 111 +++++++++++++++++++++++-------------------------------- 1 file changed, 46 insertions(+), 65 deletions(-) (limited to 'ext/flash/lib.rs') diff --git a/ext/flash/lib.rs b/ext/flash/lib.rs index 77b8b6791..6f11e14af 100644 --- a/ext/flash/lib.rs +++ b/ext/flash/lib.rs @@ -16,8 +16,6 @@ use deno_core::v8::fast_api; use deno_core::ByteString; use deno_core::CancelFuture; use deno_core::CancelHandle; -use deno_core::Extension; -use deno_core::ExtensionBuilder; use deno_core::OpState; use deno_core::StringOrBuffer; use deno_core::ZeroCopyBuf; @@ -1527,66 +1525,49 @@ pub trait FlashPermissions { ) -> Result<(), AnyError>; } -fn ext() -> ExtensionBuilder { - Extension::builder_with_deps( - env!("CARGO_PKG_NAME"), - &[ - "deno_web", - "deno_net", - "deno_fetch", - "deno_websocket", - "deno_http", - ], - ) -} - -fn ops( - ext: &mut ExtensionBuilder, - unstable: bool, -) -> &mut ExtensionBuilder { - ext - .ops(vec![ - op_flash_serve::decl::

(), - op_node_unstable_flash_serve::decl::

(), - op_flash_respond::decl(), - op_flash_respond_async::decl(), - op_flash_respond_chunked::decl(), - op_flash_method::decl(), - op_flash_path::decl(), - op_flash_headers::decl(), - op_flash_addr::decl(), - op_flash_next::decl(), - op_flash_next_server::decl(), - op_flash_next_async::decl(), - op_flash_read_body::decl(), - op_flash_upgrade_websocket::decl(), - op_flash_drive_server::decl(), - op_flash_wait_for_listening::decl(), - op_flash_first_packet::decl(), - op_flash_has_body_stream::decl(), - op_flash_close_server::decl(), - op_flash_make_request::decl(), - op_flash_write_resource::decl(), - op_try_flash_respond_chunked::decl(), - ]) - .state(move |op_state| { - op_state.put(Unstable(unstable)); - op_state.put(FlashContext { - next_server_id: 0, - join_handles: HashMap::default(), - servers: HashMap::default(), - }); - }) -} - -pub fn init_ops_and_esm( - unstable: bool, -) -> Extension { - ops::

(&mut ext(), unstable) - .esm(deno_core::include_js_files!("01_http.js",)) - .build() -} - -pub fn init_ops(unstable: bool) -> Extension { - ops::

(&mut ext(), unstable).build() -} +deno_core::extension!(deno_flash, + deps = [ + deno_web, + deno_net, + deno_fetch, + deno_websocket, + deno_http + ], + parameters = [P: FlashPermissions], + ops = [ + op_flash_serve

, + op_node_unstable_flash_serve

, + op_flash_respond, + op_flash_respond_async, + op_flash_respond_chunked, + op_flash_method, + op_flash_path, + op_flash_headers, + op_flash_addr, + op_flash_next, + op_flash_next_server, + op_flash_next_async, + op_flash_read_body, + op_flash_upgrade_websocket, + op_flash_drive_server, + op_flash_wait_for_listening, + op_flash_first_packet, + op_flash_has_body_stream, + op_flash_close_server, + op_flash_make_request, + op_flash_write_resource, + op_try_flash_respond_chunked, + ], + esm = [ "01_http.js" ], + config = { + unstable: bool, + }, + state = |state, unstable| { + state.put(Unstable(unstable)); + state.put(FlashContext { + next_server_id: 0, + join_handles: HashMap::default(), + servers: HashMap::default(), + }); + }, +); -- cgit v1.2.3