Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
6 changes: 6 additions & 0 deletions src/Exceptions/ConverterException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php
namespace PHPJava\Exceptions;

class ConverterException extends \Exception
{
}
1 change: 0 additions & 1 deletion src/Kernel/Mnemonics/_arraylength.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ final class _arraylength implements OperationInterface
public function execute(): void
{
$arrayref = $this->popFromOperandStack();

$this->pushToOperandStack(count($arrayref->toArray()));
}
}
2 changes: 1 addition & 1 deletion src/Kernel/Mnemonics/_iastore.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ final class _iastore implements OperationInterface

public function execute(): void
{
$value = Extractor::realValue($this->popFromOperandStack());
$value = $this->popFromOperandStack();
$index = Extractor::realValue($this->popFromOperandStack());

/**
Expand Down
6 changes: 6 additions & 0 deletions src/Kernel/Mnemonics/_invokevirtual.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use PHPJava\Utilities\AttributionResolver;
use PHPJava\Utilities\BinaryTool;
use PHPJava\Utilities\ClassResolver;
use PHPJava\Utilities\Converter;
use PHPJava\Utilities\Formatter;
use PHPJava\Utilities\TypeResolver;

Expand All @@ -35,6 +36,11 @@ public function execute(): void
for ($i = $length; $i >= 0; $i--) {
$arguments[$i] = $this->popFromOperandStack();
}

$arguments = Converter::normalizeArguments(
$arguments,
$signature['arguments']
);
}

$invokerClass = $this->popFromOperandStack();
Expand Down
3 changes: 2 additions & 1 deletion src/Kernel/Mnemonics/_sipush.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
namespace PHPJava\Kernel\Mnemonics;

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

final class _sipush implements OperationInterface
Expand All @@ -11,6 +12,6 @@ final class _sipush implements OperationInterface

public function execute(): void
{
$this->pushToOperandStack($this->readShort());
$this->pushToOperandStack(new _Short($this->readShort()));
}
}
11 changes: 11 additions & 0 deletions src/Kernel/Types/_Boolean.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,15 @@ class _Boolean extends Type
{
protected $nameInJava = 'boolean';
protected $nameInPHP = 'boolean';

const TRUE = 'true';
const FALSE = 'false';

public function __toString()
{
$value = (int) $this->getValue();
return $value === 1
? static::TRUE
: static::FALSE;
}
}
8 changes: 7 additions & 1 deletion src/Kernel/Types/_Char.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,11 @@
class _Char extends Type
{
protected $nameInJava = 'char';
protected $nameInPHP = 'int';
protected $nameInPHP = 'string';

public function __toString()
{
$value = $this->getValue();
return is_int($value) ? chr($value) : $value;
}
}
48 changes: 48 additions & 0 deletions src/Utilities/Converter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php
namespace PHPJava\Utilities;

use PHPJava\Exceptions\ConverterException;
use PHPJava\Kernel\Types\_Array\Collection;
use PHPJava\Kernel\Types\Type;

class Converter
{

/**
* @param array $arguments
* @param array $acceptedArguments
* @return array
* @throws ConverterException
*/
public static function normalizeArguments(array $arguments, array $acceptedArguments): array
{
if (count($arguments) !== count($acceptedArguments)) {
throw new ConverterException('Does not match arguments.');
}

foreach ($arguments as $key => &$argument) {
/**
* @var Type|Collection $argument
*/
if ($argument instanceof Collection) {
// TODO: convert an array contents.
continue;
}
$realType = $acceptedArguments[$key] ?? null;
if ($realType === null) {
throw new ConverterException('Broken arguments parser.');
}
if ($realType['type'] === 'class') {
// TODO: implements up-cast and down-cast
continue;
}
$initiateClass = TypeResolver::TYPES_MAP[$realType['type']];
if ($argument instanceof $initiateClass) {
continue;
}
$argument = new $initiateClass(Extractor::realValue($argument));
}

return $arguments;
}
}
15 changes: 15 additions & 0 deletions src/Utilities/TypeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
use PHPJava\Core\JVM\Parameters\GlobalOptions;
use PHPJava\Core\JVM\Parameters\Runtime;
use PHPJava\Exceptions\TypeException;
use PHPJava\Kernel\Types\_Byte;
use PHPJava\Kernel\Types\_Char;
use PHPJava\Kernel\Types\_Long;
use PHPJava\Kernel\Types\_Short;
use PHPJava\Packages\java\lang\_Object;
use PHPJava\Packages\java\lang\_String;
use PHPJava\Kernel\Types\_Array\Collection;
Expand Down Expand Up @@ -45,6 +49,17 @@ class TypeResolver
'L' => 'class',
];

const TYPES_MAP = [
'byte' => _Byte::class,
'char' => _Char::class,
'double' => _Double::class,
'float' => _Float::class,
'int' => _Int::class,
'long' => _Long::class,
'short' => _Short::class,
'boolean' => _Boolean::class,
];

const PHP_TO_JAVA_MAP = [
'integer' => 'int',
'string' => 'java.lang.String',
Expand Down
12 changes: 6 additions & 6 deletions tests/Packages/JavaIoPrintStreamClassTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public function testPrintlnWithStringParams()
public function testPrintlnWithCharParams()
{
$result = $this->call(explode('::', __METHOD__)[1]);
$this->assertEquals("65\n", $result);
$this->assertEquals("A\n", $result);
}

public function testPrintlnWithCharArrayParams()
Expand All @@ -58,10 +58,10 @@ public function testPrintlnWithDoubleParams()
public function testPrintlnWithBooleanParams()
{
$result = $this->call(explode('::', __METHOD__)[1] . '_true');
$this->assertEquals("1\n", $result);
$this->assertEquals("true\n", $result);

$result = $this->call(explode('::', __METHOD__)[1] . '_false');
$this->assertEquals("0\n", $result);
$this->assertEquals("false\n", $result);
}

public function testPrintWithStringParams()
Expand All @@ -73,7 +73,7 @@ public function testPrintWithStringParams()
public function testPrintWithCharParams()
{
$result = $this->call(explode('::', __METHOD__)[1]);
$this->assertEquals("65", $result);
$this->assertEquals("A", $result);
}

public function testPrintWithCharArrayParams()
Expand All @@ -97,9 +97,9 @@ public function testPrintWithDoubleParams()
public function testPrintWithBooleanParams()
{
$result = $this->call(explode('::', __METHOD__)[1] . '_true');
$this->assertEquals("1", $result);
$this->assertEquals("true", $result);

$result = $this->call(explode('::', __METHOD__)[1] . '_false');
$this->assertEquals("0", $result);
$this->assertEquals("false", $result);
}
}