Skip to content

Commit a720ffc

Browse files
authored
Merge pull request #124 from php-java/fix-for-prints-normalization
Arguments normalization
2 parents 5c5d63e + 4c786d2 commit a720ffc

File tree

10 files changed

+102
-10
lines changed

10 files changed

+102
-10
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?php
2+
namespace PHPJava\Exceptions;
3+
4+
class ConverterException extends \Exception
5+
{
6+
}

src/Kernel/Mnemonics/_arraylength.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ final class _arraylength implements OperationInterface
1313
public function execute(): void
1414
{
1515
$arrayref = $this->popFromOperandStack();
16-
1716
$this->pushToOperandStack(count($arrayref->toArray()));
1817
}
1918
}

src/Kernel/Mnemonics/_iastore.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ final class _iastore implements OperationInterface
1414

1515
public function execute(): void
1616
{
17-
$value = Extractor::realValue($this->popFromOperandStack());
17+
$value = $this->popFromOperandStack();
1818
$index = Extractor::realValue($this->popFromOperandStack());
1919

2020
/**

src/Kernel/Mnemonics/_invokevirtual.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use PHPJava\Utilities\AttributionResolver;
1111
use PHPJava\Utilities\BinaryTool;
1212
use PHPJava\Utilities\ClassResolver;
13+
use PHPJava\Utilities\Converter;
1314
use PHPJava\Utilities\Formatter;
1415
use PHPJava\Utilities\TypeResolver;
1516

@@ -35,6 +36,11 @@ public function execute(): void
3536
for ($i = $length; $i >= 0; $i--) {
3637
$arguments[$i] = $this->popFromOperandStack();
3738
}
39+
40+
$arguments = Converter::normalizeArguments(
41+
$arguments,
42+
$signature['arguments']
43+
);
3844
}
3945

4046
$invokerClass = $this->popFromOperandStack();

src/Kernel/Mnemonics/_sipush.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
namespace PHPJava\Kernel\Mnemonics;
33

44
use PHPJava\Exceptions\NotImplementedException;
5+
use PHPJava\Kernel\Types\_Short;
56
use PHPJava\Utilities\BinaryTool;
67

78
final class _sipush implements OperationInterface
@@ -11,6 +12,6 @@ final class _sipush implements OperationInterface
1112

1213
public function execute(): void
1314
{
14-
$this->pushToOperandStack($this->readShort());
15+
$this->pushToOperandStack(new _Short($this->readShort()));
1516
}
1617
}

src/Kernel/Types/_Boolean.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,15 @@ class _Boolean extends Type
66
{
77
protected $nameInJava = 'boolean';
88
protected $nameInPHP = 'boolean';
9+
10+
const TRUE = 'true';
11+
const FALSE = 'false';
12+
13+
public function __toString()
14+
{
15+
$value = (int) $this->getValue();
16+
return $value === 1
17+
? static::TRUE
18+
: static::FALSE;
19+
}
920
}

src/Kernel/Types/_Char.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,11 @@
55
class _Char extends Type
66
{
77
protected $nameInJava = 'char';
8-
protected $nameInPHP = 'int';
8+
protected $nameInPHP = 'string';
9+
10+
public function __toString()
11+
{
12+
$value = $this->getValue();
13+
return is_int($value) ? chr($value) : $value;
14+
}
915
}

src/Utilities/Converter.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
namespace PHPJava\Utilities;
3+
4+
use PHPJava\Exceptions\ConverterException;
5+
use PHPJava\Kernel\Types\_Array\Collection;
6+
use PHPJava\Kernel\Types\Type;
7+
8+
class Converter
9+
{
10+
11+
/**
12+
* @param array $arguments
13+
* @param array $acceptedArguments
14+
* @return array
15+
* @throws ConverterException
16+
*/
17+
public static function normalizeArguments(array $arguments, array $acceptedArguments): array
18+
{
19+
if (count($arguments) !== count($acceptedArguments)) {
20+
throw new ConverterException('Does not match arguments.');
21+
}
22+
23+
foreach ($arguments as $key => &$argument) {
24+
/**
25+
* @var Type|Collection $argument
26+
*/
27+
if ($argument instanceof Collection) {
28+
// TODO: convert an array contents.
29+
continue;
30+
}
31+
$realType = $acceptedArguments[$key] ?? null;
32+
if ($realType === null) {
33+
throw new ConverterException('Broken arguments parser.');
34+
}
35+
if ($realType['type'] === 'class') {
36+
// TODO: implements up-cast and down-cast
37+
continue;
38+
}
39+
$initiateClass = TypeResolver::TYPES_MAP[$realType['type']];
40+
if ($argument instanceof $initiateClass) {
41+
continue;
42+
}
43+
$argument = new $initiateClass(Extractor::realValue($argument));
44+
}
45+
46+
return $arguments;
47+
}
48+
}

src/Utilities/TypeResolver.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55
use PHPJava\Core\JVM\Parameters\GlobalOptions;
66
use PHPJava\Core\JVM\Parameters\Runtime;
77
use PHPJava\Exceptions\TypeException;
8+
use PHPJava\Kernel\Types\_Byte;
9+
use PHPJava\Kernel\Types\_Char;
10+
use PHPJava\Kernel\Types\_Long;
11+
use PHPJava\Kernel\Types\_Short;
812
use PHPJava\Packages\java\lang\_Object;
913
use PHPJava\Packages\java\lang\_String;
1014
use PHPJava\Kernel\Types\_Array\Collection;
@@ -45,6 +49,17 @@ class TypeResolver
4549
'L' => 'class',
4650
];
4751

52+
const TYPES_MAP = [
53+
'byte' => _Byte::class,
54+
'char' => _Char::class,
55+
'double' => _Double::class,
56+
'float' => _Float::class,
57+
'int' => _Int::class,
58+
'long' => _Long::class,
59+
'short' => _Short::class,
60+
'boolean' => _Boolean::class,
61+
];
62+
4863
const PHP_TO_JAVA_MAP = [
4964
'integer' => 'int',
5065
'string' => 'java.lang.String',

tests/Packages/JavaIoPrintStreamClassTest.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public function testPrintlnWithStringParams()
3434
public function testPrintlnWithCharParams()
3535
{
3636
$result = $this->call(explode('::', __METHOD__)[1]);
37-
$this->assertEquals("65\n", $result);
37+
$this->assertEquals("A\n", $result);
3838
}
3939

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

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

6767
public function testPrintWithStringParams()
@@ -73,7 +73,7 @@ public function testPrintWithStringParams()
7373
public function testPrintWithCharParams()
7474
{
7575
$result = $this->call(explode('::', __METHOD__)[1]);
76-
$this->assertEquals("65", $result);
76+
$this->assertEquals("A", $result);
7777
}
7878

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

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

0 commit comments

Comments
 (0)