parity_wasm/builder/
memory.rs1use super::invoke::{Identity, Invoke};
2use crate::elements;
3use alloc::vec::Vec;
4
5#[derive(Debug, PartialEq)]
7pub struct MemoryDefinition {
8 pub min: u32,
10 pub max: Option<u32>,
12 pub data: Vec<MemoryDataDefinition>,
14}
15
16#[derive(Debug, PartialEq)]
18pub struct MemoryDataDefinition {
19 pub offset: elements::InitExpr,
21 pub values: Vec<u8>,
23}
24
25pub struct MemoryBuilder<F = Identity> {
27 callback: F,
28 memory: MemoryDefinition,
29}
30
31impl MemoryBuilder {
32 pub fn new() -> Self {
34 MemoryBuilder::with_callback(Identity)
35 }
36}
37
38impl Default for MemoryBuilder {
39 fn default() -> Self {
40 Self::new()
41 }
42}
43
44impl<F> MemoryBuilder<F>
45where
46 F: Invoke<MemoryDefinition>,
47{
48 pub fn with_callback(callback: F) -> Self {
50 MemoryBuilder { callback, memory: Default::default() }
51 }
52
53 pub fn with_min(mut self, min: u32) -> Self {
55 self.memory.min = min;
56 self
57 }
58
59 pub fn with_max(mut self, max: Option<u32>) -> Self {
61 self.memory.max = max;
62 self
63 }
64
65 pub fn with_data(mut self, index: u32, values: Vec<u8>) -> Self {
67 self.memory.data.push(MemoryDataDefinition {
68 offset: elements::InitExpr::new(vec![
69 elements::Instruction::I32Const(index as i32),
70 elements::Instruction::End,
71 ]),
72 values,
73 });
74 self
75 }
76
77 pub fn build(self) -> F::Result {
79 self.callback.invoke(self.memory)
80 }
81}
82
83impl Default for MemoryDefinition {
84 fn default() -> Self {
85 MemoryDefinition { min: 1, max: None, data: Vec::new() }
86 }
87}