rust-asm 0.2.1

ObjectWeb ASM implementation in Rust
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
use crate::insn::Handle;
use std::collections::HashMap;

/// A builder for the constant pool of a class.
///
/// This struct manages the deduplication of constant pool entries, ensuring that
/// strings, classes, and member references are stored efficiently.
#[derive(Debug, Default)]
pub struct ConstantPoolBuilder {
    cp: Vec<CpInfo>,
    utf8: HashMap<String, u16>,
    class: HashMap<String, u16>,
    module: HashMap<String, u16>,
    package: HashMap<String, u16>,
    string: HashMap<String, u16>,
    name_and_type: HashMap<(String, String), u16>,
    field_ref: HashMap<(String, String, String), u16>,
    method_ref: HashMap<(String, String, String), u16>,
    interface_method_ref: HashMap<(String, String, String), u16>,
    method_type: HashMap<String, u16>,
    method_handle: HashMap<(u8, String, String, String, bool), u16>,
    invoke_dynamic: HashMap<(u16, String, String), u16>,
}

impl ConstantPoolBuilder {
    /// Creates a new, empty `ConstantPoolBuilder`.
    ///
    /// The constant pool starts with a dummy entry at index 0, as per JVM spec.
    pub fn new() -> Self {
        Self {
            cp: vec![CpInfo::Unusable],
            ..Default::default()
        }
    }

