feat: added support for invoke static

This commit is contained in:
2024-03-29 15:29:40 +01:00
parent 58a40f3a24
commit c103ead828
6 changed files with 31 additions and 6 deletions

View File

@@ -4,7 +4,7 @@
<content url="file://$MODULE_DIR$"> <content url="file://$MODULE_DIR$">
<excludeFolder url="file://$MODULE_DIR$/venv" /> <excludeFolder url="file://$MODULE_DIR$/venv" />
</content> </content>
<orderEntry type="jdk" jdkName="Pipenv (JavaClassFileParser)" jdkType="Python SDK" /> <orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" /> <orderEntry type="sourceFolder" forTests="false" />
</component> </component>
<component name="PackageRequirementsSettings"> <component name="PackageRequirementsSettings">

5
.idea/misc.xml generated
View File

@@ -1,7 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<project version="4"> <project version="4">
<component name="Black">
<option name="sdkName" value="Pipenv (PJVM)" />
</component>
<component name="JavaScriptSettings"> <component name="JavaScriptSettings">
<option name="languageLevel" value="ES6" /> <option name="languageLevel" value="ES6" />
</component> </component>
<component name="ProjectRootManager" version="2" project-jdk-name="Pipenv (JavaClassFileParser)" project-jdk-type="Python SDK" /> <component name="ProjectRootManager" version="2" project-jdk-name="Pipenv (PJVM)" project-jdk-type="Python SDK" />
</project> </project>

View File

@@ -85,13 +85,30 @@ def op_ldc(vm: "PJVirtualMachine", opcode: int, frame: Frame):
frame.stack.append(resulting_value) frame.stack.append(resulting_value)
@Opcode(0xb8, size=3, no_ret=True)
def op_invokestatic(vm: "PJVirtualMachine", opcode: int, frame: Frame):
index = frame.get_short(frame.ip + 1)
class_name, method_name, descriptor = frame.clazz.cp_get_methodref(index)
LOGGER.info(f"Calling static {class_name} -> {method_name} {descriptor}")
args_types = utils.get_argument_count_types_descriptor(descriptor)
# todo type check
args = []
for _ in args_types:
args.append(frame.stack.pop())
result = vm.execute_static_method(class_name, method_name, args)
if descriptor[-1] != 'V':
frame.stack.append(result)
@Opcode(0xb6, size=3, no_ret=True) @Opcode(0xb6, size=3, no_ret=True)
def op_invokevirtual(vm: "PJVirtualMachine", opcode: int, frame: Frame): def op_invokevirtual(vm: "PJVirtualMachine", opcode: int, frame: Frame):
index = frame.get_short(frame.ip + 1) index = frame.get_short(frame.ip + 1)
result: JObj result: JObj
class_name, method_name, descriptor = frame.clazz.cp_get_methodref(index) class_name, method_name, descriptor = frame.clazz.cp_get_methodref(index)
LOGGER.info(f"Calling {class_name} -> {method_name} {descriptor}") LOGGER.info(f"Calling virtual {class_name} -> {method_name} {descriptor}")
args_types = utils.get_argument_count_types_descriptor(descriptor) args_types = utils.get_argument_count_types_descriptor(descriptor)
# todo type check # todo type check
args = [] args = []

View File

@@ -49,7 +49,7 @@ class PJVirtualMachine:
if main_class not in self.classes: if main_class not in self.classes:
raise PJVMException("main_class not found!") raise PJVMException("main_class not found!")
self.execute_static_method(main_class, 'Main', [j_args]) self.execute_static_method(main_class, 'main', [j_args])
def execute_static_method(self, class_name: str, method_name: str, args: List[JObj]): def execute_static_method(self, class_name: str, method_name: str, args: List[JObj]):
clazz = self.classes[class_name] clazz = self.classes[class_name]

View File

@@ -1,7 +1,12 @@
package testclass; package testclass;
public class AdvancedTest { public class AdvancedTest {
public static void Main(String[] args) throws Exception{ private static void method() {
System.out.println("Hello");
}
public static void main(String[] args) throws Exception{
method();
char c = (char) System.in.read(); char c = (char) System.in.read();
if (c == 'a') { if (c == 'a') {
System.out.println("Is a"); System.out.println("Is a");

View File

@@ -3,7 +3,7 @@ package testclass;
public class Test { public class Test {
String testfield; String testfield;
public static int otherField; public static int otherField;
public static void Main(String[] args) { public static void main(String[] args) {
System.out.println("Hello World!"); System.out.println("Hello World!");
} }
} }