expose as wasm

This commit is contained in:
Corwin 2024-04-18 00:44:13 +01:00
parent f269e6364a
commit 1d9a7d51a3
No known key found for this signature in database
3 changed files with 79 additions and 0 deletions

View file

@ -26,6 +26,7 @@ members = [
"emulator/mgba",
"emulator/mgba-sys",
"emulator/test-runner",
"agb-wasm",
]
exclude = [

29
agb-wasm/Cargo.toml Normal file
View file

@ -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"

49
agb-wasm/src/lib.rs Normal file
View file

@ -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<Vec<u32>, 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<DebugFile, JsError> {
Ok(DebugFile {
dwarf: Context::from_dwarf(load_dwarf(data)?)?,
})
}
pub fn address_info(&self, address: u32) -> Result<Vec<AddressInfo>, 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)
}
}