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
use std::{
    alloc::Layout,
    collections::HashMap,
    fmt,
    marker::PhantomData,
    mem::{offset_of, size_of},
};

use parse::{
    classfile::ClassFile,
    flags::{ClassFileAccessFlag, ClassFileAccessFlags},
};
use support::encoding::{decode_string, CompactEncoding};

use crate::{
    error::Throwable,
    internal,
    native::{NameAndDescriptor, NativeFunction},
};

use super::{
    layout::{types, ClassFileLayout, FieldInfo},
    mem::{FieldRef, HasObjectHeader, RefTo},
};

#[repr(C)]
#[derive(Debug)]
pub struct Object {
    pub class: RefTo<Class>,
    pub super_class: RefTo<Class>,
    pub ref_count: u64,
}

impl Object {
    pub fn field<T>(&mut self, field: NameAndDescriptor) -> Option<FieldRef<T>> {
        self.ref_count += 1;
        let class = self.class();
        let layout = &class.borrow().layout_for_instances_of_this_class;
        let field_info = layout.field_info(&field.0)?;

        let location = field_info.location;

        Some(FieldRef::new(self, location))
    }

    pub fn class(&self) -> RefTo<Class> {
        self.class.clone()
    }

    pub fn super_class(&self) -> RefTo<Class> {
        self.super_class.clone()
    }

    pub fn ref_count(&self) -> u64 {
        self.ref_count
    }

