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
use std::{alloc::Layout, collections::HashMap, fs, path::PathBuf};

use crate::{object::builtins::{ArrayPrimitive, ArrayType}, error::Throwable, internal, internalise};

use super::{
    builtins::{Class, Object},
    layout::{full_layout, types, ClassFileLayout},
    mem::{HasObjectHeader, RefTo},
};
use parse::{classfile::Resolvable, parser::Parser};
use tracing::debug;

pub fn base_layout() -> Layout {
    Layout::new::<Object>()
}

pub struct ClassLoader {
    class_path: Vec<PathBuf>,
    classes: HashMap<String, RefTo<Class>>,
    meta_class: RefTo<Class>,
}

pub struct BootstrappedClasses {
    pub java_lang_class: RefTo<Class>,
    pub java_lang_object: RefTo<Class>,
    pub java_lang_string: RefTo<Class>,
}

impl ClassLoader {
    pub fn new() -> Self {
        Self {
            class_path: vec![],
            classes: HashMap::new(),
            meta_class: RefTo::null(),
        }
    }

    pub fn for_bytes(&mut self, name_key: String, bytes: &[u8]) -> Result<RefTo<Class>, Throwable> {
        let mut parser = Parser::new(bytes);
        let class_file = parser.parse()?;

        let mut super_class: Option<RefTo<Class>> = None;
        if let Some(ref cls) = class_file.super_class {
            let super_class_name = cls.resolve().name.resolve().string();
            super_class = Some(self.for_name(super_class_name)?);
        }

        // Layout for the actual class we are loading
        let mut layout = full_layout(&class_file, base_layout())?;

        // Add all the superclass fields after our own
        {
            let mut _super = super_class.clone();
            while let Some(sup) = &_super {
                // Get the superclass layout
                let super_classfile = sup.borrow().class_file();

                // We need to construct our own instead of just using the one stored by the class
                // because it includes the header, which is not relevant for inherited fields.
                let mut super_layout = full_layout(super_classfile, Layout::new::<()>())?;

                // Extend our layout based on it
                let (mut our_new_layout, offset_from_base) =
                    layout.layout().extend(super_layout.layout()).map_err(internalise!())?;

                // Align the new layout
                our_new_layout = our_new_layout.pad_to_align();

                // Adjust the offset of the superclass fields to be based on the new offsets
                super_layout.field_info.iter_mut().for_each(|(_, field)| {
                    field.location.offset += offset_from_base;
                });

                // Push superclass fields into our layout
                layout.field_info.extend(super_layout.field_info);

                // Assign our layout to the newly computed one
                layout.layout = our_new_layout;

                let next_super = sup.borrow().super_class();
                if !next_super.is_null() {
                    _super = Some(next_super);
                } else {
                    _super = None;
                }
            }
        }

        let cls = Class::new(
            Object {
                super_class: super_class.unwrap_or(RefTo::null()),
                // Layout for the meta class is stored in here.
                // This will be set to the real meta class after bootstrap
                class: self.meta_class.clone(),
                ref_count: 0,
            },
            name_key.clone(),
            class_file,
            layout,
        );

        let object = RefTo::new(cls);

        self.classes.insert(name_key, object.clone());

        Ok(object)
    }

    pub fn for_name(&mut self, name: String) -> Result<RefTo<Class>, Throwable> {
        let formatted_name = format!("{}.class", name);

        if let Some(object) = self.classes.get(&name) {
            debug!("Fast path: {} ({})", name, formatted_name);
            return Ok(object.clone());
        }

        debug!("Slow path: {} ({})", &name, &formatted_name);

        let found_path = self.resolve_name(formatted_name.clone());
        if let Some(path) = found_path {
            let bytes = fs::read(path).map_err(internalise!())?;
            return self.for_bytes(name, &bytes);
        }

        Err(internal!("Could not locate classfile {}", formatted_name))
    }

    fn resolve_name(&self, name: String) -> Option<PathBuf> {
        let mut found_path: Option<PathBuf> = None;

        for root in self.class_path.iter() {
            let path = root.join::<PathBuf>(name.clone().into());
            if path.exists() {
                found_path = Some(path);
                break;
            }
        }

        found_path
    }

    pub fn classes(&self) -> &HashMap<String, RefTo<Class>> {
        &self.classes
    }

    pub fn add_path(&mut self, path: impl Into<PathBuf>) -> &mut Self {
        self.class_path.push(path.into());
        self
    }

