phoron_core 0.4.0

A serializer and deserializer for JVM bytecode
Documentation
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
#[derive(Debug)]
pub enum AttributeInfo {
    SourceFile {
        attribute_name_index: u16,
        attribute_length: u32,
        sourcefile_index: u16,
    },

    ConstantValue {
        attribute_name_index: u16,
        attribute_length: u32,
        constantvalue_index: u16,
    },

    Code {
        attribute_name_index: u16,
        attribute_length: u32,
        max_stack: u16,
        max_locals: u16,
        code_length: u32,
        code: Vec<u8>,
        exception_table_length: u16,
        exception_table: Vec<ExceptionHandler>,
        code_attributes_count: u16,
        code_attributes: Vec<AttributeInfo>,
    },

    Exceptions {
        attribute_name_index: u16,
        attribute_length: u32,
        number_of_exceptions: u16,
        exception_index_table: Vec<u16>,
    },

    LineNumberTable {
        attribute_name_index: u16,
        attribute_length: u32,
        line_number_table_length: u16,
        line_number_table: Vec<LineNumber>,
    },

    LocalVariableTable {
        attribute_name_index: u16,
        attribute_length: u32,
        local_variable_table_length: u16,
        local_variable_table: Vec<LocalVariable>,
    },

    StackMapTable {
        attribute_name_index: u16,
        attribute_length: u32,
        number_of_entries: u16,
        entries: Vec<StackMapFrame>,
    },

    InnerClasses {
        attribute_name_index: u16,
        attribute_length: u32,
        number_of_classes: u16,
        classes: Vec<Class>,
    },

    EnclosingMethod {
        attribute_name_index: u16,
        attribute_length: u32,
        class_index: u16,
        method_index: u16,
    },

    Synthetic {
        attribute_name_index: u16,
        attribute_length: u32,
    },

    Signature {
        attribute_name_index: u16,
        attribute_length: u32,
        signature_index: u16,
    },

    SourceDebugExtension {
        attribute_name_index: u16,
        attribute_length: u32,
        debug_extension: Vec<u8>,
    },

    LocalVariableTypeTable {
        attribute_name_index: u16,
        attribute_length: u32,
        local_variable_type_table_length: u16,
        local_variable_type_table: Vec<LocalVariableType>,
    },

    Deprecated {
        attribute_name_index: u16,
        attribute_length: u32,
    },

    RuntimeVisibleAnnotations {
        attribute_name_index: u16,
        attribute_length: u32,
        num_annotations: u16,
        annotations: Vec<Annotation>,
    },

    RuntimeInvisibleAnnotations {
        attribute_name_index: u16,
        attribute_length: u32,
        num_annotations: u16,
        annotations: Vec<Annotation>,
    },

    RuntimeVisibleParameterAnnotations {
        attribute_name_index: u16,
        attribute_length: u32,
        num_parameters: u8,
        parameter_annotations: Vec<ParameterAnnotation>,
    },

    RuntimeInvisibleParameterAnnotations {
        attribute_name_index: u16,
        attribute_length: u32,
        num_parameters: u8,
        parameter_annotations: Vec<ParameterAnnotation>,
    },

    RuntimeVisibleTypeAnnotations {
        attribute_name_index: u16,
        attribute_length: u32,
        num_annotations: u16,
        annotations: Vec<TypeAnnotation>,
    },

    RuntimeInvisibleTypeAnnotations {
        attribute_name_index: u16,
        attribute_length: u32,
        num_annotations: u16,
        annotations: Vec<TypeAnnotation>,
    },

    AnnotationDefault {
        attribute_name_index: u16,
        attribute_length: u32,
        default_value: ElementValue,
    },

    BootstrapMethods {
        attribute_name_index: u16,
        attribute_length: u32,
        num_bootstrap_methods: u16,
        bootstrap_methods: Vec<BootstrapMethod>,
    },

    MethodParameters {
        attribute_name_index: u16,
        attribute_length: u32,
        parameters_count: u8,
        parameters: Vec<Parameter>,
    },

