diff options
author | Aaron O'Mullan <aaron.omullan@gmail.com> | 2021-12-29 15:21:42 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-12-29 15:21:42 +0100 |
commit | 92e8a292698f261651fe640510609713b0deed0c (patch) | |
tree | d81b227339ca699f534467bf5a6d2118a8dfbe5b /core/examples/hello_world.rs | |
parent | b33bbf6af5749edb5d5e2e8de3b8d91416a29684 (diff) |
cleanup(core): use Extensions to register ops (#13224)
In examples and tests
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. |