feat: first working version with parameters

This commit is contained in:
max_richter 2024-04-15 18:46:34 +02:00
parent e29cb11b81
commit 0254bc1ae5
45 changed files with 389 additions and 351 deletions

51
Cargo.lock generated
View File

@ -7,18 +7,18 @@ name = "array"
version = "0.1.0"
dependencies = [
"console_error_panic_hook",
"plantarium",
"serde",
"serde-wasm-bindgen",
"utils",
"wasm-bindgen",
"wasm-bindgen-test",
]
[[package]]
name = "bumpalo"
version = "3.15.4"
version = "3.16.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7ff69b9dd49fd426c69a0db9fc04dd934cdb6645ff000864d98f7e2af8830eaa"
checksum = "79296716171880943b8470b5f8d03aa55eb2e645a4874bdbb28adb49162e012c"
[[package]]
name = "cfg-if"
@ -41,9 +41,9 @@ name = "float"
version = "0.1.0"
dependencies = [
"console_error_panic_hook",
"plantarium",
"serde",
"serde-wasm-bindgen",
"utils",
"wasm-bindgen",
"wasm-bindgen-test",
]
@ -74,11 +74,12 @@ name = "math"
version = "0.1.0"
dependencies = [
"console_error_panic_hook",
"plantarium",
"serde",
"serde-wasm-bindgen",
"utils",
"wasm-bindgen",
"wasm-bindgen-test",
"web-sys",
]
[[package]]
@ -92,37 +93,29 @@ name = "output"
version = "0.1.0"
dependencies = [
"console_error_panic_hook",
"plantarium",
"serde",
"serde-wasm-bindgen",
"serde_json",
"utils",
"wasm-bindgen",
"wasm-bindgen-test",
]
[[package]]
name = "plantarium"
version = "0.1.0"
dependencies = [
"serde",
"serde_json",
"wasm-bindgen",
"web-sys",
]
[[package]]
name = "proc-macro2"
version = "1.0.79"
version = "1.0.80"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e835ff2298f5721608eb1a980ecaee1aef2c132bf95ecc026a11b7bf3c01c02e"
checksum = "a56dea16b0a29e94408b9aa5e2940a4eedbd128a1ba20e8f7ae60fd3d465af0e"
dependencies = [
"unicode-ident",
]
[[package]]
name = "quote"
version = "1.0.35"
version = "1.0.36"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "291ec9ab5efd934aaf503a6466c5d5251535d108ee747472c3977cc5acc868ef"
checksum = "0fa76aaf39101c457836aec0ce2316dbdc3ab723cdda1c6bd4e6ad4208acaca7"
dependencies = [
"proc-macro2",
]
@ -132,9 +125,9 @@ name = "random"
version = "0.1.0"
dependencies = [
"console_error_panic_hook",
"plantarium",
"serde",
"serde-wasm-bindgen",
"utils",
"wasm-bindgen",
"wasm-bindgen-test",
]
@ -198,18 +191,18 @@ name = "sum"
version = "0.1.0"
dependencies = [
"console_error_panic_hook",
"plantarium",
"serde",
"serde-wasm-bindgen",
"utils",
"wasm-bindgen",
"wasm-bindgen-test",
]
[[package]]
name = "syn"
version = "2.0.58"
version = "2.0.59"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "44cfb93f38070beee36b3fef7d4f5a16f27751d94b187b666a5cc5e9b0d30687"
checksum = "4a6531ffc7b071655e4ce2e04bd464c4830bb585a61cabb96cf808f05172615a"
dependencies = [
"proc-macro2",
"quote",
@ -221,9 +214,9 @@ name = "triangle"
version = "0.1.0"
dependencies = [
"console_error_panic_hook",
"plantarium",
"serde",
"serde-wasm-bindgen",
"utils",
"wasm-bindgen",
"wasm-bindgen-test",
]
@ -234,6 +227,16 @@ version = "1.0.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b"
[[package]]
name = "utils"
version = "0.1.0"
dependencies = [
"console_error_panic_hook",
"serde",
"serde_json",
"wasm-bindgen",
]
[[package]]
name = "wasm-bindgen"
version = "0.2.92"

View File

@ -1,6 +1,7 @@
[workspace]
resolver = "2"
members = ["nodes/max/plantarium/*", "packages/plantarium"]
members = ["nodes/max/plantarium/*", "packages/utils"]
[profile.release]
lto = true

View File

@ -4,7 +4,7 @@
"version": "0.0.0",
"type": "module",
"scripts": {
"dev": "vite --host",
"dev": "vite dev",
"build": "vite build",
"test": "vitest",
"preview": "vite preview",

View File

@ -12,7 +12,6 @@
if (registerIndex > $sizes.length) {
$sizes = [...$sizes, "1fr"];
}
console.log("registering cell", registerIndex);
return index;
});

View File

