-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest.go
114 lines (105 loc) · 2.88 KB
/
test.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
package main
import (
"fmt"
"HTVM/classpath"
"strings"
"HTVM/classfile"
"strconv"
"HTVM/runtime"
)
var startJVM = func(cmd *Command) {
cp := classpath.Parse(cmd.XjreOption, cmd.cpOption)
fmt.Printf(" Classpath: %s\n Main Class: %s\n Args: %v\n", cp, cmd.class, cmd.args)
className := strings.Replace(cmd.class, ".", "/", -1) // 将包名转化为本地路径
classFile := loadClass(className, cp)
//printClassFile(classFile)
mainClass := getMainClass(classFile)
if mainClass != nil {
interpreter(mainClass)
} else {
fmt.Printf("Main method not found in class %s\n", cmd.class)
}
}
func loadClass(className string, cp *classpath.Classpath) *classfile.ClassFile {
classData, _, err := cp.ReadClass(className)
classFile, _ := classfile.Parser(classData)
if err != nil {
fmt.Printf("Could not find or load main class %s\n", className)
return nil
}
return classFile
}
func getMainClass(cf *classfile.ClassFile) *classfile.MemberInfo {
for _, m := range cf.Methods() {
if m.Name() == "main" && m.Descriptor() == "([Ljava/lang/String;)V" {
return m
}
}
return nil
}
func printClassFile(classFile *classfile.ClassFile) {
//fmt.Printf("class data: %v\n", classData)
fmt.Println("MajorVersion: ", classFile.MajorVersion())
fmt.Println("ClassName: ", classFile.ClassName())
fmt.Println("SuperClassName: " + classFile.SuperClassName())
fmt.Println("AccessFlags: 0x" + strconv.FormatInt(int64(classFile.AccessFlags()), 16))
fmt.Println("InterfaceNames: ", classFile.InterfaceNames())
fmt.Println("Fields======================")
for i, info := range classFile.Fields() {
fmt.Println(i, info.Name(), info.Descriptor())
}
fmt.Println("Methods=====================")
for i, info := range classFile.Methods() {
fmt.Println(i, info.Name(), info.Descriptor())
}
}
func testFrame() {
//frame := runtime.NewFrame(512, 512)
//testLocalVars(frame.LocalVars())
//testOpertateStack(frame.OperateStack())
}
func testLocalVars(v runtime.LocalVars) {
v.SetInt(0, 123)
v.SetInt(1, -321)
v.SetLong(2, 2997924580)
v.SetLong(4, -2997924580)
v.SetFloat(6, 3.1415926)
v.SetFloat(7, -3.1415926)
v.SetDouble(8, 2.71828182818281828)
v.SetDouble(10, -2.71828182818281828)
println(v.GetInt(0))
println(v.GetInt(1))
println(v.GetLong(2))
println(v.GetLong(4))
println(v.GetFloat(6))
println(v.GetFloat(7))
println(v.GetDouble(8))
println(v.GetDouble(10))
}
func testOpertateStack(s *runtime.OperateStack) {
s.PushInt(100)
s.PushInt(-100)
s.PushLong(1234567890)
s.PushLong(-1234567890)
s.PushFloat(3.1415926)
s.PushDouble(2.718281828)
s.PushRef(nil)
println(s.PopRef())
println(s.PopDouble())
println(s.PopFloat())
println(s.PopLong())
println(s.PopLong())
println(s.PopInt())
println(s.PopInt())
}
func main() {
cmd := ParseCommand()
if cmd.versionFlag {
fmt.Println("version 0.0.1")
} else if cmd.helpFlag || cmd.class == "" {
PrintUsage()
} else {
startJVM(cmd)
//testFrame()
}
}