Skip to content

Commit

Permalink
Port JS API to new Context API
Browse files Browse the repository at this point in the history
  • Loading branch information
epilys committed Jun 22, 2022
1 parent 7c6e52e commit 0943422
Show file tree
Hide file tree
Showing 32 changed files with 2,727 additions and 1,496 deletions.
447 changes: 447 additions & 0 deletions lib/api/src/js/context.rs

Large diffs are not rendered by default.

219 changes: 0 additions & 219 deletions lib/api/src/js/env.rs

This file was deleted.

61 changes: 61 additions & 0 deletions lib/api/src/js/error.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use crate::js::lib::std::string::String;
use crate::js::trap::RuntimeError;
#[cfg(feature = "std")]
use std::borrow::Cow;
#[cfg(feature = "std")]
use thiserror::Error;

Expand Down Expand Up @@ -83,11 +86,27 @@ pub enum WasmError {
#[cfg_attr(feature = "std", error("Unsupported feature: {0}"))]
Unsupported(String),

/// A Javascript value could not be converted to the requested type.
#[cfg_attr(feature = "std", error("{0} doesn't match js value type {1}"))]
TypeMismatch(Cow<'static, str>, Cow<'static, str>),

/// A generic error.
#[cfg_attr(feature = "std", error("{0}"))]
Generic(String),
}

impl From<wasm_bindgen::JsValue> for WasmError {
fn from(err: wasm_bindgen::JsValue) -> Self {
Self::Generic(
if err.is_string() && err.as_string().filter(|s| !s.is_empty()).is_some() {
err.as_string().unwrap_or_default()
} else {
format!("Unexpected Javascript error: {:?}", err)
},
)
}
}

/// The Serialize error can occur when serializing a
/// compiled Module into a binary.
/// Copied from wasmer_compiler::SerializeError
Expand Down Expand Up @@ -124,3 +143,45 @@ pub enum DeserializeError {
#[cfg_attr(feature = "std", error(transparent))]
Compiler(CompileError),
}

/// An error while instantiating a module.
///
/// This is not a common WebAssembly error, however
/// we need to differentiate from a `LinkError` (an error
/// that happens while linking, on instantiation), a
/// Trap that occurs when calling the WebAssembly module
/// start function, and an error when initializing the user's
/// host environments.
#[derive(Debug)]
#[cfg_attr(feature = "std", derive(Error))]
pub enum InstantiationError {
/// A linking ocurred during instantiation.
#[cfg_attr(feature = "std", error("Link error: {0}"))]
Link(String),

/// A runtime error occured while invoking the start function
#[cfg_attr(feature = "std", error(transparent))]
Start(RuntimeError),

/// Import from a different [`Context`].
/// This error occurs when an import from a different context is used.
#[cfg_attr(feature = "std", error("cannot mix imports from different contexts"))]
BadContext,

/// A generic error occured while invoking API functions
#[cfg_attr(feature = "std", error(transparent))]
Wasm(WasmError),
}

impl From<WasmError> for InstantiationError {
fn from(original: WasmError) -> Self {
Self::Wasm(original)
}
}

#[cfg(feature = "core")]
impl std::fmt::Display for InstantiationError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "InstantiationError")
}
}
Loading

0 comments on commit 0943422

Please sign in to comment.