From 1d9a7d51a3bfa53717b0e1146c7c1e2d2eda9288 Mon Sep 17 00:00:00 2001 From: Corwin Date: Thu, 18 Apr 2024 00:44:13 +0100 Subject: [PATCH] expose as wasm --- Cargo.toml | 1 + agb-wasm/Cargo.toml | 29 +++++++++++++++++++++++++++ agb-wasm/src/lib.rs | 49 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 79 insertions(+) create mode 100644 agb-wasm/Cargo.toml create mode 100644 agb-wasm/src/lib.rs diff --git a/Cargo.toml b/Cargo.toml index 9334e895..7c273d76 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -26,6 +26,7 @@ members = [ "emulator/mgba", "emulator/mgba-sys", "emulator/test-runner", + "agb-wasm", ] exclude = [ diff --git a/agb-wasm/Cargo.toml b/agb-wasm/Cargo.toml new file mode 100644 index 00000000..ee56b986 --- /dev/null +++ b/agb-wasm/Cargo.toml @@ -0,0 +1,29 @@ +[package] +name = "agb-wasm" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[lib] +crate-type = ["cdylib", "rlib"] + +[features] +default = ["console_error_panic_hook"] + +[dependencies] +wasm-bindgen = "0.2.84" +agb-debug = { path="../agb-debug" } + +# The `console_error_panic_hook` crate provides better debugging of panics by +# logging them with `console.error`. This is great for development, but requires +# all the `std::fmt` and `std::panicking` infrastructure, so isn't great for +# code size when deploying. +console_error_panic_hook = { version = "0.1.7", optional = true } + +[dev-dependencies] +wasm-bindgen-test = "0.3.34" + +[profile.release] +# Tell `rustc` to optimize for small code size. +opt-level = "s" \ No newline at end of file diff --git a/agb-wasm/src/lib.rs b/agb-wasm/src/lib.rs new file mode 100644 index 00000000..c334fc95 --- /dev/null +++ b/agb-wasm/src/lib.rs @@ -0,0 +1,49 @@ +use agb_debug::{addr2line::Context, load_dwarf, Addr2LineContext}; +use wasm_bindgen::prelude::*; + +#[wasm_bindgen] +pub fn decode_backtrace(backtrace: &str) -> Result, JsError> { + Ok(agb_debug::gwilym_decode(backtrace)?.collect()) +} + +#[wasm_bindgen] +pub struct DebugFile { + dwarf: Addr2LineContext, +} + +#[wasm_bindgen(getter_with_clone)] +pub struct AddressInfo { + pub filename: String, + pub function_name: String, + pub line_number: u32, + pub column: u32, + pub is_interesting: bool, + pub is_inline: bool, +} + +#[wasm_bindgen] +impl DebugFile { + #[wasm_bindgen(constructor)] + pub fn new(data: &[u8]) -> Result { + Ok(DebugFile { + dwarf: Context::from_dwarf(load_dwarf(data)?)?, + }) + } + + pub fn address_info(&self, address: u32) -> Result, JsError> { + let info = agb_debug::address_info(&self.dwarf, address.into())?; + let address_infos = info + .into_iter() + .map(|x| AddressInfo { + filename: x.location.filename, + line_number: x.location.line, + column: x.location.col, + is_interesting: x.is_interesting, + is_inline: x.is_inline, + function_name: x.function, + }) + .collect(); + + Ok(address_infos) + } +}