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/lsp/testing/execution.rs | 6 +++++- cli/lsp/tsc.rs | 42 +++++++++++++++++++++--------------------- 2 files changed, 26 insertions(+), 22 deletions(-) (limited to 'cli/lsp') diff --git a/cli/lsp/testing/execution.rs b/cli/lsp/testing/execution.rs index af890cc0d..ee182c690 100644 --- a/cli/lsp/testing/execution.rs +++ b/cli/lsp/testing/execution.rs @@ -165,7 +165,11 @@ async fn test_specifier( &ps, specifier.clone(), PermissionsContainer::new(permissions), - vec![ops::testing::init(sender, fail_fast_tracker, filter)], + vec![ops::testing::deno_test::init_ops( + sender, + fail_fast_tracker, + filter, + )], Stdio { stdin: StdioPipe::Inherit, stdout, diff --git a/cli/lsp/tsc.rs b/cli/lsp/tsc.rs index 335d91ed7..910d13691 100644 --- a/cli/lsp/tsc.rs +++ b/cli/lsp/tsc.rs @@ -37,7 +37,6 @@ use deno_core::serde_json; use deno_core::serde_json::json; use deno_core::serde_json::Value; use deno_core::url::Url; -use deno_core::Extension; use deno_core::JsRuntime; use deno_core::ModuleSpecifier; use deno_core::OpState; @@ -2819,31 +2818,32 @@ fn op_script_version( /// server. fn js_runtime(performance: Arc) -> JsRuntime { JsRuntime::new(RuntimeOptions { - extensions: vec![init_extension(performance)], + extensions: vec![deno_tsc::init_ops(performance)], startup_snapshot: Some(tsc::compiler_snapshot()), ..Default::default() }) } -fn init_extension(performance: Arc) -> Extension { - Extension::builder("deno_tsc") - .ops(vec![ - op_is_cancelled::decl(), - op_is_node_file::decl(), - op_load::decl(), - op_resolve::decl(), - op_respond::decl(), - op_script_names::decl(), - op_script_version::decl(), - ]) - .state(move |state| { - state.put(State::new( - Arc::new(StateSnapshot::default()), - performance.clone(), - )); - }) - .build() -} +deno_core::extension!(deno_tsc, + ops = [ + op_is_cancelled, + op_is_node_file, + op_load, + op_resolve, + op_respond, + op_script_names, + op_script_version, + ], + config = { + performance: Arc + }, + state = |state, performance| { + state.put(State::new( + Arc::new(StateSnapshot::default()), + performance, + )); + }, +); /// Instruct a language server runtime to start the language server and provide /// it with a minimal bootstrap configuration. -- cgit v1.2.3