    pub fn inc_count(&mut self) {
        self.ref_count += 1;
    }
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub enum ArrayPrimitive {
    Bool,
    Char,
    Float,
    Double,
    Byte,
    Short,
    Int,
    Long,
}

impl ArrayPrimitive {
    pub fn from_tag(tag: u8) -> Result<Self, Throwable> {
        Ok(match tag {
            4 => Self::Bool,
            5 => Self::Char,
            6 => Self::Float,
            7 => Self::Double,
            8 => Self::Byte,
            9 => Self::Short,
            10 => Self::Int,
            11 => Self::Long,
            _ => return Err(internal!("unknown array type {}", tag)),
        })
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ArrayType {
    Object(RefTo<Class>),
    Primitive(ArrayPrimitive),
}

#[repr(C)]
pub struct Class {
    object: Object,

    // Java's fields. For now we do not use them, so they will all be null
    cached_constructor: RefTo<Object>,
    class_name: RefTo<Object>,
    module: RefTo<Object>,
    class_loader: RefTo<Object>,
    class_data: RefTo<Object>,
    package_name: RefTo<Object>,
    _component_type: RefTo<Object>,
    reflection_data: RefTo<Object>,
    class_redefined_count: i32,
    generic_info: RefTo<Object>,
    enum_constants: RefTo<Array<RefTo<Object>>>,
    enum_constant_directory: RefTo<Object>,
    annotation_data: RefTo<Object>,
    annotation_type: RefTo<Object>,
    class_value_map: RefTo<Object>,

    // Our extra fields
    name: String,
    component_type: Option<ArrayType>,
    native_methods: HashMap<NameAndDescriptor, NativeFunction>,
    classfile: Option<ClassFile>,
    is_initialised: bool,
    layout_for_instances_of_this_class: ClassFileLayout,
}

impl Class {
    pub fn new(
        object: Object,
        name: String,
        class_file: ClassFile,
        layout: ClassFileLayout,
    ) -> Self {
        Self {
            object,

            cached_constructor: RefTo::null(),
            class_name: RefTo::null(),
            module: RefTo::null(),
            class_loader: RefTo::null(),
            class_data: RefTo::null(),
            package_name: RefTo::null(),
            _component_type: RefTo::null(),
            reflection_data: RefTo::null(),
            class_redefined_count: 0,
            generic_info: RefTo::null(),
            enum_constants: RefTo::null(),
            enum_constant_directory: RefTo::null(),
            annotation_data: RefTo::null(),
            annotation_type: RefTo::null(),
            class_value_map: RefTo::null(),

            component_type: None,
            name,
            native_methods: HashMap::new(),
            classfile: Some(class_file),
            is_initialised: false,
            layout_for_instances_of_this_class: layout,
        }
    }

    pub fn new_primitive(object: Object, name: String, layout: ClassFileLayout) -> Self {
        Self {
            object,

            cached_constructor: RefTo::null(),
            class_name: RefTo::null(),
            module: RefTo::null(),
            class_loader: RefTo::null(),
            class_data: RefTo::null(),
            package_name: RefTo::null(),
            _component_type: RefTo::null(),
            reflection_data: RefTo::null(),
            class_redefined_count: 0,
            generic_info: RefTo::null(),
            enum_constants: RefTo::null(),
            enum_constant_directory: RefTo::null(),
            annotation_data: RefTo::null(),
            annotation_type: RefTo::null(),
            class_value_map: RefTo::null(),

            component_type: None,
            name,
            native_methods: HashMap::new(),
            classfile: None,
            is_initialised: false,
            layout_for_instances_of_this_class: layout,
        }
    }

    pub fn new_array(
        object: Object,
        ty_name: String,
        ty: ArrayType,
        layout: ClassFileLayout,
    ) -> Self {
        Self {
            object,

            cached_constructor: RefTo::null(),
            class_name: RefTo::null(),
            module: RefTo::null(),
            class_loader: RefTo::null(),
            class_data: RefTo::null(),
            package_name: RefTo::null(),
            _component_type: RefTo::null(),
            reflection_data: RefTo::null(),
            class_redefined_count: 0,
            generic_info: RefTo::null(),
            enum_constants: RefTo::null(),
            enum_constant_directory: RefTo::null(),
            annotation_data: RefTo::null(),
            annotation_type: RefTo::null(),
            class_value_map: RefTo::null(),

            component_type: Some(ty),
            name: ty_name,
            native_methods: HashMap::new(),
            classfile: None,
            is_initialised: false,
            layout_for_instances_of_this_class: layout,
        }
    }

    pub fn instance_layout(&self) -> &ClassFileLayout {
        &self.layout_for_instances_of_this_class
    }

    pub fn static_field_info(&self, field: NameAndDescriptor) -> Option<&FieldInfo> {
        self.layout_for_instances_of_this_class
            .static_field_info(&field.0)
    }

    pub fn static_field_info_mut(&mut self, field: NameAndDescriptor) -> Option<&mut FieldInfo> {
        self.layout_for_instances_of_this_class
            .static_field_info_mut(&field.0)
    }

    pub fn native_methods(&self) -> &HashMap<NameAndDescriptor, NativeFunction> {
        &self.native_methods
    }

    pub fn native_methods_mut(&mut self) -> &mut HashMap<NameAndDescriptor, NativeFunction> {
        &mut self.native_methods
    }

    pub fn class_file(&self) -> &ClassFile {
        self.classfile.as_ref().unwrap()
    }

    pub fn name(&self) -> &String {
        &self.name
    }

    pub fn is_initialised(&self) -> bool {
        self.is_initialised
    }

    pub fn set_initialised(&mut self, value: bool) {
        self.is_initialised = value
    }

    pub fn is_assignable_to(&self, _other: &Class) -> bool {
        // warn!("Need to implement is_assignable_to");
        true
    }

    pub fn flags(&self) -> ClassFileAccessFlags {
        self.class_file().access_flags
    }

    pub fn is_interface(&self) -> bool {
        self.flags().has(ClassFileAccessFlag::INTERFACE)
    }

    pub fn is_array(&self) -> bool {
        self.component_type.is_some()
    }

    pub fn is_primitive(&self) -> bool {
        // Not an array and not a class, must be primitive
        self.component_type.is_none() && self.classfile.is_none()
    }

    pub fn super_class(&self) -> RefTo<Class> {
        self.object.super_class.clone()
    }

    pub fn component_type(&self) -> Option<ArrayType> {
        self.component_type.clone()
    }
}

impl fmt::Debug for Class {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("Class")
            .field("object", &self.object)
            .field("name", &self.name)
            .field("is_initialised", &self.is_initialised)
            .finish_non_exhaustive()
    }
}

#[repr(C)]
#[derive(Debug)]
pub struct BuiltinString {
    pub object: Object,

    pub value: RefTo<Array<u8>>,
    pub coder: u8,
    pub hash: i32,
    pub hash_is_zero: i32,
}

impl BuiltinString {
    pub fn string(&self) -> Result<String, Throwable> {
        let encoding = CompactEncoding::from_coder(self.coder);
        let bytes = self.value.borrow().slice().to_vec();

        decode_string((encoding, bytes)).map_err(|f| internal!(f))
    }
}

#[repr(C)]
#[derive(Debug)]
pub struct Array<T> {
    object: Object,

