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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
use crate::{error::WasmEdgeError, WasmEdgeResult};
use wasmedge_sys as sys;
use wasmedge_types::MemoryType;

/// Defines a linear memory.
#[derive(Debug, Clone)]
pub struct Memory {
    pub(crate) inner: sys::Memory,
    pub(crate) name: Option<String>,
    pub(crate) mod_name: Option<String>,
    pub(crate) ty: MemoryType,
}
impl Memory {
    /// Creates a new wasm memory instance with the given type.
    ///
    /// # Argument
    ///
    /// * `ty` - The type of the memory instance to be created.
    ///
    /// # Error
    ///
    /// * If fail to create the memory instance, then [WasmEdgeError::Mem(MemError::Create)](crate::error::MemError) is returned.
    pub fn new(ty: MemoryType) -> WasmEdgeResult<Self> {
        let inner = sys::Memory::create(&ty.clone().into())?;
        Ok(Self {
            inner,
            name: None,
            mod_name: None,
            ty,
        })
    }

    /// Returns the exported name of this memory.
    ///
    /// Notice that this field is meaningful only if this memory is used as an exported instance.
    pub fn name(&self) -> Option<&str> {
        match &self.name {
            Some(name) => Some(name.as_ref()),
            None => None,
        }
    }

    /// Returns the name of the [module instance](crate::Instance) from which this memory exports.
    ///
    /// Notice that this field is meaningful only if this memory is used as an exported instance.
    pub fn mod_name(&self) -> Option<&str> {
        match &self.mod_name {
            Some(mod_name) => Some(mod_name.as_ref()),
            None => None,
        }
    }

    /// Returns a reference to the type of this memory.
    pub fn ty(&self) -> &MemoryType {
        &self.ty
    }

    /// Returns the size, in WebAssembly pages (64 KiB of each page), of this wasm memory.
    pub fn page(&self) -> u32 {
        self.inner.size()
    }

    /// Returns the byte length of this memory. The returned value will be a multiple of the wasm page size, 64k.
    pub fn size(&self) -> u64 {
        self.page() as u64 * 65536_u64
    }

    /// Safely reads memory contents at the given offset into a buffer.
    ///
    /// # Arguments
    ///
    /// * `offset` - The offset from which to read.
    ///
    /// * `len` - the length of bytes to read.
    ///
    /// # Error
    ///
    /// If fail to read the memory, then an error is returned.
    pub fn read(&self, offset: u32, len: u32) -> WasmEdgeResult<Vec<u8>> {
        let data = self.inner.get_data(offset, len)?;
        Ok(data)
    }

    /// Returns a string of byte length `len` from this memory, starting at `offset`.
    ///
    /// # Arguments
    ///
    /// * `offset` - The offset from which to read.
    ///
    /// * `len` - the length of bytes to read.
    ///
    /// # Error
    ///
    /// If fail to read, then an error is returned.
    pub fn read_string(&self, offset: u32, len: u32) -> WasmEdgeResult<String> {
        let slice = self.read(offset, len)?;
        Ok(std::str::from_utf8(&slice)
            .map_err(WasmEdgeError::Utf8)?
            .to_string())
    }

    /// Safely writes contents of a buffer to this memory at the given offset.
    ///
    /// # Arguments
    ///
    /// * `data` - The bytes to write to this memory..
    ///
    /// * `offset` - The offset at which to write.
    ///
    /// # Error
    ///
    /// If fail to write to the memory, then an error is returned.
    pub fn write(&mut self, data: impl AsRef<[u8]>, offset: u32) -> WasmEdgeResult<()> {
        self.inner.set_data(data, offset)?;
        Ok(())
    }

    /// Grows this memory by the `count` pages.
    ///
    /// # Argument
    ///
    /// * `count` - The number of pages to grow the memory by.
    ///
    /// # Error
    ///
    /// If fail to grow the memory, then an error is returned.
    pub fn grow(&mut self, count: u32) -> WasmEdgeResult<()> {
        self.inner.grow(count)?;
        Ok(())
    }

    /// Returns the const data pointer to this memory.
    ///
    /// # Arguments
    ///
    /// * `offset` - The data start offset in this memory.
    ///
    /// * `len` - The requested data length. If the size of `offset` + `len` is larger
    /// than the data size in this memory.
    ///   
    ///
    /// # Errors
    ///
    /// If fail to get the data pointer, then an error is returned.
    ///
    pub fn data_pointer(&self, offset: u32, len: u32) -> WasmEdgeResult<*const u8> {
        self.inner.data_pointer(offset, len)
    }

