Fix execute after free issue with llvm (and presumably cranelift?)

This commit is contained in:
Lachlan Sneff
2019-03-04 14:41:56 -08:00
parent 039ebdcf75
commit 7ef2c0dece
6 changed files with 88 additions and 48 deletions

View File

@ -1,5 +1,9 @@
use crate::export::Export;
use hashbrown::{hash_map::Entry, HashMap};
use std::{
cell::{Ref, RefCell},
rc::Rc,
};
pub trait LikeNamespace {
fn get_export(&self, name: &str) -> Option<Export>;
@ -37,14 +41,14 @@ impl IsExport for Export {
/// }
/// ```
pub struct ImportObject {
map: HashMap<String, Box<dyn LikeNamespace>>,
map: Rc<RefCell<HashMap<String, Box<dyn LikeNamespace>>>>,
}
impl ImportObject {
/// Create a new `ImportObject`.
pub fn new() -> Self {
Self {
map: HashMap::new(),
map: Rc::new(RefCell::new(HashMap::new())),
}
}
@ -67,7 +71,9 @@ impl ImportObject {
S: Into<String>,
N: LikeNamespace + 'static,
{
match self.map.entry(name.into()) {
let mut map = self.map.borrow_mut();
match map.entry(name.into()) {
Entry::Vacant(empty) => {
empty.insert(Box::new(namespace));
None
@ -76,8 +82,20 @@ impl ImportObject {
}
}
pub fn get_namespace(&self, namespace: &str) -> Option<&(dyn LikeNamespace + 'static)> {
self.map.get(namespace).map(|namespace| &**namespace)
pub fn get_namespace(&self, namespace: &str) -> Option<Ref<dyn LikeNamespace + 'static>> {
let map_ref = self.map.borrow();
if map_ref.contains_key(namespace) {
Some(Ref::map(map_ref, |map| &*map[namespace]))
} else {
None
}
}
pub fn clone_ref(&self) -> Self {
Self {
map: Rc::clone(&self.map),
}
}
}