pyodide_webassembly_runtime_layer/
module.rs

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
344
345
346
347
348
349
350
351
352
use std::sync::Arc;

use anyhow::Context;
use fxhash::FxHashMap;
use pyo3::{prelude::*, sync::GILOnceCell};
use wasm_runtime_layer::{
    backend::WasmModule, ExportType, ExternType, FuncType, GlobalType, ImportType, MemoryType,
    TableType, ValueType,
};

use crate::{
    conversion::js_uint8_array_new, features::UnsupportedWasmFeatureExtensionError, Engine,
};

#[derive(Debug)]
/// A WASM module.
///
/// This type wraps a [`WebAssembly.Module`] from the JavaScript API.
///
/// [`WebAssembly.Module`]: https://developer.mozilla.org/en-US/docs/WebAssembly/JavaScript_interface/Module
pub struct Module {
    /// The inner module
    module: Py<PyAny>,
    /// The parsed module, containing import and export signatures
    parsed: Arc<ParsedModule>,
}

impl Clone for Module {
    fn clone(&self) -> Self {
        Python::with_gil(|py| Self {
            module: self.module.clone_ref(py),
            parsed: self.parsed.clone(),
        })
    }
}

impl WasmModule<Engine> for Module {
    fn new(_engine: &Engine, mut stream: impl std::io::Read) -> anyhow::Result<Self> {
        Python::with_gil(|py| {
            #[cfg(feature = "tracing")]
            let _span = tracing::debug_span!("Module::new").entered();

            let mut bytes = Vec::new();
            stream
                .read_to_end(&mut bytes)
                .context("Failed to read module bytes")?;

            let parsed = ParsedModule::parse(&bytes)?;

            let buffer = js_uint8_array_new(py)?.call1((bytes.as_slice(),))?;

            let module = match web_assembly_module_new(py)?.call1((buffer,)) {
                Ok(module) => module,
                // check if the error comes from missing feature support
                // - if so, report the more informative unsupported feature error instead
                // - if not, bubble up the error that made module instantiation fail
                Err(err) => match Python::with_gil(|py| {
                    UnsupportedWasmFeatureExtensionError::check_support(py, &bytes)
                })? {
                    Ok(()) => anyhow::bail!(err),
                    Err(unsupported) => anyhow::bail!(unsupported),
                },
            };

            let parsed = Arc::new(parsed);

            Ok(Self {
                module: module.unbind(),
                parsed,
            })
        })
    }

    fn exports(&self) -> Box<dyn '_ + Iterator<Item = ExportType<'_>>> {
        Box::new(self.parsed.exports.iter().map(|(name, ty)| ExportType {
            name: name.as_str(),
            ty: ty.clone(),
        }))
    }

    fn get_export(&self, name: &str) -> Option<ExternType> {
        self.parsed.exports.get(name).cloned()
    }

    fn imports(&self) -> Box<dyn '_ + Iterator<Item = ImportType<'_>>> {
        Box::new(
            self.parsed
                .imports
                .iter()
                .map(|((module, name), kind)| ImportType {
                    module,
                    name,
                    ty: kind.clone(),
                }),
        )
    }
}

impl Module {
    pub(crate) fn module(&self, py: Python) -> Py<PyAny> {
        self.module.clone_ref(py)
    }
}

#[derive(Debug)]
/// A parsed core module with imports and exports
struct ParsedModule {
    /// Import signatures
    imports: FxHashMap<(String, String), ExternType>,
    /// Export signatures
    exports: FxHashMap<String, ExternType>,
}