    Module {
        attribute_name_index: u16,
        attribute_length: u32,
        module_name_index: u16,
        module_flags: u16,
        module_version_index: u16,
        requires_count: u16,
        requires: Vec<Require>,
        exports_count: u16,
        exports: Vec<Export>,
        opens_count: u16,
        opens: Vec<Open>,
        uses_count: u16,
        uses_index: Vec<u16>,
        provides_count: u16,
        provides: Vec<Provide>,
    },

    ModulePackages {
        attribute_name_index: u16,
        attribute_length: u32,
        package_count: u16,
        package_index: Vec<u16>,
    },

    ModuleMainClass {
        attribute_name_index: u16,
        attribute_length: u32,
        main_class_index: u16,
    },

    NestHost {
        attribute_name_index: u16,
        attribute_length: u32,
        host_class_index: u16,
    },

    NestMembers {
        attribute_name_index: u16,
        attribute_length: u32,
        number_of_classes: u16,
        classes: Vec<u16>,
    },

    Record {
        attribute_name_index: u16,
        attribute_length: u32,
        components_count: u16,
        components: Vec<RecordComponentInfo>,
    },

    PermittedSubclasses {
        attribute_name_index: u16,
        attribute_length: u32,
        number_of_classes: u16,
        classes: Vec<u16>,
    },
}

#[derive(Default, Debug)]
pub struct RecordComponentInfo {
    pub name_index: u16,
    pub descriptor_index: u16,
    pub attributes_count: u16,
    pub attributes: Vec<AttributeInfo>,
}

#[derive(Default, Debug)]
pub struct Provide {
    pub provides_index: u16,
    pub provides_with_count: u16,
    pub provides_with_index: Vec<u16>,
}

#[derive(Default, Debug)]
pub struct Open {
    pub opens_index: u16,
    pub opens_flags: u16,
    pub opens_to_count: u16,
    pub opens_to_index: Vec<u16>,
}

#[derive(Default, Debug)]
pub struct Export {
    pub exports_index: u16,
    pub exports_flags: u16,
    pub exports_to_count: u16,
    pub exports_to_index: Vec<u16>,
}

#[derive(Default, Debug)]
pub struct Require {
    pub requires_index: u16,
    pub requires_flags: u16,
    pub requires_version_index: u16,
}

#[derive(Default, Debug)]
pub struct Parameter {
    pub name_index: u16,
    pub access_flags: u16,
}

#[derive(Default, Debug)]
pub struct BootstrapMethod {
    pub bootstrap_method_ref: u16,
    pub num_bootstrap_arguments: u16,
    pub bootstrap_arguments: Vec<u16>,
}

#[derive(Default, Debug)]
pub enum TargetInfo {
    TypeParameterTarget {
        type_parameter_index: u8,
    },
    SuperTypeTarget {
        supertype_index: u16,
    },
    TypeParameterBoundTarget {
        type_parameter_index: u8,
        bound_index: u8,
    },
    #[default]
    EmptyTarget,
    FormalParameterTarget {
        formal_parameter_index: u8,
    },
    ThrowsTarget {
        throws_type_index: u16,
    },
    LocalVarTarget {
        table_length: u16,
        table: Vec<LocalVarEntry>,
    },
    CatchTarget {
        exception_table_index: u16,
    },
    OffsetTarget {
        offset: u16,
    },
    TypeArgumentTarget {
        offset: u16,
        type_argument_index: u8,
    },
}

#[derive(Default, Debug)]
pub struct Path {
    pub type_path_kind: u8,
    pub type_argument_index: u8,
}

#[derive(Default, Debug)]
pub struct TypePath {
    pub path_length: u8,
    pub path: Vec<Path>,
}

#[derive(Default, Debug)]
pub struct LocalVarEntry {
    pub start_pc: u16,
    pub length: u16,
    pub index: u16,
}

#[derive(Default, Debug)]
pub struct TypeAnnotation {
    pub target_type: u8,
    pub target_info: TargetInfo,
    pub target_path: TypePath,
    pub type_index: u16,
    pub num_element_value_pairs: u16,
    pub element_value_pairs: Vec<ElementValuePair>,
}

