Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Global Store #128

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions src/core/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ pub enum StoreInstantiationError {
TooManyMemories(usize),
}

#[derive(Debug, PartialEq, Eq, Clone)]
pub enum LinkerError {
UnmetImport,
}

#[derive(Debug, PartialEq, Eq, Clone)]
pub enum Error {
/// The magic number at the very start of the given WASM file is invalid.
Expand Down Expand Up @@ -107,6 +112,7 @@ pub enum Error {
TooManyLocals(usize),
UnsupportedProposal(Proposal),
Overflow,
LinkerError(LinkerError),
}

impl Display for Error {
Expand Down Expand Up @@ -264,6 +270,7 @@ impl Display for Error {
f.write_fmt(format_args!("Unsupported proposal: {:?}", proposal))
}
Error::Overflow => f.write_str("Overflow"),
Error::LinkerError(err) => err.fmt(f)
}
}
}
Expand Down Expand Up @@ -316,6 +323,15 @@ impl Display for StoreInstantiationError {
}
}

impl Display for LinkerError {
fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
use LinkerError::*;
match self {
UnmetImport => f.write_str("Unmet import"),
}
}
}

pub type Result<T> = core::result::Result<T, Error>;

impl From<RuntimeError> for Error {
Expand Down
38 changes: 25 additions & 13 deletions src/execution/execution_info.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use alloc::string::{String, ToString};
use alloc::vec::Vec;

use crate::core::reader::types::export::Export;
use crate::core::reader::types::FuncType;
use crate::core::reader::WasmReader;
use crate::execution::Store;
Expand All @@ -12,18 +13,29 @@ pub struct ExecutionInfo<'r> {
pub name: String,
pub wasm_bytecode: &'r [u8],
pub wasm_reader: WasmReader<'r>,
pub fn_types: Vec<FuncType>,
pub store: Store,
}

impl<'r> ExecutionInfo<'r> {
pub fn new(name: &str, wasm_bytecode: &'r [u8], fn_types: Vec<FuncType>, store: Store) -> Self {
ExecutionInfo {
name: name.to_string(),
wasm_bytecode,
wasm_reader: WasmReader::new(wasm_bytecode),
fn_types,
store,
}
}
pub functions: Vec<usize>,
pub functions_offset: usize,
pub imported_functions_len: usize,

pub memories: Vec<usize>,
pub memories_offset: usize,
pub imported_memories_len: usize,

pub globals: Vec<usize>,
pub globals_offset: usize,
pub imported_globals_len: usize,

pub tables: Vec<usize>,
pub tables_offset: usize,
pub imported_tables_len: usize,

pub data: Vec<usize>,
pub data_offset: usize,

pub elements: Vec<usize>,
pub elements_offset: usize,

pub passive_element_indexes: Vec<usize>,
pub exports: Vec<Export>,
}
8 changes: 6 additions & 2 deletions src/execution/function_ref.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ use alloc::vec::Vec;
use crate::execution::{hooks::HookSet, value::InteropValueList, RuntimeInstance};
use crate::{RuntimeError, ValType, Value};

use super::store::Store;

pub struct FunctionRef {
pub(crate) module_name: String,
pub(crate) function_name: String,
Expand All @@ -22,17 +24,19 @@ impl FunctionRef {
&self,
runtime: &mut RuntimeInstance<H>,
params: Param,
store: &mut Store,
) -> Result<Returns, RuntimeError> {
runtime.invoke(self, params)
runtime.invoke(self, params, store)
}

pub fn invoke_dynamic<H: HookSet>(
&self,
runtime: &mut RuntimeInstance<H>,
params: Vec<Value>,
ret_types: &[ValType],
store: &mut Store,
) -> Result<Vec<Value>, RuntimeError> {
runtime.invoke_dynamic(self, params, ret_types)
runtime.invoke_dynamic(self, params, ret_types, store)
}

// pub fn get_return_types(&self) -> Vec<Value
Expand Down
3 changes: 2 additions & 1 deletion src/execution/interpreter_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ use crate::{
#[cfg(feature = "hooks")]
use crate::execution::hooks::HookSet;

use super::{execution_info::ExecutionInfo, lut::Lut};
use super::{execution_info::ExecutionInfo, lut::Lut, store::Store};

/// Interprets a functions. Parameters and return values are passed on the stack.
pub(super) fn run<H: HookSet>(
Expand All @@ -42,6 +42,7 @@ pub(super) fn run<H: HookSet>(
lut: &Lut,
stack: &mut Stack,
mut hooks: H,
store: &mut Store,
) -> Result<(), RuntimeError> {
let func_inst = modules[*current_module_idx]
.store
Expand Down
Loading
Loading