diff --git a/src/Kernel/Mnemonics/_baload.php b/src/Kernel/Mnemonics/_baload.php index 87b84e98..05fc02ef 100644 --- a/src/Kernel/Mnemonics/_baload.php +++ b/src/Kernel/Mnemonics/_baload.php @@ -1,7 +1,7 @@ popFromOperandStack()); + $arrayref = $this->popFromOperandStack(); + + $this->pushToOperandStack( + $arrayref[$index] + ); } } diff --git a/src/Kernel/Mnemonics/_bastore.php b/src/Kernel/Mnemonics/_bastore.php index 163059bc..44ec491c 100644 --- a/src/Kernel/Mnemonics/_bastore.php +++ b/src/Kernel/Mnemonics/_bastore.php @@ -1,7 +1,8 @@ popFromOperandStack(); + $index = Extractor::getRealValue($this->popFromOperandStack()); + + /** + * @var Type $arrayref + */ + $arrayref = $this->popFromOperandStack(); + + // The value is a ref. + $arrayref[$index] = $value; + + $this->pushToOperandStack( + $arrayref + ); } } diff --git a/src/Kernel/Mnemonics/_caload.php b/src/Kernel/Mnemonics/_caload.php index 7bd06609..fcc69375 100644 --- a/src/Kernel/Mnemonics/_caload.php +++ b/src/Kernel/Mnemonics/_caload.php @@ -1,7 +1,7 @@ popFromOperandStack()); + $arrayref = $this->popFromOperandStack(); + + $this->pushToOperandStack( + $arrayref[$index] + ); } } diff --git a/src/Kernel/Mnemonics/_daload.php b/src/Kernel/Mnemonics/_daload.php index cba13ec3..25ed5f25 100644 --- a/src/Kernel/Mnemonics/_daload.php +++ b/src/Kernel/Mnemonics/_daload.php @@ -16,6 +16,8 @@ public function execute(): void $index = Extractor::getRealValue($this->popFromOperandStack()); $arrayref = $this->popFromOperandStack(); - $this->pushToOperandStack($arrayref[$index]); + $this->pushToOperandStack( + $arrayref[$index] + ); } } diff --git a/src/Kernel/Mnemonics/_dastore.php b/src/Kernel/Mnemonics/_dastore.php index d7d066fe..702cdf41 100644 --- a/src/Kernel/Mnemonics/_dastore.php +++ b/src/Kernel/Mnemonics/_dastore.php @@ -1,6 +1,7 @@ popFromOperandStack(); $index = Extractor::getRealValue($this->popFromOperandStack()); + + /** + * @var Type $arrayref + */ $arrayref = $this->popFromOperandStack(); + // The value is a ref. $arrayref[$index] = $value; + + $this->pushToOperandStack( + $arrayref + ); } } diff --git a/src/Kernel/Mnemonics/_dneg.php b/src/Kernel/Mnemonics/_dneg.php index 32612550..7646add1 100644 --- a/src/Kernel/Mnemonics/_dneg.php +++ b/src/Kernel/Mnemonics/_dneg.php @@ -1,7 +1,9 @@ popFromOperandStack() + ); + + $this->pushToOperandStack( + _Double::get( + BinaryTool::negate($value) + ) + ); } } diff --git a/src/Kernel/Mnemonics/_dstore.php b/src/Kernel/Mnemonics/_dstore.php index c86581f6..26cd68af 100644 --- a/src/Kernel/Mnemonics/_dstore.php +++ b/src/Kernel/Mnemonics/_dstore.php @@ -1,7 +1,7 @@ readUnsignedByte(); - $value = $this->popFromOperandStack(); + $value = Extractor::getRealValue($this->popFromOperandStack()); - $this->setLocalStorage($index, BinaryTool::convertDoubleToIEEE754($value)); + $this->setLocalStorage( + $index, + $value + ); } } diff --git a/src/Kernel/Mnemonics/_faload.php b/src/Kernel/Mnemonics/_faload.php index 2855aaed..134ee9e7 100644 --- a/src/Kernel/Mnemonics/_faload.php +++ b/src/Kernel/Mnemonics/_faload.php @@ -1,7 +1,7 @@ popFromOperandStack()); + $arrayref = $this->popFromOperandStack(); + + $this->pushToOperandStack( + $arrayref[$index] + ); } } diff --git a/src/Kernel/Mnemonics/_fastore.php b/src/Kernel/Mnemonics/_fastore.php index a726b0d9..4eb75cc0 100644 --- a/src/Kernel/Mnemonics/_fastore.php +++ b/src/Kernel/Mnemonics/_fastore.php @@ -1,7 +1,8 @@ popFromOperandStack(); + $index = Extractor::getRealValue($this->popFromOperandStack()); + + /** + * @var Type $arrayref + */ + $arrayref = $this->popFromOperandStack(); + + // The value is a ref. + $arrayref[$index] = $value; + + $this->pushToOperandStack( + $arrayref + ); } } diff --git a/src/Kernel/Mnemonics/_fneg.php b/src/Kernel/Mnemonics/_fneg.php index d5baf26b..6de3753a 100644 --- a/src/Kernel/Mnemonics/_fneg.php +++ b/src/Kernel/Mnemonics/_fneg.php @@ -1,7 +1,9 @@ popFromOperandStack() + ); + + $this->pushToOperandStack( + _Float::get( + BinaryTool::negate($value) + ) + ); } } diff --git a/src/Kernel/Mnemonics/_ineg.php b/src/Kernel/Mnemonics/_ineg.php index b362a472..eff1c517 100644 --- a/src/Kernel/Mnemonics/_ineg.php +++ b/src/Kernel/Mnemonics/_ineg.php @@ -1,7 +1,9 @@ popFromOperandStack(); + $value = Extractor::getRealValue( + $this->popFromOperandStack() + ); - $this->pushToOperandStack(BinaryTool::negate($value, 4)); + $this->pushToOperandStack( + _Int::get( + BinaryTool::negate($value) + ) + ); } } diff --git a/src/Kernel/Mnemonics/_lneg.php b/src/Kernel/Mnemonics/_lneg.php index d6996902..51cfbf1f 100644 --- a/src/Kernel/Mnemonics/_lneg.php +++ b/src/Kernel/Mnemonics/_lneg.php @@ -1,7 +1,9 @@ popFromOperandStack() + ); + + $this->pushToOperandStack( + _Long::get( + BinaryTool::negate($value) + ) + ); } } diff --git a/src/Kernel/Mnemonics/_lstore_0.php b/src/Kernel/Mnemonics/_lstore_0.php index 2c8ec341..96caece7 100644 --- a/src/Kernel/Mnemonics/_lstore_0.php +++ b/src/Kernel/Mnemonics/_lstore_0.php @@ -1,8 +1,6 @@ setLocalStorage(0, $this->popFromOperandStack()); } } diff --git a/src/Kernel/Mnemonics/_lstore_1.php b/src/Kernel/Mnemonics/_lstore_1.php index e3e65dbc..34c2c6e9 100644 --- a/src/Kernel/Mnemonics/_lstore_1.php +++ b/src/Kernel/Mnemonics/_lstore_1.php @@ -1,8 +1,6 @@ setLocalStorage(1, $this->popFromOperandStack()); } } diff --git a/src/Kernel/Mnemonics/_lstore_2.php b/src/Kernel/Mnemonics/_lstore_2.php index 13c4c568..1907a806 100644 --- a/src/Kernel/Mnemonics/_lstore_2.php +++ b/src/Kernel/Mnemonics/_lstore_2.php @@ -1,8 +1,6 @@ setLocalStorage(2, $this->popFromOperandStack()); } } diff --git a/src/Kernel/Mnemonics/_lstore_3.php b/src/Kernel/Mnemonics/_lstore_3.php index 2898f836..421a0050 100644 --- a/src/Kernel/Mnemonics/_lstore_3.php +++ b/src/Kernel/Mnemonics/_lstore_3.php @@ -1,8 +1,6 @@ setLocalStorage(3, $this->popFromOperandStack()); } } diff --git a/src/Kernel/Mnemonics/_lxor.php b/src/Kernel/Mnemonics/_lxor.php index 5779ba74..f848ddd9 100644 --- a/src/Kernel/Mnemonics/_lxor.php +++ b/src/Kernel/Mnemonics/_lxor.php @@ -1,7 +1,8 @@ popFromOperandStack(); + $value1 = $this->popFromOperandStack(); + + $this->pushToOperandStack( + _Long::get( + BinaryTool::xorBits( + Extractor::getRealValue($value1), + Extractor::getRealValue($value2), + 8 + ) + ) + ); } } diff --git a/src/Kernel/Mnemonics/_saload.php b/src/Kernel/Mnemonics/_saload.php index e3382fc6..e6dc030f 100644 --- a/src/Kernel/Mnemonics/_saload.php +++ b/src/Kernel/Mnemonics/_saload.php @@ -1,7 +1,7 @@ popFromOperandStack()); + $arrayref = $this->popFromOperandStack(); + + $this->pushToOperandStack( + $arrayref[$index] + ); } } diff --git a/src/Kernel/Mnemonics/_sastore.php b/src/Kernel/Mnemonics/_sastore.php index 95d9e149..bb48a99f 100644 --- a/src/Kernel/Mnemonics/_sastore.php +++ b/src/Kernel/Mnemonics/_sastore.php @@ -1,7 +1,8 @@ popFromOperandStack(); + $index = Extractor::getRealValue($this->popFromOperandStack()); + + /** + * @var Type $arrayref + */ + $arrayref = $this->popFromOperandStack(); + + // The value is a ref. + $arrayref[$index] = $value; + + $this->pushToOperandStack( + $arrayref + ); } } diff --git a/src/Utilities/BinaryTool.php b/src/Utilities/BinaryTool.php index f993bba5..786176fd 100644 --- a/src/Utilities/BinaryTool.php +++ b/src/Utilities/BinaryTool.php @@ -52,18 +52,9 @@ final public static function toSigned($value, $bytes) return '-' . base_convert(self::addOneBit(self::reverseBits($convert)), 2, 10); } - final public static function negate($value, $bytes) + final public static function negate($value) { - $value = (int) $value; - $value = base_convert((string) $value, 10, 2); - - if (sprintf('%0' . $bytes . 's', $value) === str_repeat('0', $bytes)) { - // zero number was overflow - return '0'; - } - - $convert = self::addOneBit(self::reverseBits($value)); - return ($convert[0] === '1' ? '-' : '') . base_convert($convert, 2, 10); + return $value * -1; } final public static function multiply($value1, $value2) diff --git a/tests/ArrayTest.php b/tests/ArrayTest.php index a40e4af8..27d7016b 100644 --- a/tests/ArrayTest.php +++ b/tests/ArrayTest.php @@ -35,4 +35,64 @@ public function testCreateStringArray() $this->assertEquals('bar', $actual->offsetGet(1)); $this->assertEquals('baz', $actual->offsetGet(2)); } + + public function testCreateLongArray() + { + $actual = $this->call('createLongArray'); + + $this->assertEquals(3, $actual->count()); + $this->assertEquals('1', $actual->offsetGet(0)); + $this->assertEquals('2', $actual->offsetGet(1)); + $this->assertEquals('3', $actual->offsetGet(2)); + } + + public function testCreateFloatArray() + { + $actual = $this->call('createFloatArray'); + + $this->assertEquals(3, $actual->count()); + $this->assertEquals('1.5', $actual->offsetGet(0)); + $this->assertEquals('2.5', $actual->offsetGet(1)); + $this->assertEquals('3.5', $actual->offsetGet(2)); + } + + public function testCreateDoubleArray() + { + $actual = $this->call('createDoubleArray'); + + $this->assertEquals(3, $actual->count()); + $this->assertEquals('1.5', $actual->offsetGet(0)); + $this->assertEquals('2.5', $actual->offsetGet(1)); + $this->assertEquals('3.5', $actual->offsetGet(2)); + } + + public function testCreateBooleanArray() + { + $actual = $this->call('createBooleanArray'); + + $this->assertEquals(3, $actual->count()); + $this->assertEquals('1', $actual->offsetGet(0)); + $this->assertEquals('0', $actual->offsetGet(1)); + $this->assertEquals('1', $actual->offsetGet(2)); + } + + public function testCreateCharArray() + { + $actual = $this->call('createCharArray'); + + $this->assertEquals(3, $actual->count()); + $this->assertEquals('A', $actual->offsetGet(0)); + $this->assertEquals('B', $actual->offsetGet(1)); + $this->assertEquals('C', $actual->offsetGet(2)); + } + + public function testCreateByteArray() + { + $actual = $this->call('createByteArray'); + + $this->assertEquals(3, $actual->count()); + $this->assertEquals('1', $actual->offsetGet(0)); + $this->assertEquals('2', $actual->offsetGet(1)); + $this->assertEquals('3', $actual->offsetGet(2)); + } } diff --git a/tests/NegationTest.php b/tests/NegationTest.php new file mode 100644 index 00000000..60813343 --- /dev/null +++ b/tests/NegationTest.php @@ -0,0 +1,136 @@ +initiatedJavaClasses['NegationTest'] + ->getInvoker() + ->getStatic() + ->getMethods() + ->call( + $method, + ...$arguments + ); + + return $calculatedValue->getValue(); + } + + public function testNegateIntPattern1() + { + $actual = $this->call( + 'negateInt', + _Int::get(123) + ); + $this->assertEquals('-123', $actual); + } + + public function testNegateIntPattern2() + { + $actual = $this->call( + 'negateInt', + _Int::get(-123) + ); + $this->assertEquals('123', $actual); + } + + public function testNegateDoublePattern1() + { + $actual = $this->call( + 'negateDouble', + _Double::get(123) + ); + $this->assertEquals('-123', $actual); + } + + public function testNegateDoublePattern2() + { + $actual = $this->call( + 'negateDouble', + _Double::get(-123) + ); + $this->assertEquals('123', $actual); + } + + public function testNegateDoublePattern3() + { + $actual = $this->call( + 'negateDouble', + _Double::get(123.5) + ); + $this->assertEquals('-123.5', $actual); + } + + public function testNegateDoublePattern4() + { + $actual = $this->call( + 'negateDouble', + _Double::get(-123.5) + ); + $this->assertEquals('123.5', $actual); + } + + public function testNegateFloatPattern1() + { + $actual = $this->call( + 'negateFloat', + _Float::get(123) + ); + $this->assertEquals('-123', $actual); + } + + public function testNegateFloatPattern2() + { + $actual = $this->call( + 'negateFloat', + _Float::get(-123) + ); + $this->assertEquals('123', $actual); + } + + public function testNegateFloatPattern3() + { + $actual = $this->call( + 'negateFloat', + _Float::get(123.5) + ); + $this->assertEquals('-123.5', $actual); + } + + public function testNegateFloatPattern4() + { + $actual = $this->call( + 'negateFloat', + _Float::get(-123.5) + ); + $this->assertEquals('123.5', $actual); + } + + public function testNegateLongPattern1() + { + $actual = $this->call( + 'negateLong', + _Long::get(123) + ); + $this->assertEquals('-123', $actual); + } + + public function testNegateLongPattern2() + { + $actual = $this->call( + 'negateLong', + _Long::get(-123) + ); + $this->assertEquals('123', $actual); + } +} diff --git a/tests/fixtures/java/ArrayTest.java b/tests/fixtures/java/ArrayTest.java index 17a63612..bf27e58b 100644 --- a/tests/fixtures/java/ArrayTest.java +++ b/tests/fixtures/java/ArrayTest.java @@ -2,11 +2,43 @@ class ArrayTest { public static int[] createIntArray() { - return new int[] { 1, 2, 3 }; + return new int[] { 1, 2, 3 }; } public static String[] createStringArray() { - return new String[] { "foo", "bar", "baz" }; + return new String[] { "foo", "bar", "baz" }; + } + + public static long[] createLongArray() + { + return new long[] { 1, 2, 3 }; + } + + public static float[] createFloatArray() + { + return new float[] { 1.5F, 2.5F, 3.5F }; + } + + public static double[] createDoubleArray() + { + return new double[] { 1.5, 2.5, 3.5 }; + } + + public static boolean[] createBooleanArray() + { + return new boolean[] { true, false, true }; + } + + public static char[] createCharArray() + { + return new char[] { 'A', 'B', 'C' }; + } + + + public static byte[] createByteArray() + { + return new byte[] { 0x01, 0x02, 0x03 }; + } } diff --git a/tests/fixtures/java/NegationTest.java b/tests/fixtures/java/NegationTest.java new file mode 100644 index 00000000..99f4e264 --- /dev/null +++ b/tests/fixtures/java/NegationTest.java @@ -0,0 +1,22 @@ +class NegationTest +{ + public static int negateInt(int a) + { + return -a; + } + + public static double negateDouble(double a) + { + return -a; + } + + public static float negateFloat(float a) + { + return -a; + } + + public static long negateLong(long a) + { + return -a; + } +}