From 92e8a292698f261651fe640510609713b0deed0c Mon Sep 17 00:00:00 2001 From: Aaron O'Mullan Date: Wed, 29 Dec 2021 15:21:42 +0100 Subject: [PATCH] cleanup(core): use Extensions to register ops (#13224) In examples and tests --- core/examples/hello_world.rs | 39 +++++++++++++++++----------- core/examples/http_bench_json_ops.rs | 16 ++++++++---- core/modules.rs | 10 ++++--- core/ops_json.rs | 13 +++++++--- core/runtime.rs | 31 ++++++++++++++-------- 5 files changed, 71 insertions(+), 38 deletions(-) diff --git a/core/examples/hello_world.rs b/core/examples/hello_world.rs index fd6cabf2b7..aa822917e1 100644 --- a/core/examples/hello_world.rs +++ b/core/examples/hello_world.rs @@ -3,25 +3,34 @@ //! JavaScript. use deno_core::op_sync; +use deno_core::Extension; use deno_core::JsRuntime; +use deno_core::RuntimeOptions; fn main() { - // Initialize a runtime instance - let mut runtime = JsRuntime::new(Default::default()); + // Build a deno_core::Extension providing custom ops + let ext = Extension::builder() + .ops(vec![ + // An op for summing an array of numbers + ( + "op_sum", + // The op-layer automatically deserializes inputs + // and serializes the returned Result & value + op_sync(|_state, nums: Vec, _: ()| { + // Sum inputs + let sum = nums.iter().fold(0.0, |a, v| a + v); + // return as a Result + Ok(sum) + }), + ), + ]) + .build(); - // Register an op for summing a number array. - runtime.register_op( - "op_sum", - // The op-layer automatically deserializes inputs - // and serializes the returned Result & value - op_sync(|_state, nums: Vec, _: ()| { - // Sum inputs - let sum = nums.iter().fold(0.0, |a, v| a + v); - // return as a Result - Ok(sum) - }), - ); - runtime.sync_ops_cache(); + // Initialize a runtime instance + let mut runtime = JsRuntime::new(RuntimeOptions { + extensions: vec![ext], + ..Default::default() + }); // Now we see how to invoke the op we just defined. The runtime automatically // contains a Deno.core object with several functions for interacting with it. diff --git a/core/examples/http_bench_json_ops.rs b/core/examples/http_bench_json_ops.rs index 641483b0b1..f1dfb1039f 100644 --- a/core/examples/http_bench_json_ops.rs +++ b/core/examples/http_bench_json_ops.rs @@ -118,11 +118,17 @@ impl From for TcpStream { } fn create_js_runtime() -> JsRuntime { - let mut runtime = JsRuntime::new(Default::default()); - runtime.register_op("listen", deno_core::op_sync(op_listen)); - runtime.register_op("accept", deno_core::op_async(op_accept)); - runtime.sync_ops_cache(); - runtime + let ext = deno_core::Extension::builder() + .ops(vec![ + ("listen", deno_core::op_sync(op_listen)), + ("accept", deno_core::op_async(op_accept)), + ]) + .build(); + + JsRuntime::new(deno_core::RuntimeOptions { + extensions: vec![ext], + ..Default::default() + }) } fn op_listen(state: &mut OpState, _: (), _: ()) -> Result { diff --git a/core/modules.rs b/core/modules.rs index 45330dffda..fc505c31ef 100644 --- a/core/modules.rs +++ b/core/modules.rs @@ -1075,6 +1075,7 @@ mod tests { use super::*; use crate::ops::OpCall; use crate::serialize_op_result; + use crate::Extension; use crate::JsRuntime; use crate::Op; use crate::OpPayload; @@ -1403,7 +1404,7 @@ import "/a.js"; let dispatch_count = Arc::new(AtomicUsize::new(0)); let dispatch_count_ = dispatch_count.clone(); - let dispatcher = move |state, payload: OpPayload| -> Op { + let op_test = move |state, payload: OpPayload| -> Op { dispatch_count_.fetch_add(1, Ordering::Relaxed); let (control, _): (u8, ()) = payload.deserialize().unwrap(); assert_eq!(control, 42); @@ -1411,12 +1412,15 @@ import "/a.js"; Op::Async(OpCall::ready(resp)) }; + let ext = Extension::builder() + .ops(vec![("op_test", Box::new(op_test))]) + .build(); + let mut runtime = JsRuntime::new(RuntimeOptions { + extensions: vec![ext], module_loader: Some(loader), ..Default::default() }); - runtime.register_op("op_test", dispatcher); - runtime.sync_ops_cache(); runtime .execute_script( diff --git a/core/ops_json.rs b/core/ops_json.rs index ad4aeeb470..b7e8331293 100644 --- a/core/ops_json.rs +++ b/core/ops_json.rs @@ -123,8 +123,6 @@ mod tests { #[tokio::test] async fn op_async_stack_trace() { - let mut runtime = crate::JsRuntime::new(Default::default()); - async fn op_throw( _state: Rc>, msg: Option, @@ -134,8 +132,15 @@ mod tests { Err(crate::error::generic_error("foo")) } - runtime.register_op("op_throw", op_async(op_throw)); - runtime.sync_ops_cache(); + let ext = crate::Extension::builder() + .ops(vec![("op_throw", op_async(op_throw))]) + .build(); + + let mut runtime = crate::JsRuntime::new(crate::RuntimeOptions { + extensions: vec![ext], + ..Default::default() + }); + runtime .execute_script( "", diff --git a/core/runtime.rs b/core/runtime.rs index 643a4198de..d1b7db14ca 100644 --- a/core/runtime.rs +++ b/core/runtime.rs @@ -1651,6 +1651,7 @@ pub mod tests { futures::executor::block_on(lazy(move |cx| f(cx))); } + #[derive(Copy, Clone)] enum Mode { Async, AsyncZeroCopy(bool), @@ -1661,7 +1662,7 @@ pub mod tests { dispatch_count: Arc, } - fn dispatch(rc_op_state: Rc>, payload: OpPayload) -> Op { + fn op_test(rc_op_state: Rc>, payload: OpPayload) -> Op { let rc_op_state2 = rc_op_state.clone(); let op_state_ = rc_op_state2.borrow(); let test_state = op_state_.borrow::(); @@ -1687,16 +1688,22 @@ pub mod tests { fn setup(mode: Mode) -> (JsRuntime, Arc) { let dispatch_count = Arc::new(AtomicUsize::new(0)); - let mut runtime = JsRuntime::new(Default::default()); - let op_state = runtime.op_state(); - op_state.borrow_mut().put(TestState { - mode, - dispatch_count: dispatch_count.clone(), + let dispatch_count2 = dispatch_count.clone(); + let ext = Extension::builder() + .ops(vec![("op_test", Box::new(op_test))]) + .state(move |state| { + state.put(TestState { + mode, + dispatch_count: dispatch_count2.clone(), + }); + Ok(()) + }) + .build(); + let mut runtime = JsRuntime::new(RuntimeOptions { + extensions: vec![ext], + ..Default::default() }); - runtime.register_op("op_test", dispatch); - runtime.sync_ops_cache(); - runtime .execute_script( "setup.js", @@ -2029,12 +2036,14 @@ pub mod tests { } run_in_task(|cx| { + let ext = Extension::builder() + .ops(vec![("op_err", op_sync(op_err))]) + .build(); let mut runtime = JsRuntime::new(RuntimeOptions { + extensions: vec![ext], get_error_class_fn: Some(&get_error_class_name), ..Default::default() }); - runtime.register_op("op_err", op_sync(op_err)); - runtime.sync_ops_cache(); runtime .execute_script( "error_builder_test.js",