From 7e758f4d684199c90ec1bb9107908e506bf736cf Mon Sep 17 00:00:00 2001 From: johannst Date: Fri, 20 Dec 2024 22:36:06 +0000 Subject: deploy: 6cc2331c8ff8e7372cd6e9a339250a9d8b58a547 --- src/juicebox_asm/disasm.rs.html | 103 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 103 insertions(+) create mode 100644 src/juicebox_asm/disasm.rs.html (limited to 'src/juicebox_asm/disasm.rs.html') diff --git a/src/juicebox_asm/disasm.rs.html b/src/juicebox_asm/disasm.rs.html new file mode 100644 index 0000000..7532d9a --- /dev/null +++ b/src/juicebox_asm/disasm.rs.html @@ -0,0 +1,103 @@ +disasm.rs - source

juicebox_asm/
disasm.rs

+1
+2
+3
+4
+5
+6
+7
+8
+9
+10
+11
+12
+13
+14
+15
+16
+17
+18
+19
+20
+21
+22
+23
+24
+25
+26
+27
+28
+29
+30
+31
+32
+33
+34
+35
+36
+37
+38
+39
+40
+41
+42
+43
+44
+45
+46
+47
+48
+49
+50
+51
use std::io::{ErrorKind, Write};
+use std::process::{Command, Stdio};
+
+/// Disassemble the code currently added to the runtime, using
+/// [`ndisasm`](https://nasm.us/index.php) and print it to _stdout_. If
+/// `ndisasm` is not available on the system this prints a warning and
+/// becomes a nop.
+///
+/// # Panics
+///
+/// Panics if anything goes wrong with spawning, writing to or reading from
+/// the `ndisasm` child process.
+pub(crate) fn disasm<T: AsRef<[u8]>>(code: T) {
+    let code = code.as_ref();
+
+    // Create ndisasm process, which expects input on stdin.
+    let mut child = match Command::new("ndisasm")
+        .args(["-b64", "-"])
+        .stdin(Stdio::piped())
+        .stdout(Stdio::piped())
+        .spawn()
+    {
+        Ok(child) => child,
+        Err(err) if err.kind() == ErrorKind::NotFound => {
+            println!("disasm: skipping, ndisasm not found");
+            return;
+        }
+        Err(err) => {
+            panic!("{:?}", err);
+        }
+    };
+
+    // Write code to stdin of ndisasm.
+    child
+        .stdin
+        .take()
+        .expect("failed to take stdin")
+        .write_all(code)
+        .expect("failed to write bytes to stdin");
+
+    // Wait for output from ndisasm and print to stdout.
+    println!(
+        "{}",
+        String::from_utf8_lossy(
+            &child
+                .wait_with_output()
+                .expect("failed to get stdout")
+                .stdout
+        )
+    );
+}
+
\ No newline at end of file -- cgit v1.2.3