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
//! Defines WasmEdge Loader struct.

use crate::{
    ast_module::{InnerModule, Module},
    ffi, utils,
    utils::check,
    Config, WasmEdgeResult,
};
use std::{path::Path, sync::Arc};
use wasmedge_types::error::WasmEdgeError;

/// [Loader](crate::Loader) is used to load WASM modules from the given WASM files or buffers.
#[derive(Debug)]
pub struct Loader {
    pub(crate) inner: InnerLoader,
    pub(crate) registered: bool,
}
impl Loader {
    /// Create a new [Loader](crate::Loader) to be associated with the given global configuration.
    ///
    /// # Arguments
    ///
    /// * `config` - A global configuration.
    ///
    /// # Error
    ///
    /// If fail to create a [Loader](crate), then an error is returned.
    pub fn create(config: Option<&Config>) -> WasmEdgeResult<Self> {
        let ctx = match config {
            Some(config) => unsafe { ffi::WasmEdge_LoaderCreate(config.inner.0) },
            None => unsafe { ffi::WasmEdge_LoaderCreate(std::ptr::null_mut()) },
        };

        match ctx.is_null() {
            true => Err(Box::new(WasmEdgeError::LoaderCreate)),
            false => Ok(Self {
                inner: InnerLoader(ctx),
                registered: false,
            }),
        }
    }

    /// Loads a WASM module from a WASM file.
    ///
    /// # Arguments
    ///
    /// * `file` - A wasm file or an AOT wasm file.
    ///
    /// # Error
    ///
    /// If fail to load, then an error is returned.
    ///
    /// # Example
    ///
    /// ```ignore
    /// let file = "path/to/foo.wasm"
    /// let module = loader.from_file(file)?;
    /// ```
    pub fn from_file(&self, file: impl AsRef<Path>) -> WasmEdgeResult<Module> {
        match file.as_ref().extension() {
            Some(extension) => match extension.to_str() {
                Some("wasm") => self.load_from_wasm_or_aot_file(&file),
                #[cfg(target_os = "macos")]
                Some("dylib") => self.load_from_wasm_or_aot_file(&file),
                #[cfg(target_os = "linux")]
                Some("so") => self.load_from_wasm_or_aot_file(&file),
                #[cfg(target_os = "windows")]
                Some("dll") => self.load_from_wasm_or_aot_file(&file),
                Some("wat") => {
                    let bytes = wat::parse_file(file.as_ref())
                        .map_err(|_| WasmEdgeError::Operation("Failed to parse wat file".into()))?;
                    self.from_bytes(bytes)
                }
                _ => Err(Box::new(WasmEdgeError::Operation(
                    "The source file's extension should be one of `wasm`, `wat`, `dylib` on macOS, `so` on Linux or `dll` on Windows.".into(),
                ))),
            },
            None => self.load_from_wasm_or_aot_file(&file),
        }
    }

    fn load_from_wasm_or_aot_file(&self, file: impl AsRef<Path>) -> WasmEdgeResult<Module> {
        let c_path = utils::path_to_cstring(file.as_ref())?;
        let mut mod_ctx = std::ptr::null_mut();
        unsafe {
            check(ffi::WasmEdge_LoaderParseFromFile(
                self.inner.0,
                &mut mod_ctx,
                c_path.as_ptr(),
            ))?;
        }

        match mod_ctx.is_null() {
            true => Err(Box::new(WasmEdgeError::ModuleCreate)),
            false => Ok(Module {
                inner: Arc::new(InnerModule(mod_ctx)),
            }),
        }
    }

    /// Loads a WASM module from a in-memory bytes.
    ///
    /// # Arguments
    ///
    /// * `bytes` - A in-memory WASM bytes.
    ///
    /// # Error
    ///
    /// If fail to load, then an error is returned.
    ///
    /// # Example
    ///
    /// ```ignore
    /// let bytes = b"\0asm\x01\0\0\0";
    /// let module = loader.from_bytes(&bytes)?;
    /// ```
    ///
    /// Note that the text format is not accepted:
    ///
    /// ```ignore
    /// assert!(loader.from_bytes(b"(module)").is_err());
    /// ```
    pub fn from_bytes(&self, bytes: impl AsRef<[u8]>) -> WasmEdgeResult<Module> {
        let mut mod_ctx: *mut ffi::WasmEdge_ASTModuleContext = std::ptr::null_mut();

        unsafe {
            let ptr = libc::malloc(bytes.as_ref().len());
            let dst = ::core::slice::from_raw_parts_mut(
                ptr.cast::<std::mem::MaybeUninit<u8>>(),
                bytes.as_ref().len(),
            );
            let src = ::core::slice::from_raw_parts(
                bytes.as_ref().as_ptr().cast::<std::mem::MaybeUninit<u8>>(),
                bytes.as_ref().len(),
            );
            dst.copy_from_slice(src);

            check(ffi::WasmEdge_LoaderParseFromBuffer(
                self.inner.0,
                &mut mod_ctx,
                ptr as *const u8,
                bytes.as_ref().len() as u32,
            ))?;

            libc::free(ptr);
        }

        match mod_ctx.is_null() {
            true => Err(Box::new(WasmEdgeError::ModuleCreate)),
            false => Ok(Module {
                inner: Arc::new(InnerModule(mod_ctx)),
            }),
        }
    }

