]>
Witch of Git - jade-rose/blob - toolchain/src/js.rs
5 use js_sys
::{self, Reflect
};
6 use std
::io
::{self, 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
).ok_or_else(|| Error
::Io
{
19 error
: io
::Error
::new(io
::ErrorKind
::InvalidInput
, "Parse error"),
21 inst
.encode(&mut result
)
22 .expect("can always write into a vector");
24 Ok(result
.into
_inner
())
28 pub fn disassemble(data
: &[u8]) -> Result
<String
, JsValue
> {
29 let mut input
= Cursor
::new(data
);
30 let mut output
= Cursor
::new(Vec
::new());
31 for inst
in super::disassemble(&mut input
)?
{
32 writeln
!(output
, "{}", inst
).unwrap
();
34 // Can use _unchecked if we want because we control the display impl
35 Ok(String
::from_utf8(output
.into
_inner
()).unwrap
())
38 impl From
<Error
> for JsValue
{
39 fn from(e
: Error
) -> JsValue
{
41 Error
::Io
{ position
, error
} => {
42 let obj
= js_sys
::Error
::new(&format
!("{}", error
)).into
();
43 Reflect
::set(&obj
, &"position".into
(), &(position
as u32).into
()).unwrap
();
58 fn test_disassemble() {
59 assert
!(disassemble(&[0xff]).is
_err
());