wasm/core/
utils.rs

1// TODO Reconsider the importance of this module. All of its functions do the same?
2
3#[cfg(debug_assertions)]
4pub fn print_beautiful_instruction_name_1_byte(first_byte: u8, pc: usize) {
5    use crate::core::reader::types::opcode::opcode_byte_to_str;
6
7    trace!(
8        "Read instruction {} at wasm_binary[{}]",
9        opcode_byte_to_str(first_byte),
10        pc
11    );
12}
13
14#[cfg(debug_assertions)]
15pub fn print_beautiful_fc_extension(second_byte: u32, pc: usize) {
16    use crate::core::reader::types::opcode::fc_extension_opcode_to_str;
17
18    trace!(
19        "Read instruction {} at wasm_binary[{}]",
20        fc_extension_opcode_to_str(second_byte),
21        pc,
22    );
23}
24
25#[cfg(debug_assertions)]
26pub fn print_beautiful_fd_extension(second_byte: u32, pc: usize) {
27    use crate::core::reader::types::opcode::fd_extension_opcode_to_str;
28
29    trace!(
30        "Read instruction {} at wasm_binary[{}]",
31        fd_extension_opcode_to_str(second_byte),
32        pc,
33    );
34}
35
36#[cfg(any(target_pointer_width = "32", target_pointer_width = "64"))]
37pub trait ToUsizeExt {
38    fn into_usize(self) -> usize;
39}
40
41#[cfg(any(target_pointer_width = "32", target_pointer_width = "64"))]
42impl ToUsizeExt for u32 {
43    fn into_usize(self) -> usize {
44        // SAFETY: The current trait only exists for architectures where
45        // pointers are at least 32 bits wide.
46        unsafe { usize::try_from(self).unwrap_unchecked() }
47    }
48}
49
50#[cfg(target_pointer_width = "16")]
51compile_error!("targets with 16 bit wide pointers are currently not supported");