    /// Provides a raw pointer to the inner Loader context.
    #[cfg(feature = "ffi")]
    #[cfg_attr(docsrs, doc(cfg(feature = "ffi")))]
    pub fn as_ptr(&self) -> *const ffi::WasmEdge_LoaderContext {
        self.inner.0 as *const _
    }
}
impl Drop for Loader {
    fn drop(&mut self) {
        if !self.registered && !self.inner.0.is_null() {
            unsafe { ffi::WasmEdge_LoaderDelete(self.inner.0) }
        }
    }
}

#[derive(Debug)]
pub(crate) struct InnerLoader(pub(crate) *mut ffi::WasmEdge_LoaderContext);
unsafe impl Send for InnerLoader {}
unsafe impl Sync for InnerLoader {}

#[cfg(test)]
mod tests {
    use super::Loader;
    use crate::Config;
    use std::{
        sync::{Arc, Mutex},
        thread,
    };
    use wasmedge_types::error::{CoreError, CoreLoadError, WasmEdgeError};

    #[test]
    #[allow(clippy::assertions_on_result_states)]
    fn test_loader() {
        // create a Loader instance without configuration
        let result = Loader::create(None);
        assert!(result.is_ok());

        // create a Loader instance with configuration
        let result = Config::create();
        assert!(result.is_ok());
        let mut config = result.unwrap();
        config.reference_types(true);
        let result = Loader::create(Some(&config));
        assert!(result.is_ok());
        let loader = result.unwrap();

        // load from file
        {
            // load .wasm file
            let path = std::env::current_dir()
                .unwrap()
                .ancestors()
                .nth(2)
                .unwrap()
                .join("examples/wasmedge-sys/data/fibonacci.wat");
            let result = loader.from_file(path);
            assert!(result.is_ok());
            let module = result.unwrap();
            assert!(!module.inner.0.is_null());

            let path = std::env::current_dir()
                .unwrap()
                .ancestors()
                .nth(2)
                .unwrap()
                .join("examples/wasmedge-sys/data/fibonacci.wat");
            let result = loader.from_file(path);
            assert!(result.is_ok());

            let result = loader.from_file("not_exist_file.wasm");
            assert!(result.is_err());
            assert_eq!(
                result.unwrap_err(),
                Box::new(WasmEdgeError::Core(CoreError::Load(
                    CoreLoadError::IllegalPath
                )))
            );
        }

        // load from buffer
        {
            let buffer = b"\0asm\x01\0\0\0";
            let result = loader.from_bytes(buffer);
            assert!(result.is_ok());
            let module = result.unwrap();
            assert!(!module.inner.0.is_null());

            // the text format is not accepted
            let result = loader.from_bytes(b"(module)");
            assert!(result.is_err());
            assert_eq!(
                result.unwrap_err(),
                Box::new(WasmEdgeError::Core(CoreError::Load(
                    CoreLoadError::MalformedMagic
                )))
            );

            // empty is not accepted
            let result = loader.from_bytes([]);
            assert!(result.is_err());
            assert_eq!(
                result.unwrap_err(),
                Box::new(WasmEdgeError::Core(CoreError::Load(
                    CoreLoadError::UnexpectedEnd
                )))
            );
        }
    }

    #[test]
    #[allow(clippy::assertions_on_result_states)]
    fn test_loader_send() {
        // create a Loader instance without configuration
        let result = Loader::create(None);
        assert!(result.is_ok());

        // create a Loader instance with configuration
        let result = Config::create();
        assert!(result.is_ok());
        let mut config = result.unwrap();
        config.reference_types(true);
        let result = Loader::create(Some(&config));
        assert!(result.is_ok());
        let loader = result.unwrap();

        let handle = thread::spawn(move || {
            assert!(!loader.inner.0.is_null());
            println!("{:?}", loader.inner);
        });

        handle.join().unwrap();
    }

    #[test]
    #[allow(clippy::assertions_on_result_states)]
    fn test_loader_sync() {
        // create a Loader instance without configuration
        let result = Loader::create(None);
        assert!(result.is_ok());

        // create a Loader instance with configuration
        let result = Config::create();
        assert!(result.is_ok());
        let mut config = result.unwrap();
        config.reference_types(true);
        let result = Loader::create(Some(&config));
        assert!(result.is_ok());
        let loader = Arc::new(Mutex::new(result.unwrap()));

        let loader_cloned = Arc::clone(&loader);
        let handle = thread::spawn(move || {
            let result = loader_cloned.lock();
            assert!(result.is_ok());
            let loader = result.unwrap();

            assert!(!loader.inner.0.is_null());
        });

        handle.join().unwrap();
    }
}