Skip to content

Commit

Permalink
Apply suggestions from code review
Browse files Browse the repository at this point in the history
Co-authored-by: Ivan Enderlin <[email protected]>
  • Loading branch information
syrusakbary and Hywan authored Jul 22, 2021
1 parent 48d32f2 commit 4ae1b77
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 38 deletions.
2 changes: 1 addition & 1 deletion lib/js-api/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "wasmer-js"
version = "2.0.0"
description = "Compile Wasmer to WebAssembly and use it in JavaScript"
description = "A crate to compile Wasmer to WebAssembly and make it run in a JavaScript host"
categories = ["wasm"]
keywords = ["wasm", "webassembly", "runtime", "vm", "javascript"]
authors = ["Wasmer Engineering Team <[email protected]>"]
Expand Down
10 changes: 5 additions & 5 deletions lib/js-api/README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# `wasmer-js` [![Build Status](https://github.com/wasmerio/wasmer/workflows/build/badge.svg?style=flat-square)](https://github.com/wasmerio/wasmer/actions?query=workflow%3Abuild) [![Join Wasmer Slack](https://img.shields.io/static/v1?label=Slack&message=join%20chat&color=brighgreen&style=flat-square)](https://slack.wasmer.io) [![MIT License](https://img.shields.io/github/license/wasmerio/wasmer.svg?style=flat-square)](https://github.com/wasmerio/wasmer/blob/master/LICENSE) [![crates.io](https://img.shields.io/crates/v/wasmer-js.svg)](https://crates.io/crates/wasmer-js)

