コード例 #1
0
ファイル: reference.py プロジェクト: kimi641/pyJVM
    def Execute(self, frame:rtda.Frame):
        currentClass = frame.Method().Class()
        cp = frame.Method().Class().ConstantPool()
        methodRef = cp.GetConstant(self.Index)
        resolvedMethod = methodRef.ResolvedMethod()
        if resolvedMethod.IsStatic():
            raise ValueError("java.lang.IncompatibleClassChangeError")

        ref = frame.OperandStack().GetRefFromTop(resolvedMethod.ArgSlotCount() - 1)
        if ref == None:
            # hack System.out.println()
            if methodRef.Name() == "println":
                _println(frame.OperandStack(), methodRef.Descriptor())
                return
            raise ValueError("java.lang.NullPointerException")

        if resolvedMethod.IsProtected() and \
           resolvedMethod.Class().IsSuperClassOf(currentClass) and \
           resolvedMethod.Class().GetPackageName() != currentClass.GetPackageName() and \
           ref.Class() != currentClass and \
           not ref.Class().IsSubClassOf(currentClass):
            raise ValueError("java.lang.IllegalAccessError")

        methodToBeInvoked = rtda.heap.lookupMethodInClass(ref.Class(), \
                                methodRef.Name(), methodRef.Descriptor())
        if methodToBeInvoked == None or methodToBeInvoked.IsAbstract():
            raise ValueError("java.lang.AbstractMethodError")

        instructions.base.InvokeMethod(frame, methodToBeInvoked)
コード例 #2
0
ファイル: reference.py プロジェクト: kimi641/pyJVM
    def Execute(self, frame:rtda.Frame):
        currentMethod = frame.Method()
        currentClass = currentMethod.Class()
        cp = currentClass.ConstantPool()
        fieldRef = cp.GetConstant(self.Index)
        field = fieldRef.ResolvedField()
        _class = field.Class()
        if not _class.InitStarted():
            frame.RevertNextPC()
            instructions.base.InitClass(frame.Thread(), _class)
            return

        if not field.IsStatic():
            raise ValueError("java.lang.IncompatibleClassChangeError")
        if field.IsFinal():
            if currentClass != _class or currentMethod.Name() != "<clinit>":
                raise ValueError("java.lang.IllegalAccessError")

        descriptor = field.Descriptor()
        slotId = field.SlotId()
        slots = _class.StaticVars()
        stack = frame.OperandStack()

        if descriptor[0] in ['Z','B','C','S','I']:
            slots.SetInt(slotId, stack.PopInt())
        elif descriptor[0] == 'F':
            slots.SetFloat(slotId, stack.PopFloat())
        elif descriptor[0] == 'J':
            slots.SetLong(slotId, stack.PopLong())
        elif descriptor[0] == 'D':
            slots.SetDouble(slotId, stack.PopDouble())
        elif descriptor[0] in ['L', '[']:
            slots.SetRef(slotId, stack.PopRef())
コード例 #3
0
ファイル: reference.py プロジェクト: kimi641/pyJVM
    def Execute(self, frame:rtda.Frame):
        cp = frame.Method().Class().ConstantPool()
        fieldRef = cp.GetConstant(self.Index)
        field = fieldRef.ResolvedField()

        if field.IsStatic():
            raise ValueError("java.lang.IncompatibleClassChangeError")
        
        stack = frame.OperandStack()
        ref = stack.PopRef()
        if ref == None:
            raise ValueError("java.lang.NullPointerException")

        descriptor = field.Descriptor()
        slotId = field.SlotId()
        slots = ref.Fields()

        if descriptor[0] in ['Z','B','C','S','I']:
            stack.PushInt(slots.GetInt(slotId))
        elif descriptor[0] == 'F':
            stack.PushFloat(slots.GetFloat(slotId))
        elif descriptor[0] == 'J':
            stack.PushLong(slots.GetLong(slotId))
        elif descriptor[0] == 'D':
            stack.PushDouble(slots.GetDouble(slotId))
        elif descriptor[0] in ['L', '[']:
            stack.PushRef(slots.GetRef(slotId))
コード例 #4
0
 def Execute(self, frame: rtda.Frame):
     stack = frame.OperandStack()
     slot1 = stack.PopSlot()
     slot2 = stack.PopSlot()
     stack.PushSlot(slot1)
     stack.PushSlot(slot2)
     stack.PushSLot(copy.deepcopy(slot1))
