]>
Witch of Git - jade-rose/blob - toolchain/src/js.rs
6 use std
::io
::{Cursor
, Write
};
7 use wasm_bindgen
::prelude
::*;
10 pub fn assemble(code
: &str) -> Result
<Vec
<u8>, JsValue
> {
11 let mut result
= Cursor
::new(Vec
::new());
12 for (i
, line
) in code
.lines().enumerate() {
13 let line
= line
.trim();
17 let inst
= Inst
::parse(line
)
18 .ok_or_else(|| js_sys
::Error
::new(&format
!("Error parsing line {}", i
+ 1)))?
;
19 inst
.encode(&mut result
)
20 .expect("can always write into a vector");
22 Ok(result
.into
_inner
())
26 pub fn disassemble(data
: Vec
<u8>) -> Result
<String
, JsValue
> {
27 let mut input
= Cursor
::new(data
);
28 let mut output
= Cursor
::new(Vec
::new());
29 for inst
in super::disassemble(&mut input
)?
{
30 writeln
!(output
, "{}", inst
).unwrap
();
32 // Can use _unchecked if we want because we control the display impl
33 Ok(String
::from_utf8(output
.into
_inner
()).unwrap
())
36 impl From
<Error
> for JsValue
{
37 fn from(e
: Error
) -> JsValue
{
39 Error
::Io
{ position
, error
} => {
40 js_sys
::Error
::new(&format
!("Error at byte {}: {}", position
, error
)).into
()