-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathruntime.go
1151 lines (910 loc) · 22.1 KB
/
runtime.go
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
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package main
import "fmt"
type Runtime struct {
resources *Resources
}
func CreateRuntime(resources map[string]ClassFile) Runtime {
return Runtime{
resources: &Resources{
classes: resources,
},
}
}
func (r *Runtime) AddResource(name string, class ClassFile) {
r.resources.classes[name] = class
}
func (r *Runtime) AddResources(resources map[string]ClassFile) {
for name, class := range resources {
r.resources.classes[name] = class
}
}
func (r *Runtime) GetResource(name string) ClassFile {
if resource, ok := r.resources.classes[name]; ok {
return resource
}
panic(fmt.Sprintf("Unknown class %s", name))
}
func (r *Runtime) GetResources() map[string]ClassFile {
return r.resources.classes
}
func (env *JEnv) createClassLoader(parent *ClassLoader) *ClassLoader {
cl := ClassLoader{
parent: parent,
}
env.classLoaders = append(env.classLoaders, &cl)
if parent != nil {
env.rootClassLoader = &cl
}
return &cl
}
func (r *Runtime) makeEnv() *JEnv {
return &JEnv{
runtime: r,
frame: &Frame{
/* root frame */
},
}
}
func (r *Runtime) Run(class string) {
env := r.makeEnv()
classLoader := env.createClassLoader(nil)
klass := classLoader.findOrLoadClass(env, class)
klass.FindMethod("main", "([Ljava/lang/String;)V").Invoke(env, klass, nil, []interface{}{})
}
func (c *RuntimeClass) FindMethod(name, descriptor string) *Method {
for _, method := range c.methods {
if method.name == name && method.rawDescriptor == descriptor {
return method
}
}
return nil
}
func (jmo *JMetaObject) init(env *JEnv, cdesc string /* default ()V */, args []interface{}) {
jmo.class.FindMethod("<init>", cdesc).Invoke(env, jmo.class, jmo, args)
}
func (m *Method) Invoke(env *JEnv, klass RuntimeClass, this *JMetaObject, args []interface{}) bool {
if m == nil {
// this method doesn't exist, tf you're tryna do?
return false // fail
}
// TODO call <clinit>
if this != nil && !this.initialized {
this.initialized = true
//klass.FindMethod("<init>", "()V").Invoke(env, klass, this, []interface{}{})
this.init(env, "()V", []interface{}{})
}
env.frame = &Frame{
stack: StackOf(m.maxStack),
root: env.frame,
classLoader: klass.classLoader,
}
RunCode(m.code, putLocals(m.locals, this, args), env, this, klass)
return true // success!
}
func putLocals(locals []RuntimeLocalVariable, this *JMetaObject, args []interface{}) []RuntimeLocalVariable {
locals[0].obj = this
for i := 1; i < len(args); i++ {
locals[i].obj = args[i-1]
}
return locals
}
type RuntimeLocalVariable struct {
name string
slot int
obj interface{}
}
type JEnv struct {
runtime *Runtime
classLoaders []*ClassLoader
rootClassLoader *ClassLoader
frame *Frame
}
type Resources struct {
classes map[string]ClassFile
}
type Frame struct {
stack *Stack
root *Frame
classLoader *ClassLoader
pc uint16
tpc uint16
}
type Stack struct {
values []interface{}
resizable bool
maxSize int
}
func CreateStack() *Stack {
return ResizableStack()
}
func StackOf(size int) *Stack {
return &Stack{
values: make([]interface{}, size),
resizable: false,
maxSize: size,
}
}
func ResizableStack() *Stack {
return &Stack{
values: make([]interface{}, 0),
resizable: true,
maxSize: 0,
}
}
func (s *Stack) ready(n int) {
if s.resizable {
if n > s.maxSize {
panic("Stack overflow")
}
} else {
if n > len(s.values)+1 { // no idea if this actually works
panic("Stack overflow")
}
}
}
func (s *Stack) check(n int) {
if n >= len(s.values) {
panic("Stack underflow")
}
}
func (s *Stack) PopRef() (o interface{}) {
s.check(1)
o = s.values[len(s.values)-1]
s.values = s.values[:len(s.values)-1]
return
}
func (s *Stack) PopArray() JArray {
return s.PopRef().(JArray)
}
func (s *Stack) PopInt() PrimitiveInt {
return s.PopRef().(JPrimitive).primitive.(PrimitiveInt)
}
func (s *Stack) PopByte() PrimitiveByte {
return s.PopRef().(JPrimitive).primitive.(PrimitiveByte)
}
func (s *Stack) PopFloat() PrimitiveFloat {
return s.PopRef().(JPrimitive).primitive.(PrimitiveFloat)
}
func (s *Stack) PopDouble() PrimitiveDouble {
return s.PopRef().(JPrimitive).primitive.(PrimitiveDouble)
}
func (s *Stack) PopLong() PrimitiveLong {
return s.PopRef().(JPrimitive).primitive.(PrimitiveLong)
}
func (s *Stack) PushRef(o interface{}) {
s.ready(1)
s.values = append(s.values, o)
}
func (s *Stack) PushInt(i PrimitiveInt) {
s.PushRef(NewPrimitiveInt(i))
}
func (s *Stack) PushByte(b PrimitiveByte) {
s.PushRef(NewPrimitiveByte(b))
}
func (s *Stack) PushFloat(f PrimitiveFloat) {
s.PushRef(NewPrimitiveFloat(f))
}
func (s *Stack) PushDouble(d PrimitiveDouble) {
s.PushRef(NewPrimitiveDouble(d))
}
func (s *Stack) PushLong(l PrimitiveLong) {
s.PushRef(NewPrimitiveLong(l))
}
func (s *Stack) PushShort(p PrimitiveShort) {
s.PushRef(NewPrimitiveShort(p))
}
func (s *Stack) PushChar(p PrimitiveChar) {
s.PushRef(NewPrimitiveChar(p))
}
func NewPrimitiveInt(i PrimitiveInt) interface{} {
return JPrimitive{
primitive: i,
}
}
func NewPrimitiveIntD(i int32) interface{} {
return JPrimitive{
primitive: PrimitiveInt{
value: i,
},
}
}
func NewPrimitiveIntRD(i int32) PrimitiveInt {
return PrimitiveInt{
value: i,
}
}
func NewPrimitiveByte(b PrimitiveByte) interface{} {
return JPrimitive{
primitive: b,
}
}
func NewPrimitiveByteD(b byte) interface{} {
return JPrimitive{
primitive: PrimitiveByte{
value: b,
},
}
}
func NewPrimitiveByteRD(b byte) PrimitiveByte {
return PrimitiveByte{
value: b,
}
}
func NewPrimitiveFloat(f PrimitiveFloat) interface{} {
return JPrimitive{
primitive: f,
}
}
func NewPrimitiveFloatRD(f float32) PrimitiveFloat {
return PrimitiveFloat{
value: f,
}
}
func NewPrimitiveDouble(d PrimitiveDouble) interface{} {
return JPrimitive{
primitive: d,
}
}
func NewPrimitiveDoubleRD(d float64) PrimitiveDouble {
return PrimitiveDouble{
value: d,
}
}
func NewPrimitiveLong(l PrimitiveLong) interface{} {
return JPrimitive{
primitive: l,
}
}
func NewPrimitiveLongRD(l int64) PrimitiveLong {
return PrimitiveLong{
value: l,
}
}
func NewPrimitiveShort(s PrimitiveShort) interface{} {
return JPrimitive{
primitive: s,
}
}
func NewPrimitiveShortRD(l int16) PrimitiveShort {
return PrimitiveShort{
value: l,
}
}
func NewPrimitiveChar(c PrimitiveChar) interface{} {
return JPrimitive{
primitive: c,
}
}
func NewPrimitiveCharRD(c int32) PrimitiveChar {
return PrimitiveChar{
value: c,
}
}
func (a JArray) SetRef(i PrimitiveInt, o interface{}) {
a.SetRefD(i.value, o)
}
func (a JArray) SetRefD(i int32, o interface{}) {
a.data[i] = o
}
func (a JArray) GetRef(i PrimitiveInt) interface{} {
return a.GetRefD(i.value)
}
func (a JArray) GetRefD(i int32) interface{} {
return a.data[i]
}
func (a JArray) ArrayLength() PrimitiveInt {
return NewPrimitiveIntRD(int32(len(a.data)))
}
func (a JArray) ArrayLengthD() int32 {
return int32(len(a.data))
}
//type JObject interface{}
//type JObject A
type JMetaObject struct {
//JObject
class RuntimeClass
initialized bool
data map[string]interface{} // fields
}
type JArray struct {
//JObject
atype byte
data []interface{}
}
func (env *JEnv) createArray(atype byte, length int32) JArray {
return JArray{
atype: atype,
data: make([]interface{}, length),
}
}
func (cl *ClassLoader) makeString(env *JEnv, value string) *JMetaObject {
// create the object
obj := cl.createObject(env, "java/lang/String")
// init it with the str value
// by first creating a char array
carr := env.createArray(jaChar, int32(len(value)))
// put the chars into the array
for i, c := range value {
carr.SetRefD(int32(i), NewPrimitiveCharRD(c))
}
// call the constructor
obj.init(env, "([C)V", []interface{}{carr})
// finally, return the string object
return obj
}
const (
jaReference = 0
jaArray = 1
jaBoolean = 2
jaChar = 3
jaFloat = 4
jaDouble = 5
jaByte = 6
jaShort = 7
jaInt = 8
jaLong = 9
)
type JPrimitive struct {
//JObject
primitive Primitive
}
type Primitive interface{}
type PrimitiveInt struct {
//Primitive
value int32
}
func (i PrimitiveInt) ToByte() PrimitiveByte {
return NewPrimitiveByteRD(byte(i.value))
}
func (i PrimitiveInt) ToChar() PrimitiveChar {
return NewPrimitiveCharRD(i.value)
}
func (i PrimitiveInt) ToDouble() PrimitiveDouble {
return NewPrimitiveDoubleRD(float64(i.value))
}
func (i PrimitiveInt) ToFloat() PrimitiveFloat {
return NewPrimitiveFloatRD(float32(i.value))
}
func (i PrimitiveInt) ToLong() PrimitiveLong {
return NewPrimitiveLongRD(int64(i.value))
}
func (i PrimitiveInt) ToShort() PrimitiveShort {
return NewPrimitiveShortRD(int16(i.value))
}
func (i PrimitiveInt) Add(j PrimitiveInt) PrimitiveInt {
return NewPrimitiveIntRD(i.value + j.value)
}
func (i PrimitiveInt) And(j PrimitiveInt) PrimitiveInt {
return NewPrimitiveIntRD(i.value & j.value)
}
func (i PrimitiveInt) Div(j PrimitiveInt) PrimitiveInt {
return NewPrimitiveIntRD(i.value / j.value)
}
func (i PrimitiveInt) Mul(j PrimitiveInt) PrimitiveInt {
return NewPrimitiveIntRD(i.value * j.value)
}
func (i PrimitiveInt) Neg() PrimitiveInt {
return NewPrimitiveIntRD(-i.value)
}
func (i PrimitiveInt) Or(j PrimitiveInt) PrimitiveInt {
return NewPrimitiveIntRD(i.value | j.value)
}
func (i PrimitiveInt) Rem(j PrimitiveInt) PrimitiveInt {
return NewPrimitiveIntRD(i.value % j.value)
}
func (i PrimitiveInt) Shl(j PrimitiveInt) PrimitiveInt {
return NewPrimitiveIntRD(i.value << (j.value & 0x1f))
}
func (i PrimitiveInt) Shr(j PrimitiveInt) PrimitiveInt {
return NewPrimitiveIntRD(i.value >> (j.value & 0x1f))
}
func (i PrimitiveInt) Sub(j PrimitiveInt) PrimitiveInt {
return NewPrimitiveIntRD(i.value - j.value)
}
func (i PrimitiveInt) Xor(j PrimitiveInt) PrimitiveInt {
return NewPrimitiveIntRD(i.value ^ j.value)
}
func (i PrimitiveInt) Ushr(j PrimitiveInt) PrimitiveInt {
return NewPrimitiveIntRD(int32(uint32(i.value) >> (j.value & 0x1f)))
}
type PrimitiveLong struct {
//Primitive
value int64
}
func (l PrimitiveLong) ToDouble() PrimitiveDouble {
return NewPrimitiveDoubleRD(float64(l.value))
}
func (l PrimitiveLong) ToFloat() PrimitiveFloat {
return NewPrimitiveFloatRD(float32(l.value))
}
func (l PrimitiveLong) ToInt() PrimitiveInt {
return NewPrimitiveIntRD(int32(l.value))
}
func (l PrimitiveLong) Add(j PrimitiveLong) PrimitiveLong {
return NewPrimitiveLongRD(l.value + j.value)
}
func (l PrimitiveLong) Div(j PrimitiveLong) PrimitiveLong {
return NewPrimitiveLongRD(l.value / j.value)
}
func (l PrimitiveLong) Mul(j PrimitiveLong) PrimitiveLong {
return NewPrimitiveLongRD(l.value * j.value)
}
func (l PrimitiveLong) Neg() PrimitiveLong {
return NewPrimitiveLongRD(-l.value)
}
func (l PrimitiveLong) Rem(j PrimitiveLong) PrimitiveLong {
return NewPrimitiveLongRD(l.value % j.value)
}
func (l PrimitiveLong) Or(j PrimitiveLong) PrimitiveLong {
return NewPrimitiveLongRD(l.value | j.value)
}
func (l PrimitiveLong) Shl(j PrimitiveLong) PrimitiveLong {
return NewPrimitiveLongRD(l.value << (j.value & 0x3f))
}
func (l PrimitiveLong) Shr(j PrimitiveLong) PrimitiveLong {
return NewPrimitiveLongRD(l.value >> (j.value & 0x3f))
}
func (l PrimitiveLong) Sub(j PrimitiveLong) PrimitiveLong {
return NewPrimitiveLongRD(l.value - j.value)
}
func (l PrimitiveLong) Xor(j PrimitiveLong) PrimitiveLong {
return NewPrimitiveLongRD(l.value ^ j.value)
}
func (l PrimitiveLong) Ushr(j PrimitiveLong) PrimitiveLong {
return NewPrimitiveLongRD(int64(uint64(l.value) >> (j.value & 0x3f)))
}
type PrimitiveFloat struct {
//Primitive
value float32
}
func (f PrimitiveFloat) Add(o PrimitiveFloat) PrimitiveFloat {
return PrimitiveFloat{
value: f.value + o.value,
}
}
func (f PrimitiveFloat) Neg() PrimitiveFloat {
return PrimitiveFloat{
value: -f.value,
}
}
func (f PrimitiveFloat) Sub(o PrimitiveFloat) PrimitiveFloat {
return f.Add(o.Neg())
}
func (f PrimitiveFloat) Mul(o PrimitiveFloat) PrimitiveFloat {
return PrimitiveFloat{
value: f.value * o.value,
}
}
func (f PrimitiveFloat) Div(o PrimitiveFloat) PrimitiveFloat {
return PrimitiveFloat{
value: f.value / o.value,
}
}
func (f PrimitiveFloat) Rem(o PrimitiveFloat) PrimitiveFloat {
return PrimitiveFloat{
value: float32(int32(f.value) % int32(o.value)),
}
}
func (f PrimitiveFloat) ToDouble() PrimitiveDouble {
return NewPrimitiveDoubleRD(float64(f.value))
}
func (f PrimitiveFloat) ToInt() PrimitiveInt {
return NewPrimitiveIntRD(int32(f.value))
}
func (f PrimitiveFloat) ToLong() PrimitiveLong {
return NewPrimitiveLongRD(int64(f.value))
}
type PrimitiveDouble struct {
//Primitive
value float64
}
func (d PrimitiveDouble) Add(o PrimitiveDouble) PrimitiveDouble {
return PrimitiveDouble{
value: d.value + o.value,
}
}
func (d PrimitiveDouble) Neg() PrimitiveDouble {
return PrimitiveDouble{
value: -d.value,
}
}
func (d PrimitiveDouble) Sub(o PrimitiveDouble) PrimitiveDouble {
return d.Add(o.Neg())
}
func (d PrimitiveDouble) Mul(o PrimitiveDouble) PrimitiveDouble {
return PrimitiveDouble{
value: d.value * o.value,
}
}
func (d PrimitiveDouble) Div(o PrimitiveDouble) PrimitiveDouble {
return PrimitiveDouble{
value: d.value / o.value,
}
}
func (d PrimitiveDouble) Rem(o PrimitiveDouble) PrimitiveDouble {
return PrimitiveDouble{
value: float64(int64(d.value) % int64(o.value)), // TODO might not work
}
}
func (d PrimitiveDouble) ToFloat() PrimitiveFloat {
return NewPrimitiveFloatRD(float32(d.value))
}
func (d PrimitiveDouble) ToInt() PrimitiveInt {
return NewPrimitiveIntRD(int32(d.value))
}
func (d PrimitiveDouble) ToLong() PrimitiveLong {
return NewPrimitiveLongRD(int64(d.value))
}
type PrimitiveBoolean struct {
Primitive
value bool
}
type PrimitiveByte struct {
Primitive
value byte
}
type PrimitiveShort struct {
Primitive
value int16
}
type PrimitiveChar struct {
Primitive
value int32
}
type PrimitiveVoid struct {
Primitive
}
type ClassLoader struct {
parent *ClassLoader
classes map[string]*RuntimeClass
}
type RuntimeClass struct {
classLoader *ClassLoader
methods map[string]*Method
fields map[string]*Field
name string
flags []ClassAccessFlag
constantPool *RuntimeConstantPool
staticFields map[string]interface{}
}
type Method struct {
name string
rawDescriptor string
accessFlags []MethodAccessFlag
code map[uint16]Instruction
maxStack int
maxLocals int
descriptor MethodDescriptor
locals []RuntimeLocalVariable
}
type Field struct {
name string
descriptor string
tipe Type
accessFlags []FieldAccessFlag
}
type Type interface{}
type TypeByte struct {
Type
}
type TypeChar struct {
Type
}
type TypeDouble struct {
Type
}
type TypeFloat struct {
Type
}
type TypeInt struct {
Type
}
type TypeLong struct {
Type
}
type TypeShort struct {
Type
}
type TypeBoolean struct {
Type
}
type TypeVoid struct {
Type
}
type TypeArray struct {
Type
componentType Type
}
type TypeClass struct {
Type
name string
}
type AccessFlag interface{}
type ClassAccessFlag struct {
AccessFlag
value uint16
}
type MethodAccessFlag struct {
AccessFlag
value uint16
}
type FieldAccessFlag struct {
AccessFlag
value uint16
}
// Class Access Flags
var (
CAccPublic = cCAF(0x0001)
CAccFinal = cCAF(0x0010)
CAccSuper = cCAF(0x0020)
CAccInterface = cCAF(0x0200)
CAccAbstract = cCAF(0x0400)
CAccSynthetic = cCAF(0x1000)
CAccAnnotation = cCAF(0x2000)
CAccEnum = cCAF(0x4000)
CAccModule = cCAF(0x8000)
)
// Method Access Flags
var (
MAccPublic = cMAF(0x0001)
MAccPrivate = cMAF(0x0002)
MAccProtected = cMAF(0x0004)
MAccStatic = cMAF(0x0008)
MAccFinal = cMAF(0x0010)
MAccSynchronized = cMAF(0x0020)
MAccBridge = cMAF(0x0040)
MAccVarargs = cMAF(0x0080)
MAccNative = cMAF(0x0100)
MAccAbstract = cMAF(0x0400)
MAccStrict = cMAF(0x0800)
MAccSynthetic = cMAF(0x1000)
)
// Field Access Flags
var (
FAccPublic = cFAF(0x0001)
FAccPrivate = cFAF(0x0002)
FAccProtected = cFAF(0x0004)
FAccStatic = cFAF(0x0008)
FAccFinal = cFAF(0x0010)
FAccVolatile = cFAF(0x0040)
FAccTransient = cFAF(0x0080)
FAccSynthetic = cFAF(0x1000)
FAccEnum = cFAF(0x4000)
)
func cCAF(value uint16) ClassAccessFlag {
return ClassAccessFlag{
value: value,
}
}
func cMAF(value uint16) MethodAccessFlag {
return MethodAccessFlag{
value: value,
}
}
func cFAF(value uint16) FieldAccessFlag {
return FieldAccessFlag{
value: value,
}
}
func (cl *ClassLoader) createRuntimeClass(env *JEnv, class ClassFile) (cls RuntimeClass) {
cls = RuntimeClass{
classLoader: cl,
methods: mapMethods(class.methods, class.constantPool),
fields: mapFields(class, class.constantPool),
name: AsString(class.constantPool[class.constantPool[class.thisClass].(*Class).nameIndex]),
flags: mapCAccessFlags(class.accessFlags),
constantPool: Transform(class), // added in post lmao
}
cls.FindMethod("<clinit>", "()V").Invoke(env, cls, nil, []interface{}{})
return
}
func (cl *ClassLoader) findOrLoadClass(env *JEnv, name string) RuntimeClass {
if cls, ok := cl.classes[name]; ok {
return *cls
}
return cl.createRuntimeClass(env, env.runtime.GetResource(name))
}
func (cl *ClassLoader) createObject(env *JEnv, class string) *JMetaObject {
return &JMetaObject{
class: cl.findOrLoadClass(env, class),
data: map[string]interface{}{},
}
}
func mapMethods(members []MemberInfo, cp []ClassPoolInfo) map[string]*Method {
methods := make(map[string]*Method, len(members))
for _, member := range members {
name := AsString(cp[member.nameIndex])
mai := findMethodAttributeInfo(member, cp)
methods[name] = &Method{
name: name,
rawDescriptor: AsString(cp[member.descriptorIndex]),
accessFlags: mapMAccessFlags(member.accessFlags),
code: makeCode(mai),
maxStack: int(mai.maxStack),
maxLocals: int(mai.maxLocals),
descriptor: MakeMethodDescriptor(CreateStringConsumer(AsString(cp[member.descriptorIndex]))),
locals: mapLocalVariables(int(mai.maxLocals), findLocals(mai.codeAttribute.attributes, cp), cp),
}
}
return methods
}
func findLocals(attributes []AttributeInfo, cp []ClassPoolInfo) LocalVariableTable {
name := func(info AttributeInfo) string {
return AsString(cp[info.attributeNameIndex])
}
for _, attribute := range attributes {
if name(attribute) == "LocalVariableTable" {
return attribute.attrType.(LocalVariableTable)
}
}
return LocalVariableTable{}
}
func mapLocalVariables(maxLocals int, lvt LocalVariableTable, cp []ClassPoolInfo) (rlv []RuntimeLocalVariable) {
rlv = make([]RuntimeLocalVariable, maxLocals)
for i, lvt := range lvt.localVariableTable {
rlv[i] = RuntimeLocalVariable{
name: AsString(cp[lvt.nameIndex]),
slot: int(lvt.index),
obj: nil,
}
}
return
}
func makeCode(mai MethodAttributeInfo) map[uint16]Instruction {
if mai.code == nil {
return nil
}
return ParseInstructions(mai.code)
}
type MethodAttributeInfo struct {
maxStack uint16
maxLocals uint16
code []uint8
codeAttribute Code
}
func findMethodAttributeInfo(member MemberInfo, cp []ClassPoolInfo) MethodAttributeInfo {
name := func(info AttributeInfo) string {
return AsString(cp[info.attributeNameIndex])
}
mai := MethodAttributeInfo{}
for _, attr := range member.attributes {
if name(attr) == "Code" {
mai.maxStack = attr.attrType.(Code).maxStack
mai.maxLocals = attr.attrType.(Code).maxLocals
mai.code = attr.attrType.(Code).code
mai.codeAttribute = attr.attrType.(Code)
break
}
}
return mai
}
type MethodDescriptor struct {
arguments []Type