[`Wasmer`](https://wasmer.io/) is the most popular
[WebAssembly](https://webassembly.org/) runtime for Rust. This runtime is an adapted version of the Wasmer API that compiles to
WebAssembly via `wasm-bindgen`.

`wasmer-js` uses the same WebAssembly runtime of your environment (browser or Node.js).
[WebAssembly](https://webassembly.org/) runtime for Rust. This crate mimics the same Rust
API than the `wasmer` crate, but when compiled to WebAssembly, it only targets
a JavaScript host. It means that it is possible to write a Rust program that uses Wasmer,
and compiles everything to WebAssembly to run in a browser, Node.js, Deno and so on.

## Usage

Expand Down Expand Up @@ -38,7 +38,7 @@ pub extern fn do_add_one_in_wasmer() -> i32 {
let instance = Instance::new(&module, &import_object).unwrap();

let add_one = instance.exports.get_function("add_one").unwrap();
let result = add_one.call(&[Value::I32(42)])?;
let result = add_one.call(&[Value::I32(42)]).unwrap();
assert_eq!(result[0], Value::I32(43));
result[0].unwrap_i32()
}
Expand Down
24 changes: 18 additions & 6 deletions lib/js-api/src/externals/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,19 @@ extern "C" {
/// Takes the number of pages to grow (64KiB in size) and returns the
/// previous size of memory, in pages.
///
/// # Reimplementation
///
/// We re-implement `WebAssembly.Memory.grow` because it is
/// different from what `wasm-bindgen` declares. It marks the function
/// as `catch`, which means it can throw an exception.
///
/// See [the opened patch](https://github.com/rustwasm/wasm-bindgen/pull/2599).
///
/// # Exceptions
///
/// A `RangeError` is thrown if adding pages would exceed the maximum
/// memory.
///
/// [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WebAssembly/Memory/grow)
#[wasm_bindgen(catch, method, js_namespace = WebAssembly)]
pub fn grow(this: &JSMemory, pages: u32) -> Result<u32, JsValue>;
Expand Down Expand Up @@ -141,7 +154,7 @@ impl Memory {
/// modify the memory contents in any way including by calling a wasm
/// function that writes to the memory or by resizing the memory.
pub unsafe fn data_unchecked(&self) -> &[u8] {
unimplemented!("direct data pointer access is not possible in js");
unimplemented!("direct data pointer access is not possible in JavaScript");
}

/// Retrieve a mutable slice of the memory contents.
Expand All @@ -155,21 +168,20 @@ impl Memory {
/// by resizing this Memory.
#[allow(clippy::mut_from_ref)]
pub unsafe fn data_unchecked_mut(&self) -> &mut [u8] {
unimplemented!("direct data pointer access is not possible in js");
unimplemented!("direct data pointer access is not possible in JavaScript");
}

/// Returns the pointer to the raw bytes of the `Memory`.
pub fn data_ptr(&self) -> *mut u8 {
unimplemented!("direct data pointer access is not possible in js");
unimplemented!("direct data pointer access is not possible in JavaScript");
}

/// Returns the size (in bytes) of the `Memory`.
pub fn data_size(&self) -> u64 {
let bytes = js_sys::Reflect::get(&self.vm_memory.memory.buffer(), &"byteLength".into())
js_sys::Reflect::get(&self.vm_memory.memory.buffer(), &"byteLength".into())
.unwrap()
.as_f64()
.unwrap() as u64;
return bytes;
.unwrap() as _
}

/// Returns the size (in [`Pages`]) of the `Memory`.
Expand Down
18 changes: 0 additions & 18 deletions lib/js-api/src/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use crate::exports::Exports;
use crate::externals::Extern;
use crate::module::Module;
use crate::store::Store;
// use crate::{HostEnvInitError, LinkError, RuntimeError};
use crate::resolver::Resolver;
use js_sys::WebAssembly;
use std::fmt;
Expand Down Expand Up @@ -42,25 +41,8 @@ pub enum InstantiationError {
/// A runtime error occured while invoking the start function
#[cfg_attr(feature = "std", error("Start error: {0}"))]
Start(String),
// /// Error occurred when initializing the host environment.
// #[error(transparent)]
// HostEnvInitialization(HostEnvInitError),
}

// impl From<wasmer_engine::InstantiationError> for InstantiationError {
// fn from(other: wasmer_engine::InstantiationError) -> Self {
// match other {
// wasmer_engine::InstantiationError::Link(e) => Self::Link(e),
// wasmer_engine::InstantiationError::Start(e) => Self::Start(e),
// }
// }
// }

// impl From<HostEnvInitError> for InstantiationError {
// fn from(other: HostEnvInitError) -> Self {
// Self::HostEnvInitialization(other)
// }
// }

impl Instance {
/// Creates a new `Instance` from a WebAssembly [`Module`] and a
Expand Down
2 changes: 0 additions & 2 deletions lib/js-api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,6 @@ pub use crate::types::{
pub use crate::types::{Val as Value, ValType as Type};
pub use crate::utils::is_wasm;

// #[cfg(feature = "experimental-reference-types-extern-ref")]
// pub use wasmer_types::ExternRef;
pub use wasmer_types::{
Atomically, Bytes, ExportIndex, GlobalInit, LocalFunctionIndex, MemoryView, Pages, ValueType,
WASM_MAX_PAGES, WASM_MIN_PAGES, WASM_PAGE_SIZE,
Expand Down
4 changes: 0 additions & 4 deletions lib/js-api/src/trap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,6 @@ impl fmt::Display for RuntimeErrorSource {
}
}

// fn _assert_trap_is_sync_and_send(t: &Trap) -> (&dyn Sync, &dyn Send) {
// (t, t)
// }

impl RuntimeError {
/// Creates a new generic `RuntimeError` with the given `message`.
///
Expand Down
4 changes: 2 additions & 2 deletions lib/js-api/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ pub fn param_from_js(ty: &ValType, js_val: &JsValue) -> Val {
ValType::I64 => Val::I64(js_val.as_f64().unwrap() as _),
ValType::F32 => Val::F32(js_val.as_f64().unwrap() as _),
ValType::F64 => Val::F64(js_val.as_f64().unwrap()),
_ => unimplemented!("The type is not yet supported in the JS Function API"),
t => unimplemented!("The type `{:?}` is not yet supported in the JS Function API", t),
}
}

Expand All @@ -40,7 +40,7 @@ impl AsJs for Val {
Self::F32(f) => JsValue::from_f64(*f as f64),
Self::F64(f) => JsValue::from_f64(*f),
Self::FuncRef(func) => func.as_ref().unwrap().exported.function.clone().into(),
_ => unimplemented!("The type is not yet supported in the JS Function API"),
v => unimplemented!("The value `{:?}` is not yet supported in the JS Function API", v),
}
}
}

0 comments on commit 4ae1b77

Please sign in to comment.