refactor: merge get_outputs macros into define_node and include_definition

This commit is contained in:
max_richter 2024-04-16 13:58:10 +02:00
parent 097b99ef55
commit 8f594aebe3
19 changed files with 130 additions and 149 deletions

View File

@ -1,10 +1,15 @@
{ {
"value": { "outputs": [
"type": "float", "float"
"value": 4.2 ],
}, "inputs": {
"amount": { "value": {
"type": "integer", "type": "float",
"value": 2 "value": 4.2
},
"amount": {
"type": "integer",
"value": 2
}
} }
} }

View File

@ -1,11 +1,8 @@
use macros::generate_input_types_file; use macros::include_definition_file;
use utils::{evaluate_args, generate_outputs, get_args}; use utils::{evaluate_args, get_args};
use wasm_bindgen::prelude::*; use wasm_bindgen::prelude::*;
// use web_sys::console;
generate_outputs!(["float"]); include_definition_file!("src/input.json");
generate_input_types_file!("src/input.json");
#[wasm_bindgen] #[wasm_bindgen]
pub fn execute(input: &[i32]) -> Vec<i32> { pub fn execute(input: &[i32]) -> Vec<i32> {

View File

@ -1,7 +1,12 @@
{ {
"value": { "outputs": [
"type": "float", "float"
"value": 0.1, ],
"internal": true "inputs": {
"value": {
"type": "float",
"value": 0.1,
"internal": true
}
} }
} }

View File

@ -1,9 +1,7 @@
use macros::generate_input_types_file; use macros::include_definition_file;
use utils::generate_outputs;
use wasm_bindgen::prelude::*; use wasm_bindgen::prelude::*;
generate_outputs!(["float"]); include_definition_file!("src/input.json");
generate_input_types_file!("src/input.json");
#[wasm_bindgen] #[wasm_bindgen]
pub fn execute(args: &[i32]) -> Vec<i32> { pub fn execute(args: &[i32]) -> Vec<i32> {

View File

@ -1,27 +1,32 @@
{ {
"op_type": { "outputs": [
"label": "type", "float"
"type": "select", ],
"labels": [ "inputs": {
"add", "op_type": {
"subtract", "label": "type",
"multiply", "type": "select",
"divide" "labels": [
], "add",
"internal": true, "subtract",
"value": 0 "multiply",
}, "divide"
"a": { ],
"type": "float", "internal": true,
"value": 2 "value": 0
}, },
"b": { "a": {
"type": "float", "type": "float",
"value": 2 "value": 2
}, },
"clip": { "b": {
"type": "boolean", "type": "float",
"value": 0, "value": 2
"setting": "math.clipping" },
"clip": {
"type": "boolean",
"value": 0,
"setting": "math.clipping"
}
} }
} }

View File

@ -1,10 +1,7 @@
use macros::generate_input_types_file; use macros::include_definition_file;
use utils::generate_outputs;
use wasm_bindgen::prelude::*; use wasm_bindgen::prelude::*;
generate_outputs!(["float"]); include_definition_file!("src/input.json");
generate_input_types_file!("src/input.json");
#[wasm_bindgen] #[wasm_bindgen]
pub fn execute(args: &[i32]) -> Vec<i32> { pub fn execute(args: &[i32]) -> Vec<i32> {

View File

@ -1,6 +1,9 @@
{ {
"input": { "outputs": [],
"type": "plant", "inputs": {
"external": true "input": {
"type": "plant",
"external": true
}
} }
} }

View File

@ -1,8 +1,8 @@
use macros::generate_input_types_file; use macros::include_definition_file;
use utils::evaluate_args; use utils::evaluate_args;
use wasm_bindgen::prelude::*; use wasm_bindgen::prelude::*;
generate_input_types_file!("src/inputs.json"); include_definition_file!("src/inputs.json");
#[wasm_bindgen] #[wasm_bindgen]
pub fn execute(args: &[i32]) -> Vec<i32> { pub fn execute(args: &[i32]) -> Vec<i32> {

View File

@ -1,26 +1,16 @@
use macros::define_node;
use wasm_bindgen::prelude::*; use wasm_bindgen::prelude::*;
// lifted from the `console_log` example define_node!(
#[wasm_bindgen]
extern "C" {
#[wasm_bindgen(js_namespace = console)]
fn log(s: &str);
}
#[wasm_bindgen]
pub fn get_outputs() -> Vec<String> {
vec!["float".to_string()]
}
#[wasm_bindgen]
pub fn get_input_types() -> String {
r#"{ r#"{
"min": { "type": "float", "value": 2 }, "outputs": ["float"],
"max": { "type": "float", "value": 2 }, "inputs": {
"seed": { "type": "seed" } "min": { "type": "float", "value": 2 },
"max": { "type": "float", "value": 2 },
"seed": { "type": "seed" }
}
}"# }"#
.to_string() );
}
#[wasm_bindgen] #[wasm_bindgen]
pub fn execute(args: &[i32]) -> Vec<i32> { pub fn execute(args: &[i32]) -> Vec<i32> {

View File

@ -1,15 +1,20 @@
{ {
"length": { "outputs": [
"type": "float", "plant"
"value": 2 ],
}, "inputs": {
"thickness": { "length": {
"type": "float", "type": "float",
"value": 2 "value": 2
}, },
"resolution": { "thickness": {
"type": "integer", "type": "float",
"value": 32, "value": 2
"setting": "resolution.stem" },
"resolution": {
"type": "integer",
"value": 32,
"setting": "resolution.stem"
}
} }
} }

View File

@ -1,10 +1,9 @@
use macros::generate_input_types_file; use macros::include_definition_file;
use utils::{evaluate_args, generate_outputs, get_args}; use utils::{evaluate_args, get_args};
use wasm_bindgen::prelude::*; use wasm_bindgen::prelude::*;
use web_sys::console; use web_sys::console;
generate_outputs!(["plant"]); include_definition_file!("src/input.json");
generate_input_types_file!("src/input.json");
#[wasm_bindgen] #[wasm_bindgen]
pub fn execute(input: &[i32]) -> Vec<i32> { pub fn execute(input: &[i32]) -> Vec<i32> {

View File

@ -1,7 +1,12 @@
{ {
"array": { "outputs": [
"type": "float", "float"
"value": 2, ],
"external": true "inputs": {
"array": {
"type": "float",
"value": 2,
"external": true
}
} }
} }

View File

@ -1,10 +1,8 @@
use macros::generate_input_types_file; use macros::include_definition_file;
use utils::{decode_float, encode_float, generate_outputs}; use utils::{decode_float, encode_float};
use wasm_bindgen::prelude::*; use wasm_bindgen::prelude::*;
generate_outputs!(["float"]); include_definition_file!("src/input.json");
generate_input_types_file!("src/input.json");
#[wasm_bindgen] #[wasm_bindgen]
pub fn execute(input: &[i32]) -> Vec<i32> { pub fn execute(input: &[i32]) -> Vec<i32> {

View File

@ -8,7 +8,7 @@ use std::path::Path;
use syn::{parse_macro_input, LitStr}; use syn::{parse_macro_input, LitStr};
#[proc_macro] #[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(); let input_string = parse_macro_input!(input as LitStr).value();
// Validate JSON format // Validate JSON format
@ -23,7 +23,7 @@ pub fn generate_input_types(input: TokenStream) -> TokenStream {
// Generate the output function // Generate the output function
let expanded = quote! { let expanded = quote! {
#[wasm_bindgen] #[wasm_bindgen]
pub fn get_input_types() -> String { pub fn get_definition() -> String {
String::from(#formatted_json) String::from(#formatted_json)
} }
}; };
@ -33,7 +33,7 @@ pub fn generate_input_types(input: TokenStream) -> TokenStream {
} }
#[proc_macro] #[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(); let file_path = syn::parse_macro_input!(input as syn::LitStr).value();
// Retrieve the directory containing the Cargo.toml file // 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 // Generate the function that returns the JSON string
let expanded = quote! { let expanded = quote! {
#[wasm_bindgen] #[wasm_bindgen]
pub fn get_input_types() -> String { pub fn get_definition() -> String {
String::from(#json_content) String::from(#json_content)
} }
}; };

View File

@ -1,7 +1,5 @@
import { createWasmWrapper } from "@nodes/utils" import { createWasmWrapper } from "@nodes/utils"
export async function getNodeWasm(id: `${string}/${string}/${string}`) { export async function getNodeWasm(id: `${string}/${string}/${string}`) {
const wasmResponse = await fetch(`/n/${id}/wasm`); 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 wrapper = await getNodeWasm(id);
const outputs = wrapper?.get_outputs?.() || []; const { inputs, outputs } = wrapper?.get_definition?.();
const rawInputs = wrapper.get_inputs();
try { try {
const inputTypes = JSON.parse(rawInputs); return { id, inputs, outputs }
return { id, outputs, inputs: inputTypes }
} catch (e) { } catch (e) {
console.log(rawInputs); console.log("Failed to parse input types for node", { id });
console.log("Failed to parse input types for node", { id, rawInputs });
} }

View File

@ -8,5 +8,8 @@
}, },
"keywords": [], "keywords": [],
"author": "", "author": "",
"license": "ISC" "license": "ISC",
"dependencies": {
"@nodes/types": "link:../types"
}
} }

View File

@ -16,13 +16,3 @@ pub fn set_panic_hook() {
#[cfg(feature = "console_error_panic_hook")] #[cfg(feature = "console_error_panic_hook")]
console_error_panic_hook::set_once(); console_error_panic_hook::set_once();
} }
#[macro_export]
macro_rules! generate_outputs {
([$($item:expr),* $(,)?]) => {
#[wasm_bindgen]
pub fn get_outputs() -> Vec<String> {
vec![$($item.to_string()),*]
}
};
}

View File

@ -1,6 +1,7 @@
import { NodeType } from "@nodes/types";
const cachedTextDecoder = new TextDecoder('utf-8', { ignoreBOM: true, fatal: true }); 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' const encodeString = (typeof cachedTextEncoder.encodeInto === 'function'
@ -107,36 +108,19 @@ export function createWasmWrapper() {
return addHeapObject(ret); 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. // 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_0: number;
let deferred1_1: number; let deferred1_1: number;
try { try {
const retptr = wasm.__wbindgen_add_to_stack_pointer(-16); 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 r0 = getInt32Memory0()[retptr / 4 + 0];
var r1 = getInt32Memory0()[retptr / 4 + 1]; var r1 = getInt32Memory0()[retptr / 4 + 1];
deferred1_0 = r0; deferred1_0 = r0;
deferred1_1 = r1; deferred1_1 = r1;
return getStringFromWasm0(r0, r1); const string = getStringFromWasm0(r0, r1);
return JSON.parse(string) as Omit<NodeType, "id">;
} finally { } finally {
wasm.__wbindgen_add_to_stack_pointer(16); wasm.__wbindgen_add_to_stack_pointer(16);
wasm.__wbindgen_free(deferred1_0, deferred1_1, 1); wasm.__wbindgen_free(deferred1_0, deferred1_1, 1);
@ -144,7 +128,7 @@ export function createWasmWrapper() {
} }
function execute(args) { function execute(args: Int32Array) {
try { try {
const retptr = wasm.__wbindgen_add_to_stack_pointer(-16); const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
const ptr0 = passArray32ToWasm0(args, wasm.__wbindgen_malloc); 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) { function passStringToWasm0(arg: string, malloc: (arg0: any, arg1: number) => number, realloc: ((arg0: number, arg1: any, arg2: number, arg3: number) => number) | undefined) {
if (realloc === undefined) { if (realloc === undefined) {
@ -245,8 +228,7 @@ export function createWasmWrapper() {
// Expose other methods that interact with the wasm instance // Expose other methods that interact with the wasm instance
execute, execute,
get_outputs, get_definition,
get_inputs,
__wbindgen_string_new, __wbindgen_string_new,
__wbindgen_object_drop_ref, __wbindgen_object_drop_ref,

View File

@ -255,7 +255,11 @@ importers:
specifier: ^1.5.0 specifier: ^1.5.0
version: 1.5.0 version: 1.5.0
packages/utils: {} packages/utils:
dependencies:
'@nodes/types':
specifier: link:../types
version: link:../types
packages: packages: