-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
wasm: add an example for wasm operator
- Loading branch information
Nouzan
committed
Oct 10, 2023
1 parent
f16d697
commit 89add5f
Showing
6 changed files
with
93 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
|
||
#[cfg(feature = "wasm")] | ||
mod wasm; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} |