opcodes stub & data segment

This commit is contained in:
NikVolf 2017-04-03 17:31:51 +03:00
parent ce0c1551fa
commit a63f31a0f7
4 changed files with 98 additions and 2 deletions

View File

@ -1,5 +1,6 @@
use std::io; use std::io;
use super::{Deserialize, Error, VarUint7, use super::{
Serialize, Deserialize, Error, VarUint7,
VarUint1, VarUint32, CountedList, BlockType, VarUint1, VarUint32, CountedList, BlockType,
Uint32, VarUint64, Uint64 Uint32, VarUint64, Uint64
}; };
@ -527,3 +528,61 @@ impl Deserialize for Opcode {
) )
} }
} }
macro_rules! op {
($writer: expr, $byte: expr) => ({
let b: u8 = $byte;
$writer.write_all(&[b])?;
});
($writer: expr, $byte: expr, $s: block) => ({
op!($writer, $byte);
$s;
});
}
impl Serialize for Opcode {
type Error = Error;
fn serialize<W: io::Write>(self, writer: &mut W) -> Result<(), Self::Error> {
use self::Opcode::*;
match self {
Unreachable => op!(writer, 0x00),
Nop => op!(writer, 0x01),
Block(block_type, ops) => op!(writer, 0x02, {
block_type.serialize(writer)?;
ops.serialize(writer)?;
}),
End => op!(writer, 0x0b),
_ => unreachable!(),
}
Ok(())
}
}
impl Serialize for Opcodes {
type Error = Error;
fn serialize<W: io::Write>(self, writer: &mut W) -> Result<(), Self::Error> {
for op in self.0.into_iter() {
op.serialize(writer)?;
}
Ok(())
}
}
impl Serialize for InitExpr {
type Error = Error;
fn serialize<W: io::Write>(self, writer: &mut W) -> Result<(), Self::Error> {
for op in self.0.into_iter() {
op.serialize(writer)?;
}
Ok(())
}
}

View File

@ -125,6 +125,12 @@ impl From<VarInt7> for i8 {
} }
} }
impl From<i8> for VarInt7 {
fn from(v: i8) -> VarInt7 {
VarInt7(v)
}
}
impl Deserialize for VarInt7 { impl Deserialize for VarInt7 {
type Error = Error; type Error = Error;
@ -138,6 +144,18 @@ impl Deserialize for VarInt7 {
} }
} }
impl Serialize for VarInt7 {
type Error = Error;
fn serialize<W: io::Write>(self, writer: &mut W) -> Result<(), Self::Error> {
// todo check range?
let mut b: u8 = self.0 as u8;
if self.0 < 0 { b |= 0b0100_0000 }
writer.write_all(&[b])?;
Ok(())
}
}
#[derive(Copy, Clone)] #[derive(Copy, Clone)]
pub struct Uint32(u32); pub struct Uint32(u32);

View File

@ -68,6 +68,9 @@ impl Serialize for DataSegment {
type Error = Error; type Error = Error;
fn serialize<W: io::Write>(self, writer: &mut W) -> Result<(), Self::Error> { fn serialize<W: io::Write>(self, writer: &mut W) -> Result<(), Self::Error> {
VarUint32::from(self.index).serialize(writer)?;
self.offset.serialize(writer)?;
writer.write_all(&self.value[..])?;
Ok(()) Ok(())
} }
} }

View File

@ -1,5 +1,5 @@
use std::io; use std::io;
use super::{Deserialize, Error, VarUint7, VarInt7, VarUint1, CountedList}; use super::{Deserialize, Serialize, Error, VarUint7, VarInt7, VarUint1, CountedList};
pub enum Type { pub enum Type {
Function(FunctionType), Function(FunctionType),
@ -60,6 +60,22 @@ impl Deserialize for BlockType {
} }
} }
impl Serialize for BlockType {
type Error = Error;
fn serialize<W: io::Write>(self, writer: &mut W) -> Result<(), Self::Error> {
let val: VarInt7 = match self {
BlockType::NoResult => -0x40i8,
BlockType::Value(ValueType::I32) => -0x01,
BlockType::Value(ValueType::I64) => -0x02,
BlockType::Value(ValueType::F32) => -0x03,
BlockType::Value(ValueType::F64) => -0x04,
}.into();
val.serialize(writer)?;
Ok(())
}
}
pub struct FunctionType { pub struct FunctionType {
form: u8, form: u8,