Skip to content

Commit

Permalink
fix: wasm serde
Browse files Browse the repository at this point in the history
  • Loading branch information
wormtql committed Jul 17, 2024
1 parent 3dcbc71 commit 838b3da
Show file tree
Hide file tree
Showing 11 changed files with 57 additions and 44 deletions.
12 changes: 12 additions & 0 deletions mona_wasm/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions mona_wasm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -39,6 +39,6 @@ impl CalcArtifactBestSet {
}
// utils::log!("{:?}", arr);

JsValue::from_serde(&arr).unwrap()
serde_wasm_bindgen::to_value(&arr).unwrap()
}
}
6 changes: 3 additions & 3 deletions mona_wasm/src/applications/bonus_per_stat/interface_wasm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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(),
Expand Down
12 changes: 6 additions & 6 deletions mona_wasm/src/applications/calculator/interface_calculator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Element> = fumo.into_serde().unwrap();
let input: CalculatorConfigInterface = serde_wasm_bindgen::from_value(value).unwrap();
let fumo: Option<Element> = serde_wasm_bindgen::from_value(fumo).unwrap();

let character: Character<ComplicatedAttributeGraph> = input.character.to_character();
let weapon = input.weapon.to_weapon(&character);
Expand Down Expand Up @@ -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<SimpleAttributeGraph2> = input.character.to_character();
let weapon = input.weapon.to_weapon(&character);
Expand Down
6 changes: 3 additions & 3 deletions mona_wasm/src/applications/common_interface/get_attribute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ pub struct GetAttributeInterface {
artifact_config: Option<ArtifactEffectConfig>
}

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<ComplicatedAttributeGraph> = input.character.to_character();
let weapon: Weapon<ComplicatedAttributeGraph> = input.weapon.to_weapon(&character);
Expand All @@ -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()
}
14 changes: 7 additions & 7 deletions mona_wasm/src/applications/common_interface/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Artifact> = artifacts.into_serde().unwrap();
let artifacts: Vec<Artifact> = serde_wasm_bindgen::from_value(artifacts).unwrap();

let character = character_interface.to_character();
let weapon = weapon_interface.to_weapon(&character);
Expand All @@ -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()
}
}
16 changes: 8 additions & 8 deletions mona_wasm/src/applications/dsl/dsl_interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Artifact> = artifacts.into_serde().unwrap();
let damage_env: RunInput = serde_wasm_bindgen::from_value(damage_env).unwrap();
let artifacts: Vec<Artifact> = serde_wasm_bindgen::from_value(artifacts).unwrap();

// get all items
let character = damage_env.character.to_character();
Expand All @@ -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
};

Expand All @@ -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
Expand All @@ -121,6 +121,6 @@ impl DSLInterface {
output
};

JsValue::from_serde(&ret).unwrap()
serde_wasm_bindgen::to_value(&ret).unwrap()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<Artifact> = artifacts.into_serde().unwrap();
let artifacts: Vec<Artifact> = serde_wasm_bindgen::from_value(artifacts).unwrap();
let artifacts_ref: Vec<_> = artifacts.iter().collect();

let character = input.character.to_character();
Expand Down Expand Up @@ -64,6 +64,6 @@ impl OptimizeSingleWasm {
100
);

JsValue::from_serde(&result).unwrap()
serde_wasm_bindgen::to_value(&result).unwrap()
}
}
8 changes: 4 additions & 4 deletions mona_wasm/src/applications/potential/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Artifact> = artifacts.into_serde().unwrap();
let pf_interface = pf_interface.into_serde().unwrap();
let artifacts: Vec<Artifact> = 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()
}
}
8 changes: 4 additions & 4 deletions mona_wasm/src/applications/team_optimize/interface_wasm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Artifact> = artifacts.into_serde().unwrap();
let artifacts: Vec<Artifact> = serde_wasm_bindgen::from_value(artifacts).unwrap();

let artifacts_ref: Vec<&Artifact> = artifacts.iter().collect();
let hyper_param = match input.hyper_param {
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 838b3da

Please sign in to comment.