Files
PJVM/pjvm/jtypes/jprimitives.py
2020-01-30 22:31:36 +01:00

71 lines
1.4 KiB
Python

import struct
from typing import Union
from .jtype import JType
class JPrimitive(JType):
def __init__(self, value):
self.j_type = self.j_base_type
super(JPrimitive, self).__init__(value)
class JChar(JPrimitive):
j_base_type = 'C' # todo 16-bit unicode code point
class JDouble(JPrimitive):
j_base_type = 'D'
class JFloat(JPrimitive):
j_base_type = 'F'
class JBool(JPrimitive):
j_base_type = 'Z'
def __init__(self, value):
self.value = value
super(JBool, self).__init__(value)
# Integer types
class JPrimitiveInteger(JPrimitive):
pj_mask: int
pj_struct: str
def __init__(self, value: Union[JType, bytes, int]):
if isinstance(value, int):
self.value = value & self.pj_mask
elif isinstance(value, bytes):
self.value, = struct.unpack(self.pj_struct, value)
else:
raise NotImplementedError()
super(JPrimitiveInteger, self).__init__(value)
class JByte(JPrimitiveInteger):
j_base_type = 'B'
pj_mask = 0xff
pj_struct = 'b'
class JShort(JPrimitiveInteger):
j_base_type = 'S'
pj_mask = 0xffff
pj_struct = 'h'
class JInt(JPrimitiveInteger):
j_base_type = 'I'
pj_mask = 0xffffffff
pj_struct = 'i'
class JLong(JPrimitiveInteger):
j_base_type = 'J'
pj_mask = 0xffffffffffffffff
pj_struct = 'q'