diff options
| author | Divy Srivastava <dj.srivastava23@gmail.com> | 2022-03-14 23:14:15 +0530 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-03-14 18:44:15 +0100 |
| commit | b4e42953e1d243f2eda20e5be6b845d60b7bf688 (patch) | |
| tree | 10b3bfff165f9c04f9174c7c399d44b9b724c3b3 /core/examples/hello_world.rs | |
| parent | 4e3ed37037a2aa1edeac260dc3463a81d9cf9b88 (diff) | |
feat(core): codegen ops (#13861)
Co-authored-by: Aaron O'Mullan <aaron.omullan@gmail.com>
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(); |
