Skip to content

Commit

Permalink
wasm: add an example for wasm operator
Browse files Browse the repository at this point in the history
  • Loading branch information
Nouzan committed Oct 10, 2023
1 parent f16d697 commit 89add5f
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 3 deletions.
10 changes: 8 additions & 2 deletions examples/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,24 @@ repository.workspace = true
keywords.workspace = true
rust-version.workspace = true

[lib]
crate-type = ["cdylib", "rlib"]

[features]
context = ["indicator/context"]
wasm = ["wasm-bindgen", "context"]

[dependencies]
indicator = { path = "../crates/indicator", default-features = false }
wasm-bindgen = { version = "0.2.87", optional = true }

derive_more = { version = "0.99.11", features = ["as_ref", "from"] }
num = "0.4.1"

[dev-dependencies]
anyhow = "1.0.75"
rust_decimal.workspace = true
rust_decimal_macros.workspace = true
num = "0.4.1"
derive_more = { version = "0.99.11", features = ["as_ref", "from"] }

[[example]]
name = "context"
Expand Down
3 changes: 2 additions & 1 deletion examples/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@

#[cfg(feature = "wasm")]
mod wasm;
45 changes: 45 additions & 0 deletions examples/src/wasm.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
use indicator::prelude::*;
use num::Num;
use wasm_bindgen::prelude::*;

#[operator(input = I, generate_out_with_data)]
fn ma<T>(
#[input] x: &T,
#[data(as_ref?)] prev: Option<&T>,
#[data(as_ref)] alpha: &T,
) -> (T, Option<T>)
where
T: Num + Clone,
{
let prev = prev.cloned().unwrap_or_else(|| x.clone());
let out = x.clone() * alpha.clone() + prev.clone() * (T::one() - alpha.clone());
(out.clone(), Some(out))
}

/// Wasm Ma Operator.
#[wasm_bindgen]
pub struct Ma(BoxOperator<'static, f64, f64>);

#[wasm_bindgen]
impl Ma {
pub fn new(alpha: f64) -> Self {
use derive_more::{AsRef, From};

#[derive(AsRef, Clone)]
struct Alpha(f64);

#[derive(AsRef, From)]
struct State(f64);

let op = insert_and_output(ma::<f64, _, State, Alpha, f64, State>)
.provide(Alpha(alpha))
.finish()
.boxed();

Ma(op)
}

pub fn next(&mut self, x: f64) -> f64 {
self.0.next(x)
}
}
25 changes: 25 additions & 0 deletions examples/wasm/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Example: WASM Operator

`indicator` supports `WebAssembly`, and here is an example for building an
operator with `indicator` and
[`wasm_bindgen`](https://crates.io/crates/wasm-bindgen) and compiling it to
WebAssembly.

The source code of the operator can be found [here](../src/wasm.rs).

## Requirements

To run this example, make sure that you have installed
[`wasm-pack`](https://rustwasm.github.io/wasm-pack/installer/) and
[`deno`](https://deno.com/).

## Running

Run the following code under the `examples/wasm` dir:

```bash
deno task run
```

It will start compiling the operator to a wasm with `wasm-pack` and run a
`TypeScript` script that import and use it after the compilation.
6 changes: 6 additions & 0 deletions examples/wasm/deno.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"tasks": {
"build": "wasm-pack build -d ./wasm/pkg -t deno ../ --features wasm",
"run": "deno task build && deno run -A main.ts"
}
}
7 changes: 7 additions & 0 deletions examples/wasm/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { Ma } from "./pkg/examples.js";

const ma = Ma.new(0.1);

for (let i = 0; i < 10; i++) {
console.log(ma.next(i));
}

0 comments on commit 89add5f

Please sign in to comment.