    /// Creates a `ConstantPoolBuilder` pre-populated with an existing pool.
    ///
    /// This preserves existing indices and initializes deduplication maps
    /// based on the pool contents.
    pub fn from_pool(pool: Vec<CpInfo>) -> Self {
        let cp = if pool.is_empty() {
            vec![CpInfo::Unusable]
        } else {
            pool
        };
        let mut builder = Self {
            cp,
            ..Default::default()
        };

        fn cp_utf8(cp: &[CpInfo], index: u16) -> Option<&str> {
            match cp.get(index as usize) {
                Some(CpInfo::Utf8(value)) => Some(value.as_str()),
                _ => None,
            }
        }

        fn cp_class_name(cp: &[CpInfo], index: u16) -> Option<&str> {
            match cp.get(index as usize) {
                Some(CpInfo::Class { name_index }) => cp_utf8(cp, *name_index),
                _ => None,
            }
        }

        fn cp_module_name(cp: &[CpInfo], index: u16) -> Option<&str> {
            match cp.get(index as usize) {
                Some(CpInfo::Module { name_index }) => cp_utf8(cp, *name_index),
                _ => None,
            }
        }

        fn cp_package_name(cp: &[CpInfo], index: u16) -> Option<&str> {
            match cp.get(index as usize) {
                Some(CpInfo::Package { name_index }) => cp_utf8(cp, *name_index),
                _ => None,
            }
        }

        fn cp_name_and_type(cp: &[CpInfo], index: u16) -> Option<(&str, &str)> {
            match cp.get(index as usize) {
                Some(CpInfo::NameAndType {
                    name_index,
                    descriptor_index,
                }) => {
                    let name = cp_utf8(cp, *name_index)?;
                    let desc = cp_utf8(cp, *descriptor_index)?;
                    Some((name, desc))
                }
                _ => None,
            }
        }

        fn cp_member_ref(cp: &[CpInfo], index: u16) -> Option<(String, String, String, bool)> {
            match cp.get(index as usize) {
                Some(CpInfo::Fieldref {
                    class_index,
                    name_and_type_index,
                }) => {
                    let owner = cp_class_name(cp, *class_index)?.to_string();
                    let (name, desc) = cp_name_and_type(cp, *name_and_type_index)?;
                    Some((owner, name.to_string(), desc.to_string(), false))
                }
                Some(CpInfo::Methodref {
                    class_index,
                    name_and_type_index,
                }) => {
                    let owner = cp_class_name(cp, *class_index)?.to_string();
                    let (name, desc) = cp_name_and_type(cp, *name_and_type_index)?;
                    Some((owner, name.to_string(), desc.to_string(), false))
                }
                Some(CpInfo::InterfaceMethodref {
                    class_index,
                    name_and_type_index,
                }) => {
                    let owner = cp_class_name(cp, *class_index)?.to_string();
                    let (name, desc) = cp_name_and_type(cp, *name_and_type_index)?;
                    Some((owner, name.to_string(), desc.to_string(), true))
                }
                _ => None,
            }
        }

        for (index, entry) in builder.cp.iter().enumerate() {
            let index = index as u16;
            match entry {
                CpInfo::Utf8(value) => {
                    builder.utf8.entry(value.clone()).or_insert(index);
                }
                CpInfo::Class { name_index } => {
                    if let Some(name) = cp_utf8(&builder.cp, *name_index) {
                        builder.class.entry(name.to_string()).or_insert(index);
                    }
                }
                CpInfo::Module { .. } => {
                    if let Some(name) = cp_module_name(&builder.cp, index) {
                        builder.module.entry(name.to_string()).or_insert(index);
                    }
                }
                CpInfo::Package { .. } => {
                    if let Some(name) = cp_package_name(&builder.cp, index) {
                        builder.package.entry(name.to_string()).or_insert(index);
                    }
                }
                CpInfo::String { string_index } => {
                    if let Some(value) = cp_utf8(&builder.cp, *string_index) {
                        builder.string.entry(value.to_string()).or_insert(index);
                    }
                }
                CpInfo::NameAndType {
                    name_index,
                    descriptor_index,
                } => {
                    if let (Some(name), Some(desc)) = (
                        cp_utf8(&builder.cp, *name_index),
                        cp_utf8(&builder.cp, *descriptor_index),
                    ) {
                        builder
                            .name_and_type
                            .entry((name.to_string(), desc.to_string()))
                            .or_insert(index);
                    }
                }
                CpInfo::Fieldref {
                    class_index,
                    name_and_type_index,
                } => {
                    if let (Some(owner), Some((name, desc))) = (
                        cp_class_name(&builder.cp, *class_index),
                        cp_name_and_type(&builder.cp, *name_and_type_index),
                    ) {
                        builder
                            .field_ref
                            .entry((owner.to_string(), name.to_string(), desc.to_string()))
                            .or_insert(index);
                    }
                }
                CpInfo::Methodref {
                    class_index,
                    name_and_type_index,
                } => {
                    if let (Some(owner), Some((name, desc))) = (
                        cp_class_name(&builder.cp, *class_index),
                        cp_name_and_type(&builder.cp, *name_and_type_index),
                    ) {
                        builder
                            .method_ref
                            .entry((owner.to_string(), name.to_string(), desc.to_string()))
                            .or_insert(index);
                    }
                }
                CpInfo::InterfaceMethodref {
                    class_index,
                    name_and_type_index,
                } => {
                    if let (Some(owner), Some((name, desc))) = (
                        cp_class_name(&builder.cp, *class_index),
                        cp_name_and_type(&builder.cp, *name_and_type_index),
                    ) {
                        builder
                            .interface_method_ref
                            .entry((owner.to_string(), name.to_string(), desc.to_string()))
                            .or_insert(index);
                    }
                }
                CpInfo::MethodType { descriptor_index } => {
                    if let Some(desc) = cp_utf8(&builder.cp, *descriptor_index) {
                        builder.method_type.entry(desc.to_string()).or_insert(index);
                    }
                }
                CpInfo::MethodHandle {
                    reference_kind,
                    reference_index,
                } => {
                    if let Some((owner, name, desc, is_interface)) =
                        cp_member_ref(&builder.cp, *reference_index)
                    {
                        builder
                            .method_handle
                            .entry((*reference_kind, owner, name, desc, is_interface))
                            .or_insert(index);
                    }
                }
                CpInfo::InvokeDynamic {
                    bootstrap_method_attr_index,
                    name_and_type_index,
                } => {
                    if let Some((name, desc)) = cp_name_and_type(&builder.cp, *name_and_type_index)
                    {
                        builder
                            .invoke_dynamic
                            .entry((
                                *bootstrap_method_attr_index,
                                name.to_string(),
                                desc.to_string(),
                            ))
                            .or_insert(index);
                    }
                }
                _ => {}
            }
        }

        builder
    }

    /// Consumes the builder and returns the raw vector of `CpInfo` entries.
    pub fn into_pool(self) -> Vec<CpInfo> {
        self.cp
    }

    /// Adds a UTF-8 string to the constant pool if it doesn't exist.
    ///
    /// Returns the index of the entry.
    pub fn utf8(&mut self, value: &str) -> u16 {
        if let Some(index) = self.utf8.get(value) {
            return *index;
        }
        let index = self.push(CpInfo::Utf8(value.to_string()));
        self.utf8.insert(value.to_string(), index);
        index
    }

    /// Adds a Class constant to the pool.
    ///
    /// This will recursively add the UTF-8 name of the class.
    pub fn class(&mut self, name: &str) -> u16 {
        if let Some(index) = self.class.get(name) {
            return *index;
        }
        let name_index = self.utf8(name);
        let index = self.push(CpInfo::Class { name_index });
        self.class.insert(name.to_string(), index);
        index
    }

