1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
use io;
use std::vec::Vec;
use super::{
	Deserialize, Error, ValueType, VarUint32, CountedList, Instructions,
	Serialize, CountedWriter, CountedListWriter,
};
use elements::section::SectionReader;

/// Function signature (type reference)
#[derive(Debug, Copy, Clone, PartialEq)]
pub struct Func(u32);

impl Func {
	/// New function signature
	pub fn new(type_ref: u32) -> Self { Func(type_ref) }

	/// Function signature type reference.
	pub fn type_ref(&self) -> u32 {
		self.0
	}

	/// Function signature type reference (mutable).
	pub fn type_ref_mut(&mut self) -> &mut u32 {
		&mut self.0
	}
}

impl Serialize for Func {
	type Error = Error;

	fn serialize<W: io::Write>(self, writer: &mut W) -> Result<(), Self::Error> {
		VarUint32::from(self.0).serialize(writer)
	}
}

impl Deserialize for Func {
	 type Error = Error;

	fn deserialize<R: io::Read>(reader: &mut R) -> Result<Self, Self::Error> {
		Ok(Func(VarUint32::deserialize(reader)?.into()))
	}
}

/// Local definition inside the function body.
#[derive(Debug, Copy, Clone, PartialEq)]
pub struct Local {
	count: u32,
	value_type: ValueType,
}

impl Local {
	/// New local with `count` and `value_type`.
	pub fn new(count: u32, value_type: ValueType) -> Self {
		Local { count: count, value_type: value_type }
	}

	/// Number of locals with the shared type.
	pub fn count(&self) -> u32 { self.count }

	/// Type of the locals.
	pub fn value_type(&self) -> ValueType { self.value_type }
}

impl Deserialize for Local {
	 type Error = Error;

	fn deserialize<R: io::Read>(reader: &mut R) -> Result<Self, Self::Error> {
		let count = VarUint32::deserialize(reader)?;
		let value_type = ValueType::deserialize(reader)?;
		Ok(Local { count: count.into(), value_type: value_type })
	}
}

impl Serialize for Local {
	type Error = Error;

	fn serialize<W: io::Write>(self, writer: &mut W) -> Result<(), Self::Error> {
		VarUint32::from(self.count).serialize(writer)?;
		self.value_type.serialize(writer)?;
		Ok(())
	}
}

/// Function body definition.
#[derive(Debug, Clone, PartialEq)]
pub struct FuncBody {
	locals: Vec<Local>,
	instructions: Instructions,
}

impl FuncBody {
	/// New function body with given `locals` and `instructions`.
	pub fn new(locals: Vec<Local>, instructions: Instructions) -> Self {
		FuncBody { locals: locals, instructions: instructions }
	}

	/// List of individual instructions.
	pub fn empty() -> Self {
		FuncBody { locals: Vec::new(), instructions: Instructions::empty() }
	}

	/// Locals declared in function body.
	pub fn locals(&self) -> &[Local] { &self.locals }

	/// Instruction list of the function body. Minimal instruction list
	///
	/// is just `&[Instruction::End]`
	pub fn code(&self) -> &Instructions { &self.instructions }

	/// Locals declared in function body (mutable).
	pub fn locals_mut(&mut self) -> &mut Vec<Local> { &mut self.locals }

	/// Instruction list of the function body (mutable).
	pub fn code_mut(&mut self) -> &mut Instructions { &mut self.instructions }
}

impl Deserialize for FuncBody {
	 type Error = Error;

	fn deserialize<R: io::Read>(reader: &mut R) -> Result<Self, Self::Error> {
		let mut body_reader = SectionReader::new(reader)?;
		let locals: Vec<Local> = CountedList::<Local>::deserialize(&mut body_reader)?.into_inner();

		// The specification obliges us to count the total number of local variables while
		// decoding the binary format.
		locals
			.iter()
			.try_fold(0u32, |acc, &Local { count, .. }| acc.checked_add(count))
			.ok_or_else(|| Error::TooManyLocals)?;

		let instructions = Instructions::deserialize(&mut body_reader)?;
		body_reader.close()?;
		Ok(FuncBody { locals: locals, instructions: instructions })
	}
}

impl Serialize for FuncBody {
	type Error = Error;

	fn serialize<W: io::Write>(self, writer: &mut W) -> Result<(), Self::Error> {
		let mut counted_writer = CountedWriter::new(writer);

		let data = self.locals;
		let counted_list = CountedListWriter::<Local, _>(
			data.len(),
			data.into_iter().map(Into::into),
		);
		counted_list.serialize(&mut counted_writer)?;

		let code = self.instructions;
		code.serialize(&mut counted_writer)?;

		counted_writer.done()?;

		Ok(())
	}
}