Skip to content

Commit

Permalink
Moved JvmObject to types module
Browse files Browse the repository at this point in the history
  • Loading branch information
ronyhe committed Jul 30, 2018
1 parent dd4efe1 commit 78bfa65
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 40 deletions.
3 changes: 1 addition & 2 deletions pyjvm/core/class_loaders.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from jawa.classloader import ClassLoader as JawaLoader

from pyjvm.core.jvm_class import JvmObject
from pyjvm.core.jvm_types import RootObjectType, ObjectReferenceType
from pyjvm.core.jvm_types import RootObjectType, ObjectReferenceType, JvmObject
from pyjvm.utils.jawa_conversions import convert_class_file


Expand Down
32 changes: 0 additions & 32 deletions pyjvm/core/jvm_class.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
from typing import Dict

import attr

from pyjvm.core.jvm_types import JvmValue


@attr.s(frozen=True)
class JvmClass:
Expand Down Expand Up @@ -99,31 +95,3 @@ class MethodKey:
"""
name = attr.ib(type=str, converter=str)
descriptor = attr.ib(type=str, converter=str)


class JvmObject:
"""The value of a reference class instance at runtime
To be used in conjunction with a JvmType, as the value for reference types.
fields: Mapping[name, JvmType], the names and types of the fields in this object's class
"""

@classmethod
def defaults(cls, field_specs):
"""Return a new JvmObject with all fields initialized to their default values"""
return cls(
(name, type_.create_instance(type_.default_value)) for name, type_ in dict(field_specs).items()
)

def __init__(self, fields):
self.fields: Dict[str, JvmValue] = dict(fields)

def __eq__(self, other):
try:
return other.fields == self.fields
except AttributeError:
return False

def __repr__(self):
return f'<{self.__class__.__name__}>'
31 changes: 31 additions & 0 deletions pyjvm/core/jvm_types.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
from typing import Dict


class Type:
def __init__(self, name, *, default_value, refers_to=None,
needs_two_slots=False, is_array_reference=False):
Expand Down Expand Up @@ -132,3 +135,31 @@ def min_max_by_type(type_):

RootObjectType = ObjectReferenceType('java/lang/Object')
NULL_VALUE = RootObjectType.create_instance(NULL_OBJECT)


class JvmObject:
"""The value of a reference class instance at runtime
To be used in conjunction with a JvmType, as the value for reference types.
fields: Mapping[name, JvmType], the names and types of the fields in this object's class
"""

@classmethod
def defaults(cls, field_specs):
"""Return a new JvmObject with all fields initialized to their default values"""
return cls(
(name, type_.create_instance(type_.default_value)) for name, type_ in dict(field_specs).items()
)

def __init__(self, fields):
self.fields: Dict[str, JvmValue] = dict(fields)

def __eq__(self, other):
try:
return other.fields == self.fields
except AttributeError:
return False

def __repr__(self):
return f'<{self.__class__.__name__}>'
4 changes: 2 additions & 2 deletions pyjvm/utils/jawa_conversions.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
from jawa.cf import ClassFile
from jawa.methods import Method

from pyjvm.core.jvm_class import JvmClass, BytecodeMethod, MethodKey, JvmObject, Handlers, ExceptionHandler
from pyjvm.core.jvm_class import JvmClass, BytecodeMethod, MethodKey, Handlers, ExceptionHandler
from pyjvm.core.jvm_types import Type, Integer, Float, Long, Double, ArrayReferenceType, ObjectReferenceType, \
RootObjectType
RootObjectType, JvmObject
from pyjvm.utils.utils import split_by_predicate

_LETTERS_MAP = {
Expand Down
3 changes: 1 addition & 2 deletions test/test_instructions/test_constant_instructions.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
from jawa.constants import ConstantPool

from pyjvm.core.actions import Push
from pyjvm.core.jvm_class import JvmObject
from pyjvm.core.jvm_types import Integer, Double, ObjectReferenceType, ArrayReferenceType
from pyjvm.core.jvm_types import Integer, Double, ObjectReferenceType, ArrayReferenceType, JvmObject
from pyjvm.instructions import constant_instructions
from test.utils import assert_incrementing_instruction, constant_instruction, literal_instruction

Expand Down
4 changes: 2 additions & 2 deletions test/test_instructions/test_references.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
from pyjvm.core.actions import Push, ThrowObject, PushNewInstance, PutField, PutStatic, throw_null_pointer, Pop, \
throw_negative_array_size, throw_check_cast
from pyjvm.core.class_loaders import FixedClassLoader
from pyjvm.core.jvm_class import JvmObject, JvmClass
from pyjvm.core.jvm_class import JvmClass
from pyjvm.core.jvm_types import Integer, NULL_VALUE, ArrayReferenceType, NULL_OBJECT, ObjectReferenceType, \
RootObjectType
RootObjectType, JvmObject
from pyjvm.instructions.references import create_levels
from pyjvm.utils import value_array_type_indicators
from pyjvm.utils.utils import TRUE, FALSE, constant_operand, literal_operand
Expand Down

0 comments on commit 78bfa65

Please sign in to comment.