refactor: merge get_outputs macros into define_node and include_definition

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

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,