Use embedding api in python extension (#569)

Now embedding API is used in the Python extension, this allows us to remove ModuleData::invoke() from wasmtime-interface-types
This commit is contained in:
Yury Delendik
2019-11-14 10:40:04 -06:00
committed by GitHub
parent a495418210
commit 36cb806c54
10 changed files with 252 additions and 639 deletions

View File

@@ -2,8 +2,6 @@
extern crate alloc;
use alloc::rc::Rc;
use core::cell::RefCell;
use core::ptr;
use pyo3::class::PyBufferProtocol;
use pyo3::exceptions::BufferError;
@@ -11,60 +9,18 @@ use pyo3::ffi;
use pyo3::prelude::*;
use std::ffi::CStr;
use std::os::raw::{c_int, c_void};
use wasmtime_environ::MemoryPlan;
use wasmtime_jit::{Context, InstanceHandle};
use wasmtime_runtime::{Export, VMMemoryDefinition, VMMemoryImport};
use wasmtime_api as api;
#[pyclass]
pub struct Memory {
pub context: Rc<RefCell<Context>>,
pub instance: InstanceHandle,
pub export_name: String,
}
impl Memory {
fn descriptor(&self) -> *mut VMMemoryDefinition {
let mut instance = self.instance.clone();
if let Some(Export::Memory { definition, .. }) = instance.lookup(&self.export_name) {
definition
} else {
panic!("memory is expected");
}
}
}
impl Memory {
pub fn get_plan(&self) -> MemoryPlan {
let mut instance = self.instance.clone();
if let Some(Export::Memory { memory, .. }) = instance.lookup(&self.export_name) {
memory
} else {
panic!()
}
}
pub fn into_import(&self) -> VMMemoryImport {
let mut instance = self.instance.clone();
if let Some(Export::Memory {
definition, vmctx, ..
}) = instance.lookup(&self.export_name)
{
VMMemoryImport {
from: definition,
vmctx,
}
} else {
panic!()
}
}
pub memory: api::HostRef<api::Memory>,
}
#[pymethods]
impl Memory {
#[getter(current)]
pub fn current(&self) -> u32 {
let current_length = unsafe { (*self.descriptor()).current_length };
(current_length >> 16) as u32
self.memory.borrow().size()
}
pub fn grow(&self, _number: u32) -> u32 {
@@ -94,12 +50,10 @@ impl PyBufferProtocol for Memory {
1
};
let VMMemoryDefinition {
base,
current_length,
} = unsafe { *self.descriptor() };
unsafe {
let base = self.memory.borrow().data_ptr();
let current_length = self.memory.borrow().data_size();
(*view).buf = base as *mut c_void;
(*view).len = current_length as isize;
(*view).readonly = readonly;