Skip to content

Commit 1f4175d

Browse files
committed
Overall wasm refactoring & improvements
1 parent 0fccae1 commit 1f4175d

File tree

11 files changed

+325
-169
lines changed

11 files changed

+325
-169
lines changed

Cargo.lock

Lines changed: 59 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

objdiff-core/Cargo.toml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ mips = ["any-arch", "rabbitizer"]
2323
ppc = ["any-arch", "cwdemangle", "cwextab", "ppc750cl"]
2424
x86 = ["any-arch", "cpp_demangle", "iced-x86", "msvc-demangler"]
2525
arm = ["any-arch", "cpp_demangle", "unarm", "arm-attr"]
26-
wasm = ["serde_json"]
26+
wasm = ["serde_json", "console_error_panic_hook", "console_log"]
2727

2828
[dependencies]
2929
anyhow = "1.0.82"
@@ -40,6 +40,9 @@ serde = { version = "1", features = ["derive"] }
4040
similar = { version = "2.5.0", default-features = false }
4141
strum = { version = "0.26.2", features = ["derive"] }
4242
wasm-bindgen = "0.2.93"
43+
tsify-next = { version = "0.5.4", default-features = false, features = ["js"] }
44+
console_log = { version = "1.0.0", optional = true }
45+
console_error_panic_hook = { version = "0.1.7", optional = true }
4346

4447
# config
4548
globset = { version = "0.4.14", features = ["serde1"], optional = true }

objdiff-core/src/bindings/wasm.rs

Lines changed: 60 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,78 @@
1-
use anyhow::Context;
21
use prost::Message;
32
use wasm_bindgen::prelude::*;
43

54
use crate::{bindings::diff::DiffResult, diff, obj};
65

