Example #1
0
def _convert_const_to_string_instance(const):
    text = const.string.value
    chars = [ord(c) for c in text]
    char_array = ArrayReferenceType(Integer).create_instance(chars)
    hash_value = hash(text) % (2 ** 32)
    hash_ = Integer.create_instance(hash_value)
    ref_type = ObjectReferenceType('java/lang/String')
    return ref_type.create_instance(JvmObject({
        'hash': hash_,
        'value': char_array
    }))
Example #2
0
    def default_instance(self, class_name):
        """Returns an instance of the class with all fields initialized to their type's default value

        See core.jvm_types.py for more information regarding types and their defaults.
        """
        fields = self.collect_fields_in_super_classes(class_name)
        obj = JvmObject.defaults(fields)

        type_ = ObjectReferenceType(class_name)
        instance = type_.create_instance(obj)
        return instance
Example #3
0
def test_push_new_instance():
    machine = complex_machine()
    machine.act(
        PushNewInstance(COMPLEX_CLASS)
    )
    stack = machine.frames.peek().op_stack
    assert stack.peek().type == ObjectReferenceType(COMPLEX_CLASS_NAME)
Example #4
0
def test_instance_of_array(std_loader):
    base_type = ObjectReferenceType(OUTPUT_STREAM)
    array_type = ArrayReferenceType(base_type)

    descriptor = _add_dimensions_to_descriptor(
        class_as_descriptor(OUTPUT_STREAM))

    assert does_type_derive_from(array_type, descriptor, std_loader)
Example #5
0
def test_string_constant():
    text = 'some_text'
    consts = ConstantPool()
    const = consts.create_string(text)

    chars = [ord(c) for c in text]
    char_array = ArrayReferenceType(Integer).create_instance(chars)
    hash_value = hash(text) % (2**32)
    hash_ = Integer.create_instance(hash_value)

    reference_type = ObjectReferenceType('java/lang/String')
    jvm_object = JvmObject({'hash': hash_, 'value': char_array})

    assert_incrementing_instruction(
        instruction=constant_instruction('ldc', const),
        constants=consts,
        expected=[Push(reference_type.create_instance(jvm_object))])
Example #6
0
def test_reference_binary_branch_comparisons(std_loader):
    offset = 7
    source = 4

    npe = NPE_CLASS_NAME
    instance = std_loader.default_instance(npe).value
    values = [instance, instance]

    for name, op in BINARY_REFERENCE_COMPARISONS.items():
        _test_branch_comp(name, source, offset, values,
                          ObjectReferenceType(npe), op)
Example #7
0
    def execute(self):
        class_ = self.operand_as_constant()
        class_name = class_.name.value
        base_type = ObjectReferenceType(class_name)

        num_dimensions = self.operand_as_int(index=1)
        dimensions = [v.value for v in self.peek_many(num_dimensions)]
        if any(d < 0 for d in dimensions):
            return actions.throw_negative_array_size()

        array_type = base_type
        for _ in range(num_dimensions):
            array_type = ArrayReferenceType(array_type)

        array_value = create_levels(
            dimensions,
            lambda: base_type.create_instance(base_type.default_value))

        return IncrementProgramCounter.after(
            actions.Pop(num_dimensions),
            actions.Push(array_type.create_instance(array_value)))
Example #8
0
 def _stack_trace_implementation(self):
     return BytecodeMethod(
         name='fillInStackTrace',
         descriptor='(Ljava/lang/Throwable;)Ljava/lang/VMThrowable;',
         instructions=[
             named_tuple_replace(Instruction.create('aload_0'), pos=1),
             named_tuple_replace(Instruction.create('areturn'), pos=2)
         ],
         max_locals=1,
         max_stack=1,
         args=[ObjectReferenceType('java/lang/Throwable')],
         is_native=False,
         exception_handlers=Handlers()
     )
Example #9
0
def test_instance_of_class(std_loader):
    assert does_type_derive_from(ObjectReferenceType(FILE_OUTPUT_STREAM),
                                 class_as_descriptor(FILE_OUTPUT_STREAM),
                                 std_loader)
Example #10
0
def simple_instance_check(class_name: str, parent_name: str, loader: ClassLoader) -> bool:
    """Return true if instance of the class named `class_name` are instances of the class named `parent_name`"""
    return _Checker(loader).is_instance_of(
        ObjectReferenceType(class_name),
        ObjectReferenceType(parent_name)
    )
Example #11
0
def _atom_type(base, name):
    if base == 'L':
        return ObjectReferenceType(name)
    else:
        return _LETTERS_MAP[base.upper()]
Example #12
0
 def _get_type(self):
     index = self.operand_as_int()
     const = self.constants[index]
     class_name = const.name.value
     type_ = ObjectReferenceType(refers_to=class_name)
     return type_