46 lines
1.3 KiB
Python
46 lines
1.3 KiB
Python
import struct
|
|
from typing import List
|
|
|
|
from .clazz import Class, Method, Code
|
|
from .exceptions import PJVMException
|
|
from .old_jtypes import JObj
|
|
|
|
|
|
class Frame:
|
|
ip: int = 0
|
|
clazz: Class
|
|
method: Method
|
|
code: Code
|
|
stack: List[JObj]
|
|
this: JObj
|
|
local_variables: List
|
|
|
|
def __init__(self, clazz: Class, method: Method):
|
|
self.clazz = clazz
|
|
self.method = method
|
|
self.code = self.method.code
|
|
self.stack = []
|
|
self.local_variables = [None for _ in range(self.code.max_locals)]
|
|
|
|
def get_byte(self, idx: int) -> int:
|
|
return struct.unpack('>b', self.code.code[idx:idx + 1])[0]
|
|
|
|
def get_short(self, idx: int) -> int:
|
|
return struct.unpack('>h', self.code.code[idx:idx + 2])[0]
|
|
|
|
def get_int(self, idx: int) -> int:
|
|
return struct.unpack('>i', self.code.code[idx:idx + 4])[0]
|
|
|
|
def set_local(self, val: JObj, index: int):
|
|
if index > self.code.max_locals:
|
|
raise PJVMException("Local out of bound!")
|
|
self.local_variables[index] = val
|
|
|
|
def get_local(self, index: int) ->JObj:
|
|
if index > self.code.max_locals:
|
|
raise PJVMException("Local out of bound!")
|
|
val = self.local_variables[index]
|
|
if val is None:
|
|
raise PJVMException("Local not set")
|
|
return val
|