    /// Adds a Module constant to the pool.
    ///
    /// The name is the module name string stored in `CONSTANT_Utf8_info`.
    pub fn module(&mut self, name: &str) -> u16 {
        if let Some(index) = self.module.get(name) {
            return *index;
        }
        let name_index = self.utf8(name);
        let index = self.push(CpInfo::Module { name_index });
        self.module.insert(name.to_string(), index);
        index
    }

    /// Adds a Package constant to the pool.
    ///
    /// The name uses JVM package format such as `java/lang`.
    pub fn package(&mut self, name: &str) -> u16 {
        if let Some(index) = self.package.get(name) {
            return *index;
        }
        let name_index = self.utf8(name);
        let index = self.push(CpInfo::Package { name_index });
        self.package.insert(name.to_string(), index);
        index
    }

    /// Adds a String constant to the pool.
    ///
    /// This is for string literals (e.g., `ldc "foo"`).
    pub fn string(&mut self, value: &str) -> u16 {
        if let Some(index) = self.string.get(value) {
            return *index;
        }
        let string_index = self.utf8(value);
        let index = self.push(CpInfo::String { string_index });
        self.string.insert(value.to_string(), index);
        index
    }

    pub fn integer(&mut self, value: i32) -> u16 {
        self.push(CpInfo::Integer(value))
    }

    pub fn float(&mut self, value: f32) -> u16 {
        self.push(CpInfo::Float(value))
    }

    pub fn long(&mut self, value: i64) -> u16 {
        let index = self.push(CpInfo::Long(value));
        // Long takes two entries
        self.cp.push(CpInfo::Unusable);
        index
    }

    pub fn double(&mut self, value: f64) -> u16 {
        let index = self.push(CpInfo::Double(value));
        // Double takes two entries
        self.cp.push(CpInfo::Unusable);
        index
    }

    /// Adds a NameAndType constant to the pool.
    ///
    /// Used for field and method descriptors.
    pub fn name_and_type(&mut self, name: &str, descriptor: &str) -> u16 {
        let key = (name.to_string(), descriptor.to_string());
        if let Some(index) = self.name_and_type.get(&key) {
            return *index;
        }
        let name_index = self.utf8(name);
        let descriptor_index = self.utf8(descriptor);
        let index = self.push(CpInfo::NameAndType {
            name_index,
            descriptor_index,
        });
        self.name_and_type.insert(key, index);
        index
    }

    /// Adds a Fieldref constant to the pool.
    pub fn field_ref(&mut self, owner: &str, name: &str, descriptor: &str) -> u16 {
        let key = (owner.to_string(), name.to_string(), descriptor.to_string());
        if let Some(index) = self.field_ref.get(&key) {
            return *index;
        }
        let class_index = self.class(owner);
        let name_and_type_index = self.name_and_type(name, descriptor);
        let index = self.push(CpInfo::Fieldref {
            class_index,
            name_and_type_index,
        });
        self.field_ref.insert(key, index);
        index
    }

    /// Adds a Methodref constant to the pool.
    pub fn method_ref(&mut self, owner: &str, name: &str, descriptor: &str) -> u16 {
        let key = (owner.to_string(), name.to_string(), descriptor.to_string());
        if let Some(index) = self.method_ref.get(&key) {
            return *index;
        }
        let class_index = self.class(owner);
        let name_and_type_index = self.name_and_type(name, descriptor);
        let index = self.push(CpInfo::Methodref {
            class_index,
            name_and_type_index,
        });
        self.method_ref.insert(key, index);
        index
    }

    pub fn interface_method_ref(&mut self, owner: &str, name: &str, descriptor: &str) -> u16 {
        let key = (owner.to_string(), name.to_string(), descriptor.to_string());
        if let Some(index) = self.interface_method_ref.get(&key) {
            return *index;
        }
        let class_index = self.class(owner);
        let name_and_type_index = self.name_and_type(name, descriptor);
        let index = self.push(CpInfo::InterfaceMethodref {
            class_index,
            name_and_type_index,
        });
        self.interface_method_ref.insert(key, index);
        index
    }

    pub fn method_type(&mut self, descriptor: &str) -> u16 {
        if let Some(index) = self.method_type.get(descriptor) {
            return *index;
        }
        let descriptor_index = self.utf8(descriptor);
        let index = self.push(CpInfo::MethodType { descriptor_index });
        self.method_type.insert(descriptor.to_string(), index);
        index
    }

