From b4e42953e1d243f2eda20e5be6b845d60b7bf688 Mon Sep 17 00:00:00 2001 From: Divy Srivastava Date: Mon, 14 Mar 2022 23:14:15 +0530 Subject: feat(core): codegen ops (#13861) Co-authored-by: Aaron O'Mullan --- core/examples/hello_world.rs | 34 ++++++++++++++++++++++------------ 1 file changed, 22 insertions(+), 12 deletions(-) (limited to 'core/examples/hello_world.rs') 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, + _: (), +) -> Result { + // Sum inputs + let sum = nums.iter().fold(0.0, |a, v| a + v); + // return as a Result + 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, _: ()| { - // Sum inputs - let sum = nums.iter().fold(0.0, |a, v| a + v); - // return as a Result - Ok(sum) - }), - ), + // The op-layer automatically deserializes inputs + // and serializes the returned Result & value + op_sum::decl(), ]) .build(); -- cgit v1.2.3