From 644bcd69970b37fe3f2f492961fc24ecc34d1bc4 Mon Sep 17 00:00:00 2001 From: Max Richter Date: Wed, 10 Apr 2024 23:50:41 +0200 Subject: [PATCH] feat: add array and sum node --- Cargo.lock | 36 ++++++++++++++ app/src/lib/helpers/flat_tree.ts | 1 - app/src/lib/node-registry.ts | 5 ++ app/src/lib/runtime-executor.ts | 7 ++- app/src/routes/test/+page.svelte | 3 +- nodes/max/plantarium/array/.gitignore | 6 +++ nodes/max/plantarium/array/Cargo.toml | 30 ++++++++++++ nodes/max/plantarium/array/package.json | 5 ++ nodes/max/plantarium/array/src/lib.rs | 44 +++++++++++++++++ nodes/max/plantarium/array/src/utils.rs | 10 ++++ nodes/max/plantarium/array/tests/web.rs | 13 +++++ nodes/max/plantarium/output/src/lib.rs | 8 ++-- nodes/max/plantarium/sum/.gitignore | 6 +++ nodes/max/plantarium/sum/Cargo.toml | 30 ++++++++++++ nodes/max/plantarium/sum/package.json | 5 ++ nodes/max/plantarium/sum/src/lib.rs | 33 +++++++++++++ nodes/max/plantarium/sum/src/utils.rs | 10 ++++ nodes/max/plantarium/sum/tests/web.rs | 13 +++++ nodes/max/plantarium/triangle/.gitignore | 6 +++ nodes/max/plantarium/triangle/Cargo.toml | 30 ++++++++++++ nodes/max/plantarium/triangle/package.json | 5 ++ nodes/max/plantarium/triangle/src/lib.rs | 48 +++++++++++++++++++ nodes/max/plantarium/triangle/src/utils.rs | 10 ++++ nodes/max/plantarium/triangle/tests/web.rs | 13 +++++ .../graph-interface/src/lib/edges/Edge.svelte | 2 +- packages/node-registry/src/lib/registry.ts | 9 +++- 26 files changed, 377 insertions(+), 11 deletions(-) create mode 100644 nodes/max/plantarium/array/.gitignore create mode 100644 nodes/max/plantarium/array/Cargo.toml create mode 100644 nodes/max/plantarium/array/package.json create mode 100644 nodes/max/plantarium/array/src/lib.rs create mode 100644 nodes/max/plantarium/array/src/utils.rs create mode 100644 nodes/max/plantarium/array/tests/web.rs create mode 100644 nodes/max/plantarium/sum/.gitignore create mode 100644 nodes/max/plantarium/sum/Cargo.toml create mode 100644 nodes/max/plantarium/sum/package.json create mode 100644 nodes/max/plantarium/sum/src/lib.rs create mode 100644 nodes/max/plantarium/sum/src/utils.rs create mode 100644 nodes/max/plantarium/sum/tests/web.rs create mode 100644 nodes/max/plantarium/triangle/.gitignore create mode 100644 nodes/max/plantarium/triangle/Cargo.toml create mode 100644 nodes/max/plantarium/triangle/package.json create mode 100644 nodes/max/plantarium/triangle/src/lib.rs create mode 100644 nodes/max/plantarium/triangle/src/utils.rs create mode 100644 nodes/max/plantarium/triangle/tests/web.rs diff --git a/Cargo.lock b/Cargo.lock index 84ceb68..9a67fda 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -80,6 +80,18 @@ version = "1.3.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7d5a26814d8dcb93b0e5a0ff3c6d80a8843bafb21b39e8e18a6f05471870e110" +[[package]] +name = "array" +version = "0.1.0" +dependencies = [ + "console_error_panic_hook", + "plantarium", + "serde", + "serde-wasm-bindgen", + "wasm-bindgen", + "wasm-bindgen-test", +] + [[package]] name = "async-trait" version = "0.1.79" @@ -3305,6 +3317,18 @@ version = "0.10.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623" +[[package]] +name = "sum" +version = "0.1.0" +dependencies = [ + "console_error_panic_hook", + "plantarium", + "serde", + "serde-wasm-bindgen", + "wasm-bindgen", + "wasm-bindgen-test", +] + [[package]] name = "swift-rs" version = "1.0.6" @@ -3969,6 +3993,18 @@ dependencies = [ "serde_json", ] +[[package]] +name = "triangle" +version = "0.1.0" +dependencies = [ + "console_error_panic_hook", + "plantarium", + "serde", + "serde-wasm-bindgen", + "wasm-bindgen", + "wasm-bindgen-test", +] + [[package]] name = "try-lock" version = "0.2.5" diff --git a/app/src/lib/helpers/flat_tree.ts b/app/src/lib/helpers/flat_tree.ts index e7ad64f..96f65de 100644 --- a/app/src/lib/helpers/flat_tree.ts +++ b/app/src/lib/helpers/flat_tree.ts @@ -1,4 +1,3 @@ - type SparseArray = (T | T[] | SparseArray)[]; // Encodes a nested array into a flat array with bracket and distance notation diff --git a/app/src/lib/node-registry.ts b/app/src/lib/node-registry.ts index 501126e..c9b292a 100644 --- a/app/src/lib/node-registry.ts +++ b/app/src/lib/node-registry.ts @@ -51,6 +51,9 @@ export class RemoteNodeRegistry implements NodeRegistry { const a = performance.now(); nodeIds.push("max/plantarium/random"); nodeIds.push("max/plantarium/float"); + nodeIds.push("max/plantarium/array"); + nodeIds.push("max/plantarium/sum"); + for (const id of nodeIds) { const nodeUrl = `${this.url}/n/${id}`; const response = await fetch(nodeUrl); @@ -96,6 +99,8 @@ wasm = val;`); export class MemoryNodeRegistry implements NodeRegistry { + status: "loading" | "ready" | "error" = "ready"; + async load(nodeIds: string[]) { // Do nothing } diff --git a/app/src/lib/runtime-executor.ts b/app/src/lib/runtime-executor.ts index ab11ad1..6d99ef2 100644 --- a/app/src/lib/runtime-executor.ts +++ b/app/src/lib/runtime-executor.ts @@ -135,7 +135,12 @@ export class MemoryRuntimeExecutor implements RuntimeExecutor { } // execute the node and store the result - results[node.id] = node.tmp.type.execute(...Object.values(inputs)) as number; + try { + console.log(`Executing node ${node.tmp.type.id || node.id}`, inputs); + results[node.id] = node.tmp.type.execute(...Object.values(inputs)) as number; + } catch (e) { + console.error(`Error executing node ${node.tmp.type.id || node.id}`, e); + } } } diff --git a/app/src/routes/test/+page.svelte b/app/src/routes/test/+page.svelte index 9978819..294188a 100644 --- a/app/src/routes/test/+page.svelte +++ b/app/src/routes/test/+page.svelte @@ -8,7 +8,8 @@ console.log("INPUT"); console.log(input); - const encoded = encode(input); + let encoded = encode(input); + encoded = [0, 3, 5, ...encoded.slice(2).slice(0, -4), 5, 5, 1, 1]; console.log("ENCODED"); console.log(encoded); diff --git a/nodes/max/plantarium/array/.gitignore b/nodes/max/plantarium/array/.gitignore new file mode 100644 index 0000000..4e30131 --- /dev/null +++ b/nodes/max/plantarium/array/.gitignore @@ -0,0 +1,6 @@ +/target +**/*.rs.bk +Cargo.lock +bin/ +pkg/ +wasm-pack.log diff --git a/nodes/max/plantarium/array/Cargo.toml b/nodes/max/plantarium/array/Cargo.toml new file mode 100644 index 0000000..38c1803 --- /dev/null +++ b/nodes/max/plantarium/array/Cargo.toml @@ -0,0 +1,30 @@ +[package] +name = "array" +version = "0.1.0" +authors = ["Max Richter "] +edition = "2018" + +[lib] +crate-type = ["cdylib", "rlib"] + +[features] +default = ["console_error_panic_hook"] + +[dependencies] +wasm-bindgen = "0.2.84" + +# The `console_error_panic_hook` crate provides better debugging of panics by +# logging them with `console.error`. This is great for development, but requires +# all the `std::fmt` and `std::panicking` infrastructure, so isn't great for +# code size when deploying. +plantarium = { version = "0.1.0", path = "../../../../packages/plantarium" } +serde = { version = "1.0", features = ["derive"] } +serde-wasm-bindgen = "0.4" +console_error_panic_hook = { version = "0.1.7", optional = true } + +[dev-dependencies] +wasm-bindgen-test = "0.3.34" + +[profile.release] +# Tell `rustc` to optimize for small code size. +opt-level = "s" diff --git a/nodes/max/plantarium/array/package.json b/nodes/max/plantarium/array/package.json new file mode 100644 index 0000000..bc6fe14 --- /dev/null +++ b/nodes/max/plantarium/array/package.json @@ -0,0 +1,5 @@ +{ + "scripts": { + "build": "wasm-pack build --release --out-name index --no-default-features" + } +} diff --git a/nodes/max/plantarium/array/src/lib.rs b/nodes/max/plantarium/array/src/lib.rs new file mode 100644 index 0000000..487f6ce --- /dev/null +++ b/nodes/max/plantarium/array/src/lib.rs @@ -0,0 +1,44 @@ +mod utils; +use plantarium::{evaluate_parameters, unwrap_int, unwrap_string}; +use wasm_bindgen::prelude::*; + +// lifted from the `console_log` example +#[wasm_bindgen] +extern "C" { + #[wasm_bindgen(js_namespace = console)] + fn log(s: &str); +} + +#[wasm_bindgen] +pub fn get_outputs() -> Vec { + vec!["float".to_string()] +} + +#[wasm_bindgen] +pub fn get_input_types() -> String { + utils::set_panic_hook(); + r#"{ + "length": { "type": "float", "value": 2 } + }"# + .to_string() +} + +#[rustfmt::skip] +#[wasm_bindgen] +pub fn execute(var_length: i32) -> Vec { + utils::set_panic_hook(); + + + let length = var_length;//evaluate_parameters(var_length); + + // construct array of length + let mut res = Vec::new(); + for _ in 0..length as usize { + res.push(2.0); + } + + log("executing array"); + + return res; + +} diff --git a/nodes/max/plantarium/array/src/utils.rs b/nodes/max/plantarium/array/src/utils.rs new file mode 100644 index 0000000..b1d7929 --- /dev/null +++ b/nodes/max/plantarium/array/src/utils.rs @@ -0,0 +1,10 @@ +pub fn set_panic_hook() { + // When the `console_error_panic_hook` feature is enabled, we can call the + // `set_panic_hook` function at least once during initialization, and then + // we will get better error messages if our code ever panics. + // + // For more details see + // https://github.com/rustwasm/console_error_panic_hook#readme + #[cfg(feature = "console_error_panic_hook")] + console_error_panic_hook::set_once(); +} diff --git a/nodes/max/plantarium/array/tests/web.rs b/nodes/max/plantarium/array/tests/web.rs new file mode 100644 index 0000000..de5c1da --- /dev/null +++ b/nodes/max/plantarium/array/tests/web.rs @@ -0,0 +1,13 @@ +//! Test suite for the Web and headless browsers. + +#![cfg(target_arch = "wasm32")] + +extern crate wasm_bindgen_test; +use wasm_bindgen_test::*; + +wasm_bindgen_test_configure!(run_in_browser); + +#[wasm_bindgen_test] +fn pass() { + assert_eq!(1 + 1, 2); +} diff --git a/nodes/max/plantarium/output/src/lib.rs b/nodes/max/plantarium/output/src/lib.rs index 8bb4c90..a291e7d 100644 --- a/nodes/max/plantarium/output/src/lib.rs +++ b/nodes/max/plantarium/output/src/lib.rs @@ -1,5 +1,5 @@ mod utils; -use plantarium::evaluate_parameters; +use plantarium::{evaluate_parameters, unwrap_float}; use wasm_bindgen::prelude::*; // lifted from the `console_log` example @@ -23,10 +23,8 @@ pub fn get_input_types() -> String { .to_string() } #[wasm_bindgen] -pub fn execute(var_value: JsValue) -> f64 { +pub fn execute(var_value: f64) -> f64 { utils::set_panic_hook(); - let res = evaluate_parameters(var_value); - - return res; + return var_value; } diff --git a/nodes/max/plantarium/sum/.gitignore b/nodes/max/plantarium/sum/.gitignore new file mode 100644 index 0000000..4e30131 --- /dev/null +++ b/nodes/max/plantarium/sum/.gitignore @@ -0,0 +1,6 @@ +/target +**/*.rs.bk +Cargo.lock +bin/ +pkg/ +wasm-pack.log diff --git a/nodes/max/plantarium/sum/Cargo.toml b/nodes/max/plantarium/sum/Cargo.toml new file mode 100644 index 0000000..7cf2cc7 --- /dev/null +++ b/nodes/max/plantarium/sum/Cargo.toml @@ -0,0 +1,30 @@ +[package] +name = "sum" +version = "0.1.0" +authors = ["Max Richter "] +edition = "2018" + +[lib] +crate-type = ["cdylib", "rlib"] + +[features] +default = ["console_error_panic_hook"] + +[dependencies] +wasm-bindgen = "0.2.84" + +# The `console_error_panic_hook` crate provides better debugging of panics by +# logging them with `console.error`. This is great for development, but requires +# all the `std::fmt` and `std::panicking` infrastructure, so isn't great for +# code size when deploying. +plantarium = { version = "0.1.0", path = "../../../../packages/plantarium" } +serde = { version = "1.0", features = ["derive"] } +serde-wasm-bindgen = "0.4" +console_error_panic_hook = { version = "0.1.7", optional = true } + +[dev-dependencies] +wasm-bindgen-test = "0.3.34" + +[profile.release] +# Tell `rustc` to optimize for small code size. +opt-level = "s" diff --git a/nodes/max/plantarium/sum/package.json b/nodes/max/plantarium/sum/package.json new file mode 100644 index 0000000..bc6fe14 --- /dev/null +++ b/nodes/max/plantarium/sum/package.json @@ -0,0 +1,5 @@ +{ + "scripts": { + "build": "wasm-pack build --release --out-name index --no-default-features" + } +} diff --git a/nodes/max/plantarium/sum/src/lib.rs b/nodes/max/plantarium/sum/src/lib.rs new file mode 100644 index 0000000..06b88f9 --- /dev/null +++ b/nodes/max/plantarium/sum/src/lib.rs @@ -0,0 +1,33 @@ +mod utils; +use plantarium::{evaluate_parameters, unwrap_int, unwrap_string}; +use wasm_bindgen::prelude::*; + +// lifted from the `console_log` example +#[wasm_bindgen] +extern "C" { + #[wasm_bindgen(js_namespace = console)] + fn log(s: &str); +} + +#[wasm_bindgen] +pub fn get_outputs() -> Vec { + vec!["float".to_string()] +} + +#[wasm_bindgen] +pub fn get_input_types() -> String { + utils::set_panic_hook(); + r#"{ + "array": { "type": "float", "value": 2, "external": true } + }"# + .to_string() +} + +#[rustfmt::skip] +#[wasm_bindgen] +pub fn execute(array: &[f64]) -> f64 { + utils::set_panic_hook(); + let mut sum = 0.0; + array.iter().for_each(|x| sum += x); + return sum; +} diff --git a/nodes/max/plantarium/sum/src/utils.rs b/nodes/max/plantarium/sum/src/utils.rs new file mode 100644 index 0000000..b1d7929 --- /dev/null +++ b/nodes/max/plantarium/sum/src/utils.rs @@ -0,0 +1,10 @@ +pub fn set_panic_hook() { + // When the `console_error_panic_hook` feature is enabled, we can call the + // `set_panic_hook` function at least once during initialization, and then + // we will get better error messages if our code ever panics. + // + // For more details see + // https://github.com/rustwasm/console_error_panic_hook#readme + #[cfg(feature = "console_error_panic_hook")] + console_error_panic_hook::set_once(); +} diff --git a/nodes/max/plantarium/sum/tests/web.rs b/nodes/max/plantarium/sum/tests/web.rs new file mode 100644 index 0000000..de5c1da --- /dev/null +++ b/nodes/max/plantarium/sum/tests/web.rs @@ -0,0 +1,13 @@ +//! Test suite for the Web and headless browsers. + +#![cfg(target_arch = "wasm32")] + +extern crate wasm_bindgen_test; +use wasm_bindgen_test::*; + +wasm_bindgen_test_configure!(run_in_browser); + +#[wasm_bindgen_test] +fn pass() { + assert_eq!(1 + 1, 2); +} diff --git a/nodes/max/plantarium/triangle/.gitignore b/nodes/max/plantarium/triangle/.gitignore new file mode 100644 index 0000000..4e30131 --- /dev/null +++ b/nodes/max/plantarium/triangle/.gitignore @@ -0,0 +1,6 @@ +/target +**/*.rs.bk +Cargo.lock +bin/ +pkg/ +wasm-pack.log diff --git a/nodes/max/plantarium/triangle/Cargo.toml b/nodes/max/plantarium/triangle/Cargo.toml new file mode 100644 index 0000000..e86b624 --- /dev/null +++ b/nodes/max/plantarium/triangle/Cargo.toml @@ -0,0 +1,30 @@ +[package] +name = "triangle" +version = "0.1.0" +authors = ["Max Richter "] +edition = "2018" + +[lib] +crate-type = ["cdylib", "rlib"] + +[features] +default = ["console_error_panic_hook"] + +[dependencies] +wasm-bindgen = "0.2.84" + +# The `console_error_panic_hook` crate provides better debugging of panics by +# logging them with `console.error`. This is great for development, but requires +# all the `std::fmt` and `std::panicking` infrastructure, so isn't great for +# code size when deploying. +plantarium = { version = "0.1.0", path = "../../../../packages/plantarium" } +serde = { version = "1.0", features = ["derive"] } +serde-wasm-bindgen = "0.4" +console_error_panic_hook = { version = "0.1.7", optional = true } + +[dev-dependencies] +wasm-bindgen-test = "0.3.34" + +[profile.release] +# Tell `rustc` to optimize for small code size. +opt-level = "s" diff --git a/nodes/max/plantarium/triangle/package.json b/nodes/max/plantarium/triangle/package.json new file mode 100644 index 0000000..bc6fe14 --- /dev/null +++ b/nodes/max/plantarium/triangle/package.json @@ -0,0 +1,5 @@ +{ + "scripts": { + "build": "wasm-pack build --release --out-name index --no-default-features" + } +} diff --git a/nodes/max/plantarium/triangle/src/lib.rs b/nodes/max/plantarium/triangle/src/lib.rs new file mode 100644 index 0000000..b60ee0a --- /dev/null +++ b/nodes/max/plantarium/triangle/src/lib.rs @@ -0,0 +1,48 @@ +mod utils; +use plantarium::{evaluate_parameters, unwrap_int, unwrap_string}; +use wasm_bindgen::prelude::*; + +// lifted from the `console_log` example +#[wasm_bindgen] +extern "C" { + #[wasm_bindgen(js_namespace = console)] + fn log(s: &str); +} + +#[wasm_bindgen] +pub fn get_outputs() -> Vec { + vec!["float".to_string()] +} + +#[wasm_bindgen] +pub fn get_input_types() -> String { + utils::set_panic_hook(); + r#"{ + "min": { "type": "float", "value": 2 }, + "max": { "type": "float", "value": 2 }, + "seed": { "type": "seed" } + }"# + .to_string() +} + +struct El { + value: Option, + array: Option>, + nested: Option>, +} + +#[rustfmt::skip] +#[wasm_bindgen] +pub fn execute(var_min: JsValue, var_max: JsValue, var_seed: JsValue) -> Vec { + utils::set_panic_hook(); + + + // construct vertices of a triangle + let min= evaluate_parameters(var_min); + + return vec![ + 0.0, 0.0, 0.0, + min, 0.0, 0.0, + min, min, 0.0 + ]; +} diff --git a/nodes/max/plantarium/triangle/src/utils.rs b/nodes/max/plantarium/triangle/src/utils.rs new file mode 100644 index 0000000..b1d7929 --- /dev/null +++ b/nodes/max/plantarium/triangle/src/utils.rs @@ -0,0 +1,10 @@ +pub fn set_panic_hook() { + // When the `console_error_panic_hook` feature is enabled, we can call the + // `set_panic_hook` function at least once during initialization, and then + // we will get better error messages if our code ever panics. + // + // For more details see + // https://github.com/rustwasm/console_error_panic_hook#readme + #[cfg(feature = "console_error_panic_hook")] + console_error_panic_hook::set_once(); +} diff --git a/nodes/max/plantarium/triangle/tests/web.rs b/nodes/max/plantarium/triangle/tests/web.rs new file mode 100644 index 0000000..de5c1da --- /dev/null +++ b/nodes/max/plantarium/triangle/tests/web.rs @@ -0,0 +1,13 @@ +//! Test suite for the Web and headless browsers. + +#![cfg(target_arch = "wasm32")] + +extern crate wasm_bindgen_test; +use wasm_bindgen_test::*; + +wasm_bindgen_test_configure!(run_in_browser); + +#[wasm_bindgen_test] +fn pass() { + assert_eq!(1 + 1, 2); +} diff --git a/packages/graph-interface/src/lib/edges/Edge.svelte b/packages/graph-interface/src/lib/edges/Edge.svelte index f4ab8c9..33f28f0 100644 --- a/packages/graph-interface/src/lib/edges/Edge.svelte +++ b/packages/graph-interface/src/lib/edges/Edge.svelte @@ -27,7 +27,7 @@ import { Color } from "three/src/math/Color.js"; import { CubicBezierCurve } from "three/src/extras/curves/CubicBezierCurve.js"; import { Vector2 } from "three/src/math/Vector2.js"; - import { createEdgeGeometry } from "./createEdgeGeometry"; + import { createEdgeGeometry } from "./createEdgeGeometry.js"; export let from: { x: number; y: number }; export let to: { x: number; y: number }; diff --git a/packages/node-registry/src/lib/registry.ts b/packages/node-registry/src/lib/registry.ts index d3e4656..05191f1 100644 --- a/packages/node-registry/src/lib/registry.ts +++ b/packages/node-registry/src/lib/registry.ts @@ -36,8 +36,13 @@ export async function getNode(id: `${string}/${string}/${string}`) { const wrapper = await getNodeWasm(id); const outputs = wrapper.get_outputs(); - const inputTypes = JSON.parse(wrapper.get_input_types()); + const rawInputs = wrapper.get_input_types(); + try { + const inputTypes = JSON.parse(rawInputs); + return { id, outputs, inputs: inputTypes } + } catch (e) { + console.log("Failed to parse input types for node", { id, rawInputs }); + } - return { id, outputs, inputs: inputTypes } }