Skip to content

Commit

Permalink
Remove old conversion code
Browse files Browse the repository at this point in the history
  • Loading branch information
kajacx committed Feb 23, 2024
1 parent 0076f1d commit cb30f86
Show file tree
Hide file tree
Showing 12 changed files with 40 additions and 335 deletions.
17 changes: 11 additions & 6 deletions crates/wasm-bridge-js/src/component/component.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
use anyhow::{bail, Context};
use js_sys::{Function, Object, Reflect, WebAssembly};
use js_sys::{Function, Object, Reflect, Uint8Array, WebAssembly};
use wasm_bindgen::JsValue;
use wasm_bindgen_futures::JsFuture;

use crate::{
direct::{LazyModuleMemory, ModuleMemory},
helpers::{map_js_error, static_str_to_js},
DropHandles, Engine, Result, ToJsValue,
DropHandles, Engine, Result,
};

use super::*;
Expand All @@ -19,12 +20,12 @@ impl Component {
pub fn new(_engine: &Engine, bytes: impl AsRef<[u8]>) -> Result<Self> {
let files = ComponentLoader::generate_files(bytes.as_ref())?;

let main_module = WebAssembly::Module::new(&files.main_core.to_js_value())
let main_module = WebAssembly::Module::new(&bytes_to_js_value(&files.main_core))
.map_err(map_js_error("Synchronously compile main core module"))?;

let wasi_module = if let Some(wasi_core) = files.wasi_core {
Some(
WebAssembly::Module::new(&wasi_core.to_js_value())
WebAssembly::Module::new(&bytes_to_js_value(&wasi_core))
.map_err(map_js_error("Synchronously compile wasi core module"))?,
)
} else {
Expand All @@ -40,14 +41,14 @@ impl Component {
pub async fn new_async(_engine: &Engine, bytes: impl AsRef<[u8]>) -> Result<Self> {
let files = ComponentLoader::generate_files(bytes.as_ref())?;

let promise = WebAssembly::compile(&files.main_core.to_js_value());
let promise = WebAssembly::compile(&bytes_to_js_value(&files.main_core));
let module = JsFuture::from(promise)
.await
.map_err(map_js_error("Asynchronously compile main core module"))?;
let main_module = module.into();

let wasi_module = if let Some(wasi_core) = files.wasi_core {
let promise = WebAssembly::compile(&wasi_core.to_js_value());
let promise = WebAssembly::compile(&bytes_to_js_value(&wasi_core));
let module = JsFuture::from(promise)
.await
.map_err(map_js_error("Asynchronously compile wasi core module"))?;
Expand Down Expand Up @@ -262,3 +263,7 @@ impl Component {
pub async fn new_component_async(engine: &Engine, bytes: impl AsRef<[u8]>) -> Result<Component> {
Component::new_async(engine, bytes).await
}

fn bytes_to_js_value(bytes: &[u8]) -> JsValue {
Uint8Array::from(bytes).into()
}
143 changes: 1 addition & 142 deletions crates/wasm-bridge-js/src/conversions/from_js_value.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
use anyhow::Context;
use js_sys::Reflect;
use wasm_bindgen::{convert::FromWasmAbi, JsValue};

use crate::{
helpers::{map_js_error, static_str_to_js},
*,
};
use crate::{helpers::map_js_error, *};

pub trait FromJsValue: Sized {
type WasmAbi: FromWasmAbi;
Expand Down Expand Up @@ -41,21 +37,6 @@ impl FromJsValue for () {
}
}

impl FromJsValue for bool {
type WasmAbi = Self;

fn from_js_value(value: &JsValue) -> Result<Self> {
match value.as_bool() {
Some(value) => Ok(value),
None => Err(map_js_error("Expected a boolean value")(value)),
}
}

fn from_wasm_abi(abi: Self::WasmAbi) -> Result<Self> {
Ok(abi)
}
}

macro_rules! from_js_value_signed {
($name: ty) => {
impl FromJsValue for $name {
Expand Down Expand Up @@ -134,131 +115,9 @@ impl FromJsValue for u64 {
}
}

// TODO: not the best name, but it works
from_js_value_signed!(f32);
from_js_value_signed!(f64);

impl FromJsValue for char {
type WasmAbi = Self;

fn from_js_value(value: &JsValue) -> Result<Self> {
match value.as_string() {
Some(text) if !text.is_empty() => Ok(text.chars().next().unwrap()),
_ => Err(map_js_error("Expected a single-character string")(value)),
}
}

fn from_wasm_abi(abi: Self::WasmAbi) -> Result<Self> {
Ok(abi)
}
}

impl FromJsValue for String {
type WasmAbi = Self;

fn from_js_value(value: &JsValue) -> Result<Self, crate::Error> {
match value.as_string() {
Some(value) => Ok(value),
None => Err(map_js_error("Expected a string")(value)),
}
}

fn from_wasm_abi(abi: Self::WasmAbi) -> Result<Self> {
Ok(abi)
}
}

impl<T: FromJsValue> FromJsValue for Option<T> {
type WasmAbi = JsValue; // TODO: better ABI?

fn from_js_value(value: &JsValue) -> Result<Self> {
if value.is_undefined() || value.is_null() {
Ok(None)
} else {
Ok(Some(T::from_js_value(value)?))
}
}

fn from_wasm_abi(abi: Self::WasmAbi) -> Result<Self> {
Self::from_js_value(&abi)
}
}

impl<T: FromJsValue, E: FromJsValue> FromJsValue for Result<T, E> {
type WasmAbi = JsValue;

fn from_js_value(value: &JsValue) -> Result<Self> {
// TODO: better error handling
let tag = Reflect::get(value, static_str_to_js("tag"))
.map_err(map_js_error("Get tag from result"))?
.as_string()
.context("Result tag should be string")?;

let val = Reflect::get(value, static_str_to_js("val"))
.map_err(map_js_error("Get val from result"))?;

if tag == "ok" {
Ok(Ok(T::from_js_value(&val)?))
} else if tag == "err" {
Ok(Err(E::from_js_value(&val)?))
} else {
Err(map_js_error("Unknown result tag")(value))
}
}

fn from_fn_result(result: &Result<JsValue, JsValue>) -> Result<Self> {
Ok(match result {
Ok(val) => Ok(T::from_js_value(val)?),
Err(err) => {
let payload = Reflect::get(err, static_str_to_js("payload"))
.map_err(map_js_error("Get result error payload"))?;
Err(E::from_js_value(&payload)?)
}
})
}

fn from_wasm_abi(abi: Self::WasmAbi) -> Result<Self> {
Self::from_js_value(&abi)
}
}

impl<T: FromJsValue> FromJsValue for Vec<T> {
type WasmAbi = JsValue;

fn from_js_value(value: &JsValue) -> Result<Self> {
let length = Reflect::get(value, static_str_to_js("length"))
.map_err(map_js_error("Get length of array"))?
.as_f64()
.context("Array length should be a number")? as u32;

let mut result = Vec::with_capacity(length as usize);

for index in 0..length {
let item = Reflect::get_u32(value, index)
.map_err(map_js_error("Get array value at an index"))?;
result.push(T::from_js_value(&item)?);
}

Ok(result)
}

fn from_wasm_abi(abi: Self::WasmAbi) -> Result<Self> {
Self::from_js_value(&abi)
}
}

impl FromJsValue for JsValue {
type WasmAbi = Self;

fn from_js_value(value: &JsValue) -> Result<Self> {
Ok(value.clone())
}

fn from_wasm_abi(abi: Self::WasmAbi) -> Result<Self> {
Ok(abi)
}
}

impl FromJsValue for Val {
type WasmAbi = JsValue;

Expand Down
Loading

0 comments on commit cb30f86

Please sign in to comment.