    pub fn method_handle(&mut self, handle: &Handle) -> u16 {
        let key = (
            handle.reference_kind,
            handle.owner.clone(),
            handle.name.clone(),
            handle.descriptor.clone(),
            handle.is_interface,
        );
        if let Some(index) = self.method_handle.get(&key) {
            return *index;
        }
        let reference_index = match handle.reference_kind {
            1..=4 => self.field_ref(&handle.owner, &handle.name, &handle.descriptor),
            9 => self.interface_method_ref(&handle.owner, &handle.name, &handle.descriptor),
            _ => self.method_ref(&handle.owner, &handle.name, &handle.descriptor),
        };
        let index = self.push(CpInfo::MethodHandle {
            reference_kind: handle.reference_kind,
            reference_index,
        });
        self.method_handle.insert(key, index);
        index
    }

    pub fn invoke_dynamic(&mut self, bsm_index: u16, name: &str, descriptor: &str) -> u16 {
        let key = (bsm_index, name.to_string(), descriptor.to_string());
        if let Some(index) = self.invoke_dynamic.get(&key) {
            return *index;
        }
        let name_and_type_index = self.name_and_type(name, descriptor);
        let index = self.push(CpInfo::InvokeDynamic {
            bootstrap_method_attr_index: bsm_index,
            name_and_type_index,
        });
        self.invoke_dynamic.insert(key, index);
        index
    }

    fn push(&mut self, entry: CpInfo) -> u16 {
        self.cp.push(entry);
        (self.cp.len() - 1) as u16
    }
}
#[derive(Debug, Clone)]
pub enum CpInfo {
    Unusable,
    Utf8(String),
    Integer(i32),
    Float(f32),
    Long(i64),
    Double(f64),
    Class {
        name_index: u16,
    },
    String {
        string_index: u16,
    },
    Fieldref {
        class_index: u16,
        name_and_type_index: u16,
    },
    Methodref {
        class_index: u16,
        name_and_type_index: u16,
    },
    InterfaceMethodref {
        class_index: u16,
        name_and_type_index: u16,
    },
    NameAndType {
        name_index: u16,
        descriptor_index: u16,
    },
    MethodHandle {
        reference_kind: u8,
        reference_index: u16,
    },
    MethodType {
        descriptor_index: u16,
    },
    Dynamic {
        bootstrap_method_attr_index: u16,
        name_and_type_index: u16,
    },
    InvokeDynamic {
        bootstrap_method_attr_index: u16,
        name_and_type_index: u16,
    },
    Module {
        name_index: u16,
    },
    Package {
        name_index: u16,
    },
}

impl CpInfo {
    pub fn as_int(&self) -> Option<i32> {
        match self {
            Self::Integer(v) => Some(*v),
            _ => None,
        }
    }

    pub fn as_float(&self) -> Option<f32> {
        match self {
            Self::Float(v) => Some(*v),
            _ => None,
        }
    }

    pub fn as_long(&self) -> Option<i64> {
        match self {
            Self::Long(v) => Some(*v),
            _ => None,
        }
    }

    pub fn as_double(&self) -> Option<f64> {
        match self {
            Self::Double(v) => Some(*v),
            _ => None,
        }
    }

    pub fn as_utf8(&self) -> Option<&str> {
        match self {
            Self::Utf8(s) => Some(s.as_str()),
            _ => None,
        }
    }
}

pub trait ConstantPoolExt {
    fn resolve_utf8(&self, index: u16) -> Option<&str>;
    fn get_int(&self, index: u16) -> Option<i32>;
    fn get_long(&self, index: u16) -> Option<i64>;
    fn get_float(&self, index: u16) -> Option<f32>;
    fn get_double(&self, index: u16) -> Option<f64>;
}

impl ConstantPoolExt for [CpInfo] {
    fn resolve_utf8(&self, index: u16) -> Option<&str> {
        self.get(index as usize).and_then(|entry| entry.as_utf8())
    }

    fn get_int(&self, index: u16) -> Option<i32> {
        self.get(index as usize).and_then(|entry| entry.as_int())
    }

    fn get_long(&self, index: u16) -> Option<i64> {
        self.get(index as usize).and_then(|entry| entry.as_long())
    }

    fn get_float(&self, index: u16) -> Option<f32> {
        self.get(index as usize).and_then(|entry| entry.as_float())
    }

    fn get_double(&self, index: u16) -> Option<f64> {
        self.get(index as usize).and_then(|entry| entry.as_double())
    }
}