mirror of
https://github.com/fluencelabs/wasmer
synced 2025-06-30 17:11:33 +00:00
new way to access memory from the vmctx
This commit is contained in:
@ -51,7 +51,7 @@ impl Instance {
|
|||||||
|
|
||||||
// Initialize the vm::Ctx in-place after the backing
|
// Initialize the vm::Ctx in-place after the backing
|
||||||
// has been boxed.
|
// has been boxed.
|
||||||
*inner.vmctx = unsafe { vm::Ctx::new(&mut inner.backing, &mut inner.import_backing) };
|
*inner.vmctx = unsafe { vm::Ctx::new(&mut inner.backing, &mut inner.import_backing, &module) };
|
||||||
|
|
||||||
let mut instance = Instance {
|
let mut instance = Instance {
|
||||||
module,
|
module,
|
||||||
|
@ -1,8 +1,11 @@
|
|||||||
pub use crate::{
|
pub use crate::{
|
||||||
backing::{ImportBacking, LocalBacking},
|
backing::{ImportBacking, LocalBacking},
|
||||||
types::LocalMemoryIndex,
|
module::ModuleInner,
|
||||||
|
types::{
|
||||||
|
MemoryIndex, LocalMemoryIndex, LocalOrImport,
|
||||||
|
},
|
||||||
};
|
};
|
||||||
use std::{ffi::c_void, mem, ptr};
|
use std::{ffi::c_void, mem, ptr, slice};
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
#[repr(C)]
|
#[repr(C)]
|
||||||
@ -28,10 +31,10 @@ pub struct Ctx {
|
|||||||
/// A pointer to an array of imported functions, indexed by `FuncIndex`.
|
/// A pointer to an array of imported functions, indexed by `FuncIndex`.
|
||||||
pub(crate) imported_funcs: *mut ImportedFunc,
|
pub(crate) imported_funcs: *mut ImportedFunc,
|
||||||
|
|
||||||
/// The local backing of the parent instance.
|
pub(crate) local_backing: *mut LocalBacking,
|
||||||
pub local_backing: *mut LocalBacking,
|
pub(crate) import_backing: *mut ImportBacking,
|
||||||
/// The import backing of the parent instance.
|
module: *const ModuleInner,
|
||||||
pub import_backing: *mut ImportBacking,
|
|
||||||
|
|
||||||
pub data: *mut c_void,
|
pub data: *mut c_void,
|
||||||
pub data_finalizer: Option<extern "C" fn(data: *mut c_void)>,
|
pub data_finalizer: Option<extern "C" fn(data: *mut c_void)>,
|
||||||
@ -41,6 +44,7 @@ impl Ctx {
|
|||||||
pub unsafe fn new(
|
pub unsafe fn new(
|
||||||
local_backing: &mut LocalBacking,
|
local_backing: &mut LocalBacking,
|
||||||
import_backing: &mut ImportBacking,
|
import_backing: &mut ImportBacking,
|
||||||
|
module: &ModuleInner,
|
||||||
) -> Self {
|
) -> Self {
|
||||||
Self {
|
Self {
|
||||||
memories: local_backing.vm_memories.as_mut_ptr(),
|
memories: local_backing.vm_memories.as_mut_ptr(),
|
||||||
@ -54,6 +58,7 @@ impl Ctx {
|
|||||||
|
|
||||||
local_backing,
|
local_backing,
|
||||||
import_backing,
|
import_backing,
|
||||||
|
module,
|
||||||
|
|
||||||
data: ptr::null_mut(),
|
data: ptr::null_mut(),
|
||||||
data_finalizer: None,
|
data_finalizer: None,
|
||||||
@ -63,6 +68,7 @@ impl Ctx {
|
|||||||
pub unsafe fn new_with_data(
|
pub unsafe fn new_with_data(
|
||||||
local_backing: &mut LocalBacking,
|
local_backing: &mut LocalBacking,
|
||||||
import_backing: &mut ImportBacking,
|
import_backing: &mut ImportBacking,
|
||||||
|
module: &ModuleInner,
|
||||||
data: *mut c_void,
|
data: *mut c_void,
|
||||||
data_finalizer: extern "C" fn(*mut c_void),
|
data_finalizer: extern "C" fn(*mut c_void),
|
||||||
) -> Self {
|
) -> Self {
|
||||||
@ -78,12 +84,34 @@ impl Ctx {
|
|||||||
|
|
||||||
local_backing,
|
local_backing,
|
||||||
import_backing,
|
import_backing,
|
||||||
|
module,
|
||||||
|
|
||||||
data,
|
data,
|
||||||
data_finalizer: Some(data_finalizer),
|
data_finalizer: Some(data_finalizer),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn memory<'a>(&'a mut self, mem_index: MemoryIndex) -> &'a mut [u8] {
|
||||||
|
let module = unsafe { &*self.module };
|
||||||
|
match mem_index.local_or_import(module) {
|
||||||
|
LocalOrImport::Local(local_mem_index) => {
|
||||||
|
let local_backing = unsafe { &mut *self.local_backing };
|
||||||
|
&mut local_backing.memories[local_mem_index][..]
|
||||||
|
},
|
||||||
|
LocalOrImport::Import(import_mem_index) => {
|
||||||
|
let import_backing = unsafe { &mut *self.import_backing };
|
||||||
|
let vm_memory_import = import_backing.memories[import_mem_index].clone();
|
||||||
|
unsafe {
|
||||||
|
let memory = &*vm_memory_import.memory;
|
||||||
|
|
||||||
|
slice::from_raw_parts_mut(memory.base, memory.size)
|
||||||
|
}
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Ctx {
|
||||||
#[allow(clippy::erasing_op)] // TODO
|
#[allow(clippy::erasing_op)] // TODO
|
||||||
pub fn offset_memories() -> u8 {
|
pub fn offset_memories() -> u8 {
|
||||||
0 * (mem::size_of::<usize>() as u8)
|
0 * (mem::size_of::<usize>() as u8)
|
||||||
|
Reference in New Issue
Block a user