Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Fix mnemonics and wrap Kernel\Types for scalar values
  • Loading branch information
m3m0r7 committed Apr 5, 2019
commit 257fc9be864eb35243e4f2dbe6a34388b0e8dcf2
18 changes: 9 additions & 9 deletions src/Kernel/Mnemonics/_iadd.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
use PHPJava\Exceptions\NotImplementedException;
use PHPJava\Kernel\Types\_Int;
use PHPJava\Utilities\BinaryTool;
use PHPJava\Utilities\Extractor;

final class _iadd implements OperationInterface
{
Expand All @@ -15,14 +16,13 @@ public function execute(): void
$rightValue = $this->getStack();
$leftValue = $this->getStack();

if ($leftValue instanceof _Int) {
$leftValue = $leftValue->getValue();
}

if ($rightValue instanceof _Int) {
$rightValue = $rightValue->getValue();
}

$this->pushStack(BinaryTool::add($leftValue, $rightValue));
$this->pushStack(
new _Int(
BinaryTool::add(
Extractor::realValue($leftValue),
Extractor::realValue($rightValue)
)
)
);
}
}
14 changes: 13 additions & 1 deletion src/Kernel/Mnemonics/_idiv.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
namespace PHPJava\Kernel\Mnemonics;

use PHPJava\Exceptions\NotImplementedException;
use PHPJava\Kernel\Types\_Double;
use PHPJava\Utilities\BinaryTool;
use PHPJava\Utilities\Extractor;

final class _idiv implements OperationInterface
{
Expand All @@ -11,6 +13,16 @@ final class _idiv implements OperationInterface

public function execute(): void
{
throw new NotImplementedException(__CLASS__);
$value2 = $this->getStack();
$value1 = $this->getStack();

$this->pushStack(
new _Double(
BinaryTool::div(
Extractor::realValue($value1),
Extractor::realValue($value2)
)
)
);
}
}
11 changes: 10 additions & 1 deletion src/Kernel/Mnemonics/_imul.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
namespace PHPJava\Kernel\Mnemonics;

use PHPJava\Exceptions\NotImplementedException;
use PHPJava\Kernel\Types\_Int;
use PHPJava\Utilities\BinaryTool;
use PHPJava\Utilities\Extractor;

final class _imul implements OperationInterface
{
Expand All @@ -14,6 +16,13 @@ public function execute(): void
$value2 = $this->getStack();
$value1 = $this->getStack();

$this->pushStack(BinaryTool::multiply($value1, $value2, 4));
$this->pushStack(
new _Int(
BinaryTool::multiply(
Extractor::realValue($value1),
Extractor::realValue($value2)
)
)
);
}
}
5 changes: 4 additions & 1 deletion src/Kernel/Mnemonics/_ireturn.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ final class _ireturn implements OperationInterface
*/
public function execute()
{
return new _Int($this->getStack());
$value = $this->getStack();
return ($value instanceof _Int)
? $value
: new _Int($value);
}
}
11 changes: 10 additions & 1 deletion src/Kernel/Mnemonics/_isub.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
namespace PHPJava\Kernel\Mnemonics;

use PHPJava\Exceptions\NotImplementedException;
use PHPJava\Kernel\Types\_Int;
use PHPJava\Utilities\BinaryTool;
use PHPJava\Utilities\Extractor;

final class _isub implements OperationInterface
{
Expand All @@ -14,6 +16,13 @@ public function execute(): void
$rightValue = $this->getStack();
$leftValue = $this->getStack();

$this->pushStack(BinaryTool::sub($leftValue, $rightValue, 4));
$this->pushStack(
new _Int(
BinaryTool::sub(
Extractor::realValue($leftValue),
Extractor::realValue($rightValue)
)
)
);
}
}
11 changes: 10 additions & 1 deletion src/Kernel/Mnemonics/_ladd.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
namespace PHPJava\Kernel\Mnemonics;

use PHPJava\Exceptions\NotImplementedException;
use PHPJava\Kernel\Types\_Long;
use PHPJava\Utilities\BinaryTool;
use PHPJava\Utilities\Extractor;

final class _ladd implements OperationInterface
{
Expand All @@ -14,6 +16,13 @@ public function execute(): void
$value2 = $this->getStack();
$value1 = $this->getStack();

$this->pushStack(BinaryTool::add($value1, $value2));
$this->pushStack(
new _Long(
BinaryTool::add(
Extractor::realValue($value1),
Extractor::realValue($value2)
)
)
);
}
}
14 changes: 13 additions & 1 deletion src/Kernel/Mnemonics/_ldiv.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
namespace PHPJava\Kernel\Mnemonics;

use PHPJava\Exceptions\NotImplementedException;
use PHPJava\Kernel\Types\_Double;
use PHPJava\Utilities\BinaryTool;
use PHPJava\Utilities\Extractor;

