-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c8cdf29
commit 3f7b340
Showing
8 changed files
with
253 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
use crate::{core::reader::types::export::ExportDesc, execution::execution_info::ExecutionInfo}; | ||
use alloc::{collections::btree_map::BTreeMap, string::String, vec::Vec}; | ||
|
||
pub struct LUT { | ||
/// function_lut[local_module_idx][function_local_idx] = (foreign_module_idx, function_foreign_idx) | ||
/// | ||
/// - Module A imports a function "foo". Inside module A, the function has the index "function_local_idx". Module A | ||
/// is assigned the index "local_module_idx". | ||
/// - Module B exports a function "foo". Inside module B, the function has the index "function_foreign_idx". Module | ||
/// B is assigned the index "foreign_module_idx". | ||
function_lut: Vec<Vec<(usize, usize)>>, | ||
} | ||
|
||
impl LUT { | ||
pub fn new(modules: &[ExecutionInfo], module_map: &BTreeMap<String, usize>) -> Option<Self> { | ||
let mut function_lut = Vec::new(); | ||
for module in modules { | ||
let module_lut = module | ||
.store | ||
.funcs | ||
.iter() | ||
.filter_map(|f| f.try_into_imported()) | ||
.map(|import| { | ||
Self::manual_lookup( | ||
modules, | ||
module_map, | ||
&import.module_name, | ||
&import.function_name, | ||
) | ||
}) | ||
.collect::<Option<Vec<_>>>()?; | ||
|
||
// TODO: what do we want to do if there is a missing import/export pair? Currently we fail the entire | ||
// operation. Should it be a RuntimeError if said missing pair is called? | ||
|
||
function_lut.push(module_lut); | ||
} | ||
|
||
Some(Self { function_lut }) | ||
} | ||
|
||
pub fn lookup(&self, module_idx: usize, function_idx: usize) -> Option<(usize, usize)> { | ||
self.function_lut | ||
.get(module_idx)? | ||
.get(function_idx) | ||
.copied() | ||
} | ||
|
||
pub fn manual_lookup( | ||
modules: &[ExecutionInfo], | ||
module_map: &BTreeMap<String, usize>, | ||
module_name: &str, | ||
function_name: &str, | ||
) -> Option<(usize, usize)> { | ||
let module_idx = module_map.get(module_name)?; | ||
let module = &modules[*module_idx]; | ||
|
||
module | ||
.store | ||
.exports | ||
.iter() | ||
.filter_map(|export| { | ||
if export.name == function_name { | ||
Some(&export.desc) | ||
} else { | ||
None | ||
} | ||
}) | ||
.find_map(|desc| { | ||
if let ExportDesc::FuncIdx(func_idx) = desc { | ||
Some((*module_idx, *func_idx)) | ||
} else { | ||
None | ||
} | ||
}) | ||
} | ||
} |
Oops, something went wrong.