diff --git a/nodes/max/plantarium/array/src/input.json b/nodes/max/plantarium/array/src/input.json index e76a61a..5e1d96a 100644 --- a/nodes/max/plantarium/array/src/input.json +++ b/nodes/max/plantarium/array/src/input.json @@ -1,10 +1,15 @@ { - "value": { - "type": "float", - "value": 4.2 - }, - "amount": { - "type": "integer", - "value": 2 + "outputs": [ + "float" + ], + "inputs": { + "value": { + "type": "float", + "value": 4.2 + }, + "amount": { + "type": "integer", + "value": 2 + } } } diff --git a/nodes/max/plantarium/array/src/lib.rs b/nodes/max/plantarium/array/src/lib.rs index f1c3399..1191177 100644 --- a/nodes/max/plantarium/array/src/lib.rs +++ b/nodes/max/plantarium/array/src/lib.rs @@ -1,11 +1,8 @@ -use macros::generate_input_types_file; -use utils::{evaluate_args, generate_outputs, get_args}; +use macros::include_definition_file; +use utils::{evaluate_args, get_args}; use wasm_bindgen::prelude::*; -// use web_sys::console; -generate_outputs!(["float"]); - -generate_input_types_file!("src/input.json"); +include_definition_file!("src/input.json"); #[wasm_bindgen] pub fn execute(input: &[i32]) -> Vec { diff --git a/nodes/max/plantarium/float/src/input.json b/nodes/max/plantarium/float/src/input.json index 4f3bc7b..b1c66a7 100644 --- a/nodes/max/plantarium/float/src/input.json +++ b/nodes/max/plantarium/float/src/input.json @@ -1,7 +1,12 @@ { - "value": { - "type": "float", - "value": 0.1, - "internal": true + "outputs": [ + "float" + ], + "inputs": { + "value": { + "type": "float", + "value": 0.1, + "internal": true + } } } diff --git a/nodes/max/plantarium/float/src/lib.rs b/nodes/max/plantarium/float/src/lib.rs index 0fa99d6..44838a6 100644 --- a/nodes/max/plantarium/float/src/lib.rs +++ b/nodes/max/plantarium/float/src/lib.rs @@ -1,9 +1,7 @@ -use macros::generate_input_types_file; -use utils::generate_outputs; +use macros::include_definition_file; use wasm_bindgen::prelude::*; -generate_outputs!(["float"]); -generate_input_types_file!("src/input.json"); +include_definition_file!("src/input.json"); #[wasm_bindgen] pub fn execute(args: &[i32]) -> Vec { diff --git a/nodes/max/plantarium/math/src/input.json b/nodes/max/plantarium/math/src/input.json index de99158..48d9a2d 100644 --- a/nodes/max/plantarium/math/src/input.json +++ b/nodes/max/plantarium/math/src/input.json @@ -1,27 +1,32 @@ { - "op_type": { - "label": "type", - "type": "select", - "labels": [ - "add", - "subtract", - "multiply", - "divide" - ], - "internal": true, - "value": 0 - }, - "a": { - "type": "float", - "value": 2 - }, - "b": { - "type": "float", - "value": 2 - }, - "clip": { - "type": "boolean", - "value": 0, - "setting": "math.clipping" + "outputs": [ + "float" + ], + "inputs": { + "op_type": { + "label": "type", + "type": "select", + "labels": [ + "add", + "subtract", + "multiply", + "divide" + ], + "internal": true, + "value": 0 + }, + "a": { + "type": "float", + "value": 2 + }, + "b": { + "type": "float", + "value": 2 + }, + "clip": { + "type": "boolean", + "value": 0, + "setting": "math.clipping" + } } } diff --git a/nodes/max/plantarium/math/src/lib.rs b/nodes/max/plantarium/math/src/lib.rs index f7ba27e..4096b1e 100644 --- a/nodes/max/plantarium/math/src/lib.rs +++ b/nodes/max/plantarium/math/src/lib.rs @@ -1,10 +1,7 @@ -use macros::generate_input_types_file; -use utils::generate_outputs; +use macros::include_definition_file; use wasm_bindgen::prelude::*; -generate_outputs!(["float"]); - -generate_input_types_file!("src/input.json"); +include_definition_file!("src/input.json"); #[wasm_bindgen] pub fn execute(args: &[i32]) -> Vec { diff --git a/nodes/max/plantarium/output/src/inputs.json b/nodes/max/plantarium/output/src/inputs.json index 37912f0..16922a7 100644 --- a/nodes/max/plantarium/output/src/inputs.json +++ b/nodes/max/plantarium/output/src/inputs.json @@ -1,6 +1,9 @@ { - "input": { - "type": "plant", - "external": true + "outputs": [], + "inputs": { + "input": { + "type": "plant", + "external": true + } } } diff --git a/nodes/max/plantarium/output/src/lib.rs b/nodes/max/plantarium/output/src/lib.rs index 4589e03..973d201 100644 --- a/nodes/max/plantarium/output/src/lib.rs +++ b/nodes/max/plantarium/output/src/lib.rs @@ -1,8 +1,8 @@ -use macros::generate_input_types_file; +use macros::include_definition_file; use utils::evaluate_args; use wasm_bindgen::prelude::*; -generate_input_types_file!("src/inputs.json"); +include_definition_file!("src/inputs.json"); #[wasm_bindgen] pub fn execute(args: &[i32]) -> Vec { diff --git a/nodes/max/plantarium/random/src/lib.rs b/nodes/max/plantarium/random/src/lib.rs index 59b19b4..a62d940 100644 --- a/nodes/max/plantarium/random/src/lib.rs +++ b/nodes/max/plantarium/random/src/lib.rs @@ -1,26 +1,16 @@ +use macros::define_node; 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 { +define_node!( r#"{ - "min": { "type": "float", "value": 2 }, - "max": { "type": "float", "value": 2 }, - "seed": { "type": "seed" } + "outputs": ["float"], + "inputs": { + "min": { "type": "float", "value": 2 }, + "max": { "type": "float", "value": 2 }, + "seed": { "type": "seed" } + } }"# - .to_string() -} +); #[wasm_bindgen] pub fn execute(args: &[i32]) -> Vec { diff --git a/nodes/max/plantarium/stem/src/input.json b/nodes/max/plantarium/stem/src/input.json index acffa31..48227e5 100644 --- a/nodes/max/plantarium/stem/src/input.json +++ b/nodes/max/plantarium/stem/src/input.json @@ -1,15 +1,20 @@ { - "length": { - "type": "float", - "value": 2 - }, - "thickness": { - "type": "float", - "value": 2 - }, - "resolution": { - "type": "integer", - "value": 32, - "setting": "resolution.stem" + "outputs": [ + "plant" + ], + "inputs": { + "length": { + "type": "float", + "value": 2 + }, + "thickness": { + "type": "float", + "value": 2 + }, + "resolution": { + "type": "integer", + "value": 32, + "setting": "resolution.stem" + } } } diff --git a/nodes/max/plantarium/stem/src/lib.rs b/nodes/max/plantarium/stem/src/lib.rs index 9a8f583..49bf1f6 100644 --- a/nodes/max/plantarium/stem/src/lib.rs +++ b/nodes/max/plantarium/stem/src/lib.rs @@ -1,10 +1,9 @@ -use macros::generate_input_types_file; -use utils::{evaluate_args, generate_outputs, get_args}; +use macros::include_definition_file; +use utils::{evaluate_args, get_args}; use wasm_bindgen::prelude::*; use web_sys::console; -generate_outputs!(["plant"]); -generate_input_types_file!("src/input.json"); +include_definition_file!("src/input.json"); #[wasm_bindgen] pub fn execute(input: &[i32]) -> Vec { diff --git a/nodes/max/plantarium/sum/src/input.json b/nodes/max/plantarium/sum/src/input.json index 49b3de7..275e6d4 100644 --- a/nodes/max/plantarium/sum/src/input.json +++ b/nodes/max/plantarium/sum/src/input.json @@ -1,7 +1,12 @@ { - "array": { - "type": "float", - "value": 2, - "external": true + "outputs": [ + "float" + ], + "inputs": { + "array": { + "type": "float", + "value": 2, + "external": true + } } } diff --git a/nodes/max/plantarium/sum/src/lib.rs b/nodes/max/plantarium/sum/src/lib.rs index 3e8c4bb..52886f4 100644 --- a/nodes/max/plantarium/sum/src/lib.rs +++ b/nodes/max/plantarium/sum/src/lib.rs @@ -1,10 +1,8 @@ -use macros::generate_input_types_file; -use utils::{decode_float, encode_float, generate_outputs}; +use macros::include_definition_file; +use utils::{decode_float, encode_float}; use wasm_bindgen::prelude::*; -generate_outputs!(["float"]); - -generate_input_types_file!("src/input.json"); +include_definition_file!("src/input.json"); #[wasm_bindgen] pub fn execute(input: &[i32]) -> Vec { diff --git a/packages/macros/src/lib.rs b/packages/macros/src/lib.rs index 1b6dc52..6db4c93 100644 --- a/packages/macros/src/lib.rs +++ b/packages/macros/src/lib.rs @@ -8,7 +8,7 @@ use std::path::Path; use syn::{parse_macro_input, LitStr}; #[proc_macro] -pub fn generate_input_types(input: TokenStream) -> TokenStream { +pub fn define_node(input: TokenStream) -> TokenStream { let input_string = parse_macro_input!(input as LitStr).value(); // Validate JSON format @@ -23,7 +23,7 @@ pub fn generate_input_types(input: TokenStream) -> TokenStream { // Generate the output function let expanded = quote! { #[wasm_bindgen] - pub fn get_input_types() -> String { + pub fn get_definition() -> String { String::from(#formatted_json) } }; @@ -33,7 +33,7 @@ pub fn generate_input_types(input: TokenStream) -> TokenStream { } #[proc_macro] -pub fn generate_input_types_file(input: TokenStream) -> TokenStream { +pub fn include_definition_file(input: TokenStream) -> TokenStream { let file_path = syn::parse_macro_input!(input as syn::LitStr).value(); // Retrieve the directory containing the Cargo.toml file @@ -55,7 +55,7 @@ pub fn generate_input_types_file(input: TokenStream) -> TokenStream { // Generate the function that returns the JSON string let expanded = quote! { #[wasm_bindgen] - pub fn get_input_types() -> String { + pub fn get_definition() -> String { String::from(#json_content) } }; diff --git a/packages/node-registry/src/lib/registry.ts b/packages/node-registry/src/lib/registry.ts index fa1f692..5dadd66 100644 --- a/packages/node-registry/src/lib/registry.ts +++ b/packages/node-registry/src/lib/registry.ts @@ -1,7 +1,5 @@ import { createWasmWrapper } from "@nodes/utils" - - export async function getNodeWasm(id: `${string}/${string}/${string}`) { const wasmResponse = await fetch(`/n/${id}/wasm`); @@ -23,14 +21,11 @@ export async function getNode(id: `${string}/${string}/${string}`) { const wrapper = await getNodeWasm(id); - const outputs = wrapper?.get_outputs?.() || []; - const rawInputs = wrapper.get_inputs(); + const { inputs, outputs } = wrapper?.get_definition?.(); try { - const inputTypes = JSON.parse(rawInputs); - return { id, outputs, inputs: inputTypes } + return { id, inputs, outputs } } catch (e) { - console.log(rawInputs); - console.log("Failed to parse input types for node", { id, rawInputs }); + console.log("Failed to parse input types for node", { id }); } diff --git a/packages/utils/package.json b/packages/utils/package.json index ae6ce9b..56f51a3 100644 --- a/packages/utils/package.json +++ b/packages/utils/package.json @@ -8,5 +8,8 @@ }, "keywords": [], "author": "", - "license": "ISC" + "license": "ISC", + "dependencies": { + "@nodes/types": "link:../types" + } } diff --git a/packages/utils/src/lib.rs b/packages/utils/src/lib.rs index 7935d78..266e5f2 100644 --- a/packages/utils/src/lib.rs +++ b/packages/utils/src/lib.rs @@ -16,13 +16,3 @@ pub fn set_panic_hook() { #[cfg(feature = "console_error_panic_hook")] console_error_panic_hook::set_once(); } - -#[macro_export] -macro_rules! generate_outputs { - ([$($item:expr),* $(,)?]) => { - #[wasm_bindgen] - pub fn get_outputs() -> Vec { - vec![$($item.to_string()),*] - } - }; -} diff --git a/packages/utils/src/wasm-wrapper.ts b/packages/utils/src/wasm-wrapper.ts index cd7c2e1..71c8982 100644 --- a/packages/utils/src/wasm-wrapper.ts +++ b/packages/utils/src/wasm-wrapper.ts @@ -1,6 +1,7 @@ +import { NodeType } from "@nodes/types"; const cachedTextDecoder = new TextDecoder('utf-8', { ignoreBOM: true, fatal: true }); -const cachedTextEncoder = new TextEncoder('utf-8'); +const cachedTextEncoder = new TextEncoder(); const encodeString = (typeof cachedTextEncoder.encodeInto === 'function' @@ -107,36 +108,19 @@ export function createWasmWrapper() { return addHeapObject(ret); }; - function get_outputs() { - if (wasm === undefined || !wasm.get_outputs) { - return []; - } - - try { - const retptr = wasm.__wbindgen_add_to_stack_pointer(-16); - wasm.get_outputs(retptr); - var r0 = getInt32Memory0()[retptr / 4 + 0]; - var r1 = getInt32Memory0()[retptr / 4 + 1]; - var v1 = getArrayJsValueFromWasm0(r0, r1).slice(); - wasm.__wbindgen_free(r0, r1 * 4, 4); - return v1; - } finally { - wasm.__wbindgen_add_to_stack_pointer(16); - } - } - // Additional methods and their internal helpers can also be refactored in a similar manner. - function get_inputs() { + function get_definition() { let deferred1_0: number; let deferred1_1: number; try { const retptr = wasm.__wbindgen_add_to_stack_pointer(-16); - wasm.get_input_types(retptr); + wasm.get_definition(retptr); var r0 = getInt32Memory0()[retptr / 4 + 0]; var r1 = getInt32Memory0()[retptr / 4 + 1]; deferred1_0 = r0; deferred1_1 = r1; - return getStringFromWasm0(r0, r1); + const string = getStringFromWasm0(r0, r1); + return JSON.parse(string) as Omit; } finally { wasm.__wbindgen_add_to_stack_pointer(16); wasm.__wbindgen_free(deferred1_0, deferred1_1, 1); @@ -144,7 +128,7 @@ export function createWasmWrapper() { } - function execute(args) { + function execute(args: Int32Array) { try { const retptr = wasm.__wbindgen_add_to_stack_pointer(-16); const ptr0 = passArray32ToWasm0(args, wasm.__wbindgen_malloc); @@ -160,7 +144,6 @@ export function createWasmWrapper() { } } - function passStringToWasm0(arg: string, malloc: (arg0: any, arg1: number) => number, realloc: ((arg0: number, arg1: any, arg2: number, arg3: number) => number) | undefined) { if (realloc === undefined) { @@ -245,8 +228,7 @@ export function createWasmWrapper() { // Expose other methods that interact with the wasm instance execute, - get_outputs, - get_inputs, + get_definition, __wbindgen_string_new, __wbindgen_object_drop_ref, diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index aa0244a..a4c5d83 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -255,7 +255,11 @@ importers: specifier: ^1.5.0 version: 1.5.0 - packages/utils: {} + packages/utils: + dependencies: + '@nodes/types': + specifier: link:../types + version: link:../types packages: