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
use crate::{
    func::FuncRef,
    global::GlobalRef,
    memory::MemoryRef,
    module::ModuleRef,
    table::TableRef,
    types::{GlobalDescriptor, MemoryDescriptor, TableDescriptor},
    Error,
    Signature,
};
use alloc::{collections::BTreeMap, string::String};

/// Resolver of a module's dependencies.
///
/// A module have dependencies in a form of a list of imports (i.e.
/// tuple of a (`module_name`, `field_name`, `descriptor`)).
///
/// The job of implementations of this trait is to provide on each
/// import a corresponding concrete reference.
///
/// For simple use-cases you can use [`ImportsBuilder`].
///
/// [`ImportsBuilder`]: struct.ImportsBuilder.html
pub trait ImportResolver {
    /// Resolve a function.
    ///
    /// Returned function should match given `signature`, i.e. all parameter types and return value should have exact match.
    /// Otherwise, link-time error will occur.
    fn resolve_func(
        &self,
        _module_name: &str,
        field_name: &str,
        _signature: &Signature,
    ) -> Result<FuncRef, Error>;

    /// Resolve a global variable.
    ///
    /// Returned global should match given `descriptor`, i.e. type and mutability
    /// should match. Otherwise, link-time error will occur.
    fn resolve_global(
        &self,
        module_name: &str,
        field_name: &str,
        descriptor: &GlobalDescriptor,
    ) -> Result<GlobalRef, Error>;

    /// Resolve a memory.
    ///
    /// Returned memory should match requested memory (described by the `descriptor`),
    /// i.e. initial size of a returned memory should be equal or larger than requested memory.
    /// Furthermore, if requested memory have maximum size, returned memory either should have
    /// equal or larger maximum size or have no maximum size at all.
    /// If returned memory doesn't match the requested then link-time error will occur.
    fn resolve_memory(
        &self,
        module_name: &str,
        field_name: &str,
        descriptor: &MemoryDescriptor,
    ) -> Result<MemoryRef, Error>;

    /// Resolve a table.
    ///
    /// Returned table should match requested table (described by the `descriptor`),
    /// i.e. initial size of a returned table should be equal or larger than requested table.
    /// Furthermore, if requested memory have maximum size, returned memory either should have
    /// equal or larger maximum size or have no maximum size at all.
    /// If returned table doesn't match the requested then link-time error will occur.
    fn resolve_table(
        &self,
        module_name: &str,
        field_name: &str,
        descriptor: &TableDescriptor,
    ) -> Result<TableRef, Error>;
}

/// Convenience builder of [`ImportResolver`].
///
/// With help of this builder, you can easily create [`ImportResolver`], just by
/// adding needed [resolvers][`ModuleImportResolver`] by names.
///
/// # Examples
///
/// ```rust
/// use wasmi::{ModuleInstance, ImportsBuilder};
/// #
/// # struct EnvModuleResolver;
/// # impl ::wasmi::ModuleImportResolver for EnvModuleResolver { }
/// # fn func() -> Result<(), ::wasmi::Error> {
/// # let module = wasmi::Module::from_buffer(&[0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00]).unwrap();
/// # let other_instance = ModuleInstance::new(&module, &ImportsBuilder::default())?.assert_no_start();
///
/// let imports = ImportsBuilder::new()
///     .with_resolver("env", &EnvModuleResolver)
///     // Note, that ModuleInstance can be a resolver too.
///     .with_resolver("other_instance", &other_instance);
/// let instance = ModuleInstance::new(&module, &imports)?.assert_no_start();
///
/// # Ok(())
/// # }
/// ```
///
/// [`ImportResolver`]: trait.ImportResolver.html
/// [`ModuleImportResolver`]: trait.ModuleImportResolver.html
pub struct ImportsBuilder<'a> {
    modules: BTreeMap<String, &'a dyn ModuleImportResolver>,
}

impl<'a> Default for ImportsBuilder<'a> {
    fn default() -> Self {
        Self::new()
    }
}

impl<'a> ImportsBuilder<'a> {
    /// Create an empty `ImportsBuilder`.
    pub fn new() -> ImportsBuilder<'a> {
        ImportsBuilder {
            modules: BTreeMap::new(),
        }
    }

    /// Register an resolver by a name.
    #[must_use]
    pub fn with_resolver<N: Into<String>>(
        mut self,
        name: N,
        resolver: &'a dyn ModuleImportResolver,
    ) -> Self {
        self.modules.insert(name.into(), resolver);
        self
    }

    /// Register an resolver by a name.
    ///
    /// Mutable borrowed version.
    pub fn push_resolver<N: Into<String>>(
        &mut self,
        name: N,
        resolver: &'a dyn ModuleImportResolver,
    ) {
        self.modules.insert(name.into(), resolver);
    }

    fn resolver(&self, name: &str) -> Option<&dyn ModuleImportResolver> {
        self.modules.get(name).cloned()
    }
}

