wasmer_vm/
global.rs

1use crate::{store::MaybeInstanceOwned, vmcontext::VMGlobalDefinition};
2use std::{cell::UnsafeCell, ptr::NonNull};
3use wasmer_types::GlobalType;
4
5/// A Global instance
6#[derive(Debug)]
7pub struct VMGlobal {
8    ty: GlobalType,
9    vm_global_definition: MaybeInstanceOwned<VMGlobalDefinition>,
10}
11
12impl VMGlobal {
13    /// Create a new, zero bit-pattern initialized global from a [`GlobalType`].
14    pub fn new(global_type: GlobalType) -> Self {
15        Self {
16            ty: global_type,
17            // TODO: Currently all globals are host-owned, we should inline the
18            // VMGlobalDefinition in VMContext for instance-defined globals.
19            vm_global_definition: MaybeInstanceOwned::Host(Box::new(UnsafeCell::new(
20                VMGlobalDefinition::new(),
21            ))),
22        }
23    }
24
25    /// Get the type of the global.
26    pub fn ty(&self) -> &GlobalType {
27        &self.ty
28    }
29
30    /// Get a pointer to the underlying definition used by the generated code.
31    pub fn vmglobal(&self) -> NonNull<VMGlobalDefinition> {
32        self.vm_global_definition.as_ptr()
33    }
34
35    /// Copies this global
36    pub fn copy_on_write(&self) -> Self {
37        unsafe {
38            Self {
39                ty: self.ty,
40                vm_global_definition: MaybeInstanceOwned::Host(Box::new(UnsafeCell::new(
41                    self.vm_global_definition.as_ptr().as_ref().clone(),
42                ))),
43            }
44        }
45    }
46}