mirror of
https://github.com/encounter/objdiff.git
synced 2025-12-12 22:56:19 +00:00
Add experimental wasm bindings
Published to npm as objdiff-wasm
This commit is contained in:
@@ -1,8 +1,4 @@
|
||||
use std::{
|
||||
cmp::max,
|
||||
collections::BTreeMap,
|
||||
time::{Duration, Instant},
|
||||
};
|
||||
use std::{cmp::max, collections::BTreeMap};
|
||||
|
||||
use anyhow::{anyhow, Result};
|
||||
use similar::{capture_diff_slices_deadline, Algorithm};
|
||||
@@ -100,13 +96,8 @@ fn diff_instructions(
|
||||
left_code: &ProcessCodeResult,
|
||||
right_code: &ProcessCodeResult,
|
||||
) -> Result<()> {
|
||||
let deadline = Instant::now() + Duration::from_secs(5);
|
||||
let ops = capture_diff_slices_deadline(
|
||||
Algorithm::Patience,
|
||||
&left_code.ops,
|
||||
&right_code.ops,
|
||||
Some(deadline),
|
||||
);
|
||||
let ops =
|
||||
capture_diff_slices_deadline(Algorithm::Patience, &left_code.ops, &right_code.ops, None);
|
||||
if ops.is_empty() {
|
||||
left_diff.extend(
|
||||
left_code
|
||||
|
||||
@@ -1,7 +1,4 @@
|
||||
use std::{
|
||||
cmp::{max, min, Ordering},
|
||||
time::{Duration, Instant},
|
||||
};
|
||||
use std::cmp::{max, min, Ordering};
|
||||
|
||||
use anyhow::{anyhow, Result};
|
||||
use similar::{capture_diff_slices_deadline, get_diff_ratio, Algorithm};
|
||||
@@ -47,15 +44,13 @@ pub fn diff_data_section(
|
||||
left_section_diff: &ObjSectionDiff,
|
||||
right_section_diff: &ObjSectionDiff,
|
||||
) -> Result<(ObjSectionDiff, ObjSectionDiff)> {
|
||||
let deadline = Instant::now() + Duration::from_secs(5);
|
||||
let left_max =
|
||||
left.symbols.iter().map(|s| s.section_address + s.size).max().unwrap_or(0).min(left.size);
|
||||
let right_max =
|
||||
right.symbols.iter().map(|s| s.section_address + s.size).max().unwrap_or(0).min(right.size);
|
||||
let left_data = &left.data[..left_max as usize];
|
||||
let right_data = &right.data[..right_max as usize];
|
||||
let ops =
|
||||
capture_diff_slices_deadline(Algorithm::Patience, left_data, right_data, Some(deadline));
|
||||
let ops = capture_diff_slices_deadline(Algorithm::Patience, left_data, right_data, None);
|
||||
let match_percent = get_diff_ratio(&ops, left_data.len(), right_data.len()) * 100.0;
|
||||
|
||||
let mut left_diff = Vec::<ObjDataDiff>::new();
|
||||
@@ -157,9 +152,7 @@ pub fn diff_data_symbol(
|
||||
let right_data = &right_section.data[right_symbol.section_address as usize
|
||||
..(right_symbol.section_address + right_symbol.size) as usize];
|
||||
|
||||
let deadline = Instant::now() + Duration::from_secs(5);
|
||||
let ops =
|
||||
capture_diff_slices_deadline(Algorithm::Patience, left_data, right_data, Some(deadline));
|
||||
let ops = capture_diff_slices_deadline(Algorithm::Patience, left_data, right_data, None);
|
||||
let match_percent = get_diff_ratio(&ops, left_data.len(), right_data.len()) * 100.0;
|
||||
|
||||
Ok((
|
||||
@@ -209,15 +202,9 @@ pub fn diff_bss_section(
|
||||
left_diff: &ObjSectionDiff,
|
||||
right_diff: &ObjSectionDiff,
|
||||
) -> Result<(ObjSectionDiff, ObjSectionDiff)> {
|
||||
let deadline = Instant::now() + Duration::from_secs(5);
|
||||
let left_sizes = left.symbols.iter().map(|s| (s.section_address, s.size)).collect::<Vec<_>>();
|
||||
let right_sizes = right.symbols.iter().map(|s| (s.section_address, s.size)).collect::<Vec<_>>();
|
||||
let ops = capture_diff_slices_deadline(
|
||||
Algorithm::Patience,
|
||||
&left_sizes,
|
||||
&right_sizes,
|
||||
Some(deadline),
|
||||
);
|
||||
let ops = capture_diff_slices_deadline(Algorithm::Patience, &left_sizes, &right_sizes, None);
|
||||
let mut match_percent = get_diff_ratio(&ops, left_sizes.len(), right_sizes.len()) * 100.0;
|
||||
|
||||
// Use the highest match percent between two options:
|
||||
|
||||
@@ -12,7 +12,7 @@ pub enum DiffText<'a> {
|
||||
/// Colored text
|
||||
BasicColor(&'a str, usize),
|
||||
/// Line number
|
||||
Line(usize),
|
||||
Line(u32),
|
||||
/// Instruction address
|
||||
Address(u64),
|
||||
/// Instruction mnemonic
|
||||
@@ -49,7 +49,7 @@ pub fn display_diff<E>(
|
||||
return Ok(());
|
||||
};
|
||||
if let Some(line) = ins.line {
|
||||
cb(DiffText::Line(line as usize))?;
|
||||
cb(DiffText::Line(line))?;
|
||||
}
|
||||
cb(DiffText::Address(ins.address - base_addr))?;
|
||||
if let Some(branch) = &ins_diff.branch_from {
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
use std::collections::HashSet;
|
||||
|
||||
use anyhow::Result;
|
||||
use wasm_bindgen::prelude::wasm_bindgen;
|
||||
|
||||
use crate::{
|
||||
diff::{
|
||||
@@ -17,6 +18,7 @@ pub mod code;
|
||||
pub mod data;
|
||||
pub mod display;
|
||||
|
||||
#[wasm_bindgen]
|
||||
#[derive(
|
||||
Debug,
|
||||
Copy,
|
||||
@@ -41,6 +43,7 @@ pub enum X86Formatter {
|
||||
Masm,
|
||||
}
|
||||
|
||||
#[wasm_bindgen]
|
||||
#[derive(
|
||||
Debug,
|
||||
Copy,
|
||||
@@ -65,6 +68,7 @@ pub enum MipsAbi {
|
||||
N64,
|
||||
}
|
||||
|
||||
#[wasm_bindgen]
|
||||
#[derive(
|
||||
Debug,
|
||||
Copy,
|
||||
@@ -93,6 +97,7 @@ pub enum MipsInstrCategory {
|
||||
R5900,
|
||||
}
|
||||
|
||||
#[wasm_bindgen]
|
||||
#[derive(
|
||||
Debug,
|
||||
Copy,
|
||||
@@ -117,6 +122,7 @@ pub enum ArmArchVersion {
|
||||
V6K,
|
||||
}
|
||||
|
||||
#[wasm_bindgen]
|
||||
#[derive(
|
||||
Debug,
|
||||
Copy,
|
||||
@@ -148,6 +154,7 @@ pub enum ArmR9Usage {
|
||||
#[inline]
|
||||
const fn default_true() -> bool { true }
|
||||
|
||||
#[wasm_bindgen]
|
||||
#[derive(Debug, Clone, Eq, PartialEq, serde::Deserialize, serde::Serialize)]
|
||||
#[serde(default)]
|
||||
pub struct DiffObjConfig {
|
||||
@@ -200,6 +207,9 @@ impl DiffObjConfig {
|
||||
}
|
||||
}
|
||||
|
||||
#[wasm_bindgen]
|
||||
pub fn default_diff_obj_config() -> DiffObjConfig { Default::default() }
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct ObjSectionDiff {
|
||||
pub symbols: Vec<ObjSymbolDiff>,
|
||||
|
||||
Reference in New Issue
Block a user