Skip to content

Commit 1e47012

Browse files
cemonemwucke13
authored andcommitted
refactor: move registry to its own source file
Signed-off-by: Cem Onem <cem.oenem@dlr.de>
1 parent 4af7096 commit 1e47012

File tree

3 files changed

+65
-55
lines changed

3 files changed

+65
-55
lines changed

src/execution/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ pub mod hooks;
1919
mod interpreter_loop;
2020
pub(crate) mod linear_memory;
2121
pub(crate) mod locals;
22+
pub mod registry;
2223
pub mod store;
2324
pub mod value;
2425
pub mod value_stack;

src/execution/registry.rs

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
use crate::{Error, ModuleInst};
2+
3+
use alloc::borrow::Cow;
4+
use alloc::collections::BTreeMap;
5+
6+
use crate::ExternVal;
7+
8+
#[derive(Debug, PartialEq, PartialOrd, Eq, Ord)]
9+
struct ImportKey {
10+
module_name: Cow<'static, str>,
11+
name: Cow<'static, str>,
12+
}
13+
#[derive(Default, Debug)]
14+
pub struct Registry(BTreeMap<ImportKey, ExternVal>);
15+
16+
impl Registry {
17+
pub fn register(
18+
&mut self,
19+
module_name: Cow<'static, str>,
20+
name: Cow<'static, str>,
21+
extern_val: ExternVal,
22+
) -> Result<(), Error> {
23+
if self
24+
.0
25+
.insert(ImportKey { module_name, name }, extern_val)
26+
.is_some()
27+
{
28+
return Err(Error::InvalidImportType);
29+
}
30+
31+
Ok(())
32+
}
33+
34+
pub fn lookup(
35+
&self,
36+
module_name: Cow<'static, str>,
37+
name: Cow<'static, str>,
38+
) -> Result<&ExternVal, Error> {
39+
// Note: We cannot do a `&str` lookup on a [`String`] map key.
40+
// Thus we have to use `Cow<'static, str>` as a key
41+
// (at least this prevents allocations with static names).
42+
self.0
43+
.get(&ImportKey { module_name, name })
44+
.ok_or(Error::UnknownImport)
45+
}
46+
47+
pub fn register_module(
48+
&mut self,
49+
module_name: Cow<'static, str>,
50+
module_inst: &ModuleInst,
51+
) -> Result<(), Error> {
52+
for (entity_name, extern_val) in &module_inst.exports {
53+
// FIXME this clones module_name. Maybe prevent by using `Cow<'static, Arc<str>>`.
54+
self.register(
55+
module_name.clone(),
56+
Cow::Owned(entity_name.clone()),
57+
*extern_val,
58+
)?;
59+
}
60+
Ok(())
61+
}
62+
}

src/execution/store.rs

Lines changed: 2 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,10 @@ use crate::core::sidetable::Sidetable;
1414
use crate::execution::interpreter_loop::{memory_init, table_init};
1515
use crate::execution::value::{Ref, Value};
1616
use crate::execution::{run_const_span, Stack};
17+
use crate::registry::Registry;
1718
use crate::value::FuncAddr;
1819
use crate::{Error, Limits, RefType, RuntimeError, ValidationInfo};
19-
use alloc::borrow::{Cow, ToOwned};
20+
use alloc::borrow::ToOwned;
2021
use alloc::collections::btree_map::BTreeMap;
2122
use alloc::string::String;
2223
use alloc::vec;
@@ -1027,57 +1028,3 @@ pub struct ModuleInst<'b> {
10271028
// sidetable is not in the spec, but required for control flow
10281029
pub sidetable: Sidetable,
10291030
}
1030-
1031-
#[derive(Debug, PartialEq, PartialOrd, Eq, Ord)]
1032-
struct ImportKey {
1033-
module_name: Cow<'static, str>,
1034-
name: Cow<'static, str>,
1035-
}
1036-
#[derive(Default, Debug)]
1037-
pub struct Registry(BTreeMap<ImportKey, ExternVal>);
1038-
1039-
impl Registry {
1040-
pub fn register(
1041-
&mut self,
1042-
module_name: Cow<'static, str>,
1043-
name: Cow<'static, str>,
1044-
extern_val: ExternVal,
1045-
) -> Result<(), Error> {
1046-
if self
1047-
.0
1048-
.insert(ImportKey { module_name, name }, extern_val)
1049-
.is_some()
1050-
{
1051-
return Err(Error::InvalidImportType);
1052-
}
1053-
1054-
Ok(())
1055-
}
1056-
1057-
pub fn lookup(
1058-
&self,
1059-
module_name: Cow<'static, str>,
1060-
name: Cow<'static, str>,
1061-
) -> Result<&ExternVal, Error> {
1062-
// Note: We cannot do a &str lookup on a [`String`] map key. Thus we have to use `Cow<'static, str> as a key (at least this prevents allocations with static names).
1063-
self.0
1064-
.get(&ImportKey { module_name, name })
1065-
.ok_or(Error::UnknownImport)
1066-
}
1067-
1068-
pub fn register_module(
1069-
&mut self,
1070-
module_name: Cow<'static, str>,
1071-
module_inst: &ModuleInst,
1072-
) -> Result<(), Error> {
1073-
for (entity_name, extern_val) in &module_inst.exports {
1074-
// FIXME this clones module_name. Maybe prevent by using `Cow<'static, Arc<str>>`.
1075-
self.register(
1076-
module_name.clone(),
1077-
Cow::Owned(entity_name.clone()),
1078-
*extern_val,
1079-
)?;
1080-
}
1081-
Ok(())
1082-
}
1083-
}

0 commit comments

Comments
 (0)