コード例 #5
0
ファイル: reference.py プロジェクト: kimi641/pyJVM
    def Execute(self, frame:rtda.Frame):
        cp = frame.Method().Class().ConstantPool()
        fieldRef = cp.GetConstant(self.Index)
        field = fieldRef.ResolvedField()
        _class = field.Class()
        if not _class.InitStarted():
            frame.RevertNextPC()
            instructions.base.InitClass(frame.Thread(), _class)
            return

        if not field.IsStatic():
            raise ValueError("java.lang.IncompatibleClassChangeError")

        descriptor = field.Descriptor()
        slotId = field.SlotId()
        slots = _class.StaticVars()
        stack = frame.OperandStack()

        if descriptor[0] in ['Z','B','C','S','I']:
            stack.PushInt(slots.GetInt(slotId))
        elif descriptor[0] == 'F':
            stack.PushFloat(slots.GetFloat(slotId))
        elif descriptor[0] == 'J':
            stack.PushLong(slots.GetLong(slotId))
        elif descriptor[0] == 'D':
            stack.PushDouble(slots.GetDouble(slotId))
        elif descriptor[0] in ['L', '[']:
            stack.PushRef(slots.GetRef(slotId))
コード例 #6
0
 def Execute(self, frame: rtda.Frame):
     stack = frame.OperandStack()
     v2 = stack.PopLong()
     v1 = statk.PopLong()
     if v2 == 0:
         raise ("java.lang.ArithmeticException: / by zero")
     result = v1 / v2
     stack.PushLong(result)
コード例 #7
0
 def Execute(self, frame: rtda.Frame):
     stack = frame.OperandStack()
     v2 = stack.PopInt()
     v1 = stack.PopLong()
     bits = struct.pack('>i', v2)
     s = struct.unpack('>I', bits)[0] & 0x3f
     result = v1 >> s
     stack.PushLong(result)
コード例 #8
0
ファイル: control.py プロジェクト: kimi641/pyJVM
 def Execute(self, frame: rtda.Frame):
     key = frame.OperandStack().PopInt()
     for i in range(0, self.npairs * 2, 2):
         if self.matchOffsets[i] == key:
             offset = self.matchOffsets[i + 1]
             instructions.base.Branch(frame, int(offset))
             return
     instructions.base.Branch(frame, int(self.defaultOffset))
コード例 #9
0
ファイル: control.py プロジェクト: kimi641/pyJVM
    def Execute(self, frame: rtda.Frame):
        index = frame.OperandStack().PopInt()

        offset = 0
        if index >= self.low and index <= self.high:
            offset = int(self.jumpOffsets[index - self.low])
        else:
            offset = int(self.defaultOffset)
        instructions.base.Branch(frame, offset)
コード例 #10
0
 def Execute(self, frame: rtda.Frame):
     stack = frame.OperandStack()
     v2 = stack.PopLong()
     v1 = stack.PopLong()
     if v1 > v2:
         stack.PushInt(1)
     elif v1 == v2:
         stack.PushInt(0)
     else:
         stack.PushInt(-1)
コード例 #11
0
 def Execute(self, frame: rtda.Frame):
     stack = frame.OperandStack()
     v2 = stack.PopInt()
     v1 = stack.PopLong()
     bits = struct.pack('>i', v2)
     s = struct.unpack('>I', bits)[0] & 0x1f
     bits1 = struct.pack('>l', v1)
     s1 = struct.unpack('>L', bits1)[0]
     result = struct.unpack('>l', struct.pack('>L', s1 >> s))[0]
     stack.PushInt(result)
コード例 #12
0
ファイル: ldc.py プロジェクト: kimi641/pyJVM
    def Execute(self, frame: rtda.Frame):
        stack = frame.OperandStack()
        cp = frame.Method().Class().ConstantPool()
        c = cp.GetConstant(self.Index)

        if isinstance(c, int):
            stack.PushLong(c)
        elif isinstance(c, float):
            stack.PushDouble(c)
        else:
            raise ValueError("java.lang.ClassFormatError")