@ -1,5 +1,20 @@
import { expect, test } from 'vitest'
import { decode, encode } from './flat_tree'
import { decode, encode, concat_encoded } from './flat_tree'
test("it correctly concats nested arrays", () => {
const input_a = encode([1, 2, 3]);
const input_b = 2;
const input_c = encode([4, 5, 6]);
const output = concat_encoded([input_a, input_b, input_c]);
const decoded = decode(output);
expect(decoded[0]).toEqual([1, 2, 3]);
});
// Original test case
test('it correctly decodes/encodes complex nested arrays', () => {
@ -55,7 +70,7 @@ test('it correctly handles sequential nesting', () => {
// If not, you can ignore or remove this test.
test('it correctly handles arrays with mixed data types', () => {
const input = [1, 'text', [true, [null, ['another text']]]];
// @ts-ignore
//@ts-ignore
const decoded = decode(encode(input));
expect(decoded).toEqual(input);
});

View File

@ -1,5 +1,34 @@
type SparseArray<T = number> = (T | T[] | SparseArray<T>)[];
export function concat_encoded(input: (number | number[])[]): number[] {
if (input.length === 1 && Array.isArray(input[0])) {
return input[0]
}
const result = [0, 1]; // opening bracket
let last_closing_bracket = 1;
for (let i = 0; i < input.length; i++) {
const item = input[i];
if (Array.isArray(item)) {
result.push(...item);
if (item.length > 2) {
if (item[item.length - 2] !== 1 && item[item.length - 1] !== 1) {
result.push(1, 1); // add closing bracket if missing
}
}
last_closing_bracket = result.length - 1;
} else {
result[last_closing_bracket]++;
result.push(item);
}
}
return result
}
// Encodes a nested array into a flat array with bracket and distance notation
export function encode(array: SparseArray): number[] {
const encoded = [0, 0]; // Initialize encoded array with root bracket notation
@ -16,7 +45,7 @@ export function encode(array: SparseArray): number[] {
} else {
// Recursively encode non-empty arrays
const child = encode(item);
encoded.push(...child, 1, 0); // Note: The trailing comma after 0 can be removed
encoded.push(...child, 1, 0);
}
// Update missingBracketIndex to the position of the newly added bracket
missingBracketIndex = encoded.length - 1;

View File

@ -19,7 +19,7 @@ const nodeTypes: NodeType[] = [
"b": { type: "float" },
},
outputs: ["float"],
execute: (op_type: number, a: number, b: number) => {
execute: ([op_type, a, b]: number[]) => {
switch (op_type) {
case 0: return a + b;
case 1: return a - b;
@ -47,39 +47,49 @@ export class RemoteNodeRegistry implements NodeRegistry {
constructor(private url: string) { }
private async loadNode(id: string) {
const nodeUrl = `${this.url}/n/${id}`;
const response = await fetch(nodeUrl);
const wasmResponse = await fetch(`${nodeUrl}/wasm`);
const wrapperReponse = await fetch(`${nodeUrl}/wrapper`);
if (!wrapperReponse.ok) {
this.status = "error";
throw new Error(`Failed to load node ${id}`);
}
let wrapperCode = await wrapperReponse.text();
wrapperCode = wrapperCode.replace("wasm = val;", `if(wasm) return;
wasm = val;`);
const wasmWrapper = await import(/*@vite-ignore*/`data:text/javascript;base64,${btoa(wrapperCode)}#${id}`);
const module = new WebAssembly.Module(await wasmResponse.arrayBuffer());
const instance = new WebAssembly.Instance(module, { ["./index_bg.js"]: wasmWrapper });
wasmWrapper.__wbg_set_wasm(instance.exports);
if (!response.ok) {
this.status = "error";
throw new Error(`Failed to load node ${id}`);
} else {
log.log("loaded node", id);
}
const node = await response.json();
node.execute = wasmWrapper.execute;
return node;
}
async load(nodeIds: string[]) {
const a = performance.now();
nodeIds.push("max/plantarium/random");
nodeIds.push("max/plantarium/float");
nodeIds.push("max/plantarium/output");
nodeIds.push("max/plantarium/array");
nodeIds.push("max/plantarium/sum");
for (const id of nodeIds) {
const nodeUrl = `${this.url}/n/${id}`;
const response = await fetch(nodeUrl);
const wasmResponse = await fetch(`${nodeUrl}/wasm`);
const wrapperReponse = await fetch(`${nodeUrl}/wrapper`);
if (!wrapperReponse.ok) {
this.status = "error";
throw new Error(`Failed to load node ${id}`);
}
const nodes = await Promise.all(nodeIds.map(id => this.loadNode(id)));
let wrapperCode = await wrapperReponse.text();
wrapperCode = wrapperCode.replace("wasm = val;", `if(wasm) return;
wasm = val;`);
const wasmWrapper = await import(/*@vite-ignore*/`data:text/javascript;base64,${btoa(wrapperCode)}`);
const module = new WebAssembly.Module(await wasmResponse.arrayBuffer());
const instance = new WebAssembly.Instance(module, { ["./index_bg.js"]: wasmWrapper });
wasmWrapper.__wbg_set_wasm(instance.exports);
if (!response.ok) {
this.status = "error";
throw new Error(`Failed to load node ${id}`);
}
const node = await response.json();
node.execute = wasmWrapper.execute;
this.nodes.set(id, node);
for (const node of nodes) {
this.nodes.set(node.id, node);
}
const duration = performance.now() - a;

View File

@ -1,4 +1,6 @@
import type { Graph, NodeRegistry, NodeType, RuntimeExecutor } from "@nodes/types";
import { encodeFloat } from "./helpers/encode";
import { concat_encoded, encode } from "./helpers/flat_tree";
export class MemoryRuntimeExecutor implements RuntimeExecutor {
@ -140,15 +142,21 @@ export class MemoryRuntimeExecutor implements RuntimeExecutor {
// execute the node and store the result
try {
const node_inputs = Object.entries(inputs);
const transformed_inputs = node_inputs.map(([key, value]) => {
const input_type = node_type.inputs[key];
if (input.type === "float") {
return
const transformed_inputs = node_inputs.map(([key, value]: [string, any]) => {
const input_type = node_type.inputs?.[key]!;
if (value instanceof Int32Array) {
return [...value.slice(0, value.length)];
}
console.log(key, input_type);
if (input_type.type === "float") {
return encode(encodeFloat(value as number));
}
return value;
});
console.log(`Executing node ${node_type.id || node.id}`, node_inputs);
results[node.id] = node_type.execute(...Object.values(inputs)) as number;
const _inputs = concat_encoded(transformed_inputs);
// console.log(`Executing node ${node_type.id || node.id}`, { _inputs, inputs, node_type });
results[node.id] = node_type.execute(_inputs) as number;
// console.log("--> result", results[node.id]);
} catch (e) {
console.error(`Error executing node ${node_type.id || node.id}`, e);
}
@ -159,7 +167,6 @@ export class MemoryRuntimeExecutor implements RuntimeExecutor {
// return the result of the parent of the output node
const res = results[outputNode.id] as string
return res;
}

View File

@ -2,9 +2,14 @@
import Grid from "$lib/grid";
import GraphInterface from "@nodes/graph-interface";
import { MemoryRuntimeExecutor } from "$lib/runtime-executor";
import { MemoryNodeRegistry, RemoteNodeRegistry } from "$lib/node-registry";
import { RemoteNodeRegistry } from "$lib/node-registry";
import * as templates from "$lib/graph-templates";
import type { Graph } from "@nodes/types";
import { decode, encode } from "$lib/helpers/flat_tree";
import { decodeFloat } from "$lib/helpers/encode";
globalThis.decode = decode;
globalThis.encode = encode;
const nodeRegistry = new RemoteNodeRegistry("http://localhost:3001");
const runtimeExecutor = new MemoryRuntimeExecutor(nodeRegistry);
@ -18,7 +23,12 @@
function handleResult(event: CustomEvent<Graph>) {
let a = performance.now();
res = runtimeExecutor.execute(event.detail);
let _res: any = runtimeExecutor.execute(event.detail);
if (_res instanceof Int32Array) {
res = decodeFloat(_res[0], _res[1]);
} else {
res = _res;
}
time = performance.now() - a;
console.log(res);
}
@ -29,7 +39,14 @@
</script>
<div class="wrapper">
<header>header</header>
<header>
header
<button
on:click={() => {
graph = templates.grid(10, 10);
}}>grid stress-test</button
>
</header>
<Grid.Row>
<Grid.Cell>
result: {res}
@ -62,13 +79,6 @@
grid-template-rows: 50px 1fr;
}
.details-wrapper {
position: fixed;
z-index: 100;
bottom: 10px;
right: 10px;
}
:global(html) {
background: rgb(13, 19, 32);
background: linear-gradient(

View File

@ -1,41 +1,77 @@
<script lang="ts">
import { decodeFloat, encodeFloat } from "$lib/helpers/encode";
import { decode, encode } from "$lib/helpers/flat_tree";
import { decode, encode, concat_encoded } from "$lib/helpers/flat_tree";
let maxError = 0;
new Array(10_000).fill(null).forEach((v, i) => {
const input = i < 5_000 ? i : Math.random() * 100;
const encoded = encodeFloat(input);
const output = decodeFloat(encoded[0], encoded[1]);
console.clear();
const error = Math.abs(input - output);
if (error > maxError) {
maxError = error;
{
const input_a = encode([1, 2, 3]);
const input_b = 2;
const input_c = 89;
const input_d = encode([4, 5, 6]);
const output = concat_encoded([input_a, input_b, input_c, input_d]);
const decoded = decode(output);
console.log("CONCAT", [input_a, input_b, input_c, input_d]);
console.log(output);
console.log(decoded);
}
if (false) {
let maxError = 0;
new Array(10_000).fill(null).forEach((v, i) => {
const input = i < 5_000 ? i : Math.random() * 100;
const encoded = encodeFloat(input);
const output = decodeFloat(encoded[0], encoded[1]);
const error = Math.abs(input - output);
if (error > maxError) {
maxError = error;
}
});
console.log("DECODE FLOAT");
console.log(maxError);
console.log(encodeFloat(2.0));
console.log("----");
}
if (false) {
console.log("Turning Int32Array into Array");
const test_size = 2_000_000;
const a = new Int32Array(test_size);
let t0 = performance.now();
for (let i = 0; i < test_size; i++) {
a[i] = Math.floor(Math.random() * 100);
}
});
console.log("TIME", performance.now() - t0);
t0 = performance.now();
const b = [...a.slice(0, test_size)];
console.log("TIME", performance.now() - t0);
console.log(typeof b, Array.isArray(b), b instanceof Int32Array);
}
console.log("DECODE FLOAT");
console.log(maxError);
console.log(encodeFloat(2.0));
if (false) {
// const input = [5, [6, 1], [7, 2, [5, 1]]];
// const input = [5, [], [6, []], []];
// const input = [52];
const input = [0, 0, [0, 2, 0, 128, 0, 128], 0, 128];
// const input = [5, [6, 1], [7, 2, [5, 1]]];
// const input = [5, [], [6, []], []];
// const input = [52];
const input = [0, 0, [0, 2, 0, 128, 0, 128], 0, 128];
console.log("INPUT");
console.log(input);
console.log("INPUT");
console.log(input);
let encoded = encode(input);
// encoded = [];
console.log("ENCODED");
console.log(encoded);
let encoded = encode(input);
// encoded = [];
console.log("ENCODED");
console.log(encoded);
encoded = [0, 2, 1, 0, 4, 4, 2, 4, 1, 2, 2, 0, 3, 2, 3, 1, 1, 1, 1];
encoded = [0, 2, 1, 0, 4, 4, 2, 4, 1, 2, 2, 0, 3, 2, 3, 1, 1, 1, 1];
const decoded = decode(encoded);
console.log("DECODED");
console.log(decoded);
const decoded = decode(encoded);
console.log("DECODED");
console.log(decoded);
console.log("EQUALS", JSON.stringify(input) === JSON.stringify(decoded));
console.log("EQUALS", JSON.stringify(input) === JSON.stringify(decoded));
}
</script>

View File

@ -5,11 +5,6 @@ import wasm from "vite-plugin-wasm";
export default defineConfig({
plugins: [sveltekit(), glsl(), wasm()],
server: {
port: 8080,
},
ssr: {
noExternal: ['three'],
}

View File

@ -17,7 +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" }
utils = { version = "0.1.0", path = "../../../../packages/utils" }
serde = { version = "1.0", features = ["derive"] }
serde-wasm-bindgen = "0.4"
console_error_panic_hook = { version = "0.1.7", optional = true }

View File

@ -1,5 +1,3 @@
mod utils;
use plantarium::{evaluate_parameters, unwrap_int, unwrap_string};
use wasm_bindgen::prelude::*;
// lifted from the `console_log` example
@ -16,7 +14,6 @@ pub fn get_outputs() -> Vec<String> {
#[wasm_bindgen]
pub fn get_input_types() -> String {
utils::set_panic_hook();
r#"{
"length": { "type": "float", "value": 2 }
}"#
@ -26,8 +23,6 @@ pub fn get_input_types() -> String {
#[rustfmt::skip]
#[wasm_bindgen]
pub fn execute(var_length: i32) -> Vec<f64> {
utils::set_panic_hook();
let length = var_length;//evaluate_parameters(var_length);
@ -39,6 +34,6 @@ pub fn execute(var_length: i32) -> Vec<f64> {
log("executing array");
return res;
res
}

View File

@ -17,7 +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" }
utils = { version = "0.1.0", path = "../../../../packages/utils" }
serde = { version = "1.0", features = ["derive"] }
serde-wasm-bindgen = "0.4"
console_error_panic_hook = { version = "0.1.7", optional = true }

View File

@ -1,4 +1,3 @@
mod utils;
use wasm_bindgen::prelude::*;
#[wasm_bindgen]
@ -8,7 +7,6 @@ pub fn get_outputs() -> Vec<String> {
#[wasm_bindgen]
pub fn get_input_types() -> String {
utils::set_panic_hook();
r#"{
"value": { "type": "float", "value": 0.1, "internal": true }
}"#
@ -17,7 +15,5 @@ pub fn get_input_types() -> String {
#[wasm_bindgen]
pub fn execute(args: &[i32]) -> Vec<i32> {
utils::set_panic_hook();
args.into()
}

View File

@ -1,10 +0,0 @@
pub fn set_panic_hook() {
// When the `console_error_panic_hook` feature is enabled, we can call the
// `set_panic_hook` function at least once during initialization, and then
// we will get better error messages if our code ever panics.
//
// For more details see
// https://github.com/rustwasm/console_error_panic_hook#readme
#[cfg(feature = "console_error_panic_hook")]
console_error_panic_hook::set_once();
}

View File

@ -15,7 +15,8 @@ wasm-bindgen = "0.2.84"
serde = { version = "1.0", features = ["derive"] }
serde-wasm-bindgen = "0.4"
console_error_panic_hook = { version = "0.1.7", optional = true }
plantarium = { version = "0.1.0", path = "../../../../packages/plantarium" }
utils = { version = "0.1.0", path = "../../../../packages/utils" }
web-sys = { version = "0.3.69", features = ["console"] }
[dev-dependencies]
wasm-bindgen-test = "0.3.34"

View File

@ -1,7 +1,5 @@
mod utils;
use plantarium::*;
use wasm_bindgen::prelude::*;
// use web_sys::console;
#[wasm_bindgen]
pub fn get_outputs() -> Vec<String> {
@ -10,7 +8,6 @@ pub fn get_outputs() -> Vec<String> {
#[wasm_bindgen]
pub fn get_input_types() -> String {
utils::set_panic_hook();
r#"{
"op_type": { "label": "type", "type": "select", "labels": ["add", "subtract", "multiply", "divide"], "internal": true, "value": 0 },
"a": { "type": "float", "value": 2 },
@ -19,29 +16,21 @@ pub fn get_input_types() -> String {
}
#[wasm_bindgen]
pub fn execute(var_op_type: u8, var_a: JsValue, var_b: JsValue) -> String {
utils::set_panic_hook();
pub fn execute(args: &[i32]) -> Vec<i32> {
// let d = args
// .iter()
// .map(|&num| num.to_string()) // Convert each integer to a String
// .collect::<Vec<String>>() // Collect all Strings into a Vec
// .join(","); // Join all Strings in the Vec with a dot
// console::log_1(&format!("Math: {:?}", d).into());
let a: String;
let b: String;
let mut result = Vec::with_capacity(args.len() + 3);
result.push(0); // encoding the [ bracket
result.push(args[1] + 1);
result.push(0); // adding the node-type, math: 0
result.extend_from_slice(&args[2..]);
result.push(1);
result.push(1); // closing bracket
if var_a.is_string() {
a = unwrap_string(var_a);
} else {
a = unwrap_float(var_a).to_string();
}
if var_b.is_string() {
b = unwrap_string(var_b);
} else {
b = unwrap_float(var_b).to_string();
}
// Interpolate strings into JSON format
let json_string = format!(
r#"{{"__type": "math", "op_type": {}, "a": {}, "b": {}}}"#,
var_op_type, a, b
);
json_string
result
}

View File

@ -1,10 +0,0 @@
pub fn set_panic_hook() {
// When the `console_error_panic_hook` feature is enabled, we can call the
// `set_panic_hook` function at least once during initialization, and then
// we will get better error messages if our code ever panics.
//
// For more details see
// https://github.com/rustwasm/console_error_panic_hook#readme
#[cfg(feature = "console_error_panic_hook")]
console_error_panic_hook::set_once();
}

View File

@ -9,6 +9,7 @@ crate-type = ["cdylib", "rlib"]
[features]
default = ["console_error_panic_hook"]
console_error_panic_hook = ["dep:console_error_panic_hook"]
[dependencies]
wasm-bindgen = "0.2.84"
@ -17,11 +18,12 @@ 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" }
utils = { version = "0.1.0", path = "../../../../packages/utils" }
serde = { version = "1.0", features = ["derive"] }
serde_json = { version = "1.0", default-features = false, features = ["alloc"] }
serde-wasm-bindgen = "0.4"
console_error_panic_hook = { version = "0.1.7", optional = true }
web-sys = { version = "0.3.69", features = ["console"] }
[dev-dependencies]
wasm-bindgen-test = "0.3.34"

View File

@ -1,13 +1,7 @@
mod utils;
use plantarium::{evaluate_parameters, unwrap_float};
// use utils::decode_float;
use utils::evaluate_args;
use wasm_bindgen::prelude::*;
// lifted from the `console_log` example
#[wasm_bindgen]
extern "C" {
#[wasm_bindgen(js_namespace = console)]
fn log(s: &str);
}
// use web_sys::console;
#[wasm_bindgen]
pub fn get_outputs() -> Vec<String> {
@ -16,15 +10,21 @@ pub fn get_outputs() -> Vec<String> {
#[wasm_bindgen]
pub fn get_input_types() -> String {
utils::set_panic_hook();
r#"{
"input": { "type": "float", "value": 0.0, "external": true }
}"#
.to_string()
}
#[wasm_bindgen]
pub fn execute(var_value: f64) -> f64 {
utils::set_panic_hook();
pub fn execute(args: &[i32]) -> Vec<i32> {
// utils::set_panic_hook();
return var_value;
// console::log_1(&format!("WASM(output_node): input: {:?}", args).into());
evaluate_args(args)
// let decoded = decode_float(result[0], result[1]);
// console::log_1(&format!("WASM: output: {:?}", decoded).into());
// result
}

View File

@ -1,10 +0,0 @@
pub fn set_panic_hook() {
// When the `console_error_panic_hook` feature is enabled, we can call the
// `set_panic_hook` function at least once during initialization, and then
// we will get better error messages if our code ever panics.
//
// For more details see
// https://github.com/rustwasm/console_error_panic_hook#readme
#[cfg(feature = "console_error_panic_hook")]
console_error_panic_hook::set_once();
}

View File

@ -17,7 +17,8 @@ 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" }
utils = { version = "0.1.0", path = "../../../../packages/utils" }
serde = { version = "1.0", features = ["derive"] }
serde-wasm-bindgen = "0.4"
console_error_panic_hook = { version = "0.1.7", optional = true }

View File

@ -1,5 +1,3 @@
mod utils;
use plantarium::{unwrap_int, unwrap_string};
use wasm_bindgen::prelude::*;
// lifted from the `console_log` example
@ -16,7 +14,6 @@ pub fn get_outputs() -> Vec<String> {
#[wasm_bindgen]
pub fn get_input_types() -> String {
utils::set_panic_hook();
r#"{
"min": { "type": "float", "value": 2 },
"max": { "type": "float", "value": 2 },
@ -26,37 +23,36 @@ pub fn get_input_types() -> String {
}
#[wasm_bindgen]
pub fn execute(var_min: JsValue, var_max: JsValue, var_seed: JsValue) -> String {
utils::set_panic_hook();
pub fn execute(args: &[i32]) -> Vec<i32> {
// let min: String;
// if var_min.is_string() {
// min = unwrap_string(var_min);
// } else {
// min = unwrap_int(var_min).to_string();
// }
//
// let max: String;
// if var_max.is_string() {
// max = unwrap_string(var_max);
// } else {
// max = unwrap_int(var_max).to_string();
// }
//
// let seed: String;
// if var_seed.is_string() {
// seed = unwrap_string(var_seed);
// } else {
// seed = unwrap_int(var_seed).to_string();
// }
//
// log(&format!("min: {}, max: {}, seed: {}", min, max, seed));
//
// // Interpolate strings into JSON format
// let json_string = format!(
// r#"{{"__type": "random", "min": {}, "max": {}, "seed": {}}}"#,
// min, max, seed
// );
let min: String;
if var_min.is_string() {
min = unwrap_string(var_min);
} else {
min = unwrap_int(var_min).to_string();
}
let max: String;
if var_max.is_string() {
max = unwrap_string(var_max);
} else {
max = unwrap_int(var_max).to_string();
}
let seed: String;
if var_seed.is_string() {
seed = unwrap_string(var_seed);
} else {
seed = unwrap_int(var_seed).to_string();
}
log(&format!("min: {}, max: {}, seed: {}", min, max, seed));
// Interpolate strings into JSON format
let json_string = format!(
r#"{{"__type": "random", "min": {}, "max": {}, "seed": {}}}"#,
min, max, seed
);
json_string
// json_string
vec![1, args[0]]
}

View File

@ -1,10 +0,0 @@
pub fn set_panic_hook() {
// When the `console_error_panic_hook` feature is enabled, we can call the
// `set_panic_hook` function at least once during initialization, and then
// we will get better error messages if our code ever panics.
//
// For more details see
// https://github.com/rustwasm/console_error_panic_hook#readme
#[cfg(feature = "console_error_panic_hook")]
console_error_panic_hook::set_once();
}

View File

@ -17,7 +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" }
utils = { version = "0.1.0", path = "../../../../packages/utils" }
serde = { version = "1.0", features = ["derive"] }
serde-wasm-bindgen = "0.4"
console_error_panic_hook = { version = "0.1.7", optional = true }

View File

@ -1,5 +1,3 @@
mod utils;
use plantarium::{evaluate_parameters, unwrap_int, unwrap_string};
use wasm_bindgen::prelude::*;
// lifted from the `console_log` example
@ -16,7 +14,6 @@ pub fn get_outputs() -> Vec<String> {
#[wasm_bindgen]
pub fn get_input_types() -> String {
utils::set_panic_hook();
r#"{
"array": { "type": "float", "value": 2, "external": true }
}"#
@ -25,9 +22,8 @@ pub fn get_input_types() -> String {
#[rustfmt::skip]
#[wasm_bindgen]
pub fn execute(array: &[f64]) -> f64 {
utils::set_panic_hook();
let mut sum = 0.0;
pub fn execute(array: &[i32]) -> Vec<i32> {
let mut sum = 0;
array.iter().for_each(|x| sum += x);
return sum;
vec![1, sum]
}

View File

@ -1,10 +0,0 @@
pub fn set_panic_hook() {
// When the `console_error_panic_hook` feature is enabled, we can call the
// `set_panic_hook` function at least once during initialization, and then
// we will get better error messages if our code ever panics.
//
// For more details see
// https://github.com/rustwasm/console_error_panic_hook#readme
#[cfg(feature = "console_error_panic_hook")]
console_error_panic_hook::set_once();
}

View File

@ -17,7 +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" }
utils = { version = "0.1.0", path = "../../../../packages/utils" }
serde = { version = "1.0", features = ["derive"] }
serde-wasm-bindgen = "0.4"
console_error_panic_hook = { version = "0.1.7", optional = true }

View File

@ -1,14 +1,5 @@
mod utils;
use plantarium::{evaluate_parameters, unwrap_int, unwrap_string};
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()]
@ -16,7 +7,6 @@ pub fn get_outputs() -> Vec<String> {
#[wasm_bindgen]
pub fn get_input_types() -> String {
utils::set_panic_hook();
r#"{
"min": { "type": "float", "value": 2 },
"max": { "type": "float", "value": 2 },
@ -25,24 +15,10 @@ pub fn get_input_types() -> String {
.to_string()
}
struct El {
value: Option<f64>,
array: Option<Vec<f64>>,
nested: Option<Box<El>>,
}
#[rustfmt::skip]
#[wasm_bindgen]
pub fn execute(var_min: JsValue, var_max: JsValue, var_seed: JsValue) -> Vec<f64> {
utils::set_panic_hook();
pub fn execute(args: &[i32]) -> Vec<i32> {
// construct vertices of a triangle
let min= evaluate_parameters(var_min);
// let min = evaluate_parameters(var_min);
return vec![
0.0, 0.0, 0.0,
min, 0.0, 0.0,
min, min, 0.0
];
vec![1, 2, 3, 4, args[0]]
}

View File

@ -1,10 +0,0 @@
pub fn set_panic_hook() {
// When the `console_error_panic_hook` feature is enabled, we can call the
// `set_panic_hook` function at least once during initialization, and then
// we will get better error messages if our code ever panics.
//
// For more details see
// https://github.com/rustwasm/console_error_panic_hook#readme
#[cfg(feature = "console_error_panic_hook")]
console_error_panic_hook::set_once();
}

View File

@ -1,18 +1,18 @@
<script lang="ts">
import { T } from "@threlte/core";
import { type OrthographicCamera } from "three";
import { T } from '@threlte/core';
import { type OrthographicCamera } from 'three';
export let camera: OrthographicCamera | undefined = undefined;
export let camera: OrthographicCamera | undefined = undefined;
export let position: [number, number, number] = [0, 0, 4];
export let position: [number, number, number];
</script>
<T.OrthographicCamera
bind:ref={camera}
position.x={0}
position.y={10}
position.z={0}
rotation.x={-Math.PI / 2}
zoom={position[2]}
makeDefault
bind:ref={camera}
position.x={position[0]}
position.y={10}
position.z={position[1]}
rotation.x={-Math.PI / 2}
zoom={position[2]}
makeDefault
/>

View File

@ -393,7 +393,7 @@ export class GraphManager extends EventEmitter<{ "save": Graph, "result": any }>
const state = this.serialize();
this.history.save(state);
this.emit("save", state);
logger.log("saving graph");
logger.log("saving graphs", state);
}
getParentsOfNode(node: Node) {

View File

@ -47,10 +47,6 @@
edges: [number, number, number, string][];
} = null;
$: if (cameraPosition && loaded) {
localStorage.setItem('cameraPosition', JSON.stringify(cameraPosition));
}
let width = globalThis?.innerWidth ?? 100;
let height = globalThis?.innerHeight ?? 100;
@ -61,12 +57,14 @@
cameraPosition[1] - height / cameraPosition[2] / 2,
cameraPosition[1] + height / cameraPosition[2] / 2
];
function setCameraTransform(x: number, y: number, z: number) {
if (!camera) return;
camera.position.x = x;
camera.position.z = y;
camera.zoom = z;
function setCameraTransform(x = cameraPosition[0], y = cameraPosition[1], z = cameraPosition[2]) {
if (camera) {
camera.position.x = x;
camera.position.z = y;
camera.zoom = z;
}
cameraPosition = [x, y, z];
localStorage.setItem('cameraPosition', JSON.stringify(cameraPosition));
}
export let debug = {};
@ -534,7 +532,7 @@
});
}
if (event.key === 'a' && event.ctrlKey) {
if (event.key === 'a' && event.ctrlKey && bodyIsFocused) {
$selectedNodes = new Set($nodes.keys());
}
@ -697,7 +695,6 @@
setCameraTransform(cPosition[0], cPosition[1], cPosition[2]);
}
}
loaded = true;
});
</script>

View File

@ -8,7 +8,7 @@ export async function getNodeWrapper(id: `${string}/${string}/${string}`) {
let wrapperCode = await wrapperReponse.text();
wrapperCode = wrapperCode.replace("wasm = val;", `if(wasm) return;
wasm = val;`);
const wasmWrapper = await import(/*@vite-ignore*/`data:text/javascript;base64,${btoa(wrapperCode)}`);
const wasmWrapper = await import(/*@vite-ignore*/`data:text/javascript;base64,${btoa(wrapperCode)}?id=${id}`);
return wasmWrapper;
}

View File

@ -1,7 +0,0 @@
mod encoding;
mod helpers;
mod nodes;
mod tree;
pub use encoding::*;
pub use helpers::*;
pub use tree::*;

View File

@ -34,7 +34,7 @@ export type NodeType = {
meta?: {
title?: string;
},
execute?: (...args: (string | number | boolean)[]) => unknown;
execute?: (args: number[]) => unknown;
}
export type Socket = {

View File

@ -1,9 +1,13 @@
[package]
name = "plantarium"
name = "utils"
version = "0.1.0"
edition = "2021"
[features]
default = ["console_error_panic_hook"]
[dependencies]
wasm-bindgen = "0.2.92"
serde = { version = "1.0", features = ["derive"] }
serde_json = { version = "1.0", default-features = false, features = ["alloc"] }
console_error_panic_hook = { version = "0.1.7", optional = true }

View File

@ -1,8 +1,8 @@
{
"name": "@node/registry-client",
"name": "@nodes/utils",
"version": "1.0.0",
"description": "",
"main": "index.ts",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},

View File

@ -1,3 +1,11 @@
mod encoding;
mod helpers;
mod nodes;
mod tree;
pub use encoding::*;
pub use helpers::*;
pub use tree::*;
pub fn set_panic_hook() {
// When the `console_error_panic_hook` feature is enabled, we can call the
// `set_panic_hook` function at least once during initialization, and then

View File

@ -2,14 +2,14 @@ pub fn get_args(args: &[i32]) -> Vec<&[i32]> {
let mut idx: usize = 0;
let mut depth = -1;
let mut arg_start_index = 2;
let mut next_bracket_index = 0;
let mut last_bracket_index = 0;
let mut out_args: Vec<&[i32]> = Vec::new();
let length = args.len();
let mut arg_start_index = 2;
while idx < length {
let is_bracket = idx == next_bracket_index;
let value = args[idx];
@ -21,7 +21,6 @@ pub fn get_args(args: &[i32]) -> Vec<&[i32]> {
break;
}
last_bracket_index = next_bracket_index;
next_bracket_index = 1 + idx + args[idx + 1] as usize;
if value == 0 {
@ -31,30 +30,28 @@ pub fn get_args(args: &[i32]) -> Vec<&[i32]> {
}
if depth == 0 {
if value == 1 {
out_args.push(&args[arg_start_index..idx]);
arg_start_index = idx + 2;
}
// skip over the bracket encoding
idx += 2;
} else {
// skip to the next bracket if we are at depth > 0
idx = next_bracket_index - 1;
idx = next_bracket_index;
}
continue;
}
out_args.push(&args[arg_start_index..=idx]);
arg_start_index = idx + 1;
// this is at the end of args where normally multiple ] are encoded
if depth < 0 {
break;
}
// remove starting bracket from single numbers
if idx - arg_start_index < 3 && last_bracket_index == arg_start_index {
arg_start_index += 2;
}
out_args.push(&args[arg_start_index..=idx]);
idx += 1;
arg_start_index = idx;
}
out_args
@ -69,7 +66,7 @@ pub fn evaluate_node(input_args: &[i32]) -> (i32, i32) {
}
}
pub fn evaluate_args(input_args: &[i32]) -> (i32, i32) {
pub fn evaluate_args(input_args: &[i32]) -> Vec<i32> {
let args = get_args(input_args);
let mut resolved: Vec<i32> = Vec::new();
@ -77,20 +74,25 @@ pub fn evaluate_args(input_args: &[i32]) -> (i32, i32) {
for arg in args {
if arg.len() == 1 {
resolved.push(arg[0]);
} else if arg.len() == 4 && arg[0] == 0 && arg[1] == 3 {
resolved.push(arg[2]);
resolved.push(arg[3]);
} else {
let res = evaluate_args(arg);
resolved.push(res.0);
resolved.push(res.1);
resolved.push(res[0]);
resolved.push(res[1]);
}
}
println!("resolved: {:?}", resolved);
if resolved.len() > 1 {
evaluate_node(&resolved)
let res = evaluate_node(&resolved);
vec![res.0, res.1]
} else {
(resolved[0], resolved[1])
resolved
}
}
#[cfg(test)]
mod tests {
@ -106,13 +108,13 @@ mod tests {
// the numbers are f32 floats encoded as two i32's
let result = evaluate_args(&input);
let decoded = decode_float(result.0, result.1);
let decoded = decode_float(result[0], result[1]);
assert_eq!(decoded, 6.0);
}
#[test]
fn test_example_input_a() {
fn test_get_args_input_a() {
let input_a = vec![0, 4, 1, 2, 3, 0, 7, 1, 2, 4, 2, 4, 1, 1, 1, 1];
// -> [1, 2, 3, [1, 2, 4, 2, 4]]
@ -126,7 +128,7 @@ mod tests {
}
#[test]
fn test_example_input_b() {
fn test_get_args_input_b() {
let input_b = vec![0, 3, 7, 1, 0, 4, 4, 2, 4, 1, 2, 2, 0, 3, 2, 3, 1, 1, 1, 1];
// -> [1,[4,2,4], 2, [2,3]]
@ -139,4 +141,29 @@ mod tests {
assert_eq!(args[3], [2]);
assert_eq!(args[4], [0, 3, 2, 3]);
}
#[test]
fn test_get_args_nested_inputs() {
let input = vec![
0, 3, 0, 2, 0, 3, 0, 0, 0, 3, 7549747, 127, 1, 1, 0, 3, 0, 128, 1, 1, 1, 1, 0, 3, 0,
128, 1, 1, 1, 1,
];
// each math node has 4 numbers
// 0 -> type of node (0 -> math node)
// 1 -> op_type for math operation (0 -> add, 1 -> sub, 2 -> mul, 3 -> div)
// 2 -> first number
// 3 -> second number
let args = get_args(&input);
assert_eq!(args.len(), 4);
assert_eq!(args[0], [0]);
assert_eq!(args[1], [2]);
assert_eq!(args[3], [0, 3, 0, 128]);
let nested_args = get_args(args[2]);
assert_eq!(nested_args.len(), 4);
}
}

View File

@ -93,6 +93,10 @@ importers:
specifier: ^1.2.0
version: 1.4.0
nodes/max/plantarium/array: {}
nodes/max/plantarium/array/pkg: {}
nodes/max/plantarium/float: {}
nodes/max/plantarium/float/pkg: {}
@ -109,6 +113,14 @@ importers:
nodes/max/plantarium/random/pkg: {}
nodes/max/plantarium/sum: {}
nodes/max/plantarium/sum/pkg: {}
nodes/max/plantarium/triangle: {}
nodes/max/plantarium/triangle/pkg: {}
packages/graph-interface:
dependencies:
'@nodes/types':