    pub fn bootstrap(&mut self) -> Result<BootstrappedClasses, Throwable> {
        let jlc = self.for_name("java/lang/Class".to_string())?;
        self.meta_class = jlc.clone();

        let jlo = self.for_name("java/lang/Object".to_string())?;
        let jls = self.for_name("java/lang/String".to_string())?;

        macro_rules! primitive {
            ($layout_ty: ident, $array_ty: ident,  $name: expr) => {{
                let prim = RefTo::new(Class::new_primitive(
                    Object {
                        class: jlc.clone(),
                        super_class: jlo.clone(),
                        ref_count: 0,
                    },
                    $name.to_string(),
                    ClassFileLayout::from_java_type(types::$layout_ty)
                ));
                let array = RefTo::new(Class::new_array(
                    Object {
                        class: prim.clone(),
                        super_class: RefTo::null(),
                        ref_count: 0,
                    },
                    format!("[{}", $name.to_string()),
                    ArrayType::Primitive(ArrayPrimitive::$array_ty),
                    ClassFileLayout::from_java_type(types::$layout_ty)
                ));

                (prim, array)
            }};
        }

        macro_rules! insert {
            ($self: expr, $tup: expr, $ty: expr) => {
                self.classes.insert($ty.to_string(), $tup.0);
                self.classes.insert(format!("[{}", $ty.to_string()), $tup.1);
            };
        }

        // Primitives
        let bool = primitive!(BOOL, Bool, "Z");
        insert!(self, bool, "Z");

        let byte = primitive!(BYTE, Byte, "B");
        insert!(self, byte, "B");

        let short = primitive!(SHORT, Short, "S");
        insert!(self, short, "S");

        let char = primitive!(CHAR, Char, "C");
        insert!(self, char.clone(), "C");
        let char_2d_array = RefTo::new(Class::new_array(
            Object {
                class: char.0.clone(),
                super_class: RefTo::null(),
                ref_count: 0,
            },
            "[[C".to_string(),
            ArrayType::Primitive(ArrayPrimitive::Char),
            ClassFileLayout::from_java_type(types::CHAR)
        ));

        self.classes.insert("[[C".to_string(), char_2d_array);

        let int = primitive!(INT, Int, "I");
        insert!(self, int, "I");

        let long = primitive!(LONG, Long, "J");
        insert!(self, long, "J");

        let float = primitive!(FLOAT, Float, "F");
        insert!(self, float, "F");

        let double = primitive!(DOUBLE, Double, "D");
        insert!(self, double, "D");

        let jlo_array = RefTo::new(Class::new_array(
            Object {
                class: jlo.clone(),
                super_class: RefTo::null(),
                ref_count: 0,
            },
            "[Ljava/lang/Object;".to_string(),
            ArrayType::Object(jlo.clone()),
            ClassFileLayout::from_java_type(types::ARRAY_BASE)
        ));

        self.classes.insert("[Ljava/lang/Object;".to_string(), jlo_array);
        
        {
            let cls = self.for_name("java/util/concurrent/ConcurrentHashMap$Segment".to_string())?;
            let arr = RefTo::new(Class::new_array(
                Object {
                    super_class: cls.borrow().super_class(),
                    class: cls,
                    ref_count: 0,
                },
                "[Ljava/util/concurrent/ConcurrentHashMap$Segment;".to_string(),
                ArrayType::Object(jlo.clone()),
                ClassFileLayout::from_java_type(types::ARRAY_BASE)
            ));
    
            self.classes.insert("[Ljava/util/concurrent/ConcurrentHashMap$Segment;".to_string(), arr);
        }

        {
            let cls = self.for_name("java/util/concurrent/ConcurrentHashMap$Node".to_string())?;
            let arr = RefTo::new(Class::new_array(
                Object {
                    super_class: cls.borrow().super_class(),
                    class: cls,
                    ref_count: 0,
                },
                "[Ljava/util/concurrent/ConcurrentHashMap$Node;".to_string(),
                ArrayType::Object(jlo.clone()),
                ClassFileLayout::from_java_type(types::ARRAY_BASE)
            ));
    
            self.classes.insert("[Ljava/util/concurrent/ConcurrentHashMap$Node;".to_string(), arr);
        }

        {
            let cls = jls.clone();
            let arr = RefTo::new(Class::new_array(
                Object {
                    super_class: cls.borrow().super_class(),
                    class: cls,
                    ref_count: 0,
                },
                "[Ljava/lang/String;".to_string(),
                ArrayType::Object(jlo.clone()),
                ClassFileLayout::from_java_type(types::ARRAY_BASE)
            ));
    
            self.classes.insert("[Ljava/lang/String;".to_string(), arr);
        }
        

        self.classes.iter_mut().for_each(|(_, value)| {
            value.borrow_mut().header_mut().class = self.meta_class.clone();
        });

        Ok(BootstrappedClasses {
            java_lang_class: jlc,
            java_lang_object: jlo,
            java_lang_string: jls,
        })
    }
}