feat: first working version with parameters

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

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

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