From 838b3dab4fdb0e59d482c9c6eea5c11598e2188b Mon Sep 17 00:00:00 2001 From: wormtql <584130248@qq.com> Date: Wed, 17 Jul 2024 21:43:43 +0800 Subject: [PATCH] fix: wasm serde --- mona_wasm/Cargo.lock | 12 ++++++++++++ mona_wasm/Cargo.toml | 5 +++-- .../artifact_best_set/wasm_interface.rs | 6 +++--- .../bonus_per_stat/interface_wasm.rs | 6 +++--- .../calculator/interface_calculator.rs | 12 ++++++------ .../common_interface/get_attribute.rs | 6 +++--- .../src/applications/common_interface/mod.rs | 14 +++++++------- mona_wasm/src/applications/dsl/dsl_interface.rs | 16 ++++++++-------- .../optimize_artifacts/interface_wasm.rs | 8 ++++---- mona_wasm/src/applications/potential/mod.rs | 8 ++++---- .../applications/team_optimize/interface_wasm.rs | 8 ++++---- 11 files changed, 57 insertions(+), 44 deletions(-) diff --git a/mona_wasm/Cargo.lock b/mona_wasm/Cargo.lock index 668b0de1..f34b3ef0 100644 --- a/mona_wasm/Cargo.lock +++ b/mona_wasm/Cargo.lock @@ -357,6 +357,7 @@ dependencies = [ "rand", "rustc-hash", "serde", + "serde-wasm-bindgen", "serde_json", "smallvec", "strum 0.26.2", @@ -615,6 +616,17 @@ dependencies = [ "serde_derive", ] +[[package]] +name = "serde-wasm-bindgen" +version = "0.6.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8302e169f0eddcc139c70f139d19d6467353af16f9fce27e8c30158036a1e16b" +dependencies = [ + "js-sys", + "serde", + "wasm-bindgen", +] + [[package]] name = "serde_derive" version = "1.0.198" diff --git a/mona_wasm/Cargo.toml b/mona_wasm/Cargo.toml index 8acb3f91..d4d23b47 100644 --- a/mona_wasm/Cargo.toml +++ b/mona_wasm/Cargo.toml @@ -23,8 +23,9 @@ mona_dsl = { path = "../mona_dsl" } wasm-bindgen = { version = "0.2.92", features = ["serde-serialize"] } lazy_static = "1.4.0" rand = "0.8.4" -serde = { version = "1.0.133", features = ["derive"] } -serde_json = "1.0.78" +serde = { version = "1.0", features = ["derive"] } +serde_json = "1.0" +serde-wasm-bindgen = "0.6.5" console_error_panic_hook = { version = "0.1.7", optional = true } web-sys = { version = "0.3.55", features = ["console"] } getrandom = { version = "0.2", features = ["js"] } diff --git a/mona_wasm/src/applications/artifact_best_set/wasm_interface.rs b/mona_wasm/src/applications/artifact_best_set/wasm_interface.rs index 01863166..c32af4f8 100644 --- a/mona_wasm/src/applications/artifact_best_set/wasm_interface.rs +++ b/mona_wasm/src/applications/artifact_best_set/wasm_interface.rs @@ -9,10 +9,10 @@ pub struct CalcArtifactBestSet; #[wasm_bindgen] impl CalcArtifactBestSet { - pub fn calc_artifact_best_set(args: &JsValue) -> JsValue { + pub fn calc_artifact_best_set(args: JsValue) -> JsValue { set_panic_hook(); - let calc_best_set_interface: CalcArtifactBestSetInterface = args.into_serde().unwrap(); + let calc_best_set_interface: CalcArtifactBestSetInterface = serde_wasm_bindgen::from_value(args).unwrap(); let character = calc_best_set_interface.character.to_character(); let weapon = calc_best_set_interface.weapon.to_weapon(&character); @@ -39,6 +39,6 @@ impl CalcArtifactBestSet { } // utils::log!("{:?}", arr); - JsValue::from_serde(&arr).unwrap() + serde_wasm_bindgen::to_value(&arr).unwrap() } } diff --git a/mona_wasm/src/applications/bonus_per_stat/interface_wasm.rs b/mona_wasm/src/applications/bonus_per_stat/interface_wasm.rs index fad4a243..e0024240 100644 --- a/mona_wasm/src/applications/bonus_per_stat/interface_wasm.rs +++ b/mona_wasm/src/applications/bonus_per_stat/interface_wasm.rs @@ -48,10 +48,10 @@ pub struct BonusPerStat; #[wasm_bindgen] impl BonusPerStat { - pub fn bonus_per_stat(val: &JsValue) -> JsValue { + pub fn bonus_per_stat(val: JsValue) -> JsValue { utils::set_panic_hook(); - let input: WasmInput = val.into_serde().unwrap(); + let input: WasmInput = serde_wasm_bindgen::from_value(val).unwrap(); let character = input.character.to_character(); let weapon = input.weapon.to_weapon(&character); @@ -77,7 +77,7 @@ impl BonusPerStat { // utils::log!("{:?}", result.atk); // utils::log!("{:?}", result.atk.as_ptr()); - JsValue::from_serde(&result).unwrap() + serde_wasm_bindgen::to_value(&result).unwrap() // WasmOutput { // atk_ptr: result.atk.as_ptr(), diff --git a/mona_wasm/src/applications/calculator/interface_calculator.rs b/mona_wasm/src/applications/calculator/interface_calculator.rs index ae9884ad..6d51a86c 100644 --- a/mona_wasm/src/applications/calculator/interface_calculator.rs +++ b/mona_wasm/src/applications/calculator/interface_calculator.rs @@ -69,12 +69,12 @@ pub struct CalculatorConfigInterface { #[wasm_bindgen] impl CalculatorInterface { - pub fn get_damage_analysis(value: &JsValue, fumo: &JsValue) -> JsValue { + pub fn get_damage_analysis(value: JsValue, fumo: JsValue) -> JsValue { utils::set_panic_hook(); // utils::log!("start"); - let input: CalculatorConfigInterface = value.into_serde().unwrap(); - let fumo: Option = fumo.into_serde().unwrap(); + let input: CalculatorConfigInterface = serde_wasm_bindgen::from_value(value).unwrap(); + let fumo: Option = serde_wasm_bindgen::from_value(fumo).unwrap(); let character: Character = input.character.to_character(); let weapon = input.weapon.to_weapon(&character); @@ -106,13 +106,13 @@ impl CalculatorInterface { fumo, ); - JsValue::from_serde(&result).unwrap() + serde_wasm_bindgen::to_value(&result).unwrap() } - pub fn get_transformative_damage(value: &JsValue) -> TransformativeDamage { + pub fn get_transformative_damage(value: JsValue) -> TransformativeDamage { utils::set_panic_hook(); - let input: CalculatorConfigInterface = value.into_serde().unwrap(); + let input: CalculatorConfigInterface = serde_wasm_bindgen::from_value(value).unwrap(); let character: Character = input.character.to_character(); let weapon = input.weapon.to_weapon(&character); diff --git a/mona_wasm/src/applications/common_interface/get_attribute.rs b/mona_wasm/src/applications/common_interface/get_attribute.rs index 9f8078ca..e4e34b46 100644 --- a/mona_wasm/src/applications/common_interface/get_attribute.rs +++ b/mona_wasm/src/applications/common_interface/get_attribute.rs @@ -17,8 +17,8 @@ pub struct GetAttributeInterface { artifact_config: Option } -pub fn get_attribute(val: &JsValue) -> JsValue { - let input: GetAttributeInterface = val.into_serde().unwrap(); +pub fn get_attribute(val: JsValue) -> JsValue { + let input: GetAttributeInterface = serde_wasm_bindgen::from_value(val).unwrap(); let character: Character = input.character.to_character(); let weapon: Weapon = input.weapon.to_weapon(&character); @@ -44,5 +44,5 @@ pub fn get_attribute(val: &JsValue) -> JsValue { ); let result = AttributeNoReactive::from(&attribute); - JsValue::from_serde(&result).unwrap() + serde_wasm_bindgen::to_value(&result).unwrap() } diff --git a/mona_wasm/src/applications/common_interface/mod.rs b/mona_wasm/src/applications/common_interface/mod.rs index d9bec022..fb7cb80c 100644 --- a/mona_wasm/src/applications/common_interface/mod.rs +++ b/mona_wasm/src/applications/common_interface/mod.rs @@ -8,16 +8,16 @@ pub struct CommonInterface {} #[wasm_bindgen] impl CommonInterface { - pub fn get_attribute(val: &JsValue) -> JsValue { + pub fn get_attribute(val: JsValue) -> JsValue { get_attribute::get_attribute(val) } - pub fn get_artifacts_rank_by_character(character: &JsValue, weapon: &JsValue, tf: &JsValue, artifacts: &JsValue) -> JsValue { - let character_interface: CharacterInterface = character.into_serde().unwrap(); - let weapon_interface: WeaponInterface = weapon.into_serde().unwrap(); - let tf_interface: TargetFunctionInterface = tf.into_serde().unwrap(); + pub fn get_artifacts_rank_by_character(character: JsValue, weapon: JsValue, tf: JsValue, artifacts: JsValue) -> JsValue { + let character_interface: CharacterInterface = serde_wasm_bindgen::from_value(character).unwrap(); + let weapon_interface: WeaponInterface = serde_wasm_bindgen::from_value(weapon).unwrap(); + let tf_interface: TargetFunctionInterface = serde_wasm_bindgen::from_value(tf).unwrap(); - let artifacts: Vec = artifacts.into_serde().unwrap(); + let artifacts: Vec = serde_wasm_bindgen::from_value(artifacts).unwrap(); let character = character_interface.to_character(); let weapon = weapon_interface.to_weapon(&character); @@ -35,6 +35,6 @@ impl CommonInterface { scores.sort_by(|a, b| b.1.partial_cmp(&a.1).unwrap()); - JsValue::from_serde(&scores).unwrap() + serde_wasm_bindgen::to_value(&scores).unwrap() } } diff --git a/mona_wasm/src/applications/dsl/dsl_interface.rs b/mona_wasm/src/applications/dsl/dsl_interface.rs index 504dd39a..8f27ce04 100644 --- a/mona_wasm/src/applications/dsl/dsl_interface.rs +++ b/mona_wasm/src/applications/dsl/dsl_interface.rs @@ -51,10 +51,10 @@ impl RunResult { #[wasm_bindgen] impl DSLInterface { - pub fn run(source: &str, damage_env: &JsValue, artifacts: &JsValue) -> JsValue { + pub fn run(source: &str, damage_env: JsValue, artifacts: JsValue) -> JsValue { utils::set_panic_hook(); - let damage_env: RunInput = damage_env.into_serde().unwrap(); - let artifacts: Vec = artifacts.into_serde().unwrap(); + let damage_env: RunInput = serde_wasm_bindgen::from_value(damage_env).unwrap(); + let artifacts: Vec = serde_wasm_bindgen::from_value(artifacts).unwrap(); // get all items let character = damage_env.character.to_character(); @@ -73,7 +73,7 @@ impl DSLInterface { // compile let code_obj = match compile_source_to_code_object(source) { - Err(e) => return JsValue::from_serde(&RunResult::from_compile_error(&e)).unwrap(), + Err(e) => return serde_wasm_bindgen::to_value(&RunResult::from_compile_error(&e)).unwrap(), Ok(v) => v }; @@ -100,16 +100,16 @@ impl DSLInterface { // init if let Err(e) = env.init_prop() { - return JsValue::from_serde(&RunResult::from_runtime_error(&e)).unwrap(); + return serde_wasm_bindgen::to_value(&RunResult::from_runtime_error(&e)).unwrap(); } if let Err(e) = env.init_damage() { - return JsValue::from_serde(&RunResult::from_runtime_error(&e)).unwrap(); + return serde_wasm_bindgen::to_value(&RunResult::from_runtime_error(&e)).unwrap(); } utils::log!("{:?}", env.namespace.map.keys()); // execute if let Err(e) = env.execute() { - return JsValue::from_serde(&RunResult::from_runtime_error(&e)).unwrap(); + return serde_wasm_bindgen::to_value(&RunResult::from_runtime_error(&e)).unwrap(); } // get output @@ -121,6 +121,6 @@ impl DSLInterface { output }; - JsValue::from_serde(&ret).unwrap() + serde_wasm_bindgen::to_value(&ret).unwrap() } } \ No newline at end of file diff --git a/mona_wasm/src/applications/optimize_artifacts/interface_wasm.rs b/mona_wasm/src/applications/optimize_artifacts/interface_wasm.rs index c2acb087..32cea1e0 100644 --- a/mona_wasm/src/applications/optimize_artifacts/interface_wasm.rs +++ b/mona_wasm/src/applications/optimize_artifacts/interface_wasm.rs @@ -22,14 +22,14 @@ pub struct OptimizeSingleWasm; #[wasm_bindgen] impl OptimizeSingleWasm { - pub fn optimize(val: &JsValue, artifacts: &JsValue) -> JsValue { + pub fn optimize(val: JsValue, artifacts: JsValue) -> JsValue { utils::set_panic_hook(); - let input: OptimizeArtifactInterface = match val.into_serde() { + let input: OptimizeArtifactInterface = match serde_wasm_bindgen::from_value(val) { Ok(x) => x, Err(e) => panic!("{}", e) }; - let artifacts: Vec = artifacts.into_serde().unwrap(); + let artifacts: Vec = serde_wasm_bindgen::from_value(artifacts).unwrap(); let artifacts_ref: Vec<_> = artifacts.iter().collect(); let character = input.character.to_character(); @@ -64,6 +64,6 @@ impl OptimizeSingleWasm { 100 ); - JsValue::from_serde(&result).unwrap() + serde_wasm_bindgen::to_value(&result).unwrap() } } diff --git a/mona_wasm/src/applications/potential/mod.rs b/mona_wasm/src/applications/potential/mod.rs index a3d84e21..7ff0fdd4 100644 --- a/mona_wasm/src/applications/potential/mod.rs +++ b/mona_wasm/src/applications/potential/mod.rs @@ -20,15 +20,15 @@ pub fn get_potential(artifacts: &[Artifact], pf_interface: &PotentialFunctionInt #[wasm_bindgen] impl PotentialInterface { - pub fn get_potential(artifacts: &JsValue, pf_interface: &JsValue) -> JsValue { + pub fn get_potential(artifacts: JsValue, pf_interface: JsValue) -> JsValue { set_panic_hook(); - let artifacts: Vec = artifacts.into_serde().unwrap(); - let pf_interface = pf_interface.into_serde().unwrap(); + let artifacts: Vec = serde_wasm_bindgen::from_value(artifacts).unwrap(); + let pf_interface = serde_wasm_bindgen::from_value(pf_interface).unwrap(); let mut results = get_potential(&artifacts, &pf_interface); results.sort_by(|x, y| y.1.partial_cmp(&x.1).unwrap()); - JsValue::from_serde(&results).unwrap() + serde_wasm_bindgen::to_value(&results).unwrap() } } diff --git a/mona_wasm/src/applications/team_optimize/interface_wasm.rs b/mona_wasm/src/applications/team_optimize/interface_wasm.rs index 957d42cf..2c30a9c2 100644 --- a/mona_wasm/src/applications/team_optimize/interface_wasm.rs +++ b/mona_wasm/src/applications/team_optimize/interface_wasm.rs @@ -57,12 +57,12 @@ fn smallvec_to_optimize_entry(v: &SmallVec<[u64; 5]>, artifacts_by_id: &HashMap< #[wasm_bindgen] impl TeamOptimizationWasm { - pub fn optimize_team2(val: &JsValue, artifacts: &JsValue) -> JsValue { + pub fn optimize_team2(val: JsValue, artifacts: JsValue) -> JsValue { utils::set_panic_hook(); - let mut input: OptimizeTeamInterface2 = val.into_serde().unwrap(); + let mut input: OptimizeTeamInterface2 = serde_wasm_bindgen::from_value(val).unwrap(); - let artifacts: Vec = artifacts.into_serde().unwrap(); + let artifacts: Vec = serde_wasm_bindgen::from_value(artifacts).unwrap(); let artifacts_ref: Vec<&Artifact> = artifacts.iter().collect(); let hyper_param = match input.hyper_param { @@ -94,7 +94,7 @@ impl TeamOptimizationWasm { artifacts: results }; - JsValue::from_serde(&ret).unwrap() + serde_wasm_bindgen::to_value(&ret).unwrap() } // pub fn optimize_team(val: &JsValue) -> JsValue {