From 473f69662670c550176bebd39d5a41119cf3b455 Mon Sep 17 00:00:00 2001 From: Max Richter Date: Thu, 4 Apr 2024 21:04:51 +0200 Subject: [PATCH] feat: track images with git lfs --- Cargo.lock | 5 +- nodes/max/plantarium/math/package.json | 2 +- nodes/max/plantarium/math/src/lib.rs | 23 +- nodes/max/plantarium/random/Cargo.toml | 1 + nodes/max/plantarium/random/package.json | 2 +- nodes/max/plantarium/random/src/lib.rs | 38 +++- .../[user]/[collection]/[node]/+server.ts | 6 +- .../[collection]/[node]/wasm/+server.ts | 9 +- .../[user]/[collection]/[node]/wrap-wasm.ts | 207 ++++++++---------- packages/node-registry/static/favicon.ico | Bin 0 -> 5430 bytes packages/plantarium/src/helpers.rs | 14 ++ 11 files changed, 168 insertions(+), 139 deletions(-) create mode 100644 packages/node-registry/static/favicon.ico 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 0000000000000000000000000000000000000000..3a7e15064e725de9de488b66401c853bd2f6c5c9 GIT binary patch literal 5430 zcmc&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z@sMU-0`M7g%%wGC|%?bZyOkK(<=jKNM2hu(tA?gE6c&h-xqLv)nnx71&Gwv%`e ze|>#DQI;!*e=Hf-21 zXYbyZ4cy^ zdQe9Pp3~=253~g5(Gr$-)*;E9h-5z3CmZqc`cGiy_m}5-js(|ANl8IMLINTqBPa6v z*WPBcy^%V!C)CYB->B`u#Q)P6jR}N7&HhHs8mrbzSp(nK-;v=tMr}pg=A6{!|N8qc DV2~;2 literal 0 HcmV?d00001 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(); +}