コード例 #13
0
ファイル: ldc.py プロジェクト: kimi641/pyJVM
def _ldc(frame: rtda.Frame, index: int):
    stack = frame.OperandStack()
    cp = frame.Method().Class().ConstantPool()
    c = cp.GetConstant(index)

    if isinstance(c, int):
        stack.PushInt(c)
    elif isinstance(c, float):
        stack.PushFloat(c)
    else:
        raise ValueError("todo: ldc!")
コード例 #14
0
def _dcmp(frame: rtda.Frame, gFlag: bool):
    statck = frame.OperandStack()
    v2 = stack.PopDouble()
    v1 = stack.PopDouble()
    if v1 > v2:
        stack.PushInt(1)
    elif v1 == v2:
        stack.PushInt(0)
    elif v1 < v2:
        stack.PushInt(-1)
    else:
        stack.PushInt(-1)
コード例 #15
0
ファイル: reference.py プロジェクト: kimi641/pyJVM
    def Execute(self, frame:rtda.Frame):
        stack = frame.OperandStack()
        ref = stack.PopRef()
        stack.PushRef(ref)
        if ref == None:
            return

        cp = frame.Method().Class().ConstantPool()
        classRef = cp.GetConstant(self.Index)
        _class = classRef.ResolvedClass()
        if not ref.IsInstanceOf(_class):
            raise ValueError("java.lang.ClassCastException")
コード例 #16
0
ファイル: reference.py プロジェクト: kimi641/pyJVM
    def Execute(self, frame:rtda.Frame):
        stack = frame.OperandStack()
        ref = stack.PopRef()
        if ref == None:
            stack.PushInt(0)
            return

        cp = frame.Method().Class().ConstantPool()
        classRef = cp.GetConstant(self.Index)
        _class = classRef.ResolvedClass()

        if ref.IsInstanceOf(_class):
            stack.PushInt(1)
        else:
            stack.PushInt(0)
コード例 #17
0
ファイル: reference.py プロジェクト: kimi641/pyJVM
    def Execute(self, frame:rtda.Frame):
        cp = frame.Method().Class().ConstantPool()
        classRef = cp.GetConstant(self.Index)
        _class = classRef.ResolvedClass()

        if not _class.InitStarted():
            frame.RevertNextPC()
            instructions.base.InitClass(frame.Thread(), _class)
            return

        if _class.IsInterface() or _class.IsAbstract():
            raise ValueError("java.lang.InstantiationError")

        ref = _class.NewObject()
        frame.OperandStack().PushRef(ref)
コード例 #18
0
ファイル: reference.py プロジェクト: kimi641/pyJVM
    def Execute(self, frame:rtda.Frame):
        currentMethod = frame.Method()
        currentClass = currentMethod.Class()
        cp = currentClass.ConstantPool()
        fieldRef = cp.GetConstant(self.Index)
        field = fieldRef.ResolvedField()

        if field.IsStatic():
            raise ValueError("java.lang.IncompatibleClassChangeError")

        if field.IsFinal():
            if currentClass != field.Class() or currentMethod.Nmae() != "<init>":
                raise ValueError("java.lang.IllegalAccessError")

        descriptor = field.Descriptor()
        slotId = field.SlotId()
        stack = frame.OperandStack()

        if descriptor[0] in ['Z','B','C','S','I']:
            val = stack.PopInt()
            ref = stack.PopRef()
            if ref == None:
                raise ValueError("java.lang.NullPointerException")
            ref.Fields().SetInt(slotId, val)
        elif descriptor[0] == 'F':
            val = stack.PopFloat()
            ref = stack.PopFloat()
            if ref == None:
                raise ValueError("java.lang.NullPointerException")
            ref.Fields().SetFloat(slotId, val)
        elif descriptor[0] == 'J':
            val = stack.PopLong()
            ref = stack.PopLong()
            if ref == None:
                raise ValueError("java.lang.NullPointerException")
            ref.Fields().SetLong(slotId, val)
        elif descriptor[0] == 'D':
            val = stack.PopDouble()
            ref = stack.PopDouble()
            if ref == None:
                raise ValueError("java.lang.NullPointerException")
            ref.Fields().SetDouble(slotId, val)
        elif descriptor[0] in ['L', '[']:
            val = stack.PopRef()
            ref = stack.PopRef()
            if ref == None:
                raise ValueError("java.lang.NullPointerException")
            ref.Fields().SetRef(slotId, val)
