FuncInstances hold WeakRef

This commit is contained in:
Sergey Pepyakin 2018-01-22 19:57:00 +03:00
parent 44f61251c1
commit 730ff68599
3 changed files with 10 additions and 10 deletions

View File

@ -1,4 +1,4 @@
use std::rc::Rc;
use std::rc::{Rc, Weak};
use std::fmt;
use std::collections::HashMap;
use std::borrow::Cow;
@ -7,7 +7,7 @@ use {Error, Signature};
use host::Externals;
use runner::{prepare_function_args, FunctionContext, Interpreter};
use value::RuntimeValue;
use module::ModuleRef;
use module::ModuleInstance;
use common::stack::StackWithLimit;
use common::{DEFAULT_FRAME_STACK_LIMIT, DEFAULT_VALUE_STACK_LIMIT};
@ -36,7 +36,7 @@ impl FuncRef {
pub(crate) enum FuncInstance {
Internal {
signature: Rc<Signature>,
module: ModuleRef,
module: Weak<ModuleInstance>,
body: Rc<FuncBody>,
},
Host {
@ -69,7 +69,7 @@ impl fmt::Debug for FuncInstance {
impl FuncInstance {
pub(crate) fn alloc_internal(
module: ModuleRef,
module: Weak<ModuleInstance>,
signature: Rc<Signature>,
body: FuncBody,
) -> FuncRef {

View File

@ -15,7 +15,7 @@ use common::{DEFAULT_MEMORY_INDEX, DEFAULT_TABLE_INDEX};
use types::{GlobalDescriptor, TableDescriptor, MemoryDescriptor};
#[derive(Clone, Debug)]
pub struct ModuleRef(Rc<ModuleInstance>);
pub struct ModuleRef(pub(crate) Rc<ModuleInstance>);
impl ::std::ops::Deref for ModuleRef {
type Target = ModuleInstance;
@ -253,7 +253,7 @@ impl ModuleInstance {
labels: labels,
};
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);
}
}

View File

@ -1008,13 +1008,13 @@ impl<'a, E: Externals> Interpreter<'a, E> {
impl FunctionContext {
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() {
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"),
};
FunctionContext {
is_initialized: false,
function: function,
module: module,
module: ModuleRef(module),
return_type: signature.return_type().map(|vt| BlockType::Value(vt.into_elements())).unwrap_or(BlockType::NoResult),
value_stack: StackWithLimit::with_limit(value_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> {
let (function_locals, module, function_return_type) = {
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"),
};
let function_type = function.signature();
@ -1038,7 +1038,7 @@ impl FunctionContext {
Ok(FunctionContext {
is_initialized: false,
function: function,
module: module,
module: ModuleRef(module),
return_type: function_return_type,
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()),