wasm/validation/modules/
globals.rs

1use alloc::collections::btree_set::BTreeSet;
2
3use crate::{
4    core::{
5        decoding::reader::WasmDecoder,
6        structure::modules::{
7            globals::Global,
8            indices::{FuncIdx, IdxVec, TypeIdx},
9        },
10    },
11    validation::{
12        instructions::constant_expressions::decode_and_validate_constant_expression,
13        validation_stack::ValidationStack,
14    },
15    GlobalType, ValidationError,
16};
17
18impl Global {
19    pub fn decode_and_validate(
20        wasm: &mut WasmDecoder,
21        imported_global_types: &[GlobalType],
22        validation_context_refs: &mut BTreeSet<FuncIdx>,
23        c_funcs: &IdxVec<FuncIdx, TypeIdx>,
24    ) -> Result<Self, ValidationError> {
25        let ty = GlobalType::decode(wasm)?;
26        let stack = &mut ValidationStack::new();
27        let (init_expr, seen_func_idxs) =
28            decode_and_validate_constant_expression(wasm, stack, imported_global_types, c_funcs)?;
29
30        stack.assert_val_types(&[ty.ty], true)?;
31        validation_context_refs.extend(seen_func_idxs);
32
33        Ok(Global { ty, init_expr })
34    }
35}