hm-asm-web should not panic if it receives invalid asm code

This commit is contained in:
Henrik Böving
2021-03-05 19:59:16 +01:00
parent 3b49e98d97
commit f2a0966b17
3 changed files with 21 additions and 9 deletions

View File

@@ -9,9 +9,10 @@ use hm_asm_simulator::{
#[wasm_bindgen]
pub fn simulate(code: &str, cycles: usize) -> JsValue {
let instructions = parse_asm(
AsmParser::parse(Rule::program, &code).unwrap_or_else(|e| panic!("{}", e)),
);
let instructions = parse_asm(match AsmParser::parse(Rule::program, &code) {
Ok(instructions) => instructions,
Err(e) => return JsValue::from_str(&format!("{}", e))
});
let states = hm_asm_simulator::simulate::simulate(instructions, cycles);
JsValue::from_serde(&states).unwrap()
@@ -20,9 +21,12 @@ pub fn simulate(code: &str, cycles: usize) -> JsValue {
#[wasm_bindgen]
pub fn assemble(code: &str) -> JsValue {
let instructions = parse_asm(
AsmParser::parse(Rule::program, &code).unwrap_or_else(|e| panic!("{}", e)),
);
let instructions = parse_asm(match AsmParser::parse(Rule::program, &code) {
Ok(instructions) => instructions,
Err(e) => return JsValue::from_str(&format!("{}", e))
});
let binary = generate_binary(instructions);
JsValue::from_serde(&binary).unwrap()
}