#[derive(Default, Debug)]
pub struct ParameterAnnotation {
    pub num_annotations: u16,
    pub annotations: Vec<Annotation>,
}

#[derive(Debug)]
pub enum ElementValue {
    ConstValueIndex {
        tag: u8,
        const_value_index: u16,
    },
    ClassInfoIndex {
        tag: u8,
        class_info_index: u16,
    },
    EnumConstValue {
        tag: u8,
        type_name_index: u16,
        const_name_index: u16,
    },
    AnnotationValue {
        tag: u8,
        annotation: Annotation,
    },
    ArrayValue {
        tag: u8,
        num_values: u16,
        values: Vec<ElementValue>,
    },
}

#[derive(Debug)]
pub struct ElementValuePair {
    pub element_name_index: u16,
    pub value: ElementValue,
}

#[derive(Default, Debug)]
pub struct Annotation {
    pub type_index: u16,
    pub num_element_value_pairs: u16,
    pub element_value_pairs: Vec<ElementValuePair>,
}

#[derive(Default, Debug)]
pub struct Class {
    pub inner_class_info_index: u16,
    pub outer_class_info_index: u16,
    pub inner_name_index: u16,
    pub inner_class_access_flags: u16,
}

#[derive(Debug)]
pub enum VerificationTypeInfo {
    TopVariableInfo { tag: u8 },
    IntegerVariableInfo { tag: u8 },
    FloatVariableInfo { tag: u8 },
    NullVariableInfo { tag: u8 },
    UninitializedThisVariableInfo { tag: u8 },
    ObjectVariableInfo { tag: u8, cpool_index: u16 },
    UninitializedVariableInfo { tag: u8, offset: u16 },
    LongVariableInfo { tag: u8 },
    DoubleVariableInfo { tag: u8 },
}

#[derive(Debug)]
pub enum StackMapFrame {
    SameFrame {
        frame_type: u8,
    },
    SameLocals1StackItemFrame {
        frame_type: u8,
        stack: Vec<VerificationTypeInfo>,
    },

    SameLocals1StackItemFrameExtended {
        frame_type: u8,
        offset_delta: u16,
        stack: Vec<VerificationTypeInfo>,
    },

    ChopFrame {
        frame_type: u8,
        offset_delta: u16,
    },

    SameFrameExtended {
        frame_type: u8,
        offset_delta: u16,
    },

    AppendFrame {
        frame_type: u8,
        offset_delta: u16,
        locals: Vec<VerificationTypeInfo>,
    },

    FullFrame {
        frame_type: u8,
        offset_delta: u16,
        number_of_locals: u16,
        locals: Vec<VerificationTypeInfo>,
        number_of_stack_items: u16,
        stack: Vec<VerificationTypeInfo>,
    },
}

#[derive(Default, Debug)]
pub struct LocalVariableType {
    pub start_pc: u16,
    pub length: u16,
    pub name_index: u16,
    pub signature_index: u16,
    pub index: u16,
}

#[derive(Default, Debug)]
pub struct ExceptionHandler {
    pub start_pc: u16,
    pub end_pc: u16,
    pub handler_pc: u16,
    pub catch_type: u16,
}

#[derive(Default, Debug)]
pub struct LineNumber {
    pub start_pc: u16,
    pub line_number: u16,
}

#[derive(Default, Debug)]
pub struct LocalVariable {
    pub start_pc: u16,
    pub length: u16,
    pub name_index: u16,
    pub descriptor_index: u16,
    pub index: u16,
}

