summaryrefslogtreecommitdiff
path: root/crates/parser
diff options
context:
space:
mode:
authorHenry <mail@henrygressmann.de>2026-04-12 15:29:20 +0200
committerHenry <mail@henrygressmann.de>2026-04-12 15:29:20 +0200
commitc2499d9fb0f90d8c332d89e12207c88c025e91c1 (patch)
tree976273f7c16f0d3787ec33d757c6b5e59b0e093a /crates/parser
parentdcd8b152466ab023762e4b9b6fc50c99da0cae50 (diff)
fix: fix remaining issues with extended-const
Signed-off-by: Henry <mail@henrygressmann.de>
Diffstat (limited to 'crates/parser')
-rw-r--r--crates/parser/src/conversion.rs46
-rw-r--r--crates/parser/src/lib.rs1
2 files changed, 32 insertions, 15 deletions
diff --git a/crates/parser/src/conversion.rs b/crates/parser/src/conversion.rs
index 570d0a1..dfd5415 100644
--- a/crates/parser/src/conversion.rs
+++ b/crates/parser/src/conversion.rs
@@ -235,7 +235,7 @@ pub(crate) fn convert_valtype(valtype: &wasmparser::ValType) -> ValType {
}
}
-pub(crate) fn process_const_operators(ops: OperatorsReader<'_>) -> Result<ConstInstruction> {
+pub(crate) fn process_const_operators(ops: OperatorsReader<'_>) -> Result<Box<[ConstInstruction]>> {
let ops = ops.into_iter().collect::<wasmparser::Result<Vec<_>>>()?;
// In practice, the len can never be something other than 2,
// but we'll keep this here since it's part of the spec
@@ -243,21 +243,37 @@ pub(crate) fn process_const_operators(ops: OperatorsReader<'_>) -> Result<ConstI
assert!(ops.len() >= 2);
assert!(matches!(ops[ops.len() - 1], wasmparser::Operator::End));
- match &ops[ops.len() - 2] {
- wasmparser::Operator::RefNull { hty } => match convert_heaptype(*hty) {
- ValType::RefFunc => Ok(ConstInstruction::RefFunc(None)),
- ValType::RefExtern => Ok(ConstInstruction::RefExtern(None)),
- _ => unimplemented!("Unsupported heap type: {:?}", hty),
- },
- wasmparser::Operator::RefFunc { function_index } => Ok(ConstInstruction::RefFunc(Some(*function_index))),
- wasmparser::Operator::I32Const { value } => Ok(ConstInstruction::I32Const(*value)),
- wasmparser::Operator::I64Const { value } => Ok(ConstInstruction::I64Const(*value)),
- wasmparser::Operator::F32Const { value } => Ok(ConstInstruction::F32Const(f32::from_bits(value.bits()))),
- wasmparser::Operator::F64Const { value } => Ok(ConstInstruction::F64Const(f64::from_bits(value.bits()))),
- wasmparser::Operator::V128Const { value } => Ok(ConstInstruction::V128Const(value.i128())),
- wasmparser::Operator::GlobalGet { global_index } => Ok(ConstInstruction::GlobalGet(*global_index)),
- op => Err(crate::ParseError::UnsupportedOperator(format!("Unsupported const instruction: {op:?}"))),
+ let mut out = Vec::with_capacity(ops.len().saturating_sub(1));
+ for op in ops.iter().take(ops.len() - 1) {
+ let instr = match op {
+ wasmparser::Operator::RefNull { hty } => match convert_heaptype(*hty) {
+ ValType::RefFunc => ConstInstruction::RefFunc(None),
+ ValType::RefExtern => ConstInstruction::RefExtern(None),
+ _ => unimplemented!("Unsupported heap type: {:?}", hty),
+ },
+ wasmparser::Operator::RefFunc { function_index } => ConstInstruction::RefFunc(Some(*function_index)),
+ wasmparser::Operator::I32Const { value } => ConstInstruction::I32Const(*value),
+ wasmparser::Operator::I64Const { value } => ConstInstruction::I64Const(*value),
+ wasmparser::Operator::F32Const { value } => ConstInstruction::F32Const(f32::from_bits(value.bits())),
+ wasmparser::Operator::F64Const { value } => ConstInstruction::F64Const(f64::from_bits(value.bits())),
+ wasmparser::Operator::V128Const { value } => ConstInstruction::V128Const(value.i128()),
+ wasmparser::Operator::GlobalGet { global_index } => ConstInstruction::GlobalGet(*global_index),
+ wasmparser::Operator::I32Add => ConstInstruction::I32Add,
+ wasmparser::Operator::I32Sub => ConstInstruction::I32Sub,
+ wasmparser::Operator::I32Mul => ConstInstruction::I32Mul,
+ wasmparser::Operator::I64Add => ConstInstruction::I64Add,
+ wasmparser::Operator::I64Sub => ConstInstruction::I64Sub,
+ wasmparser::Operator::I64Mul => ConstInstruction::I64Mul,
+ other => {
+ return Err(crate::ParseError::UnsupportedOperator(format!(
+ "Unsupported const instruction: {other:?}"
+ )));
+ }
+ };
+ out.push(instr);
}
+
+ Ok(out.into_boxed_slice())
}
pub(crate) fn convert_heaptype(heap: wasmparser::HeapType) -> ValType {
diff --git a/crates/parser/src/lib.rs b/crates/parser/src/lib.rs
index a0c0c7e..850042c 100644
--- a/crates/parser/src/lib.rs
+++ b/crates/parser/src/lib.rs
@@ -90,6 +90,7 @@ impl Parser {
| WasmFeatures::BULK_MEMORY
| WasmFeatures::SATURATING_FLOAT_TO_INT
| WasmFeatures::SIGN_EXTENSION
+ | WasmFeatures::EXTENDED_CONST
| WasmFeatures::FUNCTION_REFERENCES
| WasmFeatures::TAIL_CALL
| WasmFeatures::MULTI_MEMORY