From 0254bc1ae5ed9428a64d5d4d6a9d23f3c4085cc4 Mon Sep 17 00:00:00 2001 From: Max Richter Date: Mon, 15 Apr 2024 18:46:34 +0200 Subject: [PATCH] feat: first working version with parameters --- Cargo.lock | 51 +++++----- Cargo.toml | 3 +- app/package.json | 2 +- app/src/lib/grid/Row.svelte | 1 - app/src/lib/helpers/flat_tree.test.ts | 19 +++- app/src/lib/helpers/flat_tree.ts | 31 +++++- app/src/lib/node-registry.ts | 62 +++++++----- app/src/lib/runtime-executor.ts | 23 +++-- app/src/routes/+page.svelte | 30 ++++-- app/src/routes/test/+page.svelte | 94 +++++++++++++------ app/vite.config.ts | 5 - nodes/max/plantarium/array/Cargo.toml | 2 +- nodes/max/plantarium/array/src/lib.rs | 7 +- nodes/max/plantarium/float/Cargo.toml | 2 +- nodes/max/plantarium/float/src/lib.rs | 4 - nodes/max/plantarium/float/src/utils.rs | 10 -- nodes/max/plantarium/math/Cargo.toml | 3 +- nodes/max/plantarium/math/src/lib.rs | 43 ++++----- nodes/max/plantarium/math/src/utils.rs | 10 -- nodes/max/plantarium/output/Cargo.toml | 4 +- nodes/max/plantarium/output/src/lib.rs | 26 ++--- nodes/max/plantarium/output/src/utils.rs | 10 -- nodes/max/plantarium/random/Cargo.toml | 3 +- nodes/max/plantarium/random/src/lib.rs | 66 ++++++------- nodes/max/plantarium/random/src/utils.rs | 10 -- nodes/max/plantarium/sum/Cargo.toml | 2 +- nodes/max/plantarium/sum/src/lib.rs | 10 +- nodes/max/plantarium/sum/src/utils.rs | 10 -- nodes/max/plantarium/triangle/Cargo.toml | 2 +- nodes/max/plantarium/triangle/src/lib.rs | 30 +----- nodes/max/plantarium/triangle/src/utils.rs | 10 -- .../graph-interface/src/lib/Camera.svelte | 22 ++--- .../graph-interface/src/lib/graph-manager.ts | 2 +- .../src/lib/graph/Graph.svelte | 19 ++-- packages/node-registry/src/lib/registry.ts | 2 +- packages/plantarium/src/lib.rs | 7 -- packages/types/index.ts | 2 +- packages/{plantarium => utils}/Cargo.toml | 6 +- .../package.json | 4 +- .../{plantarium => utils}/src/encoding.rs | 0 packages/{plantarium => utils}/src/helpers.rs | 0 .../src/utils.rs => packages/utils/src/lib.rs | 8 ++ packages/{plantarium => utils}/src/nodes.rs | 0 packages/{plantarium => utils}/src/tree.rs | 71 +++++++++----- pnpm-lock.yaml | 12 +++ 45 files changed, 389 insertions(+), 351 deletions(-) delete mode 100644 nodes/max/plantarium/float/src/utils.rs delete mode 100644 nodes/max/plantarium/math/src/utils.rs delete mode 100644 nodes/max/plantarium/output/src/utils.rs delete mode 100644 nodes/max/plantarium/random/src/utils.rs delete mode 100644 nodes/max/plantarium/sum/src/utils.rs delete mode 100644 nodes/max/plantarium/triangle/src/utils.rs delete mode 100644 packages/plantarium/src/lib.rs rename packages/{plantarium => utils}/Cargo.toml (62%) rename packages/{node-registry-client => utils}/package.json (75%) rename packages/{plantarium => utils}/src/encoding.rs (100%) rename packages/{plantarium => utils}/src/helpers.rs (100%) rename nodes/max/plantarium/array/src/utils.rs => packages/utils/src/lib.rs (80%) rename packages/{plantarium => utils}/src/nodes.rs (100%) rename packages/{plantarium => utils}/src/tree.rs (66%) diff --git a/Cargo.lock b/Cargo.lock index 186089a..7c10827 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -7,18 +7,18 @@ name = "array" version = "0.1.0" dependencies = [ "console_error_panic_hook", - "plantarium", "serde", "serde-wasm-bindgen", + "utils", "wasm-bindgen", "wasm-bindgen-test", ] [[package]] name = "bumpalo" -version = "3.15.4" +version = "3.16.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7ff69b9dd49fd426c69a0db9fc04dd934cdb6645ff000864d98f7e2af8830eaa" +checksum = "79296716171880943b8470b5f8d03aa55eb2e645a4874bdbb28adb49162e012c" [[package]] name = "cfg-if" @@ -41,9 +41,9 @@ name = "float" version = "0.1.0" dependencies = [ "console_error_panic_hook", - "plantarium", "serde", "serde-wasm-bindgen", + "utils", "wasm-bindgen", "wasm-bindgen-test", ] @@ -74,11 +74,12 @@ name = "math" version = "0.1.0" dependencies = [ "console_error_panic_hook", - "plantarium", "serde", "serde-wasm-bindgen", + "utils", "wasm-bindgen", "wasm-bindgen-test", + "web-sys", ] [[package]] @@ -92,37 +93,29 @@ name = "output" version = "0.1.0" dependencies = [ "console_error_panic_hook", - "plantarium", "serde", "serde-wasm-bindgen", "serde_json", + "utils", "wasm-bindgen", "wasm-bindgen-test", -] - -[[package]] -name = "plantarium" -version = "0.1.0" -dependencies = [ - "serde", - "serde_json", - "wasm-bindgen", + "web-sys", ] [[package]] name = "proc-macro2" -version = "1.0.79" +version = "1.0.80" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e835ff2298f5721608eb1a980ecaee1aef2c132bf95ecc026a11b7bf3c01c02e" +checksum = "a56dea16b0a29e94408b9aa5e2940a4eedbd128a1ba20e8f7ae60fd3d465af0e" dependencies = [ "unicode-ident", ] [[package]] name = "quote" -version = "1.0.35" +version = "1.0.36" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "291ec9ab5efd934aaf503a6466c5d5251535d108ee747472c3977cc5acc868ef" +checksum = "0fa76aaf39101c457836aec0ce2316dbdc3ab723cdda1c6bd4e6ad4208acaca7" dependencies = [ "proc-macro2", ] @@ -132,9 +125,9 @@ name = "random" version = "0.1.0" dependencies = [ "console_error_panic_hook", - "plantarium", "serde", "serde-wasm-bindgen", + "utils", "wasm-bindgen", "wasm-bindgen-test", ] @@ -198,18 +191,18 @@ name = "sum" version = "0.1.0" dependencies = [ "console_error_panic_hook", - "plantarium", "serde", "serde-wasm-bindgen", + "utils", "wasm-bindgen", "wasm-bindgen-test", ] [[package]] name = "syn" -version = "2.0.58" +version = "2.0.59" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "44cfb93f38070beee36b3fef7d4f5a16f27751d94b187b666a5cc5e9b0d30687" +checksum = "4a6531ffc7b071655e4ce2e04bd464c4830bb585a61cabb96cf808f05172615a" dependencies = [ "proc-macro2", "quote", @@ -221,9 +214,9 @@ name = "triangle" version = "0.1.0" dependencies = [ "console_error_panic_hook", - "plantarium", "serde", "serde-wasm-bindgen", + "utils", "wasm-bindgen", "wasm-bindgen-test", ] @@ -234,6 +227,16 @@ version = "1.0.12" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b" +[[package]] +name = "utils" +version = "0.1.0" +dependencies = [ + "console_error_panic_hook", + "serde", + "serde_json", + "wasm-bindgen", +] + [[package]] name = "wasm-bindgen" version = "0.2.92" diff --git a/Cargo.toml b/Cargo.toml index 7c735dd..fc776d9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,7 @@ [workspace] resolver = "2" -members = ["nodes/max/plantarium/*", "packages/plantarium"] +members = ["nodes/max/plantarium/*", "packages/utils"] + [profile.release] lto = true diff --git a/app/package.json b/app/package.json index e1514cd..d89adb5 100644 --- a/app/package.json +++ b/app/package.json @@ -4,7 +4,7 @@ "version": "0.0.0", "type": "module", "scripts": { - "dev": "vite --host", + "dev": "vite dev", "build": "vite build", "test": "vitest", "preview": "vite preview", diff --git a/app/src/lib/grid/Row.svelte b/app/src/lib/grid/Row.svelte index df7dc3f..07bc270 100644 --- a/app/src/lib/grid/Row.svelte +++ b/app/src/lib/grid/Row.svelte @@ -12,7 +12,6 @@ if (registerIndex > $sizes.length) { $sizes = [...$sizes, "1fr"]; } - console.log("registering cell", registerIndex); return index; }); diff --git a/app/src/lib/helpers/flat_tree.test.ts b/app/src/lib/helpers/flat_tree.test.ts index 4dd6a3a..c0ff6a3 100644 --- a/app/src/lib/helpers/flat_tree.test.ts +++ b/app/src/lib/helpers/flat_tree.test.ts @@ -1,5 +1,20 @@ import { expect, test } from 'vitest' -import { decode, encode } from './flat_tree' +import { decode, encode, concat_encoded } from './flat_tree' + +test("it correctly concats nested arrays", () => { + + const input_a = encode([1, 2, 3]); + const input_b = 2; + const input_c = encode([4, 5, 6]); + + const output = concat_encoded([input_a, input_b, input_c]); + + const decoded = decode(output); + + expect(decoded[0]).toEqual([1, 2, 3]); + + +}); // Original test case test('it correctly decodes/encodes complex nested arrays', () => { @@ -55,7 +70,7 @@ test('it correctly handles sequential nesting', () => { // If not, you can ignore or remove this test. test('it correctly handles arrays with mixed data types', () => { const input = [1, 'text', [true, [null, ['another text']]]]; - // @ts-ignore + //@ts-ignore const decoded = decode(encode(input)); expect(decoded).toEqual(input); }); diff --git a/app/src/lib/helpers/flat_tree.ts b/app/src/lib/helpers/flat_tree.ts index 96f65de..c0f9192 100644 --- a/app/src/lib/helpers/flat_tree.ts +++ b/app/src/lib/helpers/flat_tree.ts @@ -1,5 +1,34 @@ type SparseArray = (T | T[] | SparseArray)[]; +export function concat_encoded(input: (number | number[])[]): number[] { + + if (input.length === 1 && Array.isArray(input[0])) { + return input[0] + } + + const result = [0, 1]; // opening bracket + + let last_closing_bracket = 1; + + for (let i = 0; i < input.length; i++) { + const item = input[i]; + if (Array.isArray(item)) { + result.push(...item); + if (item.length > 2) { + if (item[item.length - 2] !== 1 && item[item.length - 1] !== 1) { + result.push(1, 1); // add closing bracket if missing + } + } + last_closing_bracket = result.length - 1; + } else { + result[last_closing_bracket]++; + result.push(item); + } + } + + return result +} + // Encodes a nested array into a flat array with bracket and distance notation export function encode(array: SparseArray): number[] { const encoded = [0, 0]; // Initialize encoded array with root bracket notation @@ -16,7 +45,7 @@ export function encode(array: SparseArray): number[] { } else { // Recursively encode non-empty arrays const child = encode(item); - encoded.push(...child, 1, 0); // Note: The trailing comma after 0 can be removed + encoded.push(...child, 1, 0); } // Update missingBracketIndex to the position of the newly added bracket missingBracketIndex = encoded.length - 1; diff --git a/app/src/lib/node-registry.ts b/app/src/lib/node-registry.ts index c9b292a..aa09d1b 100644 --- a/app/src/lib/node-registry.ts +++ b/app/src/lib/node-registry.ts @@ -19,7 +19,7 @@ const nodeTypes: NodeType[] = [ "b": { type: "float" }, }, outputs: ["float"], - execute: (op_type: number, a: number, b: number) => { + execute: ([op_type, a, b]: number[]) => { switch (op_type) { case 0: return a + b; case 1: return a - b; @@ -47,39 +47,49 @@ export class RemoteNodeRegistry implements NodeRegistry { constructor(private url: string) { } + private async loadNode(id: string) { + const nodeUrl = `${this.url}/n/${id}`; + const response = await fetch(nodeUrl); + const wasmResponse = await fetch(`${nodeUrl}/wasm`); + const wrapperReponse = await fetch(`${nodeUrl}/wrapper`); + if (!wrapperReponse.ok) { + this.status = "error"; + throw new Error(`Failed to load node ${id}`); + } + + let wrapperCode = await wrapperReponse.text(); + wrapperCode = wrapperCode.replace("wasm = val;", `if(wasm) return; +wasm = val;`); + const wasmWrapper = await import(/*@vite-ignore*/`data:text/javascript;base64,${btoa(wrapperCode)}#${id}`); + + const module = new WebAssembly.Module(await wasmResponse.arrayBuffer()); + const instance = new WebAssembly.Instance(module, { ["./index_bg.js"]: wasmWrapper }); + wasmWrapper.__wbg_set_wasm(instance.exports); + + if (!response.ok) { + this.status = "error"; + throw new Error(`Failed to load node ${id}`); + } else { + log.log("loaded node", id); + } + const node = await response.json(); + node.execute = wasmWrapper.execute; + return node; + } + async load(nodeIds: string[]) { const a = performance.now(); + nodeIds.push("max/plantarium/random"); nodeIds.push("max/plantarium/float"); + nodeIds.push("max/plantarium/output"); 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); - const wasmResponse = await fetch(`${nodeUrl}/wasm`); - const wrapperReponse = await fetch(`${nodeUrl}/wrapper`); - if (!wrapperReponse.ok) { - this.status = "error"; - throw new Error(`Failed to load node ${id}`); - } + const nodes = await Promise.all(nodeIds.map(id => this.loadNode(id))); - let wrapperCode = await wrapperReponse.text(); - wrapperCode = wrapperCode.replace("wasm = val;", `if(wasm) return; -wasm = val;`); - const wasmWrapper = await import(/*@vite-ignore*/`data:text/javascript;base64,${btoa(wrapperCode)}`); - - const module = new WebAssembly.Module(await wasmResponse.arrayBuffer()); - const instance = new WebAssembly.Instance(module, { ["./index_bg.js"]: wasmWrapper }); - wasmWrapper.__wbg_set_wasm(instance.exports); - - if (!response.ok) { - this.status = "error"; - throw new Error(`Failed to load node ${id}`); - } - const node = await response.json(); - node.execute = wasmWrapper.execute; - this.nodes.set(id, node); + for (const node of nodes) { + this.nodes.set(node.id, node); } const duration = performance.now() - a; diff --git a/app/src/lib/runtime-executor.ts b/app/src/lib/runtime-executor.ts index 2689b89..c882884 100644 --- a/app/src/lib/runtime-executor.ts +++ b/app/src/lib/runtime-executor.ts @@ -1,4 +1,6 @@ import type { Graph, NodeRegistry, NodeType, RuntimeExecutor } from "@nodes/types"; +import { encodeFloat } from "./helpers/encode"; +import { concat_encoded, encode } from "./helpers/flat_tree"; export class MemoryRuntimeExecutor implements RuntimeExecutor { @@ -140,15 +142,21 @@ export class MemoryRuntimeExecutor implements RuntimeExecutor { // execute the node and store the result try { const node_inputs = Object.entries(inputs); - const transformed_inputs = node_inputs.map(([key, value]) => { - const input_type = node_type.inputs[key]; - if (input.type === "float") { - return + const transformed_inputs = node_inputs.map(([key, value]: [string, any]) => { + const input_type = node_type.inputs?.[key]!; + if (value instanceof Int32Array) { + return [...value.slice(0, value.length)]; } - console.log(key, input_type); + + if (input_type.type === "float") { + return encode(encodeFloat(value as number)); + } + return value; }); - console.log(`Executing node ${node_type.id || node.id}`, node_inputs); - results[node.id] = node_type.execute(...Object.values(inputs)) as number; + const _inputs = concat_encoded(transformed_inputs); + // console.log(`Executing node ${node_type.id || node.id}`, { _inputs, inputs, node_type }); + results[node.id] = node_type.execute(_inputs) as number; + // console.log("--> result", results[node.id]); } catch (e) { console.error(`Error executing node ${node_type.id || node.id}`, e); } @@ -159,7 +167,6 @@ export class MemoryRuntimeExecutor implements RuntimeExecutor { // return the result of the parent of the output node const res = results[outputNode.id] as string - return res; } diff --git a/app/src/routes/+page.svelte b/app/src/routes/+page.svelte index a2d56d2..9eb3051 100644 --- a/app/src/routes/+page.svelte +++ b/app/src/routes/+page.svelte @@ -2,9 +2,14 @@ import Grid from "$lib/grid"; import GraphInterface from "@nodes/graph-interface"; import { MemoryRuntimeExecutor } from "$lib/runtime-executor"; - import { MemoryNodeRegistry, RemoteNodeRegistry } from "$lib/node-registry"; + import { RemoteNodeRegistry } from "$lib/node-registry"; import * as templates from "$lib/graph-templates"; import type { Graph } from "@nodes/types"; + import { decode, encode } from "$lib/helpers/flat_tree"; + import { decodeFloat } from "$lib/helpers/encode"; + + globalThis.decode = decode; + globalThis.encode = encode; const nodeRegistry = new RemoteNodeRegistry("http://localhost:3001"); const runtimeExecutor = new MemoryRuntimeExecutor(nodeRegistry); @@ -18,7 +23,12 @@ function handleResult(event: CustomEvent) { let a = performance.now(); - res = runtimeExecutor.execute(event.detail); + let _res: any = runtimeExecutor.execute(event.detail); + if (_res instanceof Int32Array) { + res = decodeFloat(_res[0], _res[1]); + } else { + res = _res; + } time = performance.now() - a; console.log(res); } @@ -29,7 +39,14 @@
-
header
+
+ header + +
result: {res} @@ -62,13 +79,6 @@ grid-template-rows: 50px 1fr; } - .details-wrapper { - position: fixed; - z-index: 100; - bottom: 10px; - right: 10px; - } - :global(html) { background: rgb(13, 19, 32); background: linear-gradient( diff --git a/app/src/routes/test/+page.svelte b/app/src/routes/test/+page.svelte index a3a612c..6126142 100644 --- a/app/src/routes/test/+page.svelte +++ b/app/src/routes/test/+page.svelte @@ -1,41 +1,77 @@ diff --git a/app/vite.config.ts b/app/vite.config.ts index 75dd485..90d2ca5 100644 --- a/app/vite.config.ts +++ b/app/vite.config.ts @@ -5,11 +5,6 @@ import wasm from "vite-plugin-wasm"; export default defineConfig({ plugins: [sveltekit(), glsl(), wasm()], - - server: { - port: 8080, - }, - ssr: { noExternal: ['three'], } diff --git a/nodes/max/plantarium/array/Cargo.toml b/nodes/max/plantarium/array/Cargo.toml index 77c439c..f7ad5fc 100644 --- a/nodes/max/plantarium/array/Cargo.toml +++ b/nodes/max/plantarium/array/Cargo.toml @@ -17,7 +17,7 @@ wasm-bindgen = "0.2.84" # 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" } +utils = { version = "0.1.0", path = "../../../../packages/utils" } serde = { version = "1.0", features = ["derive"] } serde-wasm-bindgen = "0.4" console_error_panic_hook = { version = "0.1.7", optional = true } diff --git a/nodes/max/plantarium/array/src/lib.rs b/nodes/max/plantarium/array/src/lib.rs index 487f6ce..5431910 100644 --- a/nodes/max/plantarium/array/src/lib.rs +++ b/nodes/max/plantarium/array/src/lib.rs @@ -1,5 +1,3 @@ -mod utils; -use plantarium::{evaluate_parameters, unwrap_int, unwrap_string}; use wasm_bindgen::prelude::*; // lifted from the `console_log` example @@ -16,7 +14,6 @@ pub fn get_outputs() -> Vec { #[wasm_bindgen] pub fn get_input_types() -> String { - utils::set_panic_hook(); r#"{ "length": { "type": "float", "value": 2 } }"# @@ -26,8 +23,6 @@ pub fn get_input_types() -> String { #[rustfmt::skip] #[wasm_bindgen] pub fn execute(var_length: i32) -> Vec { - utils::set_panic_hook(); - let length = var_length;//evaluate_parameters(var_length); @@ -39,6 +34,6 @@ pub fn execute(var_length: i32) -> Vec { log("executing array"); - return res; + res } diff --git a/nodes/max/plantarium/float/Cargo.toml b/nodes/max/plantarium/float/Cargo.toml index 4bab7a4..3f53d45 100644 --- a/nodes/max/plantarium/float/Cargo.toml +++ b/nodes/max/plantarium/float/Cargo.toml @@ -17,7 +17,7 @@ wasm-bindgen = "0.2.84" # 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" } +utils = { version = "0.1.0", path = "../../../../packages/utils" } serde = { version = "1.0", features = ["derive"] } serde-wasm-bindgen = "0.4" console_error_panic_hook = { version = "0.1.7", optional = true } diff --git a/nodes/max/plantarium/float/src/lib.rs b/nodes/max/plantarium/float/src/lib.rs index 102b0bd..94bf7b3 100644 --- a/nodes/max/plantarium/float/src/lib.rs +++ b/nodes/max/plantarium/float/src/lib.rs @@ -1,4 +1,3 @@ -mod utils; use wasm_bindgen::prelude::*; #[wasm_bindgen] @@ -8,7 +7,6 @@ pub fn get_outputs() -> Vec { #[wasm_bindgen] pub fn get_input_types() -> String { - utils::set_panic_hook(); r#"{ "value": { "type": "float", "value": 0.1, "internal": true } }"# @@ -17,7 +15,5 @@ pub fn get_input_types() -> String { #[wasm_bindgen] pub fn execute(args: &[i32]) -> Vec { - utils::set_panic_hook(); - args.into() } diff --git a/nodes/max/plantarium/float/src/utils.rs b/nodes/max/plantarium/float/src/utils.rs deleted file mode 100644 index b1d7929..0000000 --- a/nodes/max/plantarium/float/src/utils.rs +++ /dev/null @@ -1,10 +0,0 @@ -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/math/Cargo.toml b/nodes/max/plantarium/math/Cargo.toml index 34f141f..bf6c970 100644 --- a/nodes/max/plantarium/math/Cargo.toml +++ b/nodes/max/plantarium/math/Cargo.toml @@ -15,7 +15,8 @@ wasm-bindgen = "0.2.84" serde = { version = "1.0", features = ["derive"] } serde-wasm-bindgen = "0.4" console_error_panic_hook = { version = "0.1.7", optional = true } -plantarium = { version = "0.1.0", path = "../../../../packages/plantarium" } +utils = { version = "0.1.0", path = "../../../../packages/utils" } +web-sys = { version = "0.3.69", features = ["console"] } [dev-dependencies] wasm-bindgen-test = "0.3.34" diff --git a/nodes/max/plantarium/math/src/lib.rs b/nodes/max/plantarium/math/src/lib.rs index 1abb125..0b0dca1 100644 --- a/nodes/max/plantarium/math/src/lib.rs +++ b/nodes/max/plantarium/math/src/lib.rs @@ -1,7 +1,5 @@ -mod utils; - -use plantarium::*; use wasm_bindgen::prelude::*; +// use web_sys::console; #[wasm_bindgen] pub fn get_outputs() -> Vec { @@ -10,7 +8,6 @@ pub fn get_outputs() -> Vec { #[wasm_bindgen] pub fn get_input_types() -> String { - utils::set_panic_hook(); r#"{ "op_type": { "label": "type", "type": "select", "labels": ["add", "subtract", "multiply", "divide"], "internal": true, "value": 0 }, "a": { "type": "float", "value": 2 }, @@ -19,29 +16,21 @@ pub fn get_input_types() -> String { } #[wasm_bindgen] -pub fn execute(var_op_type: u8, var_a: JsValue, var_b: JsValue) -> String { - utils::set_panic_hook(); +pub fn execute(args: &[i32]) -> Vec { + // let d = args + // .iter() + // .map(|&num| num.to_string()) // Convert each integer to a String + // .collect::>() // Collect all Strings into a Vec + // .join(","); // Join all Strings in the Vec with a dot + // console::log_1(&format!("Math: {:?}", d).into()); - let a: String; - let b: String; + let mut result = Vec::with_capacity(args.len() + 3); + result.push(0); // encoding the [ bracket + result.push(args[1] + 1); + result.push(0); // adding the node-type, math: 0 + result.extend_from_slice(&args[2..]); + result.push(1); + result.push(1); // closing bracket - if var_a.is_string() { - a = unwrap_string(var_a); - } else { - a = unwrap_float(var_a).to_string(); - } - - if var_b.is_string() { - b = unwrap_string(var_b); - } else { - b = unwrap_float(var_b).to_string(); - } - - // Interpolate strings into JSON format - let json_string = format!( - r#"{{"__type": "math", "op_type": {}, "a": {}, "b": {}}}"#, - var_op_type, a, b - ); - - json_string + result } diff --git a/nodes/max/plantarium/math/src/utils.rs b/nodes/max/plantarium/math/src/utils.rs deleted file mode 100644 index b1d7929..0000000 --- a/nodes/max/plantarium/math/src/utils.rs +++ /dev/null @@ -1,10 +0,0 @@ -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/output/Cargo.toml b/nodes/max/plantarium/output/Cargo.toml index 9a4b6fc..68a7478 100644 --- a/nodes/max/plantarium/output/Cargo.toml +++ b/nodes/max/plantarium/output/Cargo.toml @@ -9,6 +9,7 @@ crate-type = ["cdylib", "rlib"] [features] default = ["console_error_panic_hook"] +console_error_panic_hook = ["dep:console_error_panic_hook"] [dependencies] wasm-bindgen = "0.2.84" @@ -17,11 +18,12 @@ wasm-bindgen = "0.2.84" # 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" } +utils = { version = "0.1.0", path = "../../../../packages/utils" } serde = { version = "1.0", features = ["derive"] } serde_json = { version = "1.0", default-features = false, features = ["alloc"] } serde-wasm-bindgen = "0.4" console_error_panic_hook = { version = "0.1.7", optional = true } +web-sys = { version = "0.3.69", features = ["console"] } [dev-dependencies] wasm-bindgen-test = "0.3.34" diff --git a/nodes/max/plantarium/output/src/lib.rs b/nodes/max/plantarium/output/src/lib.rs index a291e7d..c8f94d1 100644 --- a/nodes/max/plantarium/output/src/lib.rs +++ b/nodes/max/plantarium/output/src/lib.rs @@ -1,13 +1,7 @@ -mod utils; -use plantarium::{evaluate_parameters, unwrap_float}; +// use utils::decode_float; +use utils::evaluate_args; use wasm_bindgen::prelude::*; - -// lifted from the `console_log` example -#[wasm_bindgen] -extern "C" { - #[wasm_bindgen(js_namespace = console)] - fn log(s: &str); -} +// use web_sys::console; #[wasm_bindgen] pub fn get_outputs() -> Vec { @@ -16,15 +10,21 @@ pub fn get_outputs() -> Vec { #[wasm_bindgen] pub fn get_input_types() -> String { - utils::set_panic_hook(); r#"{ "input": { "type": "float", "value": 0.0, "external": true } }"# .to_string() } #[wasm_bindgen] -pub fn execute(var_value: f64) -> f64 { - utils::set_panic_hook(); +pub fn execute(args: &[i32]) -> Vec { + // utils::set_panic_hook(); - return var_value; + // console::log_1(&format!("WASM(output_node): input: {:?}", args).into()); + + evaluate_args(args) + // let decoded = decode_float(result[0], result[1]); + + // console::log_1(&format!("WASM: output: {:?}", decoded).into()); + + // result } diff --git a/nodes/max/plantarium/output/src/utils.rs b/nodes/max/plantarium/output/src/utils.rs deleted file mode 100644 index b1d7929..0000000 --- a/nodes/max/plantarium/output/src/utils.rs +++ /dev/null @@ -1,10 +0,0 @@ -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/random/Cargo.toml b/nodes/max/plantarium/random/Cargo.toml index 2c2b45c..b8becaf 100644 --- a/nodes/max/plantarium/random/Cargo.toml +++ b/nodes/max/plantarium/random/Cargo.toml @@ -17,7 +17,8 @@ wasm-bindgen = "0.2.84" # 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" } +utils = { version = "0.1.0", path = "../../../../packages/utils" } + serde = { version = "1.0", features = ["derive"] } serde-wasm-bindgen = "0.4" console_error_panic_hook = { version = "0.1.7", optional = true } diff --git a/nodes/max/plantarium/random/src/lib.rs b/nodes/max/plantarium/random/src/lib.rs index 67398ef..59b19b4 100644 --- a/nodes/max/plantarium/random/src/lib.rs +++ b/nodes/max/plantarium/random/src/lib.rs @@ -1,5 +1,3 @@ -mod utils; -use plantarium::{unwrap_int, unwrap_string}; use wasm_bindgen::prelude::*; // lifted from the `console_log` example @@ -16,7 +14,6 @@ pub fn get_outputs() -> Vec { #[wasm_bindgen] pub fn get_input_types() -> String { - utils::set_panic_hook(); r#"{ "min": { "type": "float", "value": 2 }, "max": { "type": "float", "value": 2 }, @@ -26,37 +23,36 @@ pub fn get_input_types() -> String { } #[wasm_bindgen] -pub fn execute(var_min: JsValue, var_max: JsValue, var_seed: JsValue) -> String { - utils::set_panic_hook(); +pub fn execute(args: &[i32]) -> Vec { + // let min: String; + // if var_min.is_string() { + // min = unwrap_string(var_min); + // } else { + // min = unwrap_int(var_min).to_string(); + // } + // + // let max: String; + // if var_max.is_string() { + // max = unwrap_string(var_max); + // } else { + // max = unwrap_int(var_max).to_string(); + // } + // + // let seed: String; + // if var_seed.is_string() { + // seed = unwrap_string(var_seed); + // } else { + // seed = unwrap_int(var_seed).to_string(); + // } + // + // log(&format!("min: {}, max: {}, seed: {}", min, max, seed)); + // + // // Interpolate strings into JSON format + // let json_string = format!( + // r#"{{"__type": "random", "min": {}, "max": {}, "seed": {}}}"#, + // min, max, seed + // ); - let min: String; - if var_min.is_string() { - min = unwrap_string(var_min); - } else { - min = unwrap_int(var_min).to_string(); - } - - let max: String; - if var_max.is_string() { - max = unwrap_string(var_max); - } else { - max = unwrap_int(var_max).to_string(); - } - - let seed: String; - if var_seed.is_string() { - seed = unwrap_string(var_seed); - } else { - seed = unwrap_int(var_seed).to_string(); - } - - log(&format!("min: {}, max: {}, seed: {}", min, max, seed)); - - // Interpolate strings into JSON format - let json_string = format!( - r#"{{"__type": "random", "min": {}, "max": {}, "seed": {}}}"#, - min, max, seed - ); - - json_string + // json_string + vec![1, args[0]] } diff --git a/nodes/max/plantarium/random/src/utils.rs b/nodes/max/plantarium/random/src/utils.rs deleted file mode 100644 index b1d7929..0000000 --- a/nodes/max/plantarium/random/src/utils.rs +++ /dev/null @@ -1,10 +0,0 @@ -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/Cargo.toml b/nodes/max/plantarium/sum/Cargo.toml index 2a0498b..fefbe94 100644 --- a/nodes/max/plantarium/sum/Cargo.toml +++ b/nodes/max/plantarium/sum/Cargo.toml @@ -17,7 +17,7 @@ wasm-bindgen = "0.2.84" # 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" } +utils = { version = "0.1.0", path = "../../../../packages/utils" } serde = { version = "1.0", features = ["derive"] } serde-wasm-bindgen = "0.4" console_error_panic_hook = { version = "0.1.7", optional = true } diff --git a/nodes/max/plantarium/sum/src/lib.rs b/nodes/max/plantarium/sum/src/lib.rs index 06b88f9..b42a0fb 100644 --- a/nodes/max/plantarium/sum/src/lib.rs +++ b/nodes/max/plantarium/sum/src/lib.rs @@ -1,5 +1,3 @@ -mod utils; -use plantarium::{evaluate_parameters, unwrap_int, unwrap_string}; use wasm_bindgen::prelude::*; // lifted from the `console_log` example @@ -16,7 +14,6 @@ pub fn get_outputs() -> Vec { #[wasm_bindgen] pub fn get_input_types() -> String { - utils::set_panic_hook(); r#"{ "array": { "type": "float", "value": 2, "external": true } }"# @@ -25,9 +22,8 @@ pub fn get_input_types() -> String { #[rustfmt::skip] #[wasm_bindgen] -pub fn execute(array: &[f64]) -> f64 { - utils::set_panic_hook(); - let mut sum = 0.0; +pub fn execute(array: &[i32]) -> Vec { + let mut sum = 0; array.iter().for_each(|x| sum += x); - return sum; + vec![1, sum] } diff --git a/nodes/max/plantarium/sum/src/utils.rs b/nodes/max/plantarium/sum/src/utils.rs deleted file mode 100644 index b1d7929..0000000 --- a/nodes/max/plantarium/sum/src/utils.rs +++ /dev/null @@ -1,10 +0,0 @@ -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/Cargo.toml b/nodes/max/plantarium/triangle/Cargo.toml index 6f97414..e1d3339 100644 --- a/nodes/max/plantarium/triangle/Cargo.toml +++ b/nodes/max/plantarium/triangle/Cargo.toml @@ -17,7 +17,7 @@ wasm-bindgen = "0.2.84" # 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" } +utils = { version = "0.1.0", path = "../../../../packages/utils" } serde = { version = "1.0", features = ["derive"] } serde-wasm-bindgen = "0.4" console_error_panic_hook = { version = "0.1.7", optional = true } diff --git a/nodes/max/plantarium/triangle/src/lib.rs b/nodes/max/plantarium/triangle/src/lib.rs index b60ee0a..a1015b9 100644 --- a/nodes/max/plantarium/triangle/src/lib.rs +++ b/nodes/max/plantarium/triangle/src/lib.rs @@ -1,14 +1,5 @@ -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()] @@ -16,7 +7,6 @@ pub fn get_outputs() -> Vec { #[wasm_bindgen] pub fn get_input_types() -> String { - utils::set_panic_hook(); r#"{ "min": { "type": "float", "value": 2 }, "max": { "type": "float", "value": 2 }, @@ -25,24 +15,10 @@ pub fn get_input_types() -> String { .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(); - - +pub fn execute(args: &[i32]) -> Vec { // construct vertices of a triangle - let min= evaluate_parameters(var_min); + // let min = evaluate_parameters(var_min); - return vec![ - 0.0, 0.0, 0.0, - min, 0.0, 0.0, - min, min, 0.0 - ]; + vec![1, 2, 3, 4, args[0]] } diff --git a/nodes/max/plantarium/triangle/src/utils.rs b/nodes/max/plantarium/triangle/src/utils.rs deleted file mode 100644 index b1d7929..0000000 --- a/nodes/max/plantarium/triangle/src/utils.rs +++ /dev/null @@ -1,10 +0,0 @@ -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/packages/graph-interface/src/lib/Camera.svelte b/packages/graph-interface/src/lib/Camera.svelte index 3bac531..6a08fb8 100644 --- a/packages/graph-interface/src/lib/Camera.svelte +++ b/packages/graph-interface/src/lib/Camera.svelte @@ -1,18 +1,18 @@ diff --git a/packages/graph-interface/src/lib/graph-manager.ts b/packages/graph-interface/src/lib/graph-manager.ts index 78f79fc..c7fd509 100644 --- a/packages/graph-interface/src/lib/graph-manager.ts +++ b/packages/graph-interface/src/lib/graph-manager.ts @@ -393,7 +393,7 @@ export class GraphManager extends EventEmitter<{ "save": Graph, "result": any }> const state = this.serialize(); this.history.save(state); this.emit("save", state); - logger.log("saving graph"); + logger.log("saving graphs", state); } getParentsOfNode(node: Node) { diff --git a/packages/graph-interface/src/lib/graph/Graph.svelte b/packages/graph-interface/src/lib/graph/Graph.svelte index 3487d89..b838b01 100644 --- a/packages/graph-interface/src/lib/graph/Graph.svelte +++ b/packages/graph-interface/src/lib/graph/Graph.svelte @@ -47,10 +47,6 @@ edges: [number, number, number, string][]; } = null; - $: if (cameraPosition && loaded) { - localStorage.setItem('cameraPosition', JSON.stringify(cameraPosition)); - } - let width = globalThis?.innerWidth ?? 100; let height = globalThis?.innerHeight ?? 100; @@ -61,12 +57,14 @@ cameraPosition[1] - height / cameraPosition[2] / 2, cameraPosition[1] + height / cameraPosition[2] / 2 ]; - function setCameraTransform(x: number, y: number, z: number) { - if (!camera) return; - camera.position.x = x; - camera.position.z = y; - camera.zoom = z; + function setCameraTransform(x = cameraPosition[0], y = cameraPosition[1], z = cameraPosition[2]) { + if (camera) { + camera.position.x = x; + camera.position.z = y; + camera.zoom = z; + } cameraPosition = [x, y, z]; + localStorage.setItem('cameraPosition', JSON.stringify(cameraPosition)); } export let debug = {}; @@ -534,7 +532,7 @@ }); } - if (event.key === 'a' && event.ctrlKey) { + if (event.key === 'a' && event.ctrlKey && bodyIsFocused) { $selectedNodes = new Set($nodes.keys()); } @@ -697,7 +695,6 @@ setCameraTransform(cPosition[0], cPosition[1], cPosition[2]); } } - loaded = true; }); diff --git a/packages/node-registry/src/lib/registry.ts b/packages/node-registry/src/lib/registry.ts index 05191f1..edb30e7 100644 --- a/packages/node-registry/src/lib/registry.ts +++ b/packages/node-registry/src/lib/registry.ts @@ -8,7 +8,7 @@ export async function getNodeWrapper(id: `${string}/${string}/${string}`) { let wrapperCode = await wrapperReponse.text(); wrapperCode = wrapperCode.replace("wasm = val;", `if(wasm) return; wasm = val;`); - const wasmWrapper = await import(/*@vite-ignore*/`data:text/javascript;base64,${btoa(wrapperCode)}`); + const wasmWrapper = await import(/*@vite-ignore*/`data:text/javascript;base64,${btoa(wrapperCode)}?id=${id}`); return wasmWrapper; } diff --git a/packages/plantarium/src/lib.rs b/packages/plantarium/src/lib.rs deleted file mode 100644 index 34f0497..0000000 --- a/packages/plantarium/src/lib.rs +++ /dev/null @@ -1,7 +0,0 @@ -mod encoding; -mod helpers; -mod nodes; -mod tree; -pub use encoding::*; -pub use helpers::*; -pub use tree::*; diff --git a/packages/types/index.ts b/packages/types/index.ts index 3779c01..3258e13 100644 --- a/packages/types/index.ts +++ b/packages/types/index.ts @@ -34,7 +34,7 @@ export type NodeType = { meta?: { title?: string; }, - execute?: (...args: (string | number | boolean)[]) => unknown; + execute?: (args: number[]) => unknown; } export type Socket = { diff --git a/packages/plantarium/Cargo.toml b/packages/utils/Cargo.toml similarity index 62% rename from packages/plantarium/Cargo.toml rename to packages/utils/Cargo.toml index cded29d..c4327ea 100644 --- a/packages/plantarium/Cargo.toml +++ b/packages/utils/Cargo.toml @@ -1,9 +1,13 @@ [package] -name = "plantarium" +name = "utils" version = "0.1.0" edition = "2021" +[features] +default = ["console_error_panic_hook"] + [dependencies] wasm-bindgen = "0.2.92" serde = { version = "1.0", features = ["derive"] } serde_json = { version = "1.0", default-features = false, features = ["alloc"] } +console_error_panic_hook = { version = "0.1.7", optional = true } diff --git a/packages/node-registry-client/package.json b/packages/utils/package.json similarity index 75% rename from packages/node-registry-client/package.json rename to packages/utils/package.json index a1eef18..0734dbc 100644 --- a/packages/node-registry-client/package.json +++ b/packages/utils/package.json @@ -1,8 +1,8 @@ { - "name": "@node/registry-client", + "name": "@nodes/utils", "version": "1.0.0", "description": "", - "main": "index.ts", + "main": "index.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, diff --git a/packages/plantarium/src/encoding.rs b/packages/utils/src/encoding.rs similarity index 100% rename from packages/plantarium/src/encoding.rs rename to packages/utils/src/encoding.rs diff --git a/packages/plantarium/src/helpers.rs b/packages/utils/src/helpers.rs similarity index 100% rename from packages/plantarium/src/helpers.rs rename to packages/utils/src/helpers.rs diff --git a/nodes/max/plantarium/array/src/utils.rs b/packages/utils/src/lib.rs similarity index 80% rename from nodes/max/plantarium/array/src/utils.rs rename to packages/utils/src/lib.rs index b1d7929..266e5f2 100644 --- a/nodes/max/plantarium/array/src/utils.rs +++ b/packages/utils/src/lib.rs @@ -1,3 +1,11 @@ +mod encoding; +mod helpers; +mod nodes; +mod tree; +pub use encoding::*; +pub use helpers::*; +pub use tree::*; + 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 diff --git a/packages/plantarium/src/nodes.rs b/packages/utils/src/nodes.rs similarity index 100% rename from packages/plantarium/src/nodes.rs rename to packages/utils/src/nodes.rs diff --git a/packages/plantarium/src/tree.rs b/packages/utils/src/tree.rs similarity index 66% rename from packages/plantarium/src/tree.rs rename to packages/utils/src/tree.rs index 322bd13..1c4ae67 100644 --- a/packages/plantarium/src/tree.rs +++ b/packages/utils/src/tree.rs @@ -2,14 +2,14 @@ pub fn get_args(args: &[i32]) -> Vec<&[i32]> { let mut idx: usize = 0; let mut depth = -1; - let mut arg_start_index = 2; let mut next_bracket_index = 0; - let mut last_bracket_index = 0; let mut out_args: Vec<&[i32]> = Vec::new(); let length = args.len(); + let mut arg_start_index = 2; + while idx < length { let is_bracket = idx == next_bracket_index; let value = args[idx]; @@ -21,7 +21,6 @@ pub fn get_args(args: &[i32]) -> Vec<&[i32]> { break; } - last_bracket_index = next_bracket_index; next_bracket_index = 1 + idx + args[idx + 1] as usize; if value == 0 { @@ -31,30 +30,28 @@ pub fn get_args(args: &[i32]) -> Vec<&[i32]> { } if depth == 0 { + if value == 1 { + out_args.push(&args[arg_start_index..idx]); + arg_start_index = idx + 2; + } // skip over the bracket encoding idx += 2; } else { // skip to the next bracket if we are at depth > 0 - idx = next_bracket_index - 1; + idx = next_bracket_index; } - continue; } + out_args.push(&args[arg_start_index..=idx]); + arg_start_index = idx + 1; + // this is at the end of args where normally multiple ] are encoded if depth < 0 { break; } - // remove starting bracket from single numbers - if idx - arg_start_index < 3 && last_bracket_index == arg_start_index { - arg_start_index += 2; - } - - out_args.push(&args[arg_start_index..=idx]); - idx += 1; - arg_start_index = idx; } out_args @@ -69,7 +66,7 @@ pub fn evaluate_node(input_args: &[i32]) -> (i32, i32) { } } -pub fn evaluate_args(input_args: &[i32]) -> (i32, i32) { +pub fn evaluate_args(input_args: &[i32]) -> Vec { let args = get_args(input_args); let mut resolved: Vec = Vec::new(); @@ -77,20 +74,25 @@ pub fn evaluate_args(input_args: &[i32]) -> (i32, i32) { for arg in args { if arg.len() == 1 { resolved.push(arg[0]); + } else if arg.len() == 4 && arg[0] == 0 && arg[1] == 3 { + resolved.push(arg[2]); + resolved.push(arg[3]); } else { let res = evaluate_args(arg); - resolved.push(res.0); - resolved.push(res.1); + resolved.push(res[0]); + resolved.push(res[1]); } } + println!("resolved: {:?}", resolved); + if resolved.len() > 1 { - evaluate_node(&resolved) + let res = evaluate_node(&resolved); + vec![res.0, res.1] } else { - (resolved[0], resolved[1]) + resolved } } - #[cfg(test)] mod tests { @@ -106,13 +108,13 @@ mod tests { // the numbers are f32 floats encoded as two i32's let result = evaluate_args(&input); - let decoded = decode_float(result.0, result.1); + let decoded = decode_float(result[0], result[1]); assert_eq!(decoded, 6.0); } #[test] - fn test_example_input_a() { + fn test_get_args_input_a() { let input_a = vec![0, 4, 1, 2, 3, 0, 7, 1, 2, 4, 2, 4, 1, 1, 1, 1]; // -> [1, 2, 3, [1, 2, 4, 2, 4]] @@ -126,7 +128,7 @@ mod tests { } #[test] - fn test_example_input_b() { + fn test_get_args_input_b() { let input_b = vec![0, 3, 7, 1, 0, 4, 4, 2, 4, 1, 2, 2, 0, 3, 2, 3, 1, 1, 1, 1]; // -> [1,[4,2,4], 2, [2,3]] @@ -139,4 +141,29 @@ mod tests { assert_eq!(args[3], [2]); assert_eq!(args[4], [0, 3, 2, 3]); } + + #[test] + fn test_get_args_nested_inputs() { + let input = vec![ + 0, 3, 0, 2, 0, 3, 0, 0, 0, 3, 7549747, 127, 1, 1, 0, 3, 0, 128, 1, 1, 1, 1, 0, 3, 0, + 128, 1, 1, 1, 1, + ]; + + // each math node has 4 numbers + // 0 -> type of node (0 -> math node) + // 1 -> op_type for math operation (0 -> add, 1 -> sub, 2 -> mul, 3 -> div) + // 2 -> first number + // 3 -> second number + + let args = get_args(&input); + + assert_eq!(args.len(), 4); + assert_eq!(args[0], [0]); + assert_eq!(args[1], [2]); + assert_eq!(args[3], [0, 3, 0, 128]); + + let nested_args = get_args(args[2]); + + assert_eq!(nested_args.len(), 4); + } } diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 260ab67..015ee2c 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -93,6 +93,10 @@ importers: specifier: ^1.2.0 version: 1.4.0 + nodes/max/plantarium/array: {} + + nodes/max/plantarium/array/pkg: {} + nodes/max/plantarium/float: {} nodes/max/plantarium/float/pkg: {} @@ -109,6 +113,14 @@ importers: nodes/max/plantarium/random/pkg: {} + nodes/max/plantarium/sum: {} + + nodes/max/plantarium/sum/pkg: {} + + nodes/max/plantarium/triangle: {} + + nodes/max/plantarium/triangle/pkg: {} + packages/graph-interface: dependencies: '@nodes/types':