MCPcopy Index your code
hub / github.com/explodingcamera/tinywasm / process_const_operators

Function process_const_operators

crates/parser/src/conversion.rs:228–271  ·  view source on GitHub ↗
(ops: OperatorsReader<'_>)

Source from the content-addressed store, hash-verified

226}
227
228pub(crate) fn process_const_operators(ops: OperatorsReader<'_>) -> Result<Box<[ConstInstruction]>> {
229 let ops = ops.into_iter().collect::<wasmparser::Result<Vec<_>>>()?;
230 // In practice, the len can never be something other than 2,
231 // but we'll keep this here since it's part of the spec
232 // Invalid modules will be rejected by the validator anyway (there are also tests for this in the testsuite)
233 debug_assert!(ops.len() >= 2);
234 debug_assert!(matches!(ops[ops.len() - 1], wasmparser::Operator::End));
235
236 let mut out = Vec::with_capacity(ops.len().saturating_sub(1));
237 for op in ops.iter().take(ops.len() - 1) {
238 let instr = match op {
239 wasmparser::Operator::RefNull { hty } => match convert_heaptype(*hty)? {
240 WasmType::RefFunc => ConstInstruction::RefFunc(None),
241 WasmType::RefExtern => ConstInstruction::RefExtern(None),
242 other => {
243 return Err(crate::ParseError::UnsupportedOperator(format!(
244 "Unsupported ref.null heap type lowered to {other:?}"
245 )));
246 }
247 },
248 wasmparser::Operator::RefFunc { function_index } => ConstInstruction::RefFunc(Some(*function_index)),
249 wasmparser::Operator::I32Const { value } => ConstInstruction::I32Const(*value),
250 wasmparser::Operator::I64Const { value } => ConstInstruction::I64Const(*value),
251 wasmparser::Operator::F32Const { value } => ConstInstruction::F32Const(f32::from_bits(value.bits())),
252 wasmparser::Operator::F64Const { value } => ConstInstruction::F64Const(f64::from_bits(value.bits())),
253 wasmparser::Operator::V128Const { value } => ConstInstruction::V128Const(*value.bytes()),
254 wasmparser::Operator::GlobalGet { global_index } => ConstInstruction::GlobalGet(*global_index),
255 wasmparser::Operator::I32Add => ConstInstruction::I32Add,
256 wasmparser::Operator::I32Sub => ConstInstruction::I32Sub,
257 wasmparser::Operator::I32Mul => ConstInstruction::I32Mul,
258 wasmparser::Operator::I64Add => ConstInstruction::I64Add,
259 wasmparser::Operator::I64Sub => ConstInstruction::I64Sub,
260 wasmparser::Operator::I64Mul => ConstInstruction::I64Mul,
261 other => {
262 return Err(crate::ParseError::UnsupportedOperator(format!(
263 "Unsupported const instruction: {other:?}"
264 )));
265 }
266 };
267 out.push(instr);
268 }
269
270 Ok(out.into_boxed_slice())
271}
272
273pub(crate) fn convert_heaptype(heap: wasmparser::HeapType) -> Result<WasmType> {
274 match heap {

Callers 3

convert_module_elementFunction · 0.85
convert_module_dataFunction · 0.85
convert_module_globalsFunction · 0.85

Calls 4

convert_heaptypeFunction · 0.85
bitsMethod · 0.80
lenMethod · 0.45
pushMethod · 0.45

Tested by

no test coverage detected