From 0ab347b9e5f606d6f4cb912772f90d9d4ffb2f03 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Jo=C3=A3o=20Magalh=C3=A3es?= <joamag@gmail.com>
Date: Sat, 17 Feb 2024 22:21:54 +0000
Subject: [PATCH] fix: error returning issue using WS function

These functions act as wrappers modifying the error value of the original function into a string.
---
 frontends/web/ts/gb.ts | 14 ++++++------
 src/error.rs           | 16 --------------
 src/state.rs           | 50 ++++++++++++++++++++++++++++++++++++++++--
 3 files changed, 55 insertions(+), 25 deletions(-)

diff --git a/frontends/web/ts/gb.ts b/frontends/web/ts/gb.ts
index c37cc617..d95d5006 100644
--- a/frontends/web/ts/gb.ts
+++ b/frontends/web/ts/gb.ts
@@ -804,22 +804,22 @@ export class GameboyEmulator extends EmulatorBase implements Emulator {
 
     serializeState(): Uint8Array {
         if (!this.gameBoy) throw new Error("Unable to serialize state");
-        return StateManager.save(this.gameBoy, SaveStateFormat.Bos);
+        return StateManager.save_ws(this.gameBoy, SaveStateFormat.Bos);
     }
 
     unserializeState(data: Uint8Array) {
         if (!this.gameBoy) throw new Error("Unable to unserialize state");
-        StateManager.load(data, this.gameBoy, SaveStateFormat.Bos);
+        StateManager.load_ws(data, this.gameBoy, SaveStateFormat.Bos);
     }
 
     buildState(index: number, data: Uint8Array): SaveState {
-        const state = StateManager.read_bos(data);
+        const state = StateManager.read_bos_ws(data);
         return {
             index: index,
-            timestamp: Number(state.timestamp()),
-            agent: state.agent(),
-            model: state.model(),
-            thumbnail: state.image_eager()
+            timestamp: Number(state.timestamp_ws()),
+            agent: state.agent_ws(),
+            model: state.model_ws(),
+            thumbnail: state.image_eager_ws()
         };
     }
 
diff --git a/src/error.rs b/src/error.rs
index 9d364a02..d33d4d26 100644
--- a/src/error.rs
+++ b/src/error.rs
@@ -1,11 +1,5 @@
 use std::fmt::{self, Display, Formatter};
 
-#[cfg(feature = "wasm")]
-use wasm_bindgen::prelude::*;
-
-#[cfg(feature = "wasm")]
-use wasm_bindgen::convert::*;
-
 /// Top level enum for error handling.
 /// Most of the time, you will want to use the `CustomError` variant
 /// to provide a more detailed error message.
@@ -33,13 +27,3 @@ impl Display for Error {
         write!(f, "{}", self.description())
     }
 }
-
-#[cfg(feature = "wasm")]
-impl IntoWasmAbi for Error {
-    type Abi = <Vec<u8> as IntoWasmAbi>::Abi;
-
-    #[inline]
-    fn into_abi(self) -> Self::Abi {
-        self.description().into_abi();
-    }
-}
diff --git a/src/state.rs b/src/state.rs
index 0d6f0775..166289b2 100644
--- a/src/state.rs
+++ b/src/state.rs
@@ -128,7 +128,6 @@ impl BosState {
     }
 }
 
-#[cfg_attr(feature = "wasm", wasm_bindgen)]
 impl BosState {
     pub fn timestamp(&self) -> Result<u64, Error> {
         if let Some(info) = &self.info {
@@ -163,6 +162,26 @@ impl BosState {
     }
 }
 
+#[cfg(feature = "wasm")]
+#[cfg_attr(feature = "wasm", wasm_bindgen)]
+impl BosState {
+    pub fn timestamp_ws(&self) -> Result<u64, String> {
+        Self::timestamp(self).map_err(|e| e.to_string())
+    }
+
+    pub fn agent_ws(&self) -> Result<String, String> {
+        Self::agent(self).map_err(|e| e.to_string())
+    }
+
+    pub fn model_ws(&self) -> Result<String, String> {
+        Self::model(self).map_err(|e| e.to_string())
+    }
+
+    pub fn image_eager_ws(&self) -> Result<Vec<u8>, String> {
+        Self::image_eager(self).map_err(|e| e.to_string())
+    }
+}
+
 impl Serialize for BosState {
     fn write(&mut self, buffer: &mut Cursor<Vec<u8>>) {
         self.block_count = self.build_block_count();
@@ -1465,7 +1484,6 @@ impl StateManager {
     }
 }
 
-#[cfg_attr(feature = "wasm", wasm_bindgen)]
 impl StateManager {
     pub fn save(gb: &mut GameBoy, format: Option<SaveStateFormat>) -> Result<Vec<u8>, Error> {
         let mut data = Cursor::new(vec![]);
@@ -1563,6 +1581,34 @@ impl StateManager {
     }
 }
 
+#[cfg(feature = "wasm")]
+#[cfg_attr(feature = "wasm", wasm_bindgen)]
+impl StateManager {
+    pub fn save_ws(gb: &mut GameBoy, format: Option<SaveStateFormat>) -> Result<Vec<u8>, String> {
+        Self::save(gb, format).map_err(|e| e.to_string())
+    }
+
+    pub fn load_ws(
+        data: &[u8],
+        gb: &mut GameBoy,
+        format: Option<SaveStateFormat>,
+    ) -> Result<(), String> {
+        Self::load(data, gb, format).map_err(|e| e.to_string())
+    }
+
+    pub fn read_bos_ws(data: &[u8]) -> Result<BosState, String> {
+        Self::read_bos(data).map_err(|e| e.to_string())
+    }
+
+    pub fn read_bess_ws(data: &[u8]) -> Result<BessState, String> {
+        Self::read_bess(data).map_err(|e| e.to_string())
+    }
+
+    pub fn thumbnail_ws(data: &[u8], format: Option<SaveStateFormat>) -> Result<Vec<u8>, String> {
+        Self::thumbnail(data, format).map_err(|e| e.to_string())
+    }
+}
+
 #[cfg(test)]
 mod tests {
     use super::*;
-- 
GitLab