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
use crate::store::StoreOpaque;
use crate::{GlobalType, Mutability, Val};
use std::ptr;
use wasmtime_runtime::{StoreBox, VMGlobalDefinition};

#[repr(C)]
pub struct VMHostGlobalContext {
    ty: GlobalType,
    global: VMGlobalDefinition,
}

impl Drop for VMHostGlobalContext {
    fn drop(&mut self) {
        match self.ty.content() {
            crate::ValType::I32
            | crate::ValType::I64
            | crate::ValType::F32
            | crate::ValType::F64
            | crate::ValType::V128
            | crate::ValType::FuncRef => {
                // Nothing to drop.
            }
            crate::ValType::ExternRef => unsafe {
                ptr::drop_in_place(self.global.as_externref_mut())
            },
        }
    }
}

pub fn generate_global_export(
    store: &mut StoreOpaque,
    ty: GlobalType,
    val: Val,
) -> wasmtime_runtime::ExportGlobal {
    let global = wasmtime_environ::Global {
        wasm_ty: ty.content().to_wasm_type(),
        mutability: match ty.mutability() {
            Mutability::Const => false,
            Mutability::Var => true,
        },
    };
    let ctx = StoreBox::new(VMHostGlobalContext {
        ty,
        global: VMGlobalDefinition::new(),
    });

    let definition = unsafe {
        let global = &mut (*ctx.get()).global;
        match val {
            Val::I32(x) => *global.as_i32_mut() = x,
            Val::I64(x) => *global.as_i64_mut() = x,
            Val::F32(x) => *global.as_f32_bits_mut() = x,
            Val::F64(x) => *global.as_f64_bits_mut() = x,
            Val::V128(x) => *global.as_u128_mut() = x,
            Val::FuncRef(f) => {
                *global.as_func_ref_mut() = f.map_or(ptr::null_mut(), |f| {
                    f.caller_checked_func_ref(store).as_ptr()
                })
            }
            Val::ExternRef(x) => *global.as_externref_mut() = x.map(|x| x.inner),
        }
        global
    };

    store.host_globals().push(ctx);
    wasmtime_runtime::ExportGlobal { definition, global }
}