wasmer_vm/
global.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
use crate::{store::MaybeInstanceOwned, vmcontext::VMGlobalDefinition};
use derivative::Derivative;
use std::{cell::UnsafeCell, ptr::NonNull};
use wasmer_types::GlobalType;

/// A Global instance
#[derive(Derivative)]
#[derivative(Debug)]
pub struct VMGlobal {
    ty: GlobalType,
    #[derivative(Debug = "ignore")]
    vm_global_definition: MaybeInstanceOwned<VMGlobalDefinition>,
}

impl VMGlobal {
    /// Create a new, zero bit-pattern initialized global from a [`GlobalType`].
    pub fn new(global_type: GlobalType) -> Self {
        Self {
            ty: global_type,
            // TODO: Currently all globals are host-owned, we should inline the
            // VMGlobalDefinition in VMContext for instance-defined globals.
            vm_global_definition: MaybeInstanceOwned::Host(Box::new(UnsafeCell::new(
                VMGlobalDefinition::new(),
            ))),
        }
    }

    /// Get the type of the global.
    pub fn ty(&self) -> &GlobalType {
        &self.ty
    }

    /// Get a pointer to the underlying definition used by the generated code.
    pub fn vmglobal(&self) -> NonNull<VMGlobalDefinition> {
        self.vm_global_definition.as_ptr()
    }

    /// Copies this global
    pub fn copy_on_write(&self) -> Self {
        unsafe {
            Self {
                ty: self.ty,
                vm_global_definition: MaybeInstanceOwned::Host(Box::new(UnsafeCell::new(
                    self.vm_global_definition.as_ptr().as_ref().clone(),
                ))),
            }
        }
    }
}