pub mod predefined_attributes {
    pub const SOURCE_FILE: &'static str = "SourceFile";
    pub const CONSTANT_VALUE: &'static str = "ConstantValue";
    pub const CODE: &'static str = "Code";
    pub const EXCEPTIONS: &'static str = "Exceptions";
    pub const LINE_NUMBER_TABLE: &'static str = "LineNumberTable";
    pub const LOCAL_VARIABLE_TABLE: &'static str = "LocalVariableTable";
    pub const STACK_MAP_TABLE: &'static str = "StackMapTable";
    pub const INNER_CLASSES: &'static str = "InnerClasses";
    pub const ENCLOSING_METHOD: &'static str = "EnclosingMethod";
    pub const SYNTHETIC: &'static str = "Synthetic";
    pub const SIGNATURE: &'static str = "Signature";
    pub const SOURCE_DEBUG_EXTENSION: &'static str = "SourceDebugExtension";
    pub const LOCAL_VARIABLE_TYPE_TABLE: &'static str = "LocalVariableTypeTable";
    pub const DEPRECATED: &'static str = "Deprecated";
    pub const RUNTIME_VISIBLE_ANNOTATIONS: &'static str = "RuntimeVisibleAnnotations";
    pub const RUNTIME_INVISIBLE_ANNOTATIONS: &'static str = "RuntimeInvisibleAnnotations";
    pub const RUNTIME_VISIBLE_PARAMETER_ANNOTATIONS: &'static str =
        "RuntimeVisibleParameterAnnotations";
    pub const RUNTIME_INVISIBLE_PARAMETER_ANNOTATIONS: &'static str =
        "RuntimeInvisibleParameterAnnotations";
    pub const RUNTIME_VISIBLE_TYPE_ANNOTATIONS: &'static str = "RuntimeVisibleTypeAnnotations";
    pub const RUNTIME_INVISIBLE_TYPE_ANNOTATIONS: &'static str = "RuntimeInvisibleTypeAnnotations";
    pub const ANNOTATION_DEFAULT: &'static str = "AnnotationDefault";
    pub const BOOTSTRAP_METHODS: &'static str = "BootstrapMethods";
    pub const METHOD_PARAMETERS: &'static str = "MethodParameters";
    pub const MODULE: &'static str = "Module";
    pub const MODULE_PACKAGES: &'static str = "ModulePackages";
    pub const MODULE_MAIN_CLASS: &'static str = "ModuleMainClass";
    pub const NEST_HOST: &'static str = "NestHost";
    pub const NEST_MEMBERS: &'static str = "NestMembers";
    pub const RECORD: &'static str = "Record";
    pub const PERMITTED_SUBCLASSES: &'static str = "PermittedSubclasses";
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_default_rercord_component_info() {
        let _rec_comp_info = RecordComponentInfo::default();
    }

    #[test]
    fn test_default_provide() {
        let _provide = Provide::default();
    }

    #[test]
    fn test_default_open() {
        let _open = Open::default();
    }

    #[test]
    fn test_default_export() {
        let _export = Export::default();
    }

    #[test]
    fn test_default_require() {
        let _require = Require::default();
    }

    #[test]
    fn test_default_parameter() {
        let _parameter = Parameter::default();
    }

    #[test]
    fn test_default_bootstrap_method() {
        let _bootstrap_method = BootstrapMethod::default();
    }

    #[test]
    fn test_default_path() {
        let _path = Path::default();
    }

    #[test]
    fn test_default_type_path() {
        let _type_path = TypePath::default();
    }

    #[test]
    fn test_default_local_var_entry() {
        let _local_var_entry = LocalVarEntry::default();
    }

    #[test]
    fn test_default_type_annotation() {
        let _type_annotation = TypeAnnotation::default();
    }

    #[test]
    fn test_default_parameter_annotation() {
        let _parameter_annotation = ParameterAnnotation::default();
    }

    #[test]
    fn test_default_annotation() {
        let _annot = Annotation::default();
    }

    #[test]
    fn test_default_class() {
        let _class = Class::default();
    }

    #[test]
    fn test_default_local_variable_type() {
        let _local_var_type = LocalVariableType::default();
    }

    #[test]
    fn test_default_exception_handler() {
        let _exception_handler = ExceptionHandler::default();
    }

    #[test]
    fn test_default_line_number() {
        let _line_number = LineNumber::default();
    }

    #[test]
    fn test_default_local_variable() {
        let _local_var = LocalVariable::default();
    }
}