impl<'a> ImportResolver for ImportsBuilder<'a> {
    fn resolve_func(
        &self,
        module_name: &str,
        field_name: &str,
        signature: &Signature,
    ) -> Result<FuncRef, Error> {
        self.resolver(module_name)
            .ok_or_else(|| Error::Instantiation(format!("Module {} not found", module_name)))?
            .resolve_func(field_name, signature)
    }

    fn resolve_global(
        &self,
        module_name: &str,
        field_name: &str,
        global_type: &GlobalDescriptor,
    ) -> Result<GlobalRef, Error> {
        self.resolver(module_name)
            .ok_or_else(|| Error::Instantiation(format!("Module {} not found", module_name)))?
            .resolve_global(field_name, global_type)
    }

    fn resolve_memory(
        &self,
        module_name: &str,
        field_name: &str,
        memory_type: &MemoryDescriptor,
    ) -> Result<MemoryRef, Error> {
        self.resolver(module_name)
            .ok_or_else(|| Error::Instantiation(format!("Module {} not found", module_name)))?
            .resolve_memory(field_name, memory_type)
    }

    fn resolve_table(
        &self,
        module_name: &str,
        field_name: &str,
        table_type: &TableDescriptor,
    ) -> Result<TableRef, Error> {
        self.resolver(module_name)
            .ok_or_else(|| Error::Instantiation(format!("Module {} not found", module_name)))?
            .resolve_table(field_name, table_type)
    }
}

/// Version of [`ImportResolver`] specialized for a single module.
///
/// [`ImportResolver`]: trait.ImportResolver.html
pub trait ModuleImportResolver {
    /// Resolve a function.
    ///
    /// See [`ImportResolver::resolve_func`] for details.
    ///
    /// [`ImportResolver::resolve_func`]: trait.ImportResolver.html#tymethod.resolve_func
    fn resolve_func(&self, field_name: &str, _signature: &Signature) -> Result<FuncRef, Error> {
        Err(Error::Instantiation(format!(
            "Export {} not found",
            field_name
        )))
    }

    /// Resolve a global variable.
    ///
    /// See [`ImportResolver::resolve_global`] for details.
    ///
    /// [`ImportResolver::resolve_global`]: trait.ImportResolver.html#tymethod.resolve_global
    fn resolve_global(
        &self,
        field_name: &str,
        _global_type: &GlobalDescriptor,
    ) -> Result<GlobalRef, Error> {
        Err(Error::Instantiation(format!(
            "Export {} not found",
            field_name
        )))
    }

    /// Resolve a memory.
    ///
    /// See [`ImportResolver::resolve_memory`] for details.
    ///
    /// [`ImportResolver::resolve_memory`]: trait.ImportResolver.html#tymethod.resolve_memory
    fn resolve_memory(
        &self,
        field_name: &str,
        _memory_type: &MemoryDescriptor,
    ) -> Result<MemoryRef, Error> {
        Err(Error::Instantiation(format!(
            "Export {} not found",
            field_name
        )))
    }

    /// Resolve a table.
    ///
    /// See [`ImportResolver::resolve_table`] for details.
    ///
    /// [`ImportResolver::resolve_table`]: trait.ImportResolver.html#tymethod.resolve_table
    fn resolve_table(
        &self,
        field_name: &str,
        _table_type: &TableDescriptor,
    ) -> Result<TableRef, Error> {
        Err(Error::Instantiation(format!(
            "Export {} not found",
            field_name
        )))
    }
}

impl ModuleImportResolver for ModuleRef {
    fn resolve_func(&self, field_name: &str, _signature: &Signature) -> Result<FuncRef, Error> {
        self.export_by_name(field_name)
            .ok_or_else(|| Error::Instantiation(format!("Export {} not found", field_name)))?
            .as_func()
            .cloned()
            .ok_or_else(|| Error::Instantiation(format!("Export {} is not a function", field_name)))
    }

    fn resolve_global(
        &self,
        field_name: &str,
        _global_type: &GlobalDescriptor,
    ) -> Result<GlobalRef, Error> {
        self.export_by_name(field_name)
            .ok_or_else(|| Error::Instantiation(format!("Export {} not found", field_name)))?
            .as_global()
            .cloned()
            .ok_or_else(|| Error::Instantiation(format!("Export {} is not a global", field_name)))
    }

    fn resolve_memory(
        &self,
        field_name: &str,
        _memory_type: &MemoryDescriptor,
    ) -> Result<MemoryRef, Error> {
        self.export_by_name(field_name)
            .ok_or_else(|| Error::Instantiation(format!("Export {} not found", field_name)))?
            .as_memory()
            .cloned()
            .ok_or_else(|| Error::Instantiation(format!("Export {} is not a memory", field_name)))
    }

    fn resolve_table(
        &self,
        field_name: &str,
        _table_type: &TableDescriptor,
    ) -> Result<TableRef, Error> {
        self.export_by_name(field_name)
            .ok_or_else(|| Error::Instantiation(format!("Export {} not found", field_name)))?
            .as_table()
            .cloned()
            .ok_or_else(|| Error::Instantiation(format!("Export {} is not a table", field_name)))
    }
}