mirror of
https://github.com/FranzHaidnor/haidnorJVM.git
synced 2026-03-13 21:43:42 +08:00
add ARETURN 支持返回对象
This commit is contained in:
@@ -547,7 +547,7 @@ public abstract class InstructionFactory {
|
|||||||
return new DRETURN(codeStream);
|
return new DRETURN(codeStream);
|
||||||
}
|
}
|
||||||
case Const.ARETURN -> {
|
case Const.ARETURN -> {
|
||||||
throw new Error("Not support JavaVM opcode ARETURN");
|
return new ARETURN(codeStream);
|
||||||
}
|
}
|
||||||
case Const.RETURN -> {
|
case Const.RETURN -> {
|
||||||
return new RETURN(codeStream);
|
return new RETURN(codeStream);
|
||||||
|
|||||||
26
src/main/java/haidnor/jvm/instruction/control/ARETURN.java
Normal file
26
src/main/java/haidnor/jvm/instruction/control/ARETURN.java
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
package haidnor.jvm.instruction.control;
|
||||||
|
|
||||||
|
import haidnor.jvm.instruction.Instruction;
|
||||||
|
import haidnor.jvm.runtime.Frame;
|
||||||
|
import haidnor.jvm.runtime.StackValue;
|
||||||
|
import haidnor.jvm.util.CodeStream;
|
||||||
|
import haidnor.jvm.util.JvmThreadHolder;
|
||||||
|
|
||||||
|
public class ARETURN extends Instruction {
|
||||||
|
|
||||||
|
public ARETURN(CodeStream codeStream) {
|
||||||
|
super(codeStream);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void execute(Frame frame) {
|
||||||
|
// 弹出操作数栈中的值
|
||||||
|
StackValue stackValue = frame.pop();
|
||||||
|
// 将当前栈帧从 jvm 线程栈中弹出
|
||||||
|
JvmThreadHolder.get().pop();
|
||||||
|
// 将方法返回值压入前一个栈帧的操作数栈中
|
||||||
|
Frame topFrame = JvmThreadHolder.get().peek();
|
||||||
|
topFrame.push(stackValue);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -53,7 +53,7 @@ public class INVOKESTATIC extends Instruction {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Class<?> javaClass = Class.forName(Utility.compactClassName(className,false));
|
Class<?> javaClass = Class.forName(Utility.compactClassName(className, false));
|
||||||
java.lang.reflect.Method method = javaClass.getMethod(methodName, parameterTypeArr);
|
java.lang.reflect.Method method = javaClass.getMethod(methodName, parameterTypeArr);
|
||||||
method.setAccessible(true);
|
method.setAccessible(true);
|
||||||
Object value = method.invoke(null, stacksValueArr);
|
Object value = method.invoke(null, stacksValueArr);
|
||||||
@@ -63,17 +63,20 @@ public class INVOKESTATIC extends Instruction {
|
|||||||
}
|
}
|
||||||
// 自定义类的方法
|
// 自定义类的方法
|
||||||
else {
|
else {
|
||||||
Klass meteKlass = Metaspace.getJavaClass(Utility.compactClassName(className));
|
Klass klass = Metaspace.getJavaClass(Utility.compactClassName(className));
|
||||||
if (meteKlass != null) {
|
// 调用静态方法时加载类
|
||||||
JavaClass javaClass = meteKlass.getJavaClass();
|
if (klass == null) {
|
||||||
for (Method method : javaClass.getMethods()) {
|
klass = frame.getMetaClass().getClassLoader().loadClass(className);
|
||||||
if (method.getSignature().equals(methodSignature) && method.getName().equals(methodName)) {
|
}
|
||||||
KlassMethod klassMethod = new KlassMethod(meteKlass, method);
|
JavaClass javaClass = klass.getJavaClass();
|
||||||
JavaExecutionEngine.callMethod(frame, klassMethod);
|
for (Method method : javaClass.getMethods()) {
|
||||||
break;
|
if (method.getSignature().equals(methodSignature) && method.getName().equals(methodName)) {
|
||||||
}
|
KlassMethod klassMethod = new KlassMethod(klass, method);
|
||||||
|
JavaExecutionEngine.callMethod(frame, klassMethod);
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,9 @@
|
|||||||
package haidnor.jvm.test.clazz;
|
package haidnor.jvm.test.clazz;
|
||||||
|
|
||||||
public class Human {
|
public class Human {
|
||||||
|
|
||||||
public void eat() {
|
public void eat() {
|
||||||
System.out.println("人类吃饭");
|
System.out.println("人类吃饭");
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,6 +2,16 @@ package haidnor.jvm.test.clazz;
|
|||||||
|
|
||||||
public class Student extends Human {
|
public class Student extends Human {
|
||||||
|
|
||||||
public static String name = "张三";
|
public static String school = "家里蹲大学";
|
||||||
|
|
||||||
|
public String name;
|
||||||
|
|
||||||
|
public void printSchool() {
|
||||||
|
System.out.println(school);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Student newStudent() {
|
||||||
|
return new Student();
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,16 +1,14 @@
|
|||||||
package haidnor.jvm.test.demo;
|
package haidnor.jvm.test.demo;
|
||||||
|
|
||||||
|
import haidnor.jvm.test.clazz.Student;
|
||||||
|
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
|
|
||||||
public class Demo3 {
|
public class Demo3 {
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
Student sut1 = new Student(1);
|
Student sut1 = new Student();
|
||||||
Student sut2 = new Student(2);
|
Student sut2 = new Student();
|
||||||
|
|
||||||
if (sut1 == sut2) {
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
HashMap<Student, String> hashMap = new HashMap<>();
|
HashMap<Student, String> hashMap = new HashMap<>();
|
||||||
hashMap.put(sut1, "张三");
|
hashMap.put(sut1, "张三");
|
||||||
@@ -22,13 +20,3 @@ public class Demo3 {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
class Student {
|
|
||||||
|
|
||||||
public int age;
|
|
||||||
|
|
||||||
public Student(int age) {
|
|
||||||
this.age = age;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -5,8 +5,10 @@ import haidnor.jvm.test.clazz.Student;
|
|||||||
public class Demo4 {
|
public class Demo4 {
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
Student student = new Student();
|
Student student = Student.newStudent();
|
||||||
student.eat();
|
student.eat();
|
||||||
|
student.name = "张三";
|
||||||
|
System.out.println(student.name);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user