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,4 +1,8 @@
{
"outputs": [
"float"
],
"inputs": {
"value": {
"type": "float",
"value": 4.2
@ -7,4 +11,5 @@
"type": "integer",
"value": 2
}
}
}

View File

@ -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<i32> {

View File

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

View File

@ -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<i32> {

View File

@ -1,4 +1,8 @@
{
"outputs": [
"float"
],
"inputs": {
"op_type": {
"label": "type",
"type": "select",
@ -24,4 +28,5 @@
"value": 0,
"setting": "math.clipping"
}
}
}

View File

@ -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<i32> {

View File

@ -1,6 +1,9 @@
{
"outputs": [],
"inputs": {
"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 wasm_bindgen::prelude::*;
generate_input_types_file!("src/inputs.json");
include_definition_file!("src/inputs.json");
#[wasm_bindgen]
pub fn execute(args: &[i32]) -> Vec<i32> {

View File

@ -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<String> {
vec!["float".to_string()]
}
#[wasm_bindgen]
pub fn get_input_types() -> String {
define_node!(
r#"{
"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<i32> {

View File

@ -1,4 +1,8 @@
{
"outputs": [
"plant"
],
"inputs": {
"length": {
"type": "float",
"value": 2
@ -12,4 +16,5 @@
"value": 32,
"setting": "resolution.stem"
}
}
}

View File

@ -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<i32> {

View File

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

View File

@ -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<i32> {

View File

@ -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)
}
};

View File

@ -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 });
}

View File

@ -8,5 +8,8 @@
},
"keywords": [],
"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")]
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 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<NodeType, "id">;
} 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,

View File

@ -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: