diff options
Diffstat (limited to 'core/examples')
-rw-r--r-- | core/examples/hello_world.rs | 39 | ||||
-rw-r--r-- | core/examples/http_bench_json_ops.rs | 16 |
2 files changed, 35 insertions, 20 deletions
diff --git a/core/examples/hello_world.rs b/core/examples/hello_world.rs index fd6cabf2b..aa822917e 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<f64>, _: ()| { + // Sum inputs + let sum = nums.iter().fold(0.0, |a, v| a + v); + // return as a Result<f64, AnyError> + 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<f64>, _: ()| { - // Sum inputs - let sum = nums.iter().fold(0.0, |a, v| a + v); - // return as a Result<f64, AnyError> - 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 641483b0b..f1dfb1039 100644 --- a/core/examples/http_bench_json_ops.rs +++ b/core/examples/http_bench_json_ops.rs @@ -118,11 +118,17 @@ impl From<tokio::net::TcpStream> 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<ResourceId, Error> { |