final class _ldiv implements OperationInterface
{
Expand All @@ -11,6 +13,16 @@ final class _ldiv implements OperationInterface

public function execute(): void
{
throw new NotImplementedException(__CLASS__);
$value2 = $this->getStack();
$value1 = $this->getStack();

$this->pushStack(
new _Double(
BinaryTool::div(
Extractor::realValue($value1),
Extractor::realValue($value2)
)
)
);
}
}
11 changes: 10 additions & 1 deletion src/Kernel/Mnemonics/_lmul.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
namespace PHPJava\Kernel\Mnemonics;

use PHPJava\Exceptions\NotImplementedException;
use PHPJava\Kernel\Types\_Long;
use PHPJava\Utilities\BinaryTool;
use PHPJava\Utilities\Extractor;

final class _lmul implements OperationInterface
{
Expand All @@ -14,6 +16,13 @@ public function execute(): void
$value2 = $this->getStack();
$value1 = $this->getStack();

$this->pushStack(BinaryTool::multiply($value1, $value2, 8));
$this->pushStack(
new _Long(
BinaryTool::multiply(
Extractor::realValue($value1),
Extractor::realValue($value2)
)
)
);
}
}
8 changes: 6 additions & 2 deletions src/Kernel/Mnemonics/_lreturn.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,19 @@
namespace PHPJava\Kernel\Mnemonics;

use PHPJava\Exceptions\NotImplementedException;
use PHPJava\Kernel\Types\_Long;
use PHPJava\Utilities\BinaryTool;

final class _lreturn implements OperationInterface
{
use \PHPJava\Kernel\Core\Accumulator;
use \PHPJava\Kernel\Core\ConstantPool;

public function execute(): void
public function execute()
{
return new JavaTypeLong($this->getStack());
$value = $this->getStack();
return ($value instanceof _Long)
? $value
: new _Long($value);
}
}
11 changes: 10 additions & 1 deletion src/Kernel/Mnemonics/_lsub.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
namespace PHPJava\Kernel\Mnemonics;

use PHPJava\Exceptions\NotImplementedException;
use PHPJava\Kernel\Types\_Long;
use PHPJava\Utilities\BinaryTool;
use PHPJava\Utilities\Extractor;

final class _lsub implements OperationInterface
{
Expand All @@ -14,6 +16,13 @@ public function execute(): void
$value2 = $this->getStack();
$value1 = $this->getStack();

$this->pushStack(BinaryTool::sub($value1, $value2, 8));
$this->pushStack(
new _Long(
BinaryTool::sub(
Extractor::realValue($value1),
Extractor::realValue($value2)
)
)
);
}
}
13 changes: 13 additions & 0 deletions src/Kernel/Types/Type.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,25 @@
<?php
namespace PHPJava\Kernel\Types;

use PHPJava\Exceptions\TypeException;

class Type
{
private $value = null;

public function __construct($value)
{
// Validate value which is scalar.
if (
!is_int($value) &&
!is_float($value) &&
!is_string($value) &&
!is_bool($value)
) {
throw new TypeException(
'Passed value is not scalar. The value is "' . gettype($value) . '"'
);
}
$this->value = $value;
}

Expand Down
14 changes: 3 additions & 11 deletions src/Utilities/BinaryTool.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,8 @@ final public static function negate($value, $bytes)
return ($convert[0] === '1' ? '-' : '') . base_convert($convert, 2, 10);
}

final public static function multiply($value1, $value2, $bytes)
final public static function multiply($value1, $value2)
{
$value1 = (int) $value1;
$value2 = (int) $value2;
if (function_exists('gmp_mul')) {
$a = gmp_init($value1);
$b = gmp_init($value2);
Expand All @@ -81,8 +79,6 @@ final public static function multiply($value1, $value2, $bytes)

final public static function add($value1, $value2)
{
$value1 = (int) $value1;
$value2 = (int) $value2;
if (function_exists('gmp_add')) {
$a = gmp_init($value1);
$b = gmp_init($value2);
Expand All @@ -92,10 +88,8 @@ final public static function add($value1, $value2)
return $value1 + $value2;
}

final public static function sub($value1, $value2, $bytes)
final public static function sub($value1, $value2)
{
$value1 = (int) $value1;
$value2 = (int) $value2;
if (function_exists('gmp_sub')) {
$a = gmp_init($value1);
$b = gmp_init($value2);
Expand All @@ -105,10 +99,8 @@ final public static function sub($value1, $value2, $bytes)
return $value1 - $value2;
}

final public static function div($value1, $value2, $bytes)
final public static function div($value1, $value2)
{
$value1 = (int) $value1;
$value2 = (int) $value2;
if (function_exists('gmp_div')) {
$a = gmp_init($value1);
$b = gmp_init($value2);
Expand Down
15 changes: 15 additions & 0 deletions src/Utilities/Extractor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php
namespace PHPJava\Utilities;

use PHPJava\Kernel\Types\Type;

class Extractor
{
public static function realValue($value)
{
if ($value instanceof Type) {
return $value->getValue();
}
return $value;
}
}