]>
Witch of Git - jade-rose/blob - toolchain/src/lib.rs
2 #[cfg(target_arch = "wasm32")]
7 use std
::io
::{self, Read
};
10 Io
{ position
: usize, error
: io
::Error
},
13 pub fn disassemble(reader
: &mut impl io
::Read
) -> Result
<Vec
<Inst
>, Error
> {
14 let mut results
= Vec
::new();
15 let mut reader
= PositionedRead
::new(reader
);
17 let position
= reader
.position
;
18 match Inst
::decode(&mut reader
) {
19 Ok(inst
) => results
.push(inst
),
20 Err(err
) if err
.kind() == io
::ErrorKind
::UnexpectedEof
=> break,
22 return Err(Error
::Io
{
32 struct PositionedRead
<'a
, R
> {
37 impl<'a
, R
> PositionedRead
<'a
, R
> {
38 pub fn new(inner
: &'a
mut R
) -> Self {
39 PositionedRead
{ inner
, position
: 0 }
43 impl<'a
, R
: Read
> Read
for PositionedRead
<'a
, R
> {
44 fn read(&mut self, buf
: &mut [u8]) -> io
::Result
<usize> {
45 let read
= self.inner
.read(buf
)?
;
46 self.position
+= read
;
54 use io
::{self, Cursor
};
56 fn test_disassemble_errors_at_end() {
57 let mut cursor
= Cursor
::new(&[0xff]);
58 let err
= Inst
::decode(&mut cursor
).unwrap
_err
();
59 assert_eq
!(err
.kind(), io
::ErrorKind
::InvalidInput
);
61 err
.get_ref().map(|x
| x
.to_string()),
62 Some("Extended instruction missing operand".into
())