diff options
Diffstat (limited to 'core/examples/hello_world.rs')
-rw-r--r-- | core/examples/hello_world.rs | 39 |
1 files changed, 24 insertions, 15 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. |