7-
#[wasm_bindgen]
8-
pub fn run_diff(
6+
fn parse_object(
7+
data: Option<Box<[u8]>>,
8+
config: &diff::DiffObjConfig,
9+
) -> Result<Option<obj::ObjInfo>, JsError> {
10+
data.as_ref().map(|data| obj::read::parse(data, config)).transpose().to_js()
11+
}
12+
13+
fn parse_and_run_diff(
914
left: Option<Box<[u8]>>,
1015
right: Option<Box<[u8]>>,
1116
config: diff::DiffObjConfig,
12-
) -> Result<String, JsError> {
13-
let target = left
14-
.as_ref()
15-
.map(|data| obj::read::parse(data, &config).context("Loading target"))
16-
.transpose()
17-
.map_err(|e| JsError::new(&e.to_string()))?;
18-
let base = right
19-
.as_ref()
20-
.map(|data| obj::read::parse(data, &config).context("Loading base"))
21-
.transpose()
22-
.map_err(|e| JsError::new(&e.to_string()))?;
23-
let result = diff::diff_objs(&config, target.as_ref(), base.as_ref(), None)
24-
.map_err(|e| JsError::new(&e.to_string()))?;
25-
let left = target.as_ref().and_then(|o| result.left.as_ref().map(|d| (o, d)));
26-
let right = base.as_ref().and_then(|o| result.right.as_ref().map(|d| (o, d)));
27-
let out = DiffResult::new(left, right);
28-
serde_json::to_string(&out).map_err(|e| JsError::new(&e.to_string()))
17+
) -> Result<DiffResult, JsError> {
18+
let target = parse_object(left, &config)?;
19+
let base = parse_object(right, &config)?;
20+
run_diff(target.as_ref(), base.as_ref(), config)
21+
}
22+
23+
fn run_diff(
24+
left: Option<&obj::ObjInfo>,
25+
right: Option<&obj::ObjInfo>,
26+
config: diff::DiffObjConfig,
27+
) -> Result<DiffResult, JsError> {
28+
log::debug!("Running diff with config: {:?}", config);
29+
let result = diff::diff_objs(&config, left, right, None).to_js()?;
30+
let left = left.and_then(|o| result.left.as_ref().map(|d| (o, d)));
31+
let right = right.and_then(|o| result.right.as_ref().map(|d| (o, d)));
32+
Ok(DiffResult::new(left, right))
2933
}
3034

35+
// #[wasm_bindgen]
36+
// pub fn run_diff_json(
37+
// left: Option<Box<[u8]>>,
38+
// right: Option<Box<[u8]>>,
39+
// config: diff::DiffObjConfig,
40+
// ) -> Result<String, JsError> {
41+
// let out = run_diff_opt_box(left, right, config)?;
42+
// serde_json::to_string(&out).map_err(|e| JsError::new(&e.to_string()))
43+
// }
44+
3145
#[wasm_bindgen]
3246
pub fn run_diff_proto(
3347
left: Option<Box<[u8]>>,
3448
right: Option<Box<[u8]>>,
3549
config: diff::DiffObjConfig,
3650
) -> Result<Box<[u8]>, JsError> {
37-
let target = left
38-
.as_ref()
39-
.map(|data| obj::read::parse(data, &config).context("Loading target"))
40-
.transpose()
41-
.map_err(|e| JsError::new(&e.to_string()))?;
42-
let base = right
43-
.as_ref()
44-
.map(|data| obj::read::parse(data, &config).context("Loading base"))
45-
.transpose()
46-
.map_err(|e| JsError::new(&e.to_string()))?;
47-
let result = diff::diff_objs(&config, target.as_ref(), base.as_ref(), None)
48-
.map_err(|e| JsError::new(&e.to_string()))?;
49-
let left = target.as_ref().and_then(|o| result.left.as_ref().map(|d| (o, d)));
50-
let right = base.as_ref().and_then(|o| result.right.as_ref().map(|d| (o, d)));
51-
let out = DiffResult::new(left, right);
51+
let out = parse_and_run_diff(left, right, config)?;
5252
Ok(out.encode_to_vec().into_boxed_slice())
5353
}
54+
55+
#[wasm_bindgen(start)]
56+
fn start() -> Result<(), JsError> {
57+
console_error_panic_hook::set_once();
58+
#[cfg(debug_assertions)]
59+
console_log::init_with_level(log::Level::Debug).to_js()?;
60+
#[cfg(not(debug_assertions))]
61+
console_log::init_with_level(log::Level::Info).to_js()?;
62+
Ok(())
63+
}
64+
65+
#[inline]
66+
fn to_js_error(e: impl std::fmt::Display) -> JsError { JsError::new(&e.to_string()) }
67+
68+
trait ToJsResult {
69+
type Output;
70+
71+
fn to_js(self) -> Result<Self::Output, JsError>;
72+
}
73+
74+
impl<T, E: std::fmt::Display> ToJsResult for Result<T, E> {
75+
type Output = T;
76+
77+
fn to_js(self) -> Result<T, JsError> { self.map_err(to_js_error) }
78+
}

objdiff-core/src/diff/mod.rs

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use std::collections::HashSet;
22

33
use anyhow::Result;
4-
use wasm_bindgen::prelude::wasm_bindgen;
54

65
use crate::{
76
diff::{
@@ -18,7 +17,6 @@ pub mod code;
1817
pub mod data;
1918
pub mod display;
2019

21-
#[wasm_bindgen]
2220
#[derive(
2321
Debug,
2422
Copy,
@@ -30,6 +28,7 @@ pub mod display;
3028
serde::Serialize,
3129
strum::VariantArray,
3230
strum::EnumMessage,
31+
tsify_next::Tsify,
3332
)]
3433
pub enum X86Formatter {
3534
#[default]
@@ -43,7 +42,6 @@ pub enum X86Formatter {
4342
Masm,
4443
}
4544

46-
#[wasm_bindgen]
4745
#[derive(
4846
Debug,
4947
Copy,
@@ -55,6 +53,7 @@ pub enum X86Formatter {
5553
serde::Serialize,
5654
strum::VariantArray,
5755
strum::EnumMessage,
56+
tsify_next::Tsify,
5857
)]
5958
pub enum MipsAbi {
6059
#[default]
@@ -68,7 +67,6 @@ pub enum MipsAbi {
6867
N64,
6968
}
7069

71-
#[wasm_bindgen]
7270
#[derive(
7371
Debug,
7472
Copy,
@@ -80,6 +78,7 @@ pub enum MipsAbi {
8078
serde::Serialize,
8179
strum::VariantArray,
8280
strum::EnumMessage,
81+
tsify_next::Tsify,
8382
)]
8483
pub enum MipsInstrCategory {
8584
#[default]
@@ -97,7 +96,6 @@ pub enum MipsInstrCategory {
9796
R5900,
9897
}
9998

100-
#[wasm_bindgen]
10199
#[derive(
102100
Debug,
103101
Copy,
@@ -109,6 +107,7 @@ pub enum MipsInstrCategory {
109107
serde::Serialize,
110108
strum::VariantArray,
111109
strum::EnumMessage,
110+
tsify_next::Tsify,
112111
)]
113112
pub enum ArmArchVersion {
114113
#[default]
@@ -122,7 +121,6 @@ pub enum ArmArchVersion {
122121
V6K,
123122
}
124123

125-
#[wasm_bindgen]
126124
#[derive(
127125
Debug,
128126
Copy,
@@ -134,6 +132,7 @@ pub enum ArmArchVersion {
134132
serde::Serialize,
135133
strum::VariantArray,
136134
strum::EnumMessage,
135+
tsify_next::Tsify,
137136
)]
138137
pub enum ArmR9Usage {
139138
#[default]
@@ -154,8 +153,8 @@ pub enum ArmR9Usage {
154153
#[inline]
155154
const fn default_true() -> bool { true }
156155

157-
#[wasm_bindgen]
158-
#[derive(Debug, Clone, Eq, PartialEq, serde::Deserialize, serde::Serialize)]
156+
#[derive(Debug, Clone, Eq, PartialEq, serde::Deserialize, serde::Serialize, tsify_next::Tsify)]
157+
#[tsify(from_wasm_abi)]
159158
#[serde(default)]
160159
pub struct DiffObjConfig {
161160
pub relax_reloc_diffs: bool,
@@ -207,9 +206,6 @@ impl DiffObjConfig {
207206
}
208207
}
209208

210-
#[wasm_bindgen]
211-
pub fn default_diff_obj_config() -> DiffObjConfig { Default::default() }
212-
213209
#[derive(Debug, Clone)]
214210
pub struct ObjSectionDiff {
215211
pub symbols: Vec<ObjSymbolDiff>,

objdiff-wasm/eslint.config.js

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,22 @@ export default [
77
{languageOptions: {globals: globals.browser}},
88
pluginJs.configs.recommended,
99
...tseslint.configs.recommended,
10-
{rules: {"semi": [2, "always"]}},
10+
{
11+
rules: {
12+
"semi": [2, "always"],
13+
"@typescript-eslint/no-unused-vars": [
14+
"error",
15+
// https://typescript-eslint.io/rules/no-unused-vars/#benefits-over-typescript
16+
{
17+
"args": "all",
18+
"argsIgnorePattern": "^_",
19+
"caughtErrors": "all",
20+
"caughtErrorsIgnorePattern": "^_",
21+
"destructuredArrayIgnorePattern": "^_",
22+
"varsIgnorePattern": "^_",
23+
"ignoreRestSiblings": true
24+
},
25+
],
26+
}
27+
},
1128
];

objdiff-wasm/package-lock.json

Lines changed: 7 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)