impl ParsedModule {
    #[allow(clippy::too_many_lines)]
    /// Parses a module from bytes and extracts import and export signatures
    fn parse(bytes: &[u8]) -> anyhow::Result<Self> {
        let parser = wasmparser::Parser::new(0);

        let mut imports = FxHashMap::default();
        let mut exports = FxHashMap::default();

        let mut types = Vec::new();

        let mut functions = Vec::new();
        let mut memories = Vec::new();
        let mut tables = Vec::new();
        let mut globals = Vec::new();

        parser.parse_all(bytes).try_for_each(|payload| {
            match payload? {
                wasmparser::Payload::TypeSection(section) => {
                    for ty in section {
                        let ty = ty?;

                        let mut subtypes = ty.types();
                        let subtype = subtypes.next();

                        let ty = match (subtype, subtypes.next()) {
                            (Some(subtype), None) => match &subtype.composite_type.inner {
                                wasmparser::CompositeInnerType::Func(func_type) => FuncType::new(
                                    func_type
                                        .params()
                                        .iter()
                                        .copied()
                                        .map(ValueType::from_value),
                                    func_type
                                        .results()
                                        .iter()
                                        .copied()
                                        .map(ValueType::from_value),
                                ),
                                _ => unreachable!(),
                            },
                            _ => unimplemented!(),
                        };

                        types.push(ty);
                    }
                },
                wasmparser::Payload::FunctionSection(section) => {
                    for type_index in section {
                        let type_index = type_index?;

                        let ty = &types[type_index as usize];

                        functions.push(ty.clone());
                    }
                },
                wasmparser::Payload::TableSection(section) => {
                    for table in section {
                        let table = table?;
                        tables.push(TableType::from_parsed(&table.ty)?);
                    }
                },
                wasmparser::Payload::MemorySection(section) => {
                    for memory in section {
                        let memory = memory?;
                        memories.push(MemoryType::from_parsed(&memory)?);
                    }
                },
                wasmparser::Payload::GlobalSection(section) => {
                    for global in section {
                        let global = global?;
                        globals.push(GlobalType::from_parsed(global.ty));
                    }
                },
                wasmparser::Payload::TagSection(section) => {
                    for tag in section {
                        let tag = tag?;

                        #[cfg(feature = "tracing")]
                        tracing::trace!(?tag, "tag");
                        #[cfg(not(feature = "tracing"))]
                        let _ = tag;
                    }
                },
                wasmparser::Payload::ImportSection(section) => {
                    for import in section {
                        let import = import?;
                        let ty = match import.ty {
                            wasmparser::TypeRef::Func(index) => {
                                let sig = types[index as usize].clone().with_name(import.name);
                                functions.push(sig.clone());
                                ExternType::Func(sig)
                            },
                            wasmparser::TypeRef::Table(ty) => {
                                tables.push(TableType::from_parsed(&ty)?);
                                ExternType::Table(TableType::from_parsed(&ty)?)
                            },
                            wasmparser::TypeRef::Memory(ty) => {
                                memories.push(MemoryType::from_parsed(&ty)?);
                                ExternType::Memory(MemoryType::from_parsed(&ty)?)
                            },
                            wasmparser::TypeRef::Global(ty) => {
                                globals.push(GlobalType::from_parsed(ty));
                                ExternType::Global(GlobalType::from_parsed(ty))
                            },
                            wasmparser::TypeRef::Tag(_) => {
                                unimplemented!("WebAssembly.Tag is not yet supported")
                            },
                        };

                        imports.insert((import.module.to_string(), import.name.to_string()), ty);
                    }
                },
                wasmparser::Payload::ExportSection(section) => {
                    for export in section {
                        let export = export?;
                        let index = export.index as usize;
                        let ty = match export.kind {
                            wasmparser::ExternalKind::Func => {
                                ExternType::Func(functions[index].clone().with_name(export.name))
                            },
                            wasmparser::ExternalKind::Table => ExternType::Table(tables[index]),
                            wasmparser::ExternalKind::Memory => ExternType::Memory(memories[index]),
                            wasmparser::ExternalKind::Global => ExternType::Global(globals[index]),
                            wasmparser::ExternalKind::Tag => {
                                unimplemented!("WebAssembly.Tag is not yet supported")
                            },
                        };

                        exports.insert(export.name.to_string(), ty);
                    }
                },
                wasmparser::Payload::ElementSection(section) => {
                    for element in section {
                        let element = element?;

                        #[cfg(feature = "tracing")]
                        match element.kind {
                            wasmparser::ElementKind::Passive => tracing::debug!("passive"),
                            wasmparser::ElementKind::Active { .. } => tracing::debug!("active"),
                            wasmparser::ElementKind::Declared => tracing::debug!("declared"),
                        }
                        #[cfg(not(feature = "tracing"))]
                        let _ = element;
                    }
                },
                _ => (),
            }

            anyhow::Ok(())
        })?;

        Ok(Self { imports, exports })
    }
}

trait ValueTypeFrom {
    fn from_value(value: wasmparser::ValType) -> Self;
    fn from_ref(ty: wasmparser::RefType) -> Self;
}

impl ValueTypeFrom for ValueType {
    fn from_value(value: wasmparser::ValType) -> Self {
        match value {
            wasmparser::ValType::I32 => Self::I32,
            wasmparser::ValType::I64 => Self::I64,
            wasmparser::ValType::F32 => Self::F32,
            wasmparser::ValType::F64 => Self::F64,
            wasmparser::ValType::V128 => unimplemented!("v128 is not supported"),
            wasmparser::ValType::Ref(ty) => Self::from_ref(ty),
        }
    }

    fn from_ref(ty: wasmparser::RefType) -> Self {
        if ty.is_func_ref() {
            Self::FuncRef
        } else if ty.is_extern_ref() {
            Self::ExternRef
        } else {
            unimplemented!("unsupported reference type {ty:?}")
        }
    }
}

trait TableTypeFrom: Sized {
    fn from_parsed(value: &wasmparser::TableType) -> anyhow::Result<Self>;
}

impl TableTypeFrom for TableType {
    fn from_parsed(value: &wasmparser::TableType) -> anyhow::Result<Self> {
        Ok(Self::new(
            ValueType::from_ref(value.element_type),
            value.initial.try_into()?,
            match value.maximum {
                None => None,
                Some(maximum) => Some(maximum.try_into()?),
            },
        ))
    }
}

trait MemoryTypeFrom: Sized {
    fn from_parsed(value: &wasmparser::MemoryType) -> anyhow::Result<Self>;
}

impl MemoryTypeFrom for MemoryType {
    fn from_parsed(value: &wasmparser::MemoryType) -> anyhow::Result<Self> {
        if value.memory64 {
            anyhow::bail!("memory64 is not yet supported");
        }

        if value.shared {
            anyhow::bail!("shared memory is not yet supported");
        }

        Ok(Self::new(
            value.initial.try_into()?,
            match value.maximum {
                None => None,
                Some(maximum) => Some(maximum.try_into()?),
            },
        ))
    }
}

trait GlobalTypeFrom {
    fn from_parsed(value: wasmparser::GlobalType) -> Self;
}

impl GlobalTypeFrom for GlobalType {
    fn from_parsed(value: wasmparser::GlobalType) -> Self {
        Self::new(ValueType::from_value(value.content_type), value.mutable)
    }
}

fn web_assembly_module_new(py: Python) -> Result<&Bound<PyAny>, PyErr> {
    static WEB_ASSEMBLY_MODULE: GILOnceCell<Py<PyAny>> = GILOnceCell::new();
    WEB_ASSEMBLY_MODULE.import(py, "js.WebAssembly.Module", "new")
}