wasmer_emscripten/
lib.rs

1#![deny(
2    dead_code,
3    nonstandard_style,
4    unused_imports,
5    unused_mut,
6    unused_variables,
7    unused_unsafe,
8    unreachable_patterns
9)]
10// This allow attribute is ignored when placed directly on fields that also
11// have a #[wasmer(...)] attribute. As a dirty workaround it is for now
12// allowed for the whole library.
13#![allow(clippy::type_complexity, clippy::unnecessary_cast)]
14#![doc(html_favicon_url = "https://wasmer.io/images/icons/favicon-32x32.png")]
15#![doc(html_logo_url = "https://github.com/wasmerio.png?size=200")]
16#![cfg_attr(docsrs, feature(doc_cfg, doc_auto_cfg))]
17
18#[macro_use]
19extern crate log;
20
21use lazy_static::lazy_static;
22use std::collections::HashMap;
23use std::f64;
24use std::path::PathBuf;
25use std::sync::{Arc, Mutex, RwLock};
26use wasmer::{
27    imports, namespace, AsStoreMut, AsStoreRef, ExportError, Exports, Function, FunctionEnv,
28    FunctionEnvMut, FunctionType, Global, Imports, Instance, Memory, MemoryType, Module, Pages,
29    RuntimeError, Table, TableType, TypedFunction, Value, WasmPtr,
30};
31use wasmer_types::Type as ValType;
32
33#[cfg(unix)]
34extern crate libc as libc_crate;
35#[cfg(unix)]
36use libc_crate::DIR as LibcDir;
37
38// We use a placeholder for windows
39#[cfg(not(unix))]
40type LibcDir = u8;
41
42#[macro_use]
43mod macros;
44
45// EMSCRIPTEN APIS
46mod bitwise;
47mod emscripten_target;
48mod env;
49mod errno;
50mod exception;
51mod exec;
52mod exit;
53mod inet;
54mod io;
55mod jmp;
56mod libc;
57mod linking;
58mod lock;
59mod math;
60mod memory;
61mod process;
62mod pthread;
63mod signal;
64mod storage;
65mod syscalls;
66mod time;
67mod ucontext;
68mod unistd;
69mod utils;
70mod varargs;
71
72pub use self::storage::{align_memory, static_alloc};
73pub use self::utils::{
74    allocate_cstr_on_stack, allocate_on_stack, get_emscripten_memory_size, get_emscripten_metadata,
75    get_emscripten_table_size, is_emscripten_module,
76};
77
78/// State of the emscripten environment (environment variables, CLI args)
79#[derive(Debug, Clone)]
80pub struct EmscriptenState {
81    /// Environment variables in a [key -> value] mapping
82    pub env_vars: HashMap<String, String>,
83    /// Command line arguments that this module received
84    pub cli_args: Vec<String>,
85}
86
87impl Default for EmscriptenState {
88    fn default() -> Self {
89        Self {
90            env_vars: std::env::vars_os()
91                .filter_map(|(k, v)| Some((k.to_str()?.to_string(), v.to_str()?.to_string())))
92                .collect(),
93            cli_args: Vec::new(),
94        }
95    }
96}
97
98#[derive(Clone)]
99/// The environment provided to the Emscripten imports.
100pub struct EmEnv {
101    memory: Arc<RwLock<Option<Memory>>>,
102    data: Arc<Mutex<Option<EmscriptenData>>>,
103    funcs: Arc<Mutex<EmscriptenFunctions>>,
104    // State that is passed to the wasm module (environment variables, CLI args, ...)
105    #[allow(dead_code)]
106    state: Arc<Mutex<EmscriptenState>>,
107}
108
109impl Default for EmEnv {
110    fn default() -> Self {
111        Self::new()
112    }
113}
114
115impl EmEnv {
116    /// Create a new EmEnv, with default value to be set later (set_memory, set_functions and set_data)
117    pub fn new() -> Self {
118        Self {
119            memory: Arc::new(RwLock::new(None)),
120            data: Arc::new(Mutex::new(None)),
121            funcs: Arc::new(Mutex::new(EmscriptenFunctions::new())),
122            state: Arc::new(Mutex::new(EmscriptenState::default())),
123        }
124    }
125
126    pub fn new_with_state(emstate: EmscriptenState) -> Self {
127        Self {
128            memory: Arc::new(RwLock::new(None)),
129            data: Arc::new(Mutex::new(None)),
130            funcs: Arc::new(Mutex::new(EmscriptenFunctions::new())),
131            state: Arc::new(Mutex::new(emstate)),
132        }
133    }
134
135    pub fn set_memory(&self, memory: Memory) {
136        let mut w = self.memory.write().unwrap();
137        *w = Some(memory);
138    }
139
140    /// Get a reference to the memory
141    pub fn memory(&self, _mem_idx: u32) -> Memory {
142        (*self.memory.read().unwrap()).as_ref().cloned().unwrap()
143    }
144
145    pub fn set_functions(&self, funcs: EmscriptenFunctions) {
146        let mut w = self.funcs.lock().unwrap();
147        *w = funcs;
148    }
149
150    pub fn set_data(&self, data: &EmscriptenGlobalsData, mapped_dirs: HashMap<String, PathBuf>) {
151        let mut w = self.data.lock().unwrap();
152        *w = Some(EmscriptenData::new(data.clone(), mapped_dirs));
153    }
154
155    pub fn get_env_var(&self, key: &str) -> Option<String> {
156        let w = self.state.lock().ok()?;
157        let result = w.env_vars.get(key).cloned();
158        result
159    }
160
161    pub fn get_env_vars_len(&self) -> usize {
162        let w = self.state.lock().unwrap();
163        w.env_vars.len()
164    }
165
166    pub fn set_env_var(&self, key: &str, value: &str) -> Option<String> {
167        let mut w = self.state.lock().ok()?;
168        w.env_vars.insert(key.to_string(), value.to_string())
169    }
170
171    pub fn remove_env_var(&self, key: &str) -> Option<String> {
172        let mut w = self.state.lock().ok()?;
173        w.env_vars.remove(key)
174    }
175
176    pub fn get_args_size(&self) -> usize {
177        let w = self.state.lock().unwrap();
178        w.cli_args.len()
179    }
180
181    pub fn get_args(&self) -> Vec<String> {
182        let w = self.state.lock().unwrap();
183        w.cli_args.clone()
184    }
185}
186
187#[derive(Debug, Clone)]
188pub struct LibcDirWrapper(pub *mut LibcDir);
189
190impl std::ops::Deref for LibcDirWrapper {
191    type Target = *mut LibcDir;
192
193    fn deref(&self) -> &Self::Target {
194        &self.0
195    }
196}
197
198unsafe impl Send for LibcDirWrapper {}
199unsafe impl Sync for LibcDirWrapper {}
200
201// TODO: Magic number - how is this calculated?
202const TOTAL_STACK: u32 = 5_242_880;
203// TODO: make this variable
204const STATIC_BUMP: u32 = 215_536;
205
206lazy_static! {
207    static ref OLD_ABORT_ON_CANNOT_GROW_MEMORY_SIG: FunctionType =
208        FunctionType::new(vec![], vec![ValType::I32]);
209}
210
211// The address globals begin at. Very low in memory, for code size and optimization opportunities.
212// Above 0 is static memory, starting with globals.
213// Then the stack.
214// Then 'dynamic' memory for sbrk.
215const GLOBAL_BASE: u32 = 1024;
216const STATIC_BASE: u32 = GLOBAL_BASE;
217
218#[derive(Clone, Default)]
219pub struct EmscriptenFunctions {
220    pub malloc: Option<TypedFunction<u32, u32>>,
221    pub free: Option<TypedFunction<u32, ()>>,
222    pub memalign: Option<TypedFunction<(u32, u32), u32>>,
223    pub memset: Option<TypedFunction<(u32, u32, u32), u32>>,
224    pub stack_alloc: Option<TypedFunction<u32, u32>>,
225
226    pub dyn_call_i: Option<TypedFunction<i32, i32>>,
227    pub dyn_call_ii: Option<TypedFunction<(i32, i32), i32>>,
228    pub dyn_call_iii: Option<TypedFunction<(i32, i32, i32), i32>>,
229    pub dyn_call_iiii: Option<TypedFunction<(i32, i32, i32, i32), i32>>,
230    pub dyn_call_iifi: Option<TypedFunction<(i32, i32, f64, i32), i32>>,
231    pub dyn_call_v: Option<TypedFunction<i32, ()>>,
232    pub dyn_call_vi: Option<TypedFunction<(i32, i32), ()>>,
233    pub dyn_call_vii: Option<TypedFunction<(i32, i32, i32), ()>>,
234    pub dyn_call_viii: Option<TypedFunction<(i32, i32, i32, i32), ()>>,
235    pub dyn_call_viiii: Option<TypedFunction<(i32, i32, i32, i32, i32), ()>>,
236
237    // round 2
238    pub dyn_call_dii: Option<TypedFunction<(i32, i32, i32), f64>>,
239    pub dyn_call_diiii: Option<TypedFunction<(i32, i32, i32, i32, i32), f64>>,
240    pub dyn_call_iiiii: Option<TypedFunction<(i32, i32, i32, i32, i32), i32>>,
241    pub dyn_call_iiiiii: Option<TypedFunction<(i32, i32, i32, i32, i32, i32), i32>>,
242    pub dyn_call_iiiiiii: Option<TypedFunction<(i32, i32, i32, i32, i32, i32, i32), i32>>,
243    pub dyn_call_iiiiiiii: Option<TypedFunction<(i32, i32, i32, i32, i32, i32, i32, i32), i32>>,
244    pub dyn_call_iiiiiiiii:
245        Option<TypedFunction<(i32, i32, i32, i32, i32, i32, i32, i32, i32), i32>>,
246    pub dyn_call_iiiiiiiiii:
247        Option<TypedFunction<(i32, i32, i32, i32, i32, i32, i32, i32, i32, i32), i32>>,
248    pub dyn_call_iiiiiiiiiii:
249        Option<TypedFunction<(i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32), i32>>,
250    pub dyn_call_vd: Option<TypedFunction<(i32, f64), ()>>,
251    pub dyn_call_viiiii: Option<TypedFunction<(i32, i32, i32, i32, i32, i32), ()>>,
252    pub dyn_call_viiiiii: Option<TypedFunction<(i32, i32, i32, i32, i32, i32, i32), ()>>,
253    pub dyn_call_viiiiiii: Option<TypedFunction<(i32, i32, i32, i32, i32, i32, i32, i32), ()>>,
254    pub dyn_call_viiiiiiii:
255        Option<TypedFunction<(i32, i32, i32, i32, i32, i32, i32, i32, i32), ()>>,
256    pub dyn_call_viiiiiiiii:
257        Option<TypedFunction<(i32, i32, i32, i32, i32, i32, i32, i32, i32, i32), ()>>,
258    pub dyn_call_viiiiiiiiii:
259        Option<TypedFunction<(i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32), ()>>,
260    pub dyn_call_iij: Option<TypedFunction<(i32, i32, i32, i32), i32>>,
261    pub dyn_call_iji: Option<TypedFunction<(i32, i32, i32, i32), i32>>,
262    pub dyn_call_iiji: Option<TypedFunction<(i32, i32, i32, i32, i32), i32>>,
263    pub dyn_call_iiijj: Option<TypedFunction<(i32, i32, i32, i32, i32, i32, i32), i32>>,
264    pub dyn_call_j: Option<TypedFunction<i32, i32>>,
265    pub dyn_call_ji: Option<TypedFunction<(i32, i32), i32>>,
266    pub dyn_call_jii: Option<TypedFunction<(i32, i32, i32), i32>>,
267    pub dyn_call_jij: Option<TypedFunction<(i32, i32, i32, i32), i32>>,
268    pub dyn_call_jjj: Option<TypedFunction<(i32, i32, i32, i32, i32), i32>>,
269    pub dyn_call_viiij: Option<TypedFunction<(i32, i32, i32, i32, i32, i32), ()>>,
270    pub dyn_call_viiijiiii:
271        Option<TypedFunction<(i32, i32, i32, i32, i32, i32, i32, i32, i32, i32), ()>>,
272    pub dyn_call_viiijiiiiii:
273        Option<TypedFunction<(i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32), ()>>,
274    pub dyn_call_viij: Option<TypedFunction<(i32, i32, i32, i32, i32), ()>>,
275    pub dyn_call_viiji: Option<TypedFunction<(i32, i32, i32, i32, i32, i32), ()>>,
276    pub dyn_call_viijiii: Option<TypedFunction<(i32, i32, i32, i32, i32, i32, i32, i32), ()>>,
277    pub dyn_call_viijj: Option<TypedFunction<(i32, i32, i32, i32, i32, i32, i32), ()>>,
278    pub dyn_call_vj: Option<TypedFunction<(i32, i32, i32), ()>>,
279    pub dyn_call_vjji: Option<TypedFunction<(i32, i32, i32, i32, i32, i32), ()>>,
280    pub dyn_call_vij: Option<TypedFunction<(i32, i32, i32, i32), ()>>,
281    pub dyn_call_viji: Option<TypedFunction<(i32, i32, i32, i32, i32), ()>>,
282    pub dyn_call_vijiii: Option<TypedFunction<(i32, i32, i32, i32, i32, i32, i32), ()>>,
283    pub dyn_call_vijj: Option<TypedFunction<(i32, i32, i32, i32, i32, i32), ()>>,
284    pub dyn_call_viid: Option<TypedFunction<(i32, i32, i32, f64), ()>>,
285    pub dyn_call_vidd: Option<TypedFunction<(i32, i32, f64, f64), ()>>,
286    pub dyn_call_viidii: Option<TypedFunction<(i32, i32, i32, f64, i32, i32), ()>>,
287    pub dyn_call_viidddddddd:
288        Option<TypedFunction<(i32, i32, i32, f64, f64, f64, f64, f64, f64, f64, f64), ()>>,
289
290    pub stack_save: Option<TypedFunction<(), i32>>,
291    pub stack_restore: Option<TypedFunction<i32, ()>>,
292    pub set_threw: Option<TypedFunction<(i32, i32), ()>>,
293}
294
295#[derive(Clone, Default)]
296pub struct EmscriptenData {
297    pub globals: EmscriptenGlobalsData,
298
299    pub jumps: Arc<Mutex<Vec<[u32; 27]>>>,
300    pub opened_dirs: HashMap<i32, Box<LibcDirWrapper>>,
301
302    pub temp_ret_0: i32,
303
304    pub mapped_dirs: HashMap<String, PathBuf>,
305}
306
307impl EmscriptenData {
308    pub fn new(
309        globals: EmscriptenGlobalsData,
310        mapped_dirs: HashMap<String, PathBuf>,
311    ) -> EmscriptenData {
312        EmscriptenData {
313            globals,
314            temp_ret_0: 0,
315            mapped_dirs,
316            ..Default::default()
317        }
318    }
319}
320
321impl EmscriptenFunctions {
322    pub fn new() -> EmscriptenFunctions {
323        EmscriptenFunctions {
324            ..Default::default()
325        }
326    }
327    pub fn malloc_ref(&self) -> Option<&TypedFunction<u32, u32>> {
328        self.malloc.as_ref()
329    }
330    pub fn free_ref(&self) -> Option<&TypedFunction<u32, ()>> {
331        self.free.as_ref()
332    }
333    pub fn memalign_ref(&self) -> Option<&TypedFunction<(u32, u32), u32>> {
334        self.memalign.as_ref()
335    }
336    pub fn memset_ref(&self) -> Option<&TypedFunction<(u32, u32, u32), u32>> {
337        self.memset.as_ref()
338    }
339    pub fn stack_alloc_ref(&self) -> Option<&TypedFunction<u32, u32>> {
340        self.stack_alloc.as_ref()
341    }
342
343    pub fn dyn_call_i_ref(&self) -> Option<&TypedFunction<i32, i32>> {
344        self.dyn_call_i.as_ref()
345    }
346    pub fn dyn_call_ii_ref(&self) -> Option<&TypedFunction<(i32, i32), i32>> {
347        self.dyn_call_ii.as_ref()
348    }
349    pub fn dyn_call_iii_ref(&self) -> Option<&TypedFunction<(i32, i32, i32), i32>> {
350        self.dyn_call_iii.as_ref()
351    }
352    pub fn dyn_call_iiii_ref(&self) -> Option<&TypedFunction<(i32, i32, i32, i32), i32>> {
353        self.dyn_call_iiii.as_ref()
354    }
355    pub fn dyn_call_iifi_ref(&self) -> Option<&TypedFunction<(i32, i32, f64, i32), i32>> {
356        self.dyn_call_iifi.as_ref()
357    }
358    pub fn dyn_call_v_ref(&self) -> Option<&TypedFunction<i32, ()>> {
359        self.dyn_call_v.as_ref()
360    }
361    pub fn dyn_call_vi_ref(&self) -> Option<&TypedFunction<(i32, i32), ()>> {
362        self.dyn_call_vi.as_ref()
363    }
364    pub fn dyn_call_vii_ref(&self) -> Option<&TypedFunction<(i32, i32, i32), ()>> {
365        self.dyn_call_vii.as_ref()
366    }
367    pub fn dyn_call_viii_ref(&self) -> Option<&TypedFunction<(i32, i32, i32, i32), ()>> {
368        self.dyn_call_viii.as_ref()
369    }
370    pub fn dyn_call_viiii_ref(&self) -> Option<&TypedFunction<(i32, i32, i32, i32, i32), ()>> {
371        self.dyn_call_viiii.as_ref()
372    }
373    pub fn dyn_call_dii_ref(&self) -> Option<&TypedFunction<(i32, i32, i32), f64>> {
374        self.dyn_call_dii.as_ref()
375    }
376    pub fn dyn_call_diiii_ref(&self) -> Option<&TypedFunction<(i32, i32, i32, i32, i32), f64>> {
377        self.dyn_call_diiii.as_ref()
378    }
379    pub fn dyn_call_iiiii_ref(&self) -> Option<&TypedFunction<(i32, i32, i32, i32, i32), i32>> {
380        self.dyn_call_iiiii.as_ref()
381    }
382    pub fn dyn_call_iiiiii_ref(
383        &self,
384    ) -> Option<&TypedFunction<(i32, i32, i32, i32, i32, i32), i32>> {
385        self.dyn_call_iiiiii.as_ref()
386    }
387    pub fn dyn_call_iiiiiii_ref(
388        &self,
389    ) -> Option<&TypedFunction<(i32, i32, i32, i32, i32, i32, i32), i32>> {
390        self.dyn_call_iiiiiii.as_ref()
391    }
392    pub fn dyn_call_iiiiiiii_ref(
393        &self,
394    ) -> Option<&TypedFunction<(i32, i32, i32, i32, i32, i32, i32, i32), i32>> {
395        self.dyn_call_iiiiiiii.as_ref()
396    }
397    pub fn dyn_call_iiiiiiiii_ref(
398        &self,
399    ) -> Option<&TypedFunction<(i32, i32, i32, i32, i32, i32, i32, i32, i32), i32>> {
400        self.dyn_call_iiiiiiiii.as_ref()
401    }
402    pub fn dyn_call_iiiiiiiiii_ref(
403        &self,
404    ) -> Option<&TypedFunction<(i32, i32, i32, i32, i32, i32, i32, i32, i32, i32), i32>> {
405        self.dyn_call_iiiiiiiiii.as_ref()
406    }
407    pub fn dyn_call_iiiiiiiiiii_ref(
408        &self,
409    ) -> Option<&TypedFunction<(i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32), i32>> {
410        self.dyn_call_iiiiiiiiiii.as_ref()
411    }
412    pub fn dyn_call_vd_ref(&self) -> Option<&TypedFunction<(i32, f64), ()>> {
413        self.dyn_call_vd.as_ref()
414    }
415    pub fn dyn_call_viiiii_ref(
416        &self,
417    ) -> Option<&TypedFunction<(i32, i32, i32, i32, i32, i32), ()>> {
418        self.dyn_call_viiiii.as_ref()
419    }
420    pub fn dyn_call_viiiiii_ref(
421        &self,
422    ) -> Option<&TypedFunction<(i32, i32, i32, i32, i32, i32, i32), ()>> {
423        self.dyn_call_viiiiii.as_ref()
424    }
425    pub fn dyn_call_viiiiiii_ref(
426        &self,
427    ) -> Option<&TypedFunction<(i32, i32, i32, i32, i32, i32, i32, i32), ()>> {
428        self.dyn_call_viiiiiii.as_ref()
429    }
430    pub fn dyn_call_viiiiiiii_ref(
431        &self,
432    ) -> Option<&TypedFunction<(i32, i32, i32, i32, i32, i32, i32, i32, i32), ()>> {
433        self.dyn_call_viiiiiiii.as_ref()
434    }
435    pub fn dyn_call_viiiiiiiii_ref(
436        &self,
437    ) -> Option<&TypedFunction<(i32, i32, i32, i32, i32, i32, i32, i32, i32, i32), ()>> {
438        self.dyn_call_viiiiiiiii.as_ref()
439    }
440    pub fn dyn_call_viiiiiiiiii_ref(
441        &self,
442    ) -> Option<&TypedFunction<(i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32), ()>> {
443        self.dyn_call_viiiiiiiiii.as_ref()
444    }
445    pub fn dyn_call_iij_ref(&self) -> Option<&TypedFunction<(i32, i32, i32, i32), i32>> {
446        self.dyn_call_iij.as_ref()
447    }
448    pub fn dyn_call_iji_ref(&self) -> Option<&TypedFunction<(i32, i32, i32, i32), i32>> {
449        self.dyn_call_iji.as_ref()
450    }
451    pub fn dyn_call_iiji_ref(&self) -> Option<&TypedFunction<(i32, i32, i32, i32, i32), i32>> {
452        self.dyn_call_iiji.as_ref()
453    }
454    pub fn dyn_call_iiijj_ref(
455        &self,
456    ) -> Option<&TypedFunction<(i32, i32, i32, i32, i32, i32, i32), i32>> {
457        self.dyn_call_iiijj.as_ref()
458    }
459    pub fn dyn_call_j_ref(&self) -> Option<&TypedFunction<i32, i32>> {
460        self.dyn_call_j.as_ref()
461    }
462    pub fn dyn_call_ji_ref(&self) -> Option<&TypedFunction<(i32, i32), i32>> {
463        self.dyn_call_ji.as_ref()
464    }
465    pub fn dyn_call_jii_ref(&self) -> Option<&TypedFunction<(i32, i32, i32), i32>> {
466        self.dyn_call_jii.as_ref()
467    }
468    pub fn dyn_call_jij_ref(&self) -> Option<&TypedFunction<(i32, i32, i32, i32), i32>> {
469        self.dyn_call_jij.as_ref()
470    }
471    pub fn dyn_call_jjj_ref(&self) -> Option<&TypedFunction<(i32, i32, i32, i32, i32), i32>> {
472        self.dyn_call_jjj.as_ref()
473    }
474    pub fn dyn_call_viiij_ref(&self) -> Option<&TypedFunction<(i32, i32, i32, i32, i32, i32), ()>> {
475        self.dyn_call_viiij.as_ref()
476    }
477    pub fn dyn_call_viiijiiii_ref(
478        &self,
479    ) -> Option<&TypedFunction<(i32, i32, i32, i32, i32, i32, i32, i32, i32, i32), ()>> {
480        self.dyn_call_viiijiiii.as_ref()
481    }
482    pub fn dyn_call_viiijiiiiii_ref(
483        &self,
484    ) -> Option<&TypedFunction<(i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32), ()>>
485    {
486        self.dyn_call_viiijiiiiii.as_ref()
487    }
488    pub fn dyn_call_viij_ref(&self) -> Option<&TypedFunction<(i32, i32, i32, i32, i32), ()>> {
489        self.dyn_call_viij.as_ref()
490    }
491    pub fn dyn_call_viiji_ref(&self) -> Option<&TypedFunction<(i32, i32, i32, i32, i32, i32), ()>> {
492        self.dyn_call_viiji.as_ref()
493    }
494    pub fn dyn_call_viijiii_ref(
495        &self,
496    ) -> Option<&TypedFunction<(i32, i32, i32, i32, i32, i32, i32, i32), ()>> {
497        self.dyn_call_viijiii.as_ref()
498    }
499    pub fn dyn_call_viijj_ref(
500        &self,
501    ) -> Option<&TypedFunction<(i32, i32, i32, i32, i32, i32, i32), ()>> {
502        self.dyn_call_viijj.as_ref()
503    }
504    pub fn dyn_call_vj_ref(&self) -> Option<&TypedFunction<(i32, i32, i32), ()>> {
505        self.dyn_call_vj.as_ref()
506    }
507    pub fn dyn_call_vjji_ref(&self) -> Option<&TypedFunction<(i32, i32, i32, i32, i32, i32), ()>> {
508        self.dyn_call_vjji.as_ref()
509    }
510    pub fn dyn_call_vij_ref(&self) -> Option<&TypedFunction<(i32, i32, i32, i32), ()>> {
511        self.dyn_call_vij.as_ref()
512    }
513    pub fn dyn_call_viji_ref(&self) -> Option<&TypedFunction<(i32, i32, i32, i32, i32), ()>> {
514        self.dyn_call_viji.as_ref()
515    }
516    pub fn dyn_call_vijiii_ref(
517        &self,
518    ) -> Option<&TypedFunction<(i32, i32, i32, i32, i32, i32, i32), ()>> {
519        self.dyn_call_vijiii.as_ref()
520    }
521    pub fn dyn_call_vijj_ref(&self) -> Option<&TypedFunction<(i32, i32, i32, i32, i32, i32), ()>> {
522        self.dyn_call_vijj.as_ref()
523    }
524    pub fn dyn_call_viid_ref(&self) -> Option<&TypedFunction<(i32, i32, i32, f64), ()>> {
525        self.dyn_call_viid.as_ref()
526    }
527    pub fn dyn_call_vidd_ref(&self) -> Option<&TypedFunction<(i32, i32, f64, f64), ()>> {
528        self.dyn_call_vidd.as_ref()
529    }
530    pub fn dyn_call_viidii_ref(
531        &self,
532    ) -> Option<&TypedFunction<(i32, i32, i32, f64, i32, i32), ()>> {
533        self.dyn_call_viidii.as_ref()
534    }
535    pub fn dyn_call_viidddddddd_ref(
536        &self,
537    ) -> Option<&TypedFunction<(i32, i32, i32, f64, f64, f64, f64, f64, f64, f64, f64), ()>> {
538        self.dyn_call_viidddddddd.as_ref()
539    }
540
541    pub fn stack_save_ref(&self) -> Option<&TypedFunction<(), i32>> {
542        self.stack_save.as_ref()
543    }
544    pub fn stack_restore_ref(&self) -> Option<&TypedFunction<i32, ()>> {
545        self.stack_restore.as_ref()
546    }
547    pub fn set_threw_ref(&self) -> Option<&TypedFunction<(i32, i32), ()>> {
548        self.set_threw.as_ref()
549    }
550}
551
552/// Call the global constructors for C++ and set up the emscripten environment.
553///
554/// Note that this function does not completely set up Emscripten to be called.
555/// before calling this function, please initialize `Ctx::data` with a pointer
556/// to [`EmscriptenData`].
557pub fn set_up_emscripten(
558    store: &mut impl AsStoreMut,
559    instance: &mut Instance,
560) -> Result<(), RuntimeError> {
561    // ATINIT
562    // (used by C++)
563    if let Ok(func) = instance.exports.get_function("globalCtors") {
564        func.call(store, &[])?;
565    }
566
567    if let Ok(func) = instance
568        .exports
569        .get_function("___emscripten_environ_constructor")
570    {
571        func.call(store, &[])?;
572    }
573    Ok(())
574}
575
576/// Looks for variations of the main function (usually
577/// `["_main", "main"])`, then returns a reference to
578/// the name of the first found function. Useful for
579/// determining whether a module is executable.
580///
581/// Returns `ExportError` if none of the `main_func_names`
582/// were found.
583pub fn emscripten_get_main_func_name<'a>(
584    instance: &Instance,
585    main_func_names: &[&'a str],
586) -> Result<&'a str, ExportError> {
587    let mut last_err = None;
588
589    for func_name in main_func_names.iter() {
590        match instance.exports.get::<Function>(func_name) {
591            Ok(_) => {
592                return Ok(func_name);
593            }
594            Err(e) => {
595                last_err = Some(e);
596            }
597        }
598    }
599
600    match last_err {
601        None => Err(ExportError::Missing(format!("{main_func_names:?}"))),
602        Some(e) => Err(e),
603    }
604}
605
606/// Call the main function in emscripten, assumes that the emscripten state is
607/// set up.
608///
609/// If you don't want to set it up yourself, consider using [`run_emscripten_instance`].
610pub fn emscripten_call_main(
611    instance: &mut Instance,
612    function_name: &str,
613    mut env: FunctionEnvMut<EmEnv>,
614    path: &str,
615    args: &[&str],
616) -> Result<(), RuntimeError> {
617    let main_func = instance
618        .exports
619        .get::<Function>(function_name)
620        .map_err(|e| RuntimeError::new(e.to_string()))?;
621    let num_params = main_func.ty(&env).params().len();
622    match num_params {
623        2 => {
624            let mut new_args = vec![path];
625            new_args.extend(args);
626            let (argc, argv) = store_module_arguments(&mut env, new_args);
627            let func: &Function = instance
628                .exports
629                .get(function_name)
630                .map_err(|e| RuntimeError::new(e.to_string()))?;
631            func.call(
632                &mut env,
633                &[Value::I32(argc as i32), Value::I32(argv as i32)],
634            )?;
635        }
636        0 => {
637            let func: &Function = instance
638                .exports
639                .get(function_name)
640                .map_err(|e| RuntimeError::new(e.to_string()))?;
641            func.call(&mut env, &[])?;
642        }
643        _ => {
644            todo!("Update error type to be able to express this");
645            /*return Err(RuntimeError:: CallError::Resolve(ResolveError::ExportWrongType {
646                name: "main".to_string(),
647            }))*/
648        }
649    };
650
651    Ok(())
652}
653
654/// Top level function to execute emscripten
655pub fn run_emscripten_instance(
656    instance: &mut Instance,
657    mut env: FunctionEnvMut<EmEnv>,
658    globals: &mut EmscriptenGlobals,
659    path: &str,
660    args: Vec<&str>,
661    entrypoint: Option<String>,
662) -> Result<(), RuntimeError> {
663    env.data_mut().set_memory(globals.memory.clone());
664
665    // get emscripten export
666    let mut emfuncs = EmscriptenFunctions::new();
667    if let Ok(func) = instance.exports.get_typed_function(&env, "malloc") {
668        emfuncs.malloc = Some(func);
669    } else if let Ok(func) = instance.exports.get_typed_function(&env, "_malloc") {
670        emfuncs.malloc = Some(func);
671    }
672    if let Ok(func) = instance.exports.get_typed_function(&env, "free") {
673        emfuncs.free = Some(func);
674    } else if let Ok(func) = instance.exports.get_typed_function(&env, "_free") {
675        emfuncs.free = Some(func);
676    }
677    if let Ok(func) = instance.exports.get_typed_function(&env, "memalign") {
678        emfuncs.memalign = Some(func);
679    } else if let Ok(func) = instance.exports.get_typed_function(&env, "_memalign") {
680        emfuncs.memalign = Some(func);
681    }
682    if let Ok(func) = instance.exports.get_typed_function(&env, "memset") {
683        emfuncs.memset = Some(func);
684    } else if let Ok(func) = instance.exports.get_typed_function(&env, "_memset") {
685        emfuncs.memset = Some(func);
686    }
687    if let Ok(func) = instance.exports.get_typed_function(&env, "stackAlloc") {
688        emfuncs.stack_alloc = Some(func);
689    }
690    if let Ok(func) = instance.exports.get_typed_function(&env, "dynCall_i") {
691        emfuncs.dyn_call_i = Some(func);
692    }
693    if let Ok(func) = instance.exports.get_typed_function(&env, "dynCall_ii") {
694        emfuncs.dyn_call_ii = Some(func);
695    }
696    if let Ok(func) = instance.exports.get_typed_function(&env, "dynCall_iii") {
697        emfuncs.dyn_call_iii = Some(func);
698    }
699    if let Ok(func) = instance.exports.get_typed_function(&env, "dynCall_iiii") {
700        emfuncs.dyn_call_iiii = Some(func);
701    }
702    if let Ok(func) = instance.exports.get_typed_function(&env, "dynCall_iifi") {
703        emfuncs.dyn_call_iifi = Some(func);
704    }
705    if let Ok(func) = instance.exports.get_typed_function(&env, "dynCall_v") {
706        emfuncs.dyn_call_v = Some(func);
707    }
708    if let Ok(func) = instance.exports.get_typed_function(&env, "dynCall_vi") {
709        emfuncs.dyn_call_vi = Some(func);
710    }
711    if let Ok(func) = instance.exports.get_typed_function(&env, "dynCall_vii") {
712        emfuncs.dyn_call_vii = Some(func);
713    }
714    if let Ok(func) = instance.exports.get_typed_function(&env, "dynCall_viii") {
715        emfuncs.dyn_call_viii = Some(func);
716    }
717    if let Ok(func) = instance.exports.get_typed_function(&env, "dynCall_viiii") {
718        emfuncs.dyn_call_viiii = Some(func);
719    }
720    if let Ok(func) = instance.exports.get_typed_function(&env, "dynCall_dii") {
721        emfuncs.dyn_call_dii = Some(func);
722    }
723    if let Ok(func) = instance.exports.get_typed_function(&env, "dynCall_diiii") {
724        emfuncs.dyn_call_diiii = Some(func);
725    }
726    if let Ok(func) = instance.exports.get_typed_function(&env, "dynCall_iiiii") {
727        emfuncs.dyn_call_iiiii = Some(func);
728    }
729    if let Ok(func) = instance.exports.get_typed_function(&env, "dynCall_iiiiii") {
730        emfuncs.dyn_call_iiiiii = Some(func);
731    }
732    if let Ok(func) = instance.exports.get_typed_function(&env, "dynCall_iiiiiii") {
733        emfuncs.dyn_call_iiiiiii = Some(func);
734    }
735    if let Ok(func) = instance
736        .exports
737        .get_typed_function(&env, "dynCall_iiiiiiii")
738    {
739        emfuncs.dyn_call_iiiiiiii = Some(func);
740    }
741    if let Ok(func) = instance
742        .exports
743        .get_typed_function(&env, "dynCall_iiiiiiiii")
744    {
745        emfuncs.dyn_call_iiiiiiiii = Some(func);
746    }
747    if let Ok(func) = instance
748        .exports
749        .get_typed_function(&env, "dynCall_iiiiiiiiii")
750    {
751        emfuncs.dyn_call_iiiiiiiiii = Some(func);
752    }
753    if let Ok(func) = instance
754        .exports
755        .get_typed_function(&env, "dynCall_iiiiiiiiiii")
756    {
757        emfuncs.dyn_call_iiiiiiiiiii = Some(func);
758    }
759    if let Ok(func) = instance.exports.get_typed_function(&env, "dynCall_vd") {
760        emfuncs.dyn_call_vd = Some(func);
761    }
762    if let Ok(func) = instance.exports.get_typed_function(&env, "dynCall_viiiii") {
763        emfuncs.dyn_call_viiiii = Some(func);
764    }
765    if let Ok(func) = instance.exports.get_typed_function(&env, "dynCall_viiiiii") {
766        emfuncs.dyn_call_viiiiii = Some(func);
767    }
768    if let Ok(func) = instance
769        .exports
770        .get_typed_function(&env, "dynCall_viiiiiii")
771    {
772        emfuncs.dyn_call_viiiiiii = Some(func);
773    }
774    if let Ok(func) = instance
775        .exports
776        .get_typed_function(&env, "dynCall_viiiiiiii")
777    {
778        emfuncs.dyn_call_viiiiiiii = Some(func);
779    }
780    if let Ok(func) = instance
781        .exports
782        .get_typed_function(&env, "dynCall_viiiiiiiii")
783    {
784        emfuncs.dyn_call_viiiiiiiii = Some(func);
785    }
786    if let Ok(func) = instance
787        .exports
788        .get_typed_function(&env, "dynCall_viiiiiiiiii")
789    {
790        emfuncs.dyn_call_viiiiiiiiii = Some(func);
791    }
792    if let Ok(func) = instance.exports.get_typed_function(&env, "dynCall_iij") {
793        emfuncs.dyn_call_iij = Some(func);
794    }
795    if let Ok(func) = instance.exports.get_typed_function(&env, "dynCall_iji") {
796        emfuncs.dyn_call_iji = Some(func);
797    }
798    if let Ok(func) = instance.exports.get_typed_function(&env, "dynCall_iiji") {
799        emfuncs.dyn_call_iiji = Some(func);
800    }
801    if let Ok(func) = instance.exports.get_typed_function(&env, "dynCall_iiijj") {
802        emfuncs.dyn_call_iiijj = Some(func);
803    }
804    if let Ok(func) = instance.exports.get_typed_function(&env, "dynCall_j") {
805        emfuncs.dyn_call_j = Some(func);
806    }
807    if let Ok(func) = instance.exports.get_typed_function(&env, "dynCall_ji") {
808        emfuncs.dyn_call_ji = Some(func);
809    }
810    if let Ok(func) = instance.exports.get_typed_function(&env, "dynCall_jii") {
811        emfuncs.dyn_call_jii = Some(func);
812    }
813    if let Ok(func) = instance.exports.get_typed_function(&env, "dynCall_jij") {
814        emfuncs.dyn_call_jij = Some(func);
815    }
816    if let Ok(func) = instance.exports.get_typed_function(&env, "dynCall_jjj") {
817        emfuncs.dyn_call_jjj = Some(func);
818    }
819    if let Ok(func) = instance.exports.get_typed_function(&env, "dynCall_viiij") {
820        emfuncs.dyn_call_viiij = Some(func);
821    }
822    if let Ok(func) = instance
823        .exports
824        .get_typed_function(&env, "dynCall_viiijiiii")
825    {
826        emfuncs.dyn_call_viiijiiii = Some(func);
827    }
828    if let Ok(func) = instance
829        .exports
830        .get_typed_function(&env, "dynCall_viiijiiiiii")
831    {
832        emfuncs.dyn_call_viiijiiiiii = Some(func);
833    }
834    if let Ok(func) = instance.exports.get_typed_function(&env, "dynCall_viij") {
835        emfuncs.dyn_call_viij = Some(func);
836    }
837    if let Ok(func) = instance.exports.get_typed_function(&env, "dynCall_viiji") {
838        emfuncs.dyn_call_viiji = Some(func);
839    }
840    if let Ok(func) = instance.exports.get_typed_function(&env, "dynCall_viijiii") {
841        emfuncs.dyn_call_viijiii = Some(func);
842    }
843    if let Ok(func) = instance.exports.get_typed_function(&env, "dynCall_viijj") {
844        emfuncs.dyn_call_viijj = Some(func);
845    }
846    if let Ok(func) = instance.exports.get_typed_function(&env, "dynCall_vj") {
847        emfuncs.dyn_call_vj = Some(func);
848    }
849    if let Ok(func) = instance.exports.get_typed_function(&env, "dynCall_vjji") {
850        emfuncs.dyn_call_vjji = Some(func);
851    }
852    if let Ok(func) = instance.exports.get_typed_function(&env, "dynCall_vij") {
853        emfuncs.dyn_call_vij = Some(func);
854    }
855    if let Ok(func) = instance.exports.get_typed_function(&env, "dynCall_viji") {
856        emfuncs.dyn_call_viji = Some(func);
857    }
858    if let Ok(func) = instance.exports.get_typed_function(&env, "dynCall_vijiii") {
859        emfuncs.dyn_call_vijiii = Some(func);
860    }
861    if let Ok(func) = instance.exports.get_typed_function(&env, "dynCall_vijj") {
862        emfuncs.dyn_call_vijj = Some(func);
863    }
864    if let Ok(func) = instance.exports.get_typed_function(&env, "dynCall_viid") {
865        emfuncs.dyn_call_viid = Some(func);
866    }
867    if let Ok(func) = instance.exports.get_typed_function(&env, "dynCall_vidd") {
868        emfuncs.dyn_call_vidd = Some(func);
869    }
870    if let Ok(func) = instance.exports.get_typed_function(&env, "dynCall_viidii") {
871        emfuncs.dyn_call_viidii = Some(func);
872    }
873    if let Ok(func) = instance
874        .exports
875        .get_typed_function(&env, "dynCall_viidddddddd")
876    {
877        emfuncs.dyn_call_viidddddddd = Some(func);
878    }
879    if let Ok(func) = instance.exports.get_typed_function(&env, "stackSave") {
880        emfuncs.stack_save = Some(func);
881    }
882    if let Ok(func) = instance.exports.get_typed_function(&env, "stackRestore") {
883        emfuncs.stack_restore = Some(func);
884    }
885    if let Ok(func) = instance.exports.get_typed_function(&env, "setThrew") {
886        emfuncs.set_threw = Some(func);
887    }
888    env.data().set_functions(emfuncs);
889
890    set_up_emscripten(&mut env, instance)?;
891
892    let main_func_names = ["_main", "main"];
893    if let Some(ep) = entrypoint.as_ref() {
894        debug!("Running entry point: {}", ep);
895        emscripten_call_main(instance, ep, env, path, &args)?;
896    } else if let Ok(name) = emscripten_get_main_func_name(instance, &main_func_names) {
897        emscripten_call_main(instance, name, env, path, &args)?;
898    } else {
899        return Err(RuntimeError::new(format!(
900            "No main function found (searched: {main_func_names:?}) and no entrypoint specified"
901        )));
902    }
903
904    // TODO atexit for emscripten
905    // println!("{:?}", data);
906    Ok(())
907}
908
909fn store_module_arguments(env: &mut FunctionEnvMut<EmEnv>, args: Vec<&str>) -> (u32, u32) {
910    let argc = args.len() + 1;
911
912    let mut args_slice = vec![0; argc];
913    for (slot, arg) in args_slice[0..argc].iter_mut().zip(args.iter()) {
914        *slot = unsafe { allocate_cstr_on_stack(&mut env.as_mut(), arg).0 };
915    }
916
917    let (argv_offset, argv_slice): (_, &mut [u32]) =
918        unsafe { allocate_on_stack(&mut env.as_mut(), ((argc) * 4) as u32) };
919    assert!(!argv_slice.is_empty());
920    for (slot, arg) in argv_slice[0..argc].iter_mut().zip(args_slice.iter()) {
921        *slot = *arg
922    }
923    argv_slice[argc] = 0;
924
925    (argc as u32 - 1, argv_offset)
926}
927
928pub fn emscripten_set_up_memory(
929    store: &impl AsStoreRef,
930    env: &FunctionEnv<EmEnv>,
931    memory: &Memory,
932    globals: &EmscriptenGlobalsData,
933) -> Result<(), String> {
934    env.as_ref(store).set_memory(memory.clone());
935    let memory = memory.view(store);
936    let dynamictop_ptr = WasmPtr::<i32>::new(globals.dynamictop_ptr).deref(&memory);
937    let dynamic_base = globals.dynamic_base;
938
939    if dynamictop_ptr.offset() >= memory.data_size() {
940        return Err("dynamictop_ptr beyond memory len".to_string());
941    }
942    dynamictop_ptr.write(dynamic_base as i32).unwrap();
943    Ok(())
944}
945
946#[derive(Debug, Clone, Default)]
947pub struct EmscriptenGlobalsData {
948    abort: u64,
949    // Env namespace
950    stacktop: u32,
951    stack_max: u32,
952    dynamictop_ptr: u32,
953    dynamic_base: u32,
954    memory_base: u32,
955    table_base: u32,
956    temp_double_ptr: u32,
957    use_old_abort_on_cannot_grow_memory: bool,
958}
959
960pub struct EmscriptenGlobals {
961    // The emscripten data
962    pub data: EmscriptenGlobalsData,
963    // The emscripten memory
964    pub memory: Memory,
965    pub table: Table,
966    pub memory_min: Pages,
967    pub memory_max: Option<Pages>,
968    pub null_function_names: Vec<String>,
969}
970
971impl EmscriptenGlobals {
972    pub fn new(
973        mut store: &mut impl AsStoreMut,
974        env: &FunctionEnv<EmEnv>,
975        module: &Module, /*, static_bump: u32 */
976    ) -> Result<Self, String> {
977        let mut use_old_abort_on_cannot_grow_memory = false;
978        for import in module.imports().functions() {
979            if import.name() == "abortOnCannotGrowMemory" && import.module() == "env" {
980                if import.ty() == &*OLD_ABORT_ON_CANNOT_GROW_MEMORY_SIG {
981                    use_old_abort_on_cannot_grow_memory = true;
982                }
983                break;
984            }
985        }
986
987        let (table_min, table_max) = get_emscripten_table_size(module)?;
988        let (memory_min, memory_max, shared) = get_emscripten_memory_size(module)?;
989
990        // Memory initialization
991        let memory_type = MemoryType::new(memory_min, memory_max, shared);
992        let memory = Memory::new(&mut store, memory_type).unwrap();
993
994        let table_type = TableType {
995            ty: ValType::FuncRef,
996            minimum: table_min,
997            maximum: table_max,
998        };
999        let table = Table::new(&mut store, table_type, Value::FuncRef(None)).unwrap();
1000
1001        let data = {
1002            let static_bump = STATIC_BUMP;
1003
1004            let mut static_top = STATIC_BASE + static_bump;
1005
1006            let memory_base = STATIC_BASE;
1007            let table_base = 0;
1008
1009            let temp_double_ptr = static_top;
1010            static_top += 16;
1011
1012            let (dynamic_base, dynamictop_ptr) =
1013                get_emscripten_metadata(module)?.unwrap_or_else(|| {
1014                    let dynamictop_ptr = static_alloc(&mut static_top, 4);
1015                    (
1016                        align_memory(align_memory(static_top) + TOTAL_STACK),
1017                        dynamictop_ptr,
1018                    )
1019                });
1020
1021            let stacktop = align_memory(static_top);
1022            let stack_max = stacktop + TOTAL_STACK;
1023
1024            EmscriptenGlobalsData {
1025                abort: 0,
1026                stacktop,
1027                stack_max,
1028                dynamictop_ptr,
1029                dynamic_base,
1030                memory_base,
1031                table_base,
1032                temp_double_ptr,
1033                use_old_abort_on_cannot_grow_memory,
1034            }
1035        };
1036
1037        emscripten_set_up_memory(store, env, &memory, &data)?;
1038
1039        let mut null_function_names = vec![];
1040        for import in module.imports().functions() {
1041            if import.module() == "env"
1042                && (import.name().starts_with("nullFunction_")
1043                    || import.name().starts_with("nullFunc_"))
1044            {
1045                null_function_names.push(import.name().to_string())
1046            }
1047        }
1048
1049        Ok(Self {
1050            data,
1051            memory,
1052            table,
1053            memory_min,
1054            memory_max,
1055            null_function_names,
1056        })
1057    }
1058}
1059
1060pub fn generate_emscripten_env(
1061    mut store: &mut impl AsStoreMut,
1062    env: &FunctionEnv<EmEnv>,
1063    globals: &mut EmscriptenGlobals,
1064) -> Imports {
1065    let abort_on_cannot_grow_memory_export = if globals.data.use_old_abort_on_cannot_grow_memory {
1066        Function::new_typed_with_env(
1067            &mut store,
1068            env,
1069            crate::memory::abort_on_cannot_grow_memory_old,
1070        )
1071    } else {
1072        Function::new_typed_with_env(&mut store, env, crate::memory::abort_on_cannot_grow_memory)
1073    };
1074
1075    let mut env_ns: Exports = namespace! {
1076        "memory" => globals.memory.clone(),
1077        "table" => globals.table.clone(),
1078
1079        // Globals
1080        "STACKTOP" => Global::new(&mut store, Value::I32(globals.data.stacktop as i32)),
1081        "STACK_MAX" => Global::new(&mut store, Value::I32(globals.data.stack_max as i32)),
1082        "DYNAMICTOP_PTR" => Global::new(&mut store, Value::I32(globals.data.dynamictop_ptr as i32)),
1083        "fb" => Global::new(&mut store, Value::I32(globals.data.table_base as i32)),
1084        "tableBase" => Global::new(&mut store, Value::I32(globals.data.table_base as i32)),
1085        "__table_base" => Global::new(&mut store, Value::I32(globals.data.table_base as i32)),
1086        "ABORT" => Global::new(&mut store, Value::I32(globals.data.abort as i32)),
1087        "gb" => Global::new(&mut store, Value::I32(globals.data.memory_base as i32)),
1088        "memoryBase" => Global::new(&mut store, Value::I32(globals.data.memory_base as i32)),
1089        "__memory_base" => Global::new(&mut store, Value::I32(globals.data.memory_base as i32)),
1090        "tempDoublePtr" => Global::new(&mut store, Value::I32(globals.data.temp_double_ptr as i32)),
1091
1092        // inet
1093        "_inet_addr" => Function::new_typed_with_env(&mut store, env, crate::inet::addr),
1094
1095        // IO
1096        "printf" => Function::new_typed_with_env(&mut store, env, crate::io::printf),
1097        "putchar" => Function::new_typed_with_env(&mut store, env, crate::io::putchar),
1098        "___lock" => Function::new_typed_with_env(&mut store, env, crate::lock::___lock),
1099        "___unlock" => Function::new_typed_with_env(&mut store, env, crate::lock::___unlock),
1100        "___wait" => Function::new_typed_with_env(&mut store, env, crate::lock::___wait),
1101        "_flock" => Function::new_typed_with_env(&mut store, env, crate::lock::_flock),
1102        "_chroot" => Function::new_typed_with_env(&mut store, env, crate::io::chroot),
1103        "_getprotobyname" => Function::new_typed_with_env(&mut store, env, crate::io::getprotobyname),
1104        "_getprotobynumber" => Function::new_typed_with_env(&mut store, env, crate::io::getprotobynumber),
1105        "_getpwuid" => Function::new_typed_with_env(&mut store, env, crate::io::getpwuid),
1106        "_sigdelset" => Function::new_typed_with_env(&mut store, env, crate::io::sigdelset),
1107        "_sigfillset" => Function::new_typed_with_env(&mut store, env, crate::io::sigfillset),
1108        "_tzset" => Function::new_typed_with_env(&mut store, env, crate::io::tzset),
1109        "_strptime" => Function::new_typed_with_env(&mut store, env, crate::io::strptime),
1110
1111        // exec
1112        "_execvp" => Function::new_typed_with_env(&mut store, env, crate::exec::execvp),
1113        "_execl" => Function::new_typed_with_env(&mut store, env, crate::exec::execl),
1114        "_execle" => Function::new_typed_with_env(&mut store, env, crate::exec::execle),
1115
1116        // exit
1117        "__exit" => Function::new_typed_with_env(&mut store, env, crate::exit::exit),
1118
1119        // Env
1120        "___assert_fail" => Function::new_typed_with_env(&mut store, env, crate::env::___assert_fail),
1121        "_getenv" => Function::new_typed_with_env(&mut store, env, crate::env::_getenv),
1122        "_setenv" => Function::new_typed_with_env(&mut store, env, crate::env::_setenv),
1123        "_putenv" => Function::new_typed_with_env(&mut store, env, crate::env::_putenv),
1124        "_unsetenv" => Function::new_typed_with_env(&mut store, env, crate::env::_unsetenv),
1125        "_getpwnam" => Function::new_typed_with_env(&mut store, env, crate::env::_getpwnam),
1126        "_getgrnam" => Function::new_typed_with_env(&mut store, env, crate::env::_getgrnam),
1127        "___buildEnvironment" => Function::new_typed_with_env(&mut store, env, crate::env::___build_environment),
1128        "___setErrNo" => Function::new_typed_with_env(&mut store, env, crate::errno::___seterrno),
1129        "_getpagesize" => Function::new_typed_with_env(&mut store, env, crate::env::_getpagesize),
1130        "_sysconf" => Function::new_typed_with_env(&mut store, env, crate::env::_sysconf),
1131        "_getaddrinfo" => Function::new_typed_with_env(&mut store, env, crate::env::_getaddrinfo),
1132        "_times" => Function::new_typed_with_env(&mut store, env, crate::env::_times),
1133        "_pathconf" => Function::new_typed_with_env(&mut store, env, crate::env::_pathconf),
1134        "_fpathconf" => Function::new_typed_with_env(&mut store, env, crate::env::_fpathconf),
1135
1136        // Syscalls
1137        "___syscall1" => Function::new_typed_with_env(&mut store, env, crate::syscalls::___syscall1),
1138        "___syscall3" => Function::new_typed_with_env(&mut store, env, crate::syscalls::___syscall3),
1139        "___syscall4" => Function::new_typed_with_env(&mut store, env, crate::syscalls::___syscall4),
1140        "___syscall5" => Function::new_typed_with_env(&mut store, env, crate::syscalls::___syscall5),
1141        "___syscall6" => Function::new_typed_with_env(&mut store, env, crate::syscalls::___syscall6),
1142        "___syscall9" => Function::new_typed_with_env(&mut store, env, crate::syscalls::___syscall9),
1143        "___syscall10" => Function::new_typed_with_env(&mut store, env, crate::syscalls::___syscall10),
1144        "___syscall12" => Function::new_typed_with_env(&mut store, env, crate::syscalls::___syscall12),
1145        "___syscall14" => Function::new_typed_with_env(&mut store, env, crate::syscalls::___syscall14),
1146        "___syscall15" => Function::new_typed_with_env(&mut store, env, crate::syscalls::___syscall15),
1147        "___syscall20" => Function::new_typed_with_env(&mut store, env, crate::syscalls::___syscall20),
1148        "___syscall21" => Function::new_typed_with_env(&mut store, env, crate::syscalls::___syscall21),
1149        "___syscall25" => Function::new_typed_with_env(&mut store, env, crate::syscalls::___syscall25),
1150        "___syscall29" => Function::new_typed_with_env(&mut store, env, crate::syscalls::___syscall29),
1151        "___syscall32" => Function::new_typed_with_env(&mut store, env, crate::syscalls::___syscall32),
1152        "___syscall33" => Function::new_typed_with_env(&mut store, env, crate::syscalls::___syscall33),
1153        "___syscall34" => Function::new_typed_with_env(&mut store, env, crate::syscalls::___syscall34),
1154        "___syscall36" => Function::new_typed_with_env(&mut store, env, crate::syscalls::___syscall36),
1155        "___syscall39" => Function::new_typed_with_env(&mut store, env, crate::syscalls::___syscall39),
1156        "___syscall38" => Function::new_typed_with_env(&mut store, env, crate::syscalls::___syscall38),
1157        "___syscall40" => Function::new_typed_with_env(&mut store, env, crate::syscalls::___syscall40),
1158        "___syscall41" => Function::new_typed_with_env(&mut store, env, crate::syscalls::___syscall41),
1159        "___syscall42" => Function::new_typed_with_env(&mut store, env, crate::syscalls::___syscall42),
1160        "___syscall51" => Function::new_typed_with_env(&mut store, env, crate::syscalls::___syscall51),
1161        "___syscall52" => Function::new_typed_with_env(&mut store, env, crate::syscalls::___syscall52),
1162        "___syscall53" => Function::new_typed_with_env(&mut store, env, crate::syscalls::___syscall53),
1163        "___syscall54" => Function::new_typed_with_env(&mut store, env, crate::syscalls::___syscall54),
1164        "___syscall57" => Function::new_typed_with_env(&mut store, env, crate::syscalls::___syscall57),
1165        "___syscall60" => Function::new_typed_with_env(&mut store, env, crate::syscalls::___syscall60),
1166        "___syscall63" => Function::new_typed_with_env(&mut store, env, crate::syscalls::___syscall63),
1167        "___syscall64" => Function::new_typed_with_env(&mut store, env, crate::syscalls::___syscall64),
1168        "___syscall66" => Function::new_typed_with_env(&mut store, env, crate::syscalls::___syscall66),
1169        "___syscall75" => Function::new_typed_with_env(&mut store, env, crate::syscalls::___syscall75),
1170        "___syscall77" => Function::new_typed_with_env(&mut store, env, crate::syscalls::___syscall77),
1171        "___syscall83" => Function::new_typed_with_env(&mut store, env, crate::syscalls::___syscall83),
1172        "___syscall85" => Function::new_typed_with_env(&mut store, env, crate::syscalls::___syscall85),
1173        "___syscall91" => Function::new_typed_with_env(&mut store, env, crate::syscalls::___syscall91),
1174        "___syscall94" => Function::new_typed_with_env(&mut store, env, crate::syscalls::___syscall94),
1175        "___syscall96" => Function::new_typed_with_env(&mut store, env, crate::syscalls::___syscall96),
1176        "___syscall97" => Function::new_typed_with_env(&mut store, env, crate::syscalls::___syscall97),
1177        "___syscall102" => Function::new_typed_with_env(&mut store, env, crate::syscalls::___syscall102),
1178        "___syscall110" => Function::new_typed_with_env(&mut store, env, crate::syscalls::___syscall110),
1179        "___syscall114" => Function::new_typed_with_env(&mut store, env, crate::syscalls::___syscall114),
1180        "___syscall118" => Function::new_typed_with_env(&mut store, env, crate::syscalls::___syscall118),
1181        "___syscall121" => Function::new_typed_with_env(&mut store, env, crate::syscalls::___syscall121),
1182        "___syscall122" => Function::new_typed_with_env(&mut store, env, crate::syscalls::___syscall122),
1183        "___syscall125" => Function::new_typed_with_env(&mut store, env, crate::syscalls::___syscall125),
1184        "___syscall132" => Function::new_typed_with_env(&mut store, env, crate::syscalls::___syscall132),
1185        "___syscall133" => Function::new_typed_with_env(&mut store, env, crate::syscalls::___syscall133),
1186        "___syscall140" => Function::new_typed_with_env(&mut store, env, crate::syscalls::___syscall140),
1187        "___syscall142" => Function::new_typed_with_env(&mut store, env, crate::syscalls::___syscall142),
1188        "___syscall144" => Function::new_typed_with_env(&mut store, env, crate::syscalls::___syscall144),
1189        "___syscall145" => Function::new_typed_with_env(&mut store, env, crate::syscalls::___syscall145),
1190        "___syscall146" => Function::new_typed_with_env(&mut store, env, crate::syscalls::___syscall146),
1191        "___syscall147" => Function::new_typed_with_env(&mut store, env, crate::syscalls::___syscall147),
1192        "___syscall148" => Function::new_typed_with_env(&mut store, env, crate::syscalls::___syscall148),
1193        "___syscall150" => Function::new_typed_with_env(&mut store, env, crate::syscalls::___syscall150),
1194        "___syscall151" => Function::new_typed_with_env(&mut store, env, crate::syscalls::___syscall151),
1195        "___syscall152" => Function::new_typed_with_env(&mut store, env, crate::syscalls::___syscall152),
1196        "___syscall153" => Function::new_typed_with_env(&mut store, env, crate::syscalls::___syscall153),
1197        "___syscall163" => Function::new_typed_with_env(&mut store, env, crate::syscalls::___syscall163),
1198        "___syscall168" => Function::new_typed_with_env(&mut store, env, crate::syscalls::___syscall168),
1199        "___syscall180" => Function::new_typed_with_env(&mut store, env, crate::syscalls::___syscall180),
1200        "___syscall181" => Function::new_typed_with_env(&mut store, env, crate::syscalls::___syscall181),
1201        "___syscall183" => Function::new_typed_with_env(&mut store, env, crate::syscalls::___syscall183),
1202        "___syscall191" => Function::new_typed_with_env(&mut store, env, crate::syscalls::___syscall191),
1203        "___syscall192" => Function::new_typed_with_env(&mut store, env, crate::syscalls::___syscall192),
1204        "___syscall193" => Function::new_typed_with_env(&mut store, env, crate::syscalls::___syscall193),
1205        "___syscall194" => Function::new_typed_with_env(&mut store, env, crate::syscalls::___syscall194),
1206        "___syscall195" => Function::new_typed_with_env(&mut store, env, crate::syscalls::___syscall195),
1207        "___syscall196" => Function::new_typed_with_env(&mut store, env, crate::syscalls::___syscall196),
1208        "___syscall197" => Function::new_typed_with_env(&mut store, env, crate::syscalls::___syscall197),
1209        "___syscall198" => Function::new_typed_with_env(&mut store, env, crate::syscalls::___syscall198),
1210        "___syscall199" => Function::new_typed_with_env(&mut store, env, crate::syscalls::___syscall199),
1211        "___syscall200" => Function::new_typed_with_env(&mut store, env, crate::syscalls::___syscall200),
1212        "___syscall201" => Function::new_typed_with_env(&mut store, env, crate::syscalls::___syscall201),
1213        "___syscall202" => Function::new_typed_with_env(&mut store, env, crate::syscalls::___syscall202),
1214        "___syscall205" => Function::new_typed_with_env(&mut store, env, crate::syscalls::___syscall205),
1215        "___syscall207" => Function::new_typed_with_env(&mut store, env, crate::syscalls::___syscall207),
1216        "___syscall209" => Function::new_typed_with_env(&mut store, env, crate::syscalls::___syscall209),
1217        "___syscall211" => Function::new_typed_with_env(&mut store, env, crate::syscalls::___syscall211),
1218        "___syscall212" => Function::new_typed_with_env(&mut store, env, crate::syscalls::___syscall212),
1219        "___syscall218" => Function::new_typed_with_env(&mut store, env, crate::syscalls::___syscall218),
1220        "___syscall219" => Function::new_typed_with_env(&mut store, env, crate::syscalls::___syscall219),
1221        "___syscall220" => Function::new_typed_with_env(&mut store, env, crate::syscalls::___syscall220),
1222        "___syscall221" => Function::new_typed_with_env(&mut store, env, crate::syscalls::___syscall221),
1223        "___syscall268" => Function::new_typed_with_env(&mut store, env, crate::syscalls::___syscall268),
1224        "___syscall269" => Function::new_typed_with_env(&mut store, env, crate::syscalls::___syscall269),
1225        "___syscall272" => Function::new_typed_with_env(&mut store, env, crate::syscalls::___syscall272),
1226        "___syscall295" => Function::new_typed_with_env(&mut store, env, crate::syscalls::___syscall295),
1227        "___syscall296" => Function::new_typed_with_env(&mut store, env, crate::syscalls::___syscall296),
1228        "___syscall297" => Function::new_typed_with_env(&mut store, env, crate::syscalls::___syscall297),
1229        "___syscall298" => Function::new_typed_with_env(&mut store, env, crate::syscalls::___syscall298),
1230        "___syscall300" => Function::new_typed_with_env(&mut store, env, crate::syscalls::___syscall300),
1231        "___syscall301" => Function::new_typed_with_env(&mut store, env, crate::syscalls::___syscall301),
1232        "___syscall302" => Function::new_typed_with_env(&mut store, env, crate::syscalls::___syscall302),
1233        "___syscall303" => Function::new_typed_with_env(&mut store, env, crate::syscalls::___syscall303),
1234        "___syscall304" => Function::new_typed_with_env(&mut store, env, crate::syscalls::___syscall304),
1235        "___syscall305" => Function::new_typed_with_env(&mut store, env, crate::syscalls::___syscall305),
1236        "___syscall306" => Function::new_typed_with_env(&mut store, env, crate::syscalls::___syscall306),
1237        "___syscall307" => Function::new_typed_with_env(&mut store, env, crate::syscalls::___syscall307),
1238        "___syscall308" => Function::new_typed_with_env(&mut store, env, crate::syscalls::___syscall308),
1239        "___syscall320" => Function::new_typed_with_env(&mut store, env, crate::syscalls::___syscall320),
1240        "___syscall324" => Function::new_typed_with_env(&mut store, env, crate::syscalls::___syscall324),
1241        "___syscall330" => Function::new_typed_with_env(&mut store, env, crate::syscalls::___syscall330),
1242        "___syscall331" => Function::new_typed_with_env(&mut store, env, crate::syscalls::___syscall331),
1243        "___syscall333" => Function::new_typed_with_env(&mut store, env, crate::syscalls::___syscall333),
1244        "___syscall334" => Function::new_typed_with_env(&mut store, env, crate::syscalls::___syscall334),
1245        "___syscall337" => Function::new_typed_with_env(&mut store, env, crate::syscalls::___syscall337),
1246        "___syscall340" => Function::new_typed_with_env(&mut store, env, crate::syscalls::___syscall340),
1247        "___syscall345" => Function::new_typed_with_env(&mut store, env, crate::syscalls::___syscall345),
1248
1249        // Process
1250        "abort" => Function::new_typed_with_env(&mut store, env, crate::process::em_abort),
1251        "_abort" => Function::new_typed_with_env(&mut store, env, crate::process::_abort),
1252        "_prctl" => Function::new_typed_with_env(&mut store, env, crate::process::_prctl),
1253        "abortStackOverflow" => Function::new_typed_with_env(&mut store, env, crate::process::abort_stack_overflow),
1254        "_llvm_trap" => Function::new_typed_with_env(&mut store, env, crate::process::_llvm_trap),
1255        "_fork" => Function::new_typed_with_env(&mut store, env, crate::process::_fork),
1256        "_exit" => Function::new_typed_with_env(&mut store, env, crate::process::_exit),
1257        "_system" => Function::new_typed_with_env(&mut store, env, crate::process::_system),
1258        "_popen" => Function::new_typed_with_env(&mut store, env, crate::process::_popen),
1259        "_endgrent" => Function::new_typed_with_env(&mut store, env, crate::process::_endgrent),
1260        "_execve" => Function::new_typed_with_env(&mut store, env, crate::process::_execve),
1261        "_kill" => Function::new_typed_with_env(&mut store, env, crate::process::_kill),
1262        "_llvm_stackrestore" => Function::new_typed_with_env(&mut store, env, crate::process::_llvm_stackrestore),
1263        "_llvm_stacksave" => Function::new_typed_with_env(&mut store, env, crate::process::_llvm_stacksave),
1264        "_llvm_eh_typeid_for" => Function::new_typed_with_env(&mut store, env, crate::process::_llvm_eh_typeid_for),
1265        "_raise" => Function::new_typed_with_env(&mut store, env, crate::process::_raise),
1266        "_sem_init" => Function::new_typed_with_env(&mut store, env, crate::process::_sem_init),
1267        "_sem_destroy" => Function::new_typed_with_env(&mut store, env, crate::process::_sem_destroy),
1268        "_sem_post" => Function::new_typed_with_env(&mut store, env, crate::process::_sem_post),
1269        "_sem_wait" => Function::new_typed_with_env(&mut store, env, crate::process::_sem_wait),
1270        "_getgrent" => Function::new_typed_with_env(&mut store, env, crate::process::_getgrent),
1271        "_sched_yield" => Function::new_typed_with_env(&mut store, env, crate::process::_sched_yield),
1272        "_setgrent" => Function::new_typed_with_env(&mut store, env, crate::process::_setgrent),
1273        "_setgroups" => Function::new_typed_with_env(&mut store, env, crate::process::_setgroups),
1274        "_setitimer" => Function::new_typed_with_env(&mut store, env, crate::process::_setitimer),
1275        "_usleep" => Function::new_typed_with_env(&mut store, env, crate::process::_usleep),
1276        "_nanosleep" => Function::new_typed_with_env(&mut store, env, crate::process::_nanosleep),
1277        "_utime" => Function::new_typed_with_env(&mut store, env, crate::process::_utime),
1278        "_utimes" => Function::new_typed_with_env(&mut store, env, crate::process::_utimes),
1279        "_wait" => Function::new_typed_with_env(&mut store, env, crate::process::_wait),
1280        "_wait3" => Function::new_typed_with_env(&mut store, env, crate::process::_wait3),
1281        "_wait4" => Function::new_typed_with_env(&mut store, env, crate::process::_wait4),
1282        "_waitid" => Function::new_typed_with_env(&mut store, env, crate::process::_waitid),
1283        "_waitpid" => Function::new_typed_with_env(&mut store, env, crate::process::_waitpid),
1284
1285        // Emscripten
1286        "_emscripten_asm_const_i" => Function::new_typed_with_env(&mut store, env, crate::emscripten_target::asm_const_i),
1287        "_emscripten_exit_with_live_runtime" => Function::new_typed_with_env(&mut store, env, crate::emscripten_target::exit_with_live_runtime),
1288
1289        // Signal
1290        "_sigemptyset" => Function::new_typed_with_env(&mut store, env, crate::signal::_sigemptyset),
1291        "_sigaddset" => Function::new_typed_with_env(&mut store, env, crate::signal::_sigaddset),
1292        "_sigprocmask" => Function::new_typed_with_env(&mut store, env, crate::signal::_sigprocmask),
1293        "_sigaction" => Function::new_typed_with_env(&mut store, env, crate::signal::_sigaction),
1294        "_signal" => Function::new_typed_with_env(&mut store, env, crate::signal::_signal),
1295        "_sigsuspend" => Function::new_typed_with_env(&mut store, env, crate::signal::_sigsuspend),
1296
1297        // Memory
1298        "abortOnCannotGrowMemory" => abort_on_cannot_grow_memory_export,
1299        "_emscripten_memcpy_big" => Function::new_typed_with_env(&mut store, env, crate::memory::_emscripten_memcpy_big),
1300        "_emscripten_get_heap_size" => Function::new_typed_with_env(&mut store, env, crate::memory::_emscripten_get_heap_size),
1301        "_emscripten_resize_heap" => Function::new_typed_with_env(&mut store, env, crate::memory::_emscripten_resize_heap),
1302        "enlargeMemory" => Function::new_typed_with_env(&mut store, env, crate::memory::enlarge_memory),
1303        "segfault" => Function::new_typed_with_env(&mut store, env, crate::memory::segfault),
1304        "alignfault" => Function::new_typed_with_env(&mut store, env, crate::memory::alignfault),
1305        "ftfault" => Function::new_typed_with_env(&mut store, env, crate::memory::ftfault),
1306        "getTotalMemory" => Function::new_typed_with_env(&mut store, env, crate::memory::get_total_memory),
1307        "_sbrk" => Function::new_typed_with_env(&mut store, env, crate::memory::sbrk),
1308        "___map_file" => Function::new_typed_with_env(&mut store, env, crate::memory::___map_file),
1309
1310        // Exception
1311        "___cxa_allocate_exception" => Function::new_typed_with_env(&mut store, env, crate::exception::___cxa_allocate_exception),
1312        "___cxa_current_primary_exception" => Function::new_typed_with_env(&mut store, env, crate::exception::___cxa_current_primary_exception),
1313        "___cxa_decrement_exception_refcount" => Function::new_typed_with_env(&mut store, env, crate::exception::___cxa_decrement_exception_refcount),
1314        "___cxa_increment_exception_refcount" => Function::new_typed_with_env(&mut store, env, crate::exception::___cxa_increment_exception_refcount),
1315        "___cxa_rethrow_primary_exception" => Function::new_typed_with_env(&mut store, env, crate::exception::___cxa_rethrow_primary_exception),
1316        "___cxa_throw" => Function::new_typed_with_env(&mut store, env, crate::exception::___cxa_throw),
1317        "___cxa_begin_catch" => Function::new_typed_with_env(&mut store, env, crate::exception::___cxa_begin_catch),
1318        "___cxa_end_catch" => Function::new_typed_with_env(&mut store, env, crate::exception::___cxa_end_catch),
1319        "___cxa_uncaught_exception" => Function::new_typed_with_env(&mut store, env, crate::exception::___cxa_uncaught_exception),
1320        "___cxa_pure_virtual" => Function::new_typed_with_env(&mut store, env, crate::exception::___cxa_pure_virtual),
1321
1322        // Time
1323        "_gettimeofday" => Function::new_typed_with_env(&mut store, env, crate::time::_gettimeofday),
1324        "_clock_getres" => Function::new_typed_with_env(&mut store, env, crate::time::_clock_getres),
1325        "_clock_gettime" => Function::new_typed_with_env(&mut store, env, crate::time::_clock_gettime),
1326        "_clock_settime" => Function::new_typed_with_env(&mut store, env, crate::time::_clock_settime),
1327        "___clock_gettime" => Function::new_typed_with_env(&mut store, env, crate::time::_clock_gettime),
1328        "_clock" => Function::new_typed_with_env(&mut store, env, crate::time::_clock),
1329        "_difftime" => Function::new_typed_with_env(&mut store, env, crate::time::_difftime),
1330        "_asctime" => Function::new_typed_with_env(&mut store, env, crate::time::_asctime),
1331        "_asctime_r" => Function::new_typed_with_env(&mut store, env, crate::time::_asctime_r),
1332        "_localtime" => Function::new_typed_with_env(&mut store, env, crate::time::_localtime),
1333        "_time" => Function::new_typed_with_env(&mut store, env, crate::time::_time),
1334        "_timegm" => Function::new_typed_with_env(&mut store, env, crate::time::_timegm),
1335        "_strftime" => Function::new_typed_with_env(&mut store, env, crate::time::_strftime),
1336        "_strftime_l" => Function::new_typed_with_env(&mut store, env, crate::time::_strftime_l),
1337        "_localtime_r" => Function::new_typed_with_env(&mut store, env, crate::time::_localtime_r),
1338        "_gmtime_r" => Function::new_typed_with_env(&mut store, env, crate::time::_gmtime_r),
1339        "_ctime" => Function::new_typed_with_env(&mut store, env, crate::time::_ctime),
1340        "_ctime_r" => Function::new_typed_with_env(&mut store, env, crate::time::_ctime_r),
1341        "_mktime" => Function::new_typed_with_env(&mut store, env, crate::time::_mktime),
1342        "_gmtime" => Function::new_typed_with_env(&mut store, env, crate::time::_gmtime),
1343
1344        // Math
1345        "sqrt" => Function::new_typed_with_env(&mut store, env, crate::math::sqrt),
1346        "floor" => Function::new_typed_with_env(&mut store, env, crate::math::floor),
1347        "fabs" => Function::new_typed_with_env(&mut store, env, crate::math::fabs),
1348        "f64-rem" => Function::new_typed_with_env(&mut store, env, crate::math::f64_rem),
1349        "_llvm_copysign_f32" => Function::new_typed_with_env(&mut store, env, crate::math::_llvm_copysign_f32),
1350        "_llvm_copysign_f64" => Function::new_typed_with_env(&mut store, env, crate::math::_llvm_copysign_f64),
1351        "_llvm_log10_f64" => Function::new_typed_with_env(&mut store, env, crate::math::_llvm_log10_f64),
1352        "_llvm_log2_f64" => Function::new_typed_with_env(&mut store, env, crate::math::_llvm_log2_f64),
1353        "_llvm_log10_f32" => Function::new_typed_with_env(&mut store, env, crate::math::_llvm_log10_f32),
1354        "_llvm_log2_f32" => Function::new_typed_with_env(&mut store, env, crate::math::_llvm_log2_f64),
1355        "_llvm_sin_f64" => Function::new_typed_with_env(&mut store, env, crate::math::_llvm_sin_f64),
1356        "_llvm_cos_f64" => Function::new_typed_with_env(&mut store, env, crate::math::_llvm_cos_f64),
1357        "_llvm_exp2_f32" => Function::new_typed_with_env(&mut store, env, crate::math::_llvm_exp2_f32),
1358        "_llvm_exp2_f64" => Function::new_typed_with_env(&mut store, env, crate::math::_llvm_exp2_f64),
1359        "_llvm_trunc_f64" => Function::new_typed_with_env(&mut store, env, crate::math::_llvm_trunc_f64),
1360        "_llvm_fma_f64" => Function::new_typed_with_env(&mut store, env, crate::math::_llvm_fma_f64),
1361        "_emscripten_random" => Function::new_typed_with_env(&mut store, env, crate::math::_emscripten_random),
1362
1363        // Jump
1364        "__setjmp" => Function::new_typed_with_env(&mut store, env, crate::jmp::__setjmp),
1365        "__longjmp" => Function::new_typed_with_env(&mut store, env, crate::jmp::__longjmp),
1366        "_longjmp" => Function::new_typed_with_env(&mut store, env, crate::jmp::_longjmp),
1367        "_emscripten_longjmp" => Function::new_typed_with_env(&mut store, env, crate::jmp::_longjmp),
1368
1369        // Bitwise
1370        "_llvm_bswap_i64" => Function::new_typed_with_env(&mut store, env, crate::bitwise::_llvm_bswap_i64),
1371
1372        // libc
1373        "_execv" => Function::new_typed_with_env(&mut store, env, crate::libc::execv),
1374        "_endpwent" => Function::new_typed_with_env(&mut store, env, crate::libc::endpwent),
1375        "_fexecve" => Function::new_typed_with_env(&mut store, env, crate::libc::fexecve),
1376        "_fpathconf" => Function::new_typed_with_env(&mut store, env, crate::libc::fpathconf),
1377        "_getitimer" => Function::new_typed_with_env(&mut store, env, crate::libc::getitimer),
1378        "_getpwent" => Function::new_typed_with_env(&mut store, env, crate::libc::getpwent),
1379        "_killpg" => Function::new_typed_with_env(&mut store, env, crate::libc::killpg),
1380        "_pathconf" => Function::new_typed_with_env(&mut store, env, crate::libc::pathconf),
1381        "_siginterrupt" => Function::new_typed_with_env(&mut store, env, crate::signal::_siginterrupt),
1382        "_setpwent" => Function::new_typed_with_env(&mut store, env, crate::libc::setpwent),
1383        "_sigismember" => Function::new_typed_with_env(&mut store, env, crate::libc::sigismember),
1384        "_sigpending" => Function::new_typed_with_env(&mut store, env, crate::libc::sigpending),
1385        "___libc_current_sigrtmax" => Function::new_typed_with_env(&mut store, env, crate::libc::current_sigrtmax),
1386        "___libc_current_sigrtmin" => Function::new_typed_with_env(&mut store, env, crate::libc::current_sigrtmin),
1387
1388        // Linking
1389        "_dlclose" => Function::new_typed_with_env(&mut store, env, crate::linking::_dlclose),
1390        "_dlerror" => Function::new_typed_with_env(&mut store, env, crate::linking::_dlerror),
1391        "_dlopen" => Function::new_typed_with_env(&mut store, env, crate::linking::_dlopen),
1392        "_dlsym" => Function::new_typed_with_env(&mut store, env, crate::linking::_dlsym),
1393
1394        // wasm32-unknown-emscripten
1395        "_alarm" => Function::new_typed_with_env(&mut store, env, crate::emscripten_target::_alarm),
1396        "_atexit" => Function::new_typed_with_env(&mut store, env, crate::emscripten_target::_atexit),
1397        "setTempRet0" => Function::new_typed_with_env(&mut store, env, crate::emscripten_target::setTempRet0),
1398        "getTempRet0" => Function::new_typed_with_env(&mut store, env, crate::emscripten_target::getTempRet0),
1399        "invoke_i" => Function::new_typed_with_env(&mut store, env, crate::emscripten_target::invoke_i),
1400        "invoke_ii" => Function::new_typed_with_env(&mut store, env, crate::emscripten_target::invoke_ii),
1401        "invoke_iii" => Function::new_typed_with_env(&mut store, env, crate::emscripten_target::invoke_iii),
1402        "invoke_iiii" => Function::new_typed_with_env(&mut store, env, crate::emscripten_target::invoke_iiii),
1403        "invoke_iifi" => Function::new_typed_with_env(&mut store, env, crate::emscripten_target::invoke_iifi),
1404        "invoke_v" => Function::new_typed_with_env(&mut store, env, crate::emscripten_target::invoke_v),
1405        "invoke_vi" => Function::new_typed_with_env(&mut store, env, crate::emscripten_target::invoke_vi),
1406        "invoke_vj" => Function::new_typed_with_env(&mut store, env, crate::emscripten_target::invoke_vj),
1407        "invoke_vjji" => Function::new_typed_with_env(&mut store, env, crate::emscripten_target::invoke_vjji),
1408        "invoke_vii" => Function::new_typed_with_env(&mut store, env, crate::emscripten_target::invoke_vii),
1409        "invoke_viii" => Function::new_typed_with_env(&mut store, env, crate::emscripten_target::invoke_viii),
1410        "invoke_viiii" => Function::new_typed_with_env(&mut store, env, crate::emscripten_target::invoke_viiii),
1411        "__Unwind_Backtrace" => Function::new_typed_with_env(&mut store, env, crate::emscripten_target::__Unwind_Backtrace),
1412        "__Unwind_FindEnclosingFunction" => Function::new_typed_with_env(&mut store, env, crate::emscripten_target::__Unwind_FindEnclosingFunction),
1413        "__Unwind_GetIPInfo" => Function::new_typed_with_env(&mut store, env, crate::emscripten_target::__Unwind_GetIPInfo),
1414        "___cxa_find_matching_catch_2" => Function::new_typed_with_env(&mut store, env, crate::emscripten_target::___cxa_find_matching_catch_2),
1415        "___cxa_find_matching_catch_3" => Function::new_typed_with_env(&mut store, env, crate::emscripten_target::___cxa_find_matching_catch_3),
1416        "___cxa_free_exception" => Function::new_typed_with_env(&mut store, env, crate::emscripten_target::___cxa_free_exception),
1417        "___resumeException" => Function::new_typed_with_env(&mut store, env, crate::emscripten_target::___resumeException),
1418        "_dladdr" => Function::new_typed_with_env(&mut store, env, crate::emscripten_target::_dladdr),
1419        "_pthread_attr_destroy" => Function::new_typed_with_env(&mut store, env, crate::pthread::_pthread_attr_destroy),
1420        "_pthread_attr_getstack" => Function::new_typed_with_env(&mut store, env, crate::pthread::_pthread_attr_getstack),
1421        "_pthread_attr_init" => Function::new_typed_with_env(&mut store, env, crate::pthread::_pthread_attr_init),
1422        "_pthread_attr_setstacksize" => Function::new_typed_with_env(&mut store, env, crate::pthread::_pthread_attr_setstacksize),
1423        "_pthread_cleanup_pop" => Function::new_typed_with_env(&mut store, env, crate::pthread::_pthread_cleanup_pop),
1424        "_pthread_cleanup_push" => Function::new_typed_with_env(&mut store, env, crate::pthread::_pthread_cleanup_push),
1425        "_pthread_cond_destroy" => Function::new_typed_with_env(&mut store, env, crate::pthread::_pthread_cond_destroy),
1426        "_pthread_cond_init" => Function::new_typed_with_env(&mut store, env, crate::pthread::_pthread_cond_init),
1427        "_pthread_cond_signal" => Function::new_typed_with_env(&mut store, env, crate::pthread::_pthread_cond_signal),
1428        "_pthread_cond_timedwait" => Function::new_typed_with_env(&mut store, env, crate::pthread::_pthread_cond_timedwait),
1429        "_pthread_cond_wait" => Function::new_typed_with_env(&mut store, env, crate::pthread::_pthread_cond_wait),
1430        "_pthread_condattr_destroy" => Function::new_typed_with_env(&mut store, env, crate::pthread::_pthread_condattr_destroy),
1431        "_pthread_condattr_init" => Function::new_typed_with_env(&mut store, env, crate::pthread::_pthread_condattr_init),
1432        "_pthread_condattr_setclock" => Function::new_typed_with_env(&mut store, env, crate::pthread::_pthread_condattr_setclock),
1433        "_pthread_create" => Function::new_typed_with_env(&mut store, env, crate::pthread::_pthread_create),
1434        "_pthread_detach" => Function::new_typed_with_env(&mut store, env, crate::pthread::_pthread_detach),
1435        "_pthread_equal" => Function::new_typed_with_env(&mut store, env, crate::pthread::_pthread_equal),
1436        "_pthread_exit" => Function::new_typed_with_env(&mut store, env, crate::pthread::_pthread_exit),
1437        "_pthread_self" => Function::new_typed_with_env(&mut store, env, crate::pthread::_pthread_self),
1438        "_pthread_getattr_np" => Function::new_typed_with_env(&mut store, env, crate::pthread::_pthread_getattr_np),
1439        "_pthread_getspecific" => Function::new_typed_with_env(&mut store, env, crate::pthread::_pthread_getspecific),
1440        "_pthread_join" => Function::new_typed_with_env(&mut store, env, crate::pthread::_pthread_join),
1441        "_pthread_key_create" => Function::new_typed_with_env(&mut store, env, crate::pthread::_pthread_key_create),
1442        "_pthread_mutex_destroy" => Function::new_typed_with_env(&mut store, env, crate::pthread::_pthread_mutex_destroy),
1443        "_pthread_mutex_init" => Function::new_typed_with_env(&mut store, env, crate::pthread::_pthread_mutex_init),
1444        "_pthread_mutexattr_destroy" => Function::new_typed_with_env(&mut store, env, crate::pthread::_pthread_mutexattr_destroy),
1445        "_pthread_mutexattr_init" => Function::new_typed_with_env(&mut store, env, crate::pthread::_pthread_mutexattr_init),
1446        "_pthread_mutexattr_settype" => Function::new_typed_with_env(&mut store, env, crate::pthread::_pthread_mutexattr_settype),
1447        "_pthread_once" => Function::new_typed_with_env(&mut store, env, crate::pthread::_pthread_once),
1448        "_pthread_rwlock_destroy" => Function::new_typed_with_env(&mut store, env, crate::pthread::_pthread_rwlock_destroy),
1449        "_pthread_rwlock_init" => Function::new_typed_with_env(&mut store, env, crate::pthread::_pthread_rwlock_init),
1450        "_pthread_rwlock_rdlock" => Function::new_typed_with_env(&mut store, env, crate::pthread::_pthread_rwlock_rdlock),
1451        "_pthread_rwlock_unlock" => Function::new_typed_with_env(&mut store, env, crate::pthread::_pthread_rwlock_unlock),
1452        "_pthread_rwlock_wrlock" => Function::new_typed_with_env(&mut store, env, crate::pthread::_pthread_rwlock_wrlock),
1453        "_pthread_setcancelstate" => Function::new_typed_with_env(&mut store, env, crate::pthread::_pthread_setcancelstate),
1454        "_pthread_setspecific" => Function::new_typed_with_env(&mut store, env, crate::pthread::_pthread_setspecific),
1455        "_pthread_sigmask" => Function::new_typed_with_env(&mut store, env, crate::pthread::_pthread_sigmask),
1456        "___gxx_personality_v0" => Function::new_typed_with_env(&mut store, env, crate::emscripten_target::___gxx_personality_v0),
1457        "_gai_strerror" => Function::new_typed_with_env(&mut store, env, crate::env::_gai_strerror),
1458        "_getdtablesize" => Function::new_typed_with_env(&mut store, env, crate::emscripten_target::_getdtablesize),
1459        "_gethostbyaddr" => Function::new_typed_with_env(&mut store, env, crate::emscripten_target::_gethostbyaddr),
1460        "_gethostbyname" => Function::new_typed_with_env(&mut store, env, crate::emscripten_target::_gethostbyname),
1461        "_gethostbyname_r" => Function::new_typed_with_env(&mut store, env, crate::emscripten_target::_gethostbyname_r),
1462        "_getloadavg" => Function::new_typed_with_env(&mut store, env, crate::emscripten_target::_getloadavg),
1463        "_getnameinfo" => Function::new_typed_with_env(&mut store, env, crate::emscripten_target::_getnameinfo),
1464        "invoke_dii" => Function::new_typed_with_env(&mut store, env, crate::emscripten_target::invoke_dii),
1465        "invoke_diiii" => Function::new_typed_with_env(&mut store, env, crate::emscripten_target::invoke_diiii),
1466        "invoke_iiiii" => Function::new_typed_with_env(&mut store, env, crate::emscripten_target::invoke_iiiii),
1467        "invoke_iiiiii" => Function::new_typed_with_env(&mut store, env, crate::emscripten_target::invoke_iiiiii),
1468        "invoke_iiiiiii" => Function::new_typed_with_env(&mut store, env, crate::emscripten_target::invoke_iiiiiii),
1469        "invoke_iiiiiiii" => Function::new_typed_with_env(&mut store, env, crate::emscripten_target::invoke_iiiiiiii),
1470        "invoke_iiiiiiiii" => Function::new_typed_with_env(&mut store, env, crate::emscripten_target::invoke_iiiiiiiii),
1471        "invoke_iiiiiiiiii" => Function::new_typed_with_env(&mut store, env, crate::emscripten_target::invoke_iiiiiiiiii),
1472        "invoke_iiiiiiiiiii" => Function::new_typed_with_env(&mut store, env, crate::emscripten_target::invoke_iiiiiiiiiii),
1473        "invoke_vd" => Function::new_typed_with_env(&mut store, env, crate::emscripten_target::invoke_vd),
1474        "invoke_viiiii" => Function::new_typed_with_env(&mut store, env, crate::emscripten_target::invoke_viiiii),
1475        "invoke_viiiiii" => Function::new_typed_with_env(&mut store, env, crate::emscripten_target::invoke_viiiiii),
1476        "invoke_viiiiiii" => Function::new_typed_with_env(&mut store, env, crate::emscripten_target::invoke_viiiiiii),
1477        "invoke_viiiiiiii" => Function::new_typed_with_env(&mut store, env, crate::emscripten_target::invoke_viiiiiiii),
1478        "invoke_viiiiiiiii" => Function::new_typed_with_env(&mut store, env, crate::emscripten_target::invoke_viiiiiiiii),
1479        "invoke_viiiiiiiii" => Function::new_typed_with_env(&mut store, env, crate::emscripten_target::invoke_viiiiiiiii),
1480        "invoke_viiiiiiiiii" => Function::new_typed_with_env(&mut store, env, crate::emscripten_target::invoke_viiiiiiiiii),
1481        "invoke_iij" => Function::new_typed_with_env(&mut store, env, crate::emscripten_target::invoke_iij),
1482        "invoke_iji" => Function::new_typed_with_env(&mut store, env, crate::emscripten_target::invoke_iji),
1483        "invoke_iiji" => Function::new_typed_with_env(&mut store, env, crate::emscripten_target::invoke_iiji),
1484        "invoke_iiijj" => Function::new_typed_with_env(&mut store, env, crate::emscripten_target::invoke_iiijj),
1485        "invoke_j" => Function::new_typed_with_env(&mut store, env, crate::emscripten_target::invoke_j),
1486        "invoke_ji" => Function::new_typed_with_env(&mut store, env, crate::emscripten_target::invoke_ji),
1487        "invoke_jii" => Function::new_typed_with_env(&mut store, env, crate::emscripten_target::invoke_jii),
1488        "invoke_jij" => Function::new_typed_with_env(&mut store, env, crate::emscripten_target::invoke_jij),
1489        "invoke_jjj" => Function::new_typed_with_env(&mut store, env, crate::emscripten_target::invoke_jjj),
1490        "invoke_viiij" => Function::new_typed_with_env(&mut store, env, crate::emscripten_target::invoke_viiij),
1491        "invoke_viiijiiii" => Function::new_typed_with_env(&mut store, env, crate::emscripten_target::invoke_viiijiiii),
1492        "invoke_viiijiiiiii" => Function::new_typed_with_env(&mut store, env, crate::emscripten_target::invoke_viiijiiiiii),
1493        "invoke_viij" => Function::new_typed_with_env(&mut store, env, crate::emscripten_target::invoke_viij),
1494        "invoke_viiji" => Function::new_typed_with_env(&mut store, env, crate::emscripten_target::invoke_viiji),
1495        "invoke_viijiii" => Function::new_typed_with_env(&mut store, env, crate::emscripten_target::invoke_viijiii),
1496        "invoke_viijj" => Function::new_typed_with_env(&mut store, env, crate::emscripten_target::invoke_viijj),
1497        "invoke_vij" => Function::new_typed_with_env(&mut store, env, crate::emscripten_target::invoke_vij),
1498        "invoke_viji" => Function::new_typed_with_env(&mut store, env, crate::emscripten_target::invoke_viji),
1499        "invoke_vijiii" => Function::new_typed_with_env(&mut store, env, crate::emscripten_target::invoke_vijiii),
1500        "invoke_vijj" => Function::new_typed_with_env(&mut store, env, crate::emscripten_target::invoke_vijj),
1501        "invoke_vidd" => Function::new_typed_with_env(&mut store, env, crate::emscripten_target::invoke_vidd),
1502        "invoke_viid" => Function::new_typed_with_env(&mut store, env, crate::emscripten_target::invoke_viid),
1503        "invoke_viidii" => Function::new_typed_with_env(&mut store, env, crate::emscripten_target::invoke_viidii),
1504        "invoke_viidddddddd" => Function::new_typed_with_env(&mut store, env, crate::emscripten_target::invoke_viidddddddd),
1505
1506        // ucontext
1507        "_getcontext" => Function::new_typed_with_env(&mut store, env, crate::ucontext::_getcontext),
1508        "_makecontext" => Function::new_typed_with_env(&mut store, env, crate::ucontext::_makecontext),
1509        "_setcontext" => Function::new_typed_with_env(&mut store, env, crate::ucontext::_setcontext),
1510        "_swapcontext" => Function::new_typed_with_env(&mut store, env, crate::ucontext::_swapcontext),
1511
1512        // unistd
1513        "_confstr" => Function::new_typed_with_env(&mut store, env, crate::unistd::confstr),
1514    };
1515
1516    // Compatibility with newer versions of Emscripten
1517    let mut to_insert: Vec<(String, _)> = vec![];
1518    for (k, v) in env_ns.iter() {
1519        if let Some(k) = k.strip_prefix('_') {
1520            if !env_ns.contains(k) {
1521                to_insert.push((k.to_string(), v.clone()));
1522            }
1523        }
1524    }
1525
1526    for (k, v) in to_insert {
1527        env_ns.insert(k, v);
1528    }
1529
1530    for null_function_name in globals.null_function_names.iter() {
1531        env_ns.insert(
1532            null_function_name.as_str(),
1533            Function::new_typed_with_env(&mut store, env, nullfunc),
1534        );
1535    }
1536
1537    let import_object: Imports = imports! {
1538        "env" => env_ns,
1539        "global" => {
1540          "NaN" => Global::new(&mut store, Value::F64(f64::NAN)),
1541          "Infinity" => Global::new(&mut store, Value::F64(f64::INFINITY)),
1542        },
1543        "global.Math" => {
1544            "pow" => Function::new_typed_with_env(&mut store, env, crate::math::pow),
1545            "exp" => Function::new_typed_with_env(&mut store, env, crate::math::exp),
1546            "log" => Function::new_typed_with_env(&mut store, env, crate::math::log),
1547        },
1548        "asm2wasm" => {
1549            "f64-rem" => Function::new_typed_with_env(&mut store, env, crate::math::f64_rem),
1550            "f64-to-int" => Function::new_typed_with_env(&mut store, env, crate::math::f64_to_int),
1551        },
1552    };
1553
1554    import_object
1555}
1556
1557pub fn nullfunc(env: FunctionEnvMut<EmEnv>, _x: u32) {
1558    use crate::process::abort_with_message;
1559    debug!("emscripten::nullfunc_i {}", _x);
1560    abort_with_message(
1561        env,
1562        "Invalid function pointer. Perhaps this is an invalid value \
1563    (e.g. caused by calling a virtual method on a NULL pointer)? Or calling a function with an \
1564    incorrect type, which will fail? (it is worth building your source files with -Werror (\
1565    warnings are errors), as warnings can indicate undefined behavior which can cause this)",
1566    );
1567}
1568
1569/// The current version of this crate
1570pub const VERSION: &str = env!("CARGO_PKG_VERSION");