linera_wasmer_vm/
global.rs

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