FuncInstances hold WeakRef
This commit is contained in:
parent
44f61251c1
commit
730ff68599
|
@ -1,4 +1,4 @@
|
||||||
use std::rc::Rc;
|
use std::rc::{Rc, Weak};
|
||||||
use std::fmt;
|
use std::fmt;
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use std::borrow::Cow;
|
use std::borrow::Cow;
|
||||||
|
@ -7,7 +7,7 @@ use {Error, Signature};
|
||||||
use host::Externals;
|
use host::Externals;
|
||||||
use runner::{prepare_function_args, FunctionContext, Interpreter};
|
use runner::{prepare_function_args, FunctionContext, Interpreter};
|
||||||
use value::RuntimeValue;
|
use value::RuntimeValue;
|
||||||
use module::ModuleRef;
|
use module::ModuleInstance;
|
||||||
use common::stack::StackWithLimit;
|
use common::stack::StackWithLimit;
|
||||||
use common::{DEFAULT_FRAME_STACK_LIMIT, DEFAULT_VALUE_STACK_LIMIT};
|
use common::{DEFAULT_FRAME_STACK_LIMIT, DEFAULT_VALUE_STACK_LIMIT};
|
||||||
|
|
||||||
|
@ -36,7 +36,7 @@ impl FuncRef {
|
||||||
pub(crate) enum FuncInstance {
|
pub(crate) enum FuncInstance {
|
||||||
Internal {
|
Internal {
|
||||||
signature: Rc<Signature>,
|
signature: Rc<Signature>,
|
||||||
module: ModuleRef,
|
module: Weak<ModuleInstance>,
|
||||||
body: Rc<FuncBody>,
|
body: Rc<FuncBody>,
|
||||||
},
|
},
|
||||||
Host {
|
Host {
|
||||||
|
@ -69,7 +69,7 @@ impl fmt::Debug for FuncInstance {
|
||||||
|
|
||||||
impl FuncInstance {
|
impl FuncInstance {
|
||||||
pub(crate) fn alloc_internal(
|
pub(crate) fn alloc_internal(
|
||||||
module: ModuleRef,
|
module: Weak<ModuleInstance>,
|
||||||
signature: Rc<Signature>,
|
signature: Rc<Signature>,
|
||||||
body: FuncBody,
|
body: FuncBody,
|
||||||
) -> FuncRef {
|
) -> FuncRef {
|
||||||
|
|
|
@ -15,7 +15,7 @@ use common::{DEFAULT_MEMORY_INDEX, DEFAULT_TABLE_INDEX};
|
||||||
use types::{GlobalDescriptor, TableDescriptor, MemoryDescriptor};
|
use types::{GlobalDescriptor, TableDescriptor, MemoryDescriptor};
|
||||||
|
|
||||||
#[derive(Clone, Debug)]
|
#[derive(Clone, Debug)]
|
||||||
pub struct ModuleRef(Rc<ModuleInstance>);
|
pub struct ModuleRef(pub(crate) Rc<ModuleInstance>);
|
||||||
|
|
||||||
impl ::std::ops::Deref for ModuleRef {
|
impl ::std::ops::Deref for ModuleRef {
|
||||||
type Target = ModuleInstance;
|
type Target = ModuleInstance;
|
||||||
|
@ -253,7 +253,7 @@ impl ModuleInstance {
|
||||||
labels: labels,
|
labels: labels,
|
||||||
};
|
};
|
||||||
let func_instance =
|
let func_instance =
|
||||||
FuncInstance::alloc_internal(instance.clone(), signature, func_body);
|
FuncInstance::alloc_internal(Rc::downgrade(&instance.0), signature, func_body);
|
||||||
instance.push_func(func_instance);
|
instance.push_func(func_instance);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1008,13 +1008,13 @@ impl<'a, E: Externals> Interpreter<'a, E> {
|
||||||
impl FunctionContext {
|
impl FunctionContext {
|
||||||
pub fn new<'store>(function: FuncRef, value_stack_limit: usize, frame_stack_limit: usize, signature: &Signature, args: Vec<RuntimeValue>) -> Self {
|
pub fn new<'store>(function: FuncRef, value_stack_limit: usize, frame_stack_limit: usize, signature: &Signature, args: Vec<RuntimeValue>) -> Self {
|
||||||
let module = match *function.as_instance() {
|
let module = match *function.as_instance() {
|
||||||
FuncInstance::Internal { ref module, .. } => module.clone(),
|
FuncInstance::Internal { ref module, .. } => module.upgrade().expect("module deallocated"),
|
||||||
FuncInstance::Host { .. } => panic!("Host functions can't be called as internally defined functions; Thus FunctionContext can be created only with internally defined functions; qed"),
|
FuncInstance::Host { .. } => panic!("Host functions can't be called as internally defined functions; Thus FunctionContext can be created only with internally defined functions; qed"),
|
||||||
};
|
};
|
||||||
FunctionContext {
|
FunctionContext {
|
||||||
is_initialized: false,
|
is_initialized: false,
|
||||||
function: function,
|
function: function,
|
||||||
module: module,
|
module: ModuleRef(module),
|
||||||
return_type: signature.return_type().map(|vt| BlockType::Value(vt.into_elements())).unwrap_or(BlockType::NoResult),
|
return_type: signature.return_type().map(|vt| BlockType::Value(vt.into_elements())).unwrap_or(BlockType::NoResult),
|
||||||
value_stack: StackWithLimit::with_limit(value_stack_limit),
|
value_stack: StackWithLimit::with_limit(value_stack_limit),
|
||||||
frame_stack: StackWithLimit::with_limit(frame_stack_limit),
|
frame_stack: StackWithLimit::with_limit(frame_stack_limit),
|
||||||
|
@ -1026,7 +1026,7 @@ impl FunctionContext {
|
||||||
pub fn nested(&mut self, function: FuncRef) -> Result<Self, Error> {
|
pub fn nested(&mut self, function: FuncRef) -> Result<Self, Error> {
|
||||||
let (function_locals, module, function_return_type) = {
|
let (function_locals, module, function_return_type) = {
|
||||||
let module = match *function.as_instance() {
|
let module = match *function.as_instance() {
|
||||||
FuncInstance::Internal { ref module, .. } => module.clone(),
|
FuncInstance::Internal { ref module, .. } => module.upgrade().expect("module deallocated"),
|
||||||
FuncInstance::Host { .. } => panic!("Host functions can't be called as internally defined functions; Thus FunctionContext can be created only with internally defined functions; qed"),
|
FuncInstance::Host { .. } => panic!("Host functions can't be called as internally defined functions; Thus FunctionContext can be created only with internally defined functions; qed"),
|
||||||
};
|
};
|
||||||
let function_type = function.signature();
|
let function_type = function.signature();
|
||||||
|
@ -1038,7 +1038,7 @@ impl FunctionContext {
|
||||||
Ok(FunctionContext {
|
Ok(FunctionContext {
|
||||||
is_initialized: false,
|
is_initialized: false,
|
||||||
function: function,
|
function: function,
|
||||||
module: module,
|
module: ModuleRef(module),
|
||||||
return_type: function_return_type,
|
return_type: function_return_type,
|
||||||
value_stack: StackWithLimit::with_limit(self.value_stack.limit() - self.value_stack.len()),
|
value_stack: StackWithLimit::with_limit(self.value_stack.limit() - self.value_stack.len()),
|
||||||
frame_stack: StackWithLimit::with_limit(self.frame_stack.limit() - self.frame_stack.len()),
|
frame_stack: StackWithLimit::with_limit(self.frame_stack.limit() - self.frame_stack.len()),
|
||||||
|
|
Loading…
Reference in New Issue