@@ -21,6 +21,13 @@ const NEW: u8 = 187;
21
21
const ATHROW : u8 = 191 ;
22
22
const CHECKCAST : u8 = 192 ;
23
23
24
+ /// A JVM bytecode instruction.
25
+ ///
26
+ /// For more detailed information on the different types of bytecode
27
+ /// instructions, see the following section of the Java Virtual Machine
28
+ /// Specification.
29
+ ///
30
+ /// https://docs.oracle.com/javase/specs/jvms/se11/html/jvms-6.html
24
31
#[ derive( Debug ) ]
25
32
#[ derive( PartialEq ) ]
26
33
pub enum Bytecode {
@@ -49,6 +56,27 @@ pub enum Bytecode {
49
56
}
50
57
51
58
impl Bytecode {
59
+ /// Converts the given slice of bytes into the bytecode instructions that
60
+ /// they represent.
61
+ ///
62
+ /// ```
63
+ /// # use jvm_class_file_parser::Bytecode;
64
+ /// # use jvm_class_file_parser::Bytecode::*;
65
+ /// #
66
+ /// let bytes = vec![
67
+ /// 42,
68
+ /// 183, 0, 1,
69
+ /// 177,
70
+ /// ];
71
+ ///
72
+ /// let bytecodes = vec![
73
+ /// (0, Aload_0),
74
+ /// (1, Invokespecial(1)),
75
+ /// (4, Return),
76
+ /// ];
77
+ ///
78
+ /// assert_eq!(bytecodes, Bytecode::from_bytes(&bytes));
79
+ /// ```
52
80
pub fn from_bytes ( bytes : & [ u8 ] ) -> Vec < ( usize , Bytecode ) > {
53
81
use Bytecode :: * ;
54
82
@@ -222,6 +250,17 @@ impl Bytecode {
222
250
bytecode
223
251
}
224
252
253
+ /// Converts the bytecode into a String representation.
254
+ ///
255
+ /// Takes in the index of the instruction so that it can be used to display
256
+ /// bytecode instructions that contain an instruction offset.
257
+ ///
258
+ /// ```
259
+ /// # use jvm_class_file_parser::Bytecode::*;
260
+ /// #
261
+ /// assert_eq!("aconst_null", Aconst_null.to_string(2));
262
+ /// assert_eq!("ifeq 15", Ifeq(5).to_string(10));
263
+ /// ```
225
264
pub fn to_string ( & self , index : u16 ) -> String {
226
265
use Bytecode :: * ;
227
266
0 commit comments