diff options
Diffstat (limited to 'core/examples/hello_world.rs')
-rw-r--r-- | core/examples/hello_world.rs | 34 |
1 files changed, 22 insertions, 12 deletions
diff --git a/core/examples/hello_world.rs b/core/examples/hello_world.rs index 42c9779f3..bfca5447c 100644 --- a/core/examples/hello_world.rs +++ b/core/examples/hello_world.rs @@ -2,27 +2,37 @@ //! This example shows you how to define ops in Rust and then call them from //! JavaScript. -use deno_core::op_sync; +use deno_core::op; use deno_core::Extension; use deno_core::JsRuntime; +use deno_core::OpState; use deno_core::RuntimeOptions; +// This is a hack to make the `#[op]` macro work with +// deno_core examples. +// You can remove this: +use deno_core::*; + +#[op] +fn op_sum( + _state: &mut OpState, + nums: Vec<f64>, + _: (), +) -> Result<f64, deno_core::error::AnyError> { + // Sum inputs + let sum = nums.iter().fold(0.0, |a, v| a + v); + // return as a Result<f64, AnyError> + Ok(sum) +} + fn main() { // 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) - }), - ), + // The op-layer automatically deserializes inputs + // and serializes the returned Result & value + op_sum::decl(), ]) .build(); |