コード例 #19
0
ファイル: method_invoke_logic.py プロジェクト: kimi641/pyJVM
def InvokeMethod(invokerFrame: rtda.Frame, method: rtda.heap.Method):
    thread = invokerFrame.Thread()
    newFrame = thread.NewFrame(method)
    thread.PushFrame(newFrame)

    argSlotCount = int(method.ArgSlotCount())
    if argSlotCount > 0:
        for i in range(argSlotCount - 1, -1, -1):
            slot = invokerFrame.OperandStack().PopSlot()
            newFrame.LocalVars().SetSlot(i, slot)

    # hack
    if method.IsNative():
        if method.Name() == "registerNatives":
            thread.PopFrame()
        else:
            raise ValueError(
                "native method {method.Class().Name()}.{method.Name()}{method.Descriptor()}"
            )
コード例 #20
0
ファイル: reference.py プロジェクト: kimi641/pyJVM
    def Execute(self, frame:rtda.Frame):
        cp = frame.Method().Class().ConstantPool()
        methodRef = cp.GetConstant(self.index)
        resolvedMethod = methodRef.ResolvedInterfaceMethod()
        if resolvedMethod.IsStatic() or resolvedMethod.IsPrivate():
            raise ValueError(f"java.lang.IncompativleClassChangeError")

        ref = frame.OperandStack().GetRefFromTop(resolvedMethod.ArgSlotCount() - 1)
        if ref == None:
            raise ValueError("java.lang.NullPointerException")
        if not ref.Class().IsImplements(methodRef.ResolvedClass()):
            raise ValueError(f"java.lang.IncompativleClassChangeError")

        methodToBeInvoked = rtda.heap.lookupMethodInClass(ref.Class(), \
                                methodRef.Name(), methodRef.Descriptor())
        if methodToBeInvoked == None or methodToBeInvoked.IsAbstract():
            raise ValueError("java.lang.AbstractMethodError")
        if not methodToBeInvoked.IsPublic():
            raise ValueError("java.lang.IllegalAccessError")

        instructions.base.InvokeMethod(frame, methodToBeInvoked)
コード例 #21
0
def _icmpPop(frame: rtda.Frame):
    stack = frame.OperandStack()
    val2 = stack.PopInt()
    val1 = stack.PopInt()
    return val1, val2
コード例 #22
0
def _acmp(frame: rtda.Frame) -> bool:
    stack = frame.OperandStack()
    ref2 = stack.PopRef()
    ref1 = stack.PopRef()
    return ref1 == ref2  #todo
コード例 #23
0
 def Execute(self, frame: rtda.Frame):
     val = frame.OperandStack().PopInt()
     if val >= 0:
         instructions.base.Branch(frame, self.Offset)
コード例 #24
0
ファイル: stores.py プロジェクト: kimi641/pyJVM
def _fstore(frame: rtda.Frame, index):
    val = frame.OperandStack().PopFloat()
    frame.LocalVars().SetFloat(index, val)
コード例 #25
0
ファイル: stores.py プロジェクト: kimi641/pyJVM
def _istore(frame: rtda.Frame, index):
    val = frame.OperandStack().PopInt()
    frame.LocalVars().SetInt(index, val)
コード例 #26
0
ファイル: stores.py プロジェクト: kimi641/pyJVM
def _astore(frame: rtda.Frame, index):
    ref = frame.OperandStack().PopRef()
    frame.LocalVars().SetRef(index, ref)
コード例 #27
0
ファイル: stores.py プロジェクト: kimi641/pyJVM
def _dstore(frame: rtda.Frame, index):
    val = frame.OperandStack().PopDouble()
    frame.LocalVars().SetDouble(index, val)
コード例 #28
0
ファイル: stores.py プロジェクト: kimi641/pyJVM
def _lstore(frame: rtda.Frame, index):
    val = frame.OperandStack().PopLong()
    frame.LocalVars().SetLong(index, val)
コード例 #29
0
 def Execute(self, frame: rtda.Frame):
     stack = frame.OperandStack()
     i = stack.PopInt()
     d = struct.unpack('>d', struct.pack('>i', i))[0]
     stack.pushInt(d)
コード例 #30
0
 def Execute(self, frame: rtda.Frame):
     stack = frame.OperandStack()
     d = stack.PopDoulbe()
     f = struct.unpack('>f', struct.pack('>d', d))[0]
     stack.pushFloat(f)