Skip to content
Merged
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
5 changes: 3 additions & 2 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
// Use 'forwardPorts' to make a list of ports inside the container available locally.
// "forwardPorts": [],
// Use 'postCreateCommand' to run commands after the container is created.
"postCreateCommand": "rustc --version",
//"postCreateCommand": "rustc --version",
"postCreateCommand": "bash .devcontainer/finalize.sh",
// Configure tool-specific properties.
"customizations": {
"settings": {
Expand All @@ -37,4 +38,4 @@
},
// Uncomment to connect as root instead. More info: https://aka.ms/dev-containers-non-root.
"remoteUser": "vscode"
}
}
17 changes: 17 additions & 0 deletions .devcontainer/finalize.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
printf "Running 'postCreateCommand' Script\n"

# Install Rust Targets
printf "Installing Rust Targets\n"
rustup update stable --no-self-update
rustup default stable
rustup target add wasm32-unknown-unknown
rustup target add wasm32-wasi
rustup component add clippy

cargo install cargo-readme

# Install Python stuff
printf "Installing Python Dependencies"

# Install NPM dependencies
printf "Installing NPM Dependencies"
34 changes: 34 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,11 @@
//! Ok(())
//! }
//! ```
//!

use std::error;
use std::fmt;
use std::io;

pub mod function;
mod helper;
Expand All @@ -165,3 +170,32 @@ pub enum RuntimeError {
/// usually returns by `find_export_func()`
FunctionNotFound,
}

impl fmt::Display for RuntimeError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
RuntimeError::NotImplemented => write!(f, "Not implemented"),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about "Unknown error"

RuntimeError::InitializationFailure => write!(f, "Runtime initialization failure"),
RuntimeError::WasmFileFSError(e) => write!(f, "Wasm file operation error: {}", e),
RuntimeError::CompilationError(e) => write!(f, "Wasm compilation error: {}", e),
RuntimeError::InstantiationFailure(e) => write!(f, "Wasm instantiation failure: {}", e),
RuntimeError::ExecutionError(e) => write!(f, "Wasm execution error: {}", e),
RuntimeError::FunctionNotFound => write!(f, "Function not found"),
}
}
}

impl error::Error for RuntimeError {
fn source(&self) -> Option<&(dyn error::Error + 'static)> {
match self {
RuntimeError::WasmFileFSError(e) => Some(e),
_ => None,
}
}
}

impl From<io::Error> for RuntimeError {
fn from(e: io::Error) -> Self {
RuntimeError::WasmFileFSError(e)
}
}
12 changes: 4 additions & 8 deletions src/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,12 @@ impl Module {
/// If the file does not exist or the file cannot be read, an `RuntimeError::WasmFileFSError` will be returned.
/// If the wasm file is not a valid wasm file, an `RuntimeError::CompilationError` will be returned.
pub fn from_file(wasm_file: &Path) -> Result<Self, RuntimeError> {
let mut wasm_file = match File::open(wasm_file) {
Ok(f) => f,
Err(e) => return Err(RuntimeError::WasmFileFSError(e)),
};
let mut wasm_file = File::open(wasm_file)?;

let mut binary: Vec<u8> = Vec::new();
match wasm_file.read_to_end(&mut binary) {
Ok(_) => Self::from_buf(&binary),
Err(e) => Err(RuntimeError::WasmFileFSError(e)),
}
wasm_file.read_to_end(&mut binary)?;

Self::from_buf(&binary)
}

/// compile a module int the given buffer
Expand Down