diff --git a/Cargo.lock b/Cargo.lock index e685337..d1fad7e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -443,9 +443,9 @@ checksum = "06ea2b9bc92be3c2baa9334a323ebca2d6f074ff852cd1d7b11064035cd3868f" [[package]] name = "core-graphics" -version = "0.23.1" +version = "0.23.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "970a29baf4110c26fedbc7f82107d42c23f7e88e404c4577ed73fe99ff85a212" +checksum = "c07782be35f9e1140080c6b96f0d44b739e2278479f64e02fdab4e32dfd8b081" dependencies = [ "bitflags 1.3.2", "core-foundation", @@ -2682,6 +2682,7 @@ name = "random" version = "0.1.0" dependencies = [ "console_error_panic_hook", + "plantarium", "wasm-bindgen", "wasm-bindgen-test", ] diff --git a/nodes/max/plantarium/math/package.json b/nodes/max/plantarium/math/package.json index c83efa0..bc6fe14 100644 --- a/nodes/max/plantarium/math/package.json +++ b/nodes/max/plantarium/math/package.json @@ -1,5 +1,5 @@ { "scripts": { - "build": "wasm-pack build --release" + "build": "wasm-pack build --release --out-name index --no-default-features" } } diff --git a/nodes/max/plantarium/math/src/lib.rs b/nodes/max/plantarium/math/src/lib.rs index a6f38bd..bf4a5f6 100644 --- a/nodes/max/plantarium/math/src/lib.rs +++ b/nodes/max/plantarium/math/src/lib.rs @@ -24,17 +24,20 @@ pub fn get_input_types() -> String { } #[wasm_bindgen] -pub fn execute(var_op_type: JsValue, var_a: JsValue, var_b: JsValue) -> f64 { +pub fn execute(var_op_type: JsValue, var_a: JsValue, var_b: JsValue) -> String { utils::set_panic_hook(); - let op_type = unwrap_int(var_op_type); - let a = unwrap_float(var_a); - let b = unwrap_float(var_b); + utils::set_panic_hook(); + // Convert JsValues to strings + let min_str = unwrap_string(var_min); + let max_str = unwrap_string(var_max); + let seed_str = unwrap_string(var_seed); - match op_type { - 1 => return a - b, - 2 => return a * b, - 3 => return a / b, - _ => return a + b, - } + // Interpolate strings into JSON format + let json_string = format!( + r#"{{"parameter": "random", "min": {}, "max": {}, "seed": {}}}"#, + min_str, max_str, seed_str + ); + + json_string } diff --git a/nodes/max/plantarium/random/Cargo.toml b/nodes/max/plantarium/random/Cargo.toml index 07c2d05..0f01b1e 100644 --- a/nodes/max/plantarium/random/Cargo.toml +++ b/nodes/max/plantarium/random/Cargo.toml @@ -17,6 +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" } console_error_panic_hook = { version = "0.1.7", optional = true } [dev-dependencies] diff --git a/nodes/max/plantarium/random/package.json b/nodes/max/plantarium/random/package.json index c83efa0..bc6fe14 100644 --- a/nodes/max/plantarium/random/package.json +++ b/nodes/max/plantarium/random/package.json @@ -1,5 +1,5 @@ { "scripts": { - "build": "wasm-pack build --release" + "build": "wasm-pack build --release --out-name index --no-default-features" } } diff --git a/nodes/max/plantarium/random/src/lib.rs b/nodes/max/plantarium/random/src/lib.rs index d559202..acad4e2 100644 --- a/nodes/max/plantarium/random/src/lib.rs +++ b/nodes/max/plantarium/random/src/lib.rs @@ -1,13 +1,43 @@ mod utils; +use plantarium::*; use wasm_bindgen::prelude::*; #[wasm_bindgen] -extern "C" { - fn alert(s: &str); +pub fn get_outputs() -> Vec { + vec!["float".to_string()] } #[wasm_bindgen] -pub fn greet() { - alert("Hello, random!"); +pub fn get_id() -> String { + "random".to_string() } + +#[wasm_bindgen] +pub fn get_input_types() -> String { + utils::set_panic_hook(); + r#"{ + "min": { "type": "float", "value": 2 }, + "max": { "type": "float", "value": 2 }, + "seed": { "type": "seed" } + }"# + .to_string() +} + +#[wasm_bindgen] +pub fn execute(var_min: JsValue, var_max: JsValue, var_seed: JsValue) -> String { + utils::set_panic_hook(); + // Convert JsValues to strings + let min_str = unwrap_string(var_min); + let max_str = unwrap_string(var_max); + let seed_str = unwrap_string(var_seed); + + // Interpolate strings into JSON format + let json_string = format!( + r#"{{"parameter": "random", "min": {}, "max": {}, "seed": {}}}"#, + min_str, max_str, seed_str + ); + + json_string +} + diff --git a/packages/node-registry/src/routes/[user]/[collection]/[node]/+server.ts b/packages/node-registry/src/routes/[user]/[collection]/[node]/+server.ts index ae81228..c45865e 100644 --- a/packages/node-registry/src/routes/[user]/[collection]/[node]/+server.ts +++ b/packages/node-registry/src/routes/[user]/[collection]/[node]/+server.ts @@ -1,11 +1,11 @@ import { json } from "@sveltejs/kit"; import type { RequestHandler } from "./$types"; -export const GET: RequestHandler = async function GET({ fetch }) { - const d = await fetch("/max/plantarium/math/wasm"); +export const GET: RequestHandler = async function GET({ fetch, params }) { + const d = await fetch(`/${params.user}/${params.collection}/${params.node}/wasm`); const wrapWasm = await import("./wrap-wasm"); const module = new WebAssembly.Module(await d.arrayBuffer()); - const instance = new WebAssembly.Instance(module, { "./math_bg.js": wrapWasm }); + const instance = new WebAssembly.Instance(module, { ["./index_bg.js"]: wrapWasm }); wrapWasm.__wbg_set_wasm(instance.exports); const id = wrapWasm.get_id(); const outputs = wrapWasm.get_outputs(); diff --git a/packages/node-registry/src/routes/[user]/[collection]/[node]/wasm/+server.ts b/packages/node-registry/src/routes/[user]/[collection]/[node]/wasm/+server.ts index 7d8f652..915bceb 100644 --- a/packages/node-registry/src/routes/[user]/[collection]/[node]/wasm/+server.ts +++ b/packages/node-registry/src/routes/[user]/[collection]/[node]/wasm/+server.ts @@ -1,15 +1,14 @@ import type { RequestHandler } from "./$types"; import fs from "fs/promises"; +import path from "path"; export const GET: RequestHandler = async function GET({ fetch, params }) { - const path = `../../../../../../../../nodes/${params.user}/${params.collection}/${params.node}/pkg/${params.node}_bg.wasm`; + const filePath = path.resolve(`../../nodes/${params.user}/${params.collection}/${params.node}/pkg/index_bg.wasm`); - const file = await fs.readFile(path); + const file = await fs.readFile(filePath); - console.log({ file }); + const bytes = new Uint8Array(file); - - const bytes = new Uint8Array([]); return new Response(bytes, { status: 200, headers: { "Content-Type": "application/wasm" } }); } diff --git a/packages/node-registry/src/routes/[user]/[collection]/[node]/wrap-wasm.ts b/packages/node-registry/src/routes/[user]/[collection]/[node]/wrap-wasm.ts index cef5df0..1c68fda 100644 --- a/packages/node-registry/src/routes/[user]/[collection]/[node]/wrap-wasm.ts +++ b/packages/node-registry/src/routes/[user]/[collection]/[node]/wrap-wasm.ts @@ -1,9 +1,9 @@ let wasm; export function __wbg_set_wasm(val) { - if (wasm) return; wasm = val; } + const heap = new Array(128).fill(undefined); heap.push(undefined, null, true, false); @@ -24,17 +24,75 @@ function takeObject(idx) { return ret; } -function isLikeNone(x) { - return x === undefined || x === null; +let WASM_VECTOR_LEN = 0; + +let cachedUint8Memory0 = null; + +function getUint8Memory0() { + if (cachedUint8Memory0 === null || cachedUint8Memory0.byteLength === 0) { + cachedUint8Memory0 = new Uint8Array(wasm.memory.buffer); + } + return cachedUint8Memory0; } -let cachedFloat64Memory0 = null; +const lTextEncoder = typeof TextEncoder === 'undefined' ? (0, module.require)('util').TextEncoder : TextEncoder; -function getFloat64Memory0() { - if (cachedFloat64Memory0 === null || cachedFloat64Memory0.byteLength === 0) { - cachedFloat64Memory0 = new Float64Array(wasm.memory.buffer); +let cachedTextEncoder = new lTextEncoder('utf-8'); + +const encodeString = (typeof cachedTextEncoder.encodeInto === 'function' + ? function (arg, view) { + return cachedTextEncoder.encodeInto(arg, view); } - return cachedFloat64Memory0; + : function (arg, view) { + const buf = cachedTextEncoder.encode(arg); + view.set(buf); + return { + read: arg.length, + written: buf.length + }; + }); + +function passStringToWasm0(arg, malloc, realloc) { + + if (realloc === undefined) { + const buf = cachedTextEncoder.encode(arg); + const ptr = malloc(buf.length, 1) >>> 0; + getUint8Memory0().subarray(ptr, ptr + buf.length).set(buf); + WASM_VECTOR_LEN = buf.length; + return ptr; + } + + let len = arg.length; + let ptr = malloc(len, 1) >>> 0; + + const mem = getUint8Memory0(); + + let offset = 0; + + for (; offset < len; offset++) { + const code = arg.charCodeAt(offset); + if (code > 0x7F) break; + mem[ptr + offset] = code; + } + + if (offset !== len) { + if (offset !== 0) { + arg = arg.slice(offset); + } + ptr = realloc(ptr, len, len = offset + arg.length * 3, 1) >>> 0; + const view = getUint8Memory0().subarray(ptr + offset, ptr + len); + const ret = encodeString(arg, view); + + offset += ret.written; + ptr = realloc(ptr, len, offset, 1) >>> 0; + } + + WASM_VECTOR_LEN = offset; + return ptr; +} + +function isLikeNone(x) { + return x === undefined || x === null; } let cachedInt32Memory0 = null; @@ -52,15 +110,6 @@ let cachedTextDecoder = new lTextDecoder('utf-8', { ignoreBOM: true, fatal: true cachedTextDecoder.decode(); -let cachedUint8Memory0 = null; - -function getUint8Memory0() { - if (cachedUint8Memory0 === null || cachedUint8Memory0.byteLength === 0) { - cachedUint8Memory0 = new Uint8Array(wasm.memory.buffer); - } - return cachedUint8Memory0; -} - function getStringFromWasm0(ptr, len) { ptr = ptr >>> 0; return cachedTextDecoder.decode(getUint8Memory0().subarray(ptr, ptr + len)); @@ -152,102 +201,27 @@ export function get_input_types() { } /** -* @param {any} var_op_type -* @param {any} var_a -* @param {any} var_b -* @returns {number} +* @param {any} var_min +* @param {any} var_max +* @param {any} var_seed +* @returns {string} */ -export function execute(var_op_type, var_a, var_b) { - const ret = wasm.execute(addHeapObject(var_op_type), addHeapObject(var_a), addHeapObject(var_b)); - return ret; -} - -let WASM_VECTOR_LEN = 0; - -const lTextEncoder = typeof TextEncoder === 'undefined' ? (0, module.require)('util').TextEncoder : TextEncoder; - -let cachedTextEncoder = new lTextEncoder('utf-8'); - -const encodeString = (typeof cachedTextEncoder.encodeInto === 'function' - ? function (arg, view) { - return cachedTextEncoder.encodeInto(arg, view); - } - : function (arg, view) { - const buf = cachedTextEncoder.encode(arg); - view.set(buf); - return { - read: arg.length, - written: buf.length - }; - }); - -function passStringToWasm0(arg, malloc, realloc) { - - if (realloc === undefined) { - const buf = cachedTextEncoder.encode(arg); - const ptr = malloc(buf.length, 1) >>> 0; - getUint8Memory0().subarray(ptr, ptr + buf.length).set(buf); - WASM_VECTOR_LEN = buf.length; - return ptr; - } - - let len = arg.length; - let ptr = malloc(len, 1) >>> 0; - - const mem = getUint8Memory0(); - - let offset = 0; - - for (; offset < len; offset++) { - const code = arg.charCodeAt(offset); - if (code > 0x7F) break; - mem[ptr + offset] = code; - } - - if (offset !== len) { - if (offset !== 0) { - arg = arg.slice(offset); - } - ptr = realloc(ptr, len, len = offset + arg.length * 3, 1) >>> 0; - const view = getUint8Memory0().subarray(ptr + offset, ptr + len); - const ret = encodeString(arg, view); - - offset += ret.written; - ptr = realloc(ptr, len, offset, 1) >>> 0; - } - - WASM_VECTOR_LEN = offset; - return ptr; -} - -export function __wbg_new_abda76e883ba8a5f() { - const ret = new Error(); - return addHeapObject(ret); -}; - -export function __wbg_stack_658279fe44541cf6(arg0, arg1) { - const ret = getObject(arg1).stack; - const ptr1 = passStringToWasm0(ret, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); - const len1 = WASM_VECTOR_LEN; - getInt32Memory0()[arg0 / 4 + 1] = len1; - getInt32Memory0()[arg0 / 4 + 0] = ptr1; -}; - -export function __wbg_error_f851667af71bcfc6(arg0, arg1) { - let deferred0_0; - let deferred0_1; +export function execute(var_min, var_max, var_seed) { + let deferred1_0; + let deferred1_1; try { - deferred0_0 = arg0; - deferred0_1 = arg1; - console.error(getStringFromWasm0(arg0, arg1)); + const retptr = wasm.__wbindgen_add_to_stack_pointer(-16); + wasm.execute(retptr, addHeapObject(var_min), addHeapObject(var_max), addHeapObject(var_seed)); + var r0 = getInt32Memory0()[retptr / 4 + 0]; + var r1 = getInt32Memory0()[retptr / 4 + 1]; + deferred1_0 = r0; + deferred1_1 = r1; + return getStringFromWasm0(r0, r1); } finally { - wasm.__wbindgen_free(deferred0_0, deferred0_1, 1); + wasm.__wbindgen_add_to_stack_pointer(16); + wasm.__wbindgen_free(deferred1_0, deferred1_1, 1); } -}; - -export function __wbindgen_object_drop_ref(arg0) { - takeObject(arg0); -}; +} export function __wbindgen_is_undefined(arg0) { const ret = getObject(arg0) === undefined; @@ -259,11 +233,17 @@ export function __wbindgen_is_null(arg0) { return ret; }; -export function __wbindgen_number_get(arg0, arg1) { +export function __wbindgen_object_drop_ref(arg0) { + takeObject(arg0); +}; + +export function __wbindgen_string_get(arg0, arg1) { const obj = getObject(arg1); - const ret = typeof (obj) === 'number' ? obj : undefined; - getFloat64Memory0()[arg0 / 8 + 1] = isLikeNone(ret) ? 0 : ret; - getInt32Memory0()[arg0 / 4 + 0] = !isLikeNone(ret); + const ret = typeof (obj) === 'string' ? obj : undefined; + var ptr1 = isLikeNone(ret) ? 0 : passStringToWasm0(ret, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); + var len1 = WASM_VECTOR_LEN; + getInt32Memory0()[arg0 / 4 + 1] = len1; + getInt32Memory0()[arg0 / 4 + 0] = ptr1; }; export function __wbindgen_string_new(arg0, arg1) { @@ -271,3 +251,4 @@ export function __wbindgen_string_new(arg0, arg1) { return addHeapObject(ret); }; + diff --git a/packages/node-registry/static/favicon.ico b/packages/node-registry/static/favicon.ico new file mode 100644 index 0000000..3a7e150 Binary files /dev/null and b/packages/node-registry/static/favicon.ico differ diff --git a/packages/plantarium/src/helpers.rs b/packages/plantarium/src/helpers.rs index e42162d..1557126 100644 --- a/packages/plantarium/src/helpers.rs +++ b/packages/plantarium/src/helpers.rs @@ -13,3 +13,17 @@ pub fn unwrap_float(val: JsValue) -> f64 { } return val.as_f64().unwrap(); } + +pub fn unwrap_string(val: JsValue) -> String { + if val.is_undefined() || val.is_null() { + panic!("Value is undefined"); + } + return val.as_string().unwrap(); +} + +pub fn evaluate_parameter(val: JsValue) -> String { + if val.is_undefined() || val.is_null() { + panic!("Value is undefined"); + } + return val.as_string().unwrap(); +}