Skip to content

Commit

Permalink
refactor: move import.rs to tests/spce.rs
Browse files Browse the repository at this point in the history
  • Loading branch information
skanehira committed Nov 26, 2023
1 parent fe8abb3 commit 43ed2cd
Show file tree
Hide file tree
Showing 3 changed files with 171 additions and 170 deletions.
167 changes: 0 additions & 167 deletions src/execution/import.rs

This file was deleted.

2 changes: 0 additions & 2 deletions src/execution/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
pub mod error;
pub(crate) mod float;
pub mod import;
pub mod importer;
pub(crate) mod indices;
pub(crate) mod integer;
Expand All @@ -11,7 +10,6 @@ pub mod runtime;
pub mod store;
pub mod value;

pub use import::*;
pub use importer::*;
pub use runtime::*;
pub use store::*;
Expand Down
172 changes: 171 additions & 1 deletion tests/spec.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,178 @@
#[cfg(test)]
mod importer {
use anyhow::{bail, Context as _, Result};
use chibiwasm::{
module::{ExternalFuncInst, FuncInst, GlobalInst, InternalTableInst, InternalMemoryInst},
ExternalVal, Importer, Runtime, Store, Value,
};
use std::{cell::RefCell, collections::HashMap, rc::Rc};

#[derive(Default, Clone)]
pub struct Imports(pub HashMap<String, Import>);

#[derive(Clone)]
pub struct Import((String, Rc<RefCell<Store>>));

impl Import {
pub fn new(name: String, store: Rc<RefCell<Store>>) -> Self {
Self((name, store))
}
}

impl Importer for Import {
fn name(&self) -> &str {
let (name, _) = &self.0;
name.as_str()
}

fn get(&self, name: &str) -> Result<Option<Rc<RefCell<Store>>>> {
if self.name() != name {
return Ok(None);
}
let (_, store) = &self.0;
Ok(Some(Rc::clone(store)))
}

fn invoke(
&self,
store: Rc<RefCell<Store>>,
func: ExternalFuncInst,
args: Vec<Value>,
) -> Result<Option<Value>> {
let mut runtime = Runtime::instantiate(Rc::clone(&store))?;
runtime.call(func.field, args)
}

fn resolve_table(
&self,
name: &str,
field: &str,
) -> Result<Option<Rc<RefCell<InternalTableInst>>>> {
let store = self.get(name)?;
match store {
Some(store) => {
let store = store.borrow();

let export_inst = store
.module
.exports
.get(field)
.context(format!("not found exported table '{field}' from {name}"))?;

let external_val = &export_inst.desc;
let ExternalVal::Table(idx) = external_val else {
bail!("invalid export desc: {:?}", external_val);
};

let table = store
.tables
.get(*idx as usize)
.with_context(|| format!("not found table {idx} in module: {name}"))?;

Ok(Some(Rc::clone(table)))
}
None => {
bail!("cannot resolve table. not found module: {name} in imports",);
}
}
}

fn resolve_global(&self, name: &str, field: &str) -> Result<Option<GlobalInst>> {
let store = self.get(name)?;
match store {
Some(store) => {
let store = store.borrow();
let export_inst = store
.module
.exports
.get(field)
.context(format!("not found exported global '{field}' from {name}"))?;
let external_val = &export_inst.desc;

let ExternalVal::Global(idx) = external_val else {
bail!("invalid export desc: {:?}", external_val);
};
let global = store
.globals
.get(*idx as usize)
.with_context(|| format!("not found global index '{idx}' from {name}"))?;

Ok(Some(Rc::clone(global)))
}
None => {
bail!("cannot resolve global. not found module: {name} in imports",);
}
}
}

fn resolve_func(&self, name: &str, field: &str) -> Result<Option<FuncInst>> {
let store = self.get(name)?;
match store {
Some(store) => {
let store = store.borrow();

let export_inst =
store.module.exports.get(field).context(format!(
"not found exported function '{field}' from {name}"
))?;
let external_val = &export_inst.desc;

let ExternalVal::Func(idx) = external_val else {
bail!("invalid export desc: {:?}", external_val);
};
let func = store
.funcs
.get(*idx as usize)
.with_context(|| format!("not found function by {name}"))?;

Ok(Some(func.clone()))
}
None => {
bail!("cannot resolve function. not found module: {name} in imports",);
}
}
}

fn resolve_memory(
&self,
name: &str,
field: &str,
) -> Result<Option<Rc<RefCell<InternalMemoryInst>>>> {
let store = self.get(name)?;
match store {
Some(store) => {
let store = store.borrow();

let export_inst = store
.module
.exports
.get(field)
.context(format!("not found exported memory '{field}' from {name}"))?;
let external_val = &export_inst.desc;

let ExternalVal::Memory(idx) = external_val else {
bail!("invalid export desc: {:?}", external_val);
};
let memory = store
.memory
.get(*idx as usize)
.with_context(|| format!("not found memory from {name}"))?;

Ok(Some(Rc::clone(memory)))
}
None => {
bail!("cannot resolve memory. not found module: {name} in imports",);
}
}
}
}
}

#[cfg(test)]
mod tests {
use super::importer::Import;
use anyhow::Result;
use chibiwasm::execution::{Exports, Importer, Runtime, Store, Value};
use chibiwasm::Import;
use log::debug;
use paste::paste;
use std::cell::RefCell;
Expand Down

0 comments on commit 43ed2cd

Please sign in to comment.