-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinterpreter.go
88 lines (66 loc) · 2.28 KB
/
interpreter.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
package main
import (
"fmt"
"github.com/Frederick-S/jvmgo/instructions"
"github.com/Frederick-S/jvmgo/instructions/base_instructions"
"github.com/Frederick-S/jvmgo/runtime_data_area"
"github.com/Frederick-S/jvmgo/runtime_data_area/heap"
)
func interpret(method *heap.Method, arguments []string) {
thread := runtime_data_area.NewThread()
frame := thread.NewFrame(method)
thread.PushFrame(frame)
javaArguments := createArgumentsArray(method.GetClass().GetClassLoader(), arguments)
frame.GetLocalVariables().SetReferenceValue(0, javaArguments)
defer catchError(thread)
loop(thread)
}
func createArgumentsArray(classLoader *heap.ClassLoader, arguments []string) *heap.Object {
stringClass := classLoader.LoadClass("java/lang/String")
argumentsArray := stringClass.GetArrayClass().NewArray(uint(len(arguments)))
javaArguments := argumentsArray.GetReferenceArray()
for i, argument := range arguments {
javaArguments[i] = heap.ConvertGoStringToJavaString(classLoader, argument)
}
return argumentsArray
}
func catchError(thread *runtime_data_area.Thread) {
r := recover()
if r != nil {
logFrames(thread)
panic(r)
}
}
func loop(thread *runtime_data_area.Thread) {
bytecodeReader := &base_instructions.BytecodeReader{}
for {
frame := thread.GetCurrentFrame()
pc := frame.GetNextPC()
thread.SetPC(pc)
bytecodeReader.Reset(frame.GetMethod().GetCode(), pc)
operationCode := bytecodeReader.ReadUint8()
instruction := instructions.NewInstruction(operationCode)
instruction.FetchOperands(bytecodeReader)
frame.SetNextPC(bytecodeReader.GetPC())
logInstruction(frame, instruction)
instruction.Execute(frame)
if thread.IsJVMStackEmpty() {
break
}
}
}
func logInstruction(frame *runtime_data_area.Frame, instruction base_instructions.Instruction) {
method := frame.GetMethod()
className := method.GetClass().GetName()
methodName := method.GetName()
pc := frame.GetThread().GetPC()
fmt.Printf("%v.%v() #%2d %T %v\n", className, methodName, pc, instruction, instruction)
}
func logFrames(thread *runtime_data_area.Thread) {
for !thread.IsJVMStackEmpty() {
frame := thread.PopFrame()
method := frame.GetMethod()
className := method.GetClass().GetName()
fmt.Printf(">> pc:%4d %v.%v%v \n", frame.GetNextPC(), className, method.GetName(), method.GetDescriptor())
}
}