    /// Returns the data pointer to this memory.
    ///
    /// # Arguments
    ///
    /// * `offset` - The data start offset in this memory.
    ///
    /// * `len` - The requested data length. If the size of `offset` + `len` is larger than the data size in this memory.
    ///
    /// # Errors
    ///
    /// If fail to get the data pointer, then an error is returned.
    ///
    pub fn data_pointer_mut(&mut self, offset: u32, len: u32) -> WasmEdgeResult<*mut u8> {
        self.inner.data_pointer_mut(offset, len)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::{
        config::{CommonConfigOptions, ConfigBuilder},
        Executor, ImportObjectBuilder, Statistics, Store,
    };

    #[test]
    #[allow(clippy::assertions_on_result_states)]
    fn test_memory_type() {
        let result = MemoryType::new(0, None, false);
        assert!(result.is_ok());
        let ty = result.unwrap();
        assert_eq!(ty.minimum(), 0);
        assert_eq!(ty.maximum(), None);

        let result = MemoryType::new(10, Some(20), false);
        assert!(result.is_ok());
        let ty = result.unwrap();
        assert_eq!(ty.minimum(), 10);
        assert_eq!(ty.maximum(), Some(20));
    }

    #[test]
    #[allow(clippy::assertions_on_result_states)]
    fn test_memory() {
        // create a memory instance
        let result = MemoryType::new(10, Some(20), false);
        assert!(result.is_ok());
        let memory_type = result.unwrap();
        let result = Memory::new(memory_type);
        assert!(result.is_ok());
        let memory = result.unwrap();

        // create an import object
        let result = ImportObjectBuilder::new()
            .with_memory("memory", memory)
            .expect("failed to add memory")
            .build("extern");
        assert!(result.is_ok());
        let import = result.unwrap();

        // create an executor
        let result = ConfigBuilder::new(CommonConfigOptions::default()).build();
        assert!(result.is_ok());
        let config = result.unwrap();

        let result = Statistics::new();
        assert!(result.is_ok());
        let mut stat = result.unwrap();

        let result = Executor::new(Some(&config), Some(&mut stat));
        assert!(result.is_ok());
        let mut executor = result.unwrap();

        // create a store
        let result = Store::new();
        assert!(result.is_ok());
        let mut store = result.unwrap();

        let result = store.named_instance("extern");
        assert!(result.is_err());

        let result = store.register_import_module(&mut executor, &import);
        assert!(result.is_ok());

        let result = store.named_instance("extern");
        assert!(result.is_ok());
        let instance = result.unwrap();

        // get the exported memory
        let result = instance.memory("memory");
        assert!(result.is_ok());
        let mut memory = result.unwrap();

        // check memory
        assert!(memory.name().is_some());
        assert_eq!(memory.name().unwrap(), "memory");
        assert_eq!(memory.mod_name(), Some("extern"));
        assert_eq!(memory.page(), 10);

        // check memory type
        let ty = memory.ty();
        assert_eq!(ty.minimum(), 10);
        assert_eq!(ty.maximum(), Some(20));

        // read data before write data
        let result = memory.read(0, 10);
        assert!(result.is_ok());
        let data = result.unwrap();
        assert_eq!(data, vec![0; 10]);

        // write data
        // ! debug
        let data = vec![1; 10];
        let result = memory.write(data.as_slice(), 10);
        // let result = memory.write(vec![1; 10], 10);
        assert!(result.is_ok());
        // read data after write data
        let result = memory.read(10, 10);
        assert!(result.is_ok());
        let data = result.unwrap();
        assert_eq!(data, vec![1; 10]);

        // grow memory
        let result = memory.grow(5);
        assert!(result.is_ok());
        assert_eq!(memory.page(), 15);

        // get memory from instance again
        let result = instance.memory("memory");
        assert!(result.is_ok());
        let memory = result.unwrap();
        assert_eq!(memory.page(), 15);
    }

    #[test]
    fn test_memory_read() {
        let result = MemoryType::new(10, Some(20), false);
        assert!(result.is_ok());
        let memory_type = result.unwrap();
        let result = Memory::new(memory_type);
        assert!(result.is_ok());
        let mut memory = result.unwrap();

        let result = memory.read(0, 10);
        assert!(result.is_ok());
        let data = result.unwrap();
        assert_eq!(data, vec![0; 10]);

        let s = String::from("hello");
        let bytes = s.as_bytes();
        let len = bytes.len();

        let result = memory.write(bytes, 0);
        assert!(result.is_ok());

        let result = memory.read(0, len as u32);
        assert!(result.is_ok());
        let data = result.unwrap();
        assert_eq!(data, bytes);

        let result = memory.read_string(0, len as u32);
        assert!(result.is_ok());
        let data = result.unwrap();
        assert_eq!(data, s);
    }

    #[test]
    fn test_memory_clone() {
        #[derive(Debug, Clone)]
        struct RecordsMemory {
            memory: Memory,
        }

        // create a memory instance
        let result = MemoryType::new(10, Some(20), false);
        assert!(result.is_ok());
        let memory_type = result.unwrap();
        let result = Memory::new(memory_type);
        assert!(result.is_ok());
        let memory = result.unwrap();

        // create a RecordsMemory instance
        let rec_mem = RecordsMemory { memory };

        // clone the RecordsMemory instance
        let rec_mem_cloned = rec_mem.clone();

        // drop the original RecordsMemory instance
        drop(rec_mem);

        // check the cloned RecordsMemory instance
        assert_eq!(rec_mem_cloned.memory.page(), 10);
    }
}