    capacity: usize,
    count: usize,
    phantom: PhantomData<T>,
}

impl<T: Copy> Array<T> {
    pub fn copy_from_slice(ty: ArrayType, ty_name: String, data: &[T]) -> RefTo<Array<T>> {
        let base_layout = Layout::new::<Array<T>>();
        let storage_layout = Layout::array::<T>(data.len()).unwrap();
        let (layout, _) = base_layout.extend(storage_layout).unwrap();

        let array_ref = unsafe {
            let mem = std::alloc::alloc_zeroed(layout).cast::<Array<T>>();
            (*mem).object = Object {
                class: RefTo::new(Class::new_array(
                    Object {
                        class: RefTo::null(),
                        super_class: RefTo::null(),
                        ref_count: 0,
                    },
                    ty_name,
                    ty,
                    ClassFileLayout::from_java_type(types::ARRAY_BASE),
                )),
                super_class: RefTo::null(),
                ref_count: 0,
            };

            (*mem).capacity = data.len();
            (*mem).count = data.len();

            let end_offset = offset_of!(Array<T>, count);
            let count_ptr = mem.byte_add(end_offset + size_of::<usize>()).cast::<T>();
            count_ptr.copy_from(data.as_ptr(), data.len());

            mem.as_mut().unwrap()
        };

        unsafe { RefTo::from_ptr(&mut array_ref.object) }
    }
}

impl<T> Array<T> {
    pub fn from_vec(ty: ArrayType, ty_name: String, data: Vec<T>) -> RefTo<Array<T>> {
        let base_layout = Layout::new::<Array<T>>();
        let storage_layout = Layout::array::<T>(data.len()).unwrap();
        let (layout, _) = base_layout.extend(storage_layout).unwrap();

        let array_ref = unsafe {
            let mem = std::alloc::alloc_zeroed(layout).cast::<Array<T>>();
            (*mem).object = Object {
                class: RefTo::new(Class::new_array(
                    Object {
                        class: RefTo::null(),
                        super_class: RefTo::null(),
                        ref_count: 0,
                    },
                    ty_name,
                    ty,
                    ClassFileLayout::from_java_type(types::ARRAY_BASE),
                )),
                super_class: RefTo::null(),
                ref_count: 0,
            };
            (*mem).capacity = data.len();
            (*mem).count = data.len();

            let end_offset = offset_of!(Array<T>, count);
            let count_ptr = mem.byte_add(end_offset + size_of::<usize>()).cast::<T>();
            count_ptr.copy_from(data.as_ptr(), data.len());

            mem.as_mut().unwrap()
        };

        unsafe { RefTo::from_ptr(&mut array_ref.object) }
    }
}

impl<T> Array<T> {
    pub fn elements_offset() -> usize {
        size_of::<Array<T>>()
    }

    pub fn element_scale() -> usize {
        size_of::<T>()
    }

    pub fn new(object: Object) -> Self {
        Self {
            object,
            count: 0,
            capacity: 0,
            phantom: PhantomData,
        }
    }

    pub fn slice(&self) -> &[T] {
        if self.count == 0 {
            return &[];
        }

        let elements_start = self.data_ptr();
        let len = self.count;

        let slice_ptr = std::ptr::slice_from_raw_parts(elements_start, len);
        unsafe { &*slice_ptr }
    }

    pub fn data_ptr(&self) -> *const T {
        let start = self as *const Array<T>;
        let end_offset = offset_of!(Array<T>, count);

        unsafe { start.byte_add(end_offset + size_of::<usize>()).cast::<T>() }
    }

    pub fn slice_mut(&mut self) -> &mut [T] {
        if self.count == 0 {
            return &mut [];
        }

        let start = self as *mut Array<T>;
        let end_offset = offset_of!(Array<T>, count);
        let elements_start = unsafe { start.byte_add(end_offset + size_of::<usize>()).cast::<T>() };

        let len = self.count;

        let slice_ptr = std::ptr::slice_from_raw_parts_mut(elements_start, len);
        unsafe { &mut *slice_ptr }
    }

    pub fn push(&mut self, value: T) {
        if self.count == self.capacity {
            panic!("out of storage, {} == {}", self.count, self.capacity)
        }

        self.count += 1;

        let slice = self.slice_mut();
        let end = slice.as_mut_ptr_range().end;

        // Write from the end of the buffer by T bytes with the value
        unsafe { end.write(value) };
    }

    pub fn len(&self) -> usize {
        self.count
    }

    pub fn is_empty(&self) -> bool {
        self.len() == 0
    }
}

impl<T> HasObjectHeader<Array<T>> for Array<T> {
    fn header_mut(&mut self) -> &mut Object {
        &mut self.object
    }

    fn header(&self) -> &Object {
        &self.object
    }
}

impl HasObjectHeader<Class> for Class {
    fn header_mut(&mut self) -> &mut Object {
        &mut self.object
    }

    fn header(&self) -> &Object {
        &self.object
    }
}

impl HasObjectHeader<BuiltinString> for BuiltinString {
    fn header_mut(&mut self) -> &mut Object {
        &mut self.object
    }

    fn header(&self) -> &Object {
        &self.object
    }
}

impl HasObjectHeader<Object> for Object {
    fn header_mut(&mut self) -> &mut Object {
        self
    }

    fn header(&self) -> &Object {
        self
    }
}