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
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"description": "JVM emulator by PHP",
"type": "library",
"license": "MIT",
"version": "0.0.6.4-dev",
"version": "0.0.6.5-dev",
"authors": [
{
"name": "memory"
Expand All @@ -17,7 +17,8 @@
"php-java/java-util-package": "*",
"php-java/java-io-package": "*",
"php-java/java-net-package": "*",
"php-java/java-nio-package": "*"
"php-java/java-nio-package": "*",
"gabrielelana/byte-units": "dev-master"
},
"autoload": {
"psr-4": {
Expand Down
15 changes: 15 additions & 0 deletions src/Core/JVM/Cache/Cache.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php
namespace PHPJava\Core\JVM\Cache;

class Cache
{
private $items = [];

public function fetchOrPush(string $key, callable $pushFunction, ...$parameters)
{
if (isset($this->items[$key])) {
return $this->items[$key];
}
return $this->items[$key] = $pushFunction(...$parameters);
}
}
6 changes: 6 additions & 0 deletions src/Core/JVM/Cache/HeapCache.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php
namespace PHPJava\Core\JVM\Cache;

class HeapCache extends Cache
{
}
7 changes: 7 additions & 0 deletions src/Core/JVM/Cache/OperationCache.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php
namespace PHPJava\Core\JVM\Cache;

class OperationCache extends Cache
{

}
57 changes: 45 additions & 12 deletions src/Core/JVM/Invoker/Invokable.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
<?php
namespace PHPJava\Core\JVM\Invoker;

use ByteUnits\Metric;
use Monolog\Handler\StreamHandler;
use Monolog\Logger;
use PHPJava\Core\JavaClass;
use PHPJava\Core\JavaClassInvoker;
use PHPJava\Core\JVM\Cache\OperationCache;
use PHPJava\Core\JVM\FlexibleMethod;
use PHPJava\Core\JVM\Parameters\GlobalOptions;
use PHPJava\Core\JVM\Stream\BinaryReader;
Expand Down Expand Up @@ -218,6 +220,17 @@ function ($item) {
$localStorage
);

$this->debugTool->getLogger()->debug(
vsprintf(
'Used Memory: %s, Used Memory Peak: %s',
[
Metric::bytes(memory_get_usage())->format(),
Metric::bytes(memory_get_peak_usage())->format(),
]
)
);

$operationCache = new OperationCache();
while ($reader->getOffset() < $codeAttribute->getOpCodeLength()) {
if (++$executedCounter > ($this->options['max_stack_exceeded'] ?? GlobalOptions::get('max_stack_exceeded') ?? Runtime::MAX_STACK_EXCEEDED)) {
throw new RuntimeException(
Expand Down Expand Up @@ -248,6 +261,16 @@ function ($item) {
$debugTraces['mnemonic_indexes'][] = $pointer;
}

$this->debugTool->getLogger()->debug(
vsprintf(
'Used Memory: %s, Used Memory Peak: %s',
[
Metric::bytes(memory_get_usage())->format(),
Metric::bytes(memory_get_peak_usage())->format(),
]
)
);

$this->debugTool->getLogger()->debug(
vsprintf(
'OpCode: 0x%02X, Mnemonic: %s, Stacks: %d, PC: %d',
Expand All @@ -274,17 +297,23 @@ function ($item) {
/**
* @var OperationInterface|Accumulator|ConstantPool $executor
*/
$executor = new $fullName();
$executor->setConstantPool($currentConstantPool);
$executor->setParameters(
$method->getAttributes(),
$this->javaClassInvoker,
$reader,
$localStorage,
$stacks,
$pointer
$executor = $operationCache->fetchOrPush(
$fullName,
function () use ($fullName) {
return new $fullName();
}
);
$returnValue = $executor->execute();
$returnValue = $executor
->setConstantPool($currentConstantPool)
->setParameters(
$method->getAttributes(),
$this->javaClassInvoker,
$reader,
$localStorage,
$stacks,
$pointer
)
->execute();

$afterTrigger = $this->options['operations']['injections']['after'] ?? GlobalOptions::get('operations.injections.after');
if (is_callable($afterTrigger)) {
Expand All @@ -298,13 +327,17 @@ function ($item) {
}

if ($returnValue !== null) {
$this->javaClassInvoker->getJavaClass()->appendDebug($debugTraces);
if ($isEnabledTrace) {
$this->javaClassInvoker->getJavaClass()->appendDebug($debugTraces);
}
$this->debugTool->getLogger()->info('Finish operations: ' . $methodBeautified);
return $returnValue;
}
}

$this->javaClassInvoker->getJavaClass()->appendDebug($debugTraces);
if ($isEnabledTrace) {
$this->javaClassInvoker->getJavaClass()->appendDebug($debugTraces);
}
$this->debugTool->getLogger()->info('Finish operations: ' . $methodBeautified);
return null;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Core/JVM/Parameters/GlobalOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public static function get($name)
return $ref;
}

public function reset()
public static function reset()
{
static::$settings = [];
}
Expand Down
2 changes: 1 addition & 1 deletion src/Core/JVM/Parameters/Runtime.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ final class Runtime
const PRELOAD = false;
const DRY_RUN_ATTRIBUTE = false;

const OPERATIONS_ENABLE_TRACE = true;
const OPERATIONS_ENABLE_TRACE = false;
const OPERATIONS_TEMPORARY_CODE_STREAM = 'php://memory';

const VALIDATION_METHOD_ARGUMENTS_COUNT_ONLY = false;
Expand Down
9 changes: 9 additions & 0 deletions src/Core/JavaClass.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@
use PHPJava\Core\JVM\ActiveInterface;
use PHPJava\Core\JVM\ActiveMethods;
use PHPJava\Core\JVM\ConstantPool;
use PHPJava\Core\JVM\Parameters\GlobalOptions;
use PHPJava\Core\JVM\Parameters\Runtime;
use PHPJava\Core\JVM\Validations\MagicByte;
use PHPJava\Core\Stream\Reader\ReaderInterface;
use PHPJava\Exceptions\DebugTraceIsDisabledException;
use PHPJava\Exceptions\ValidatorException;
use PHPJava\Kernel\Attributes\AttributeInterface;
use PHPJava\Kernel\Attributes\InnerClassesAttribute;
Expand Down Expand Up @@ -302,6 +305,12 @@ public function getSuperClass()

public function debug(): void
{
$isEnabledTrace = $this->options['operations']['enable_trace'] ?? GlobalOptions::get('operations.enable_trace') ?? Runtime::OPERATIONS_ENABLE_TRACE;
if (!$isEnabledTrace) {
throw new DebugTraceIsDisabledException(
"Debug trace is disabled. If you want to show debug trace then enable to `enable_trace` option."
);
}
$cpInfo = $this->getConstantPool();
foreach ($this->debugTraces as $debugTraces) {
printf("[method]\n");
Expand Down
2 changes: 1 addition & 1 deletion src/Core/PHPJava.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@ final class PHPJava
/**
* As same as composer version.
*/
const VERSION = 0x000064;
const VERSION = 0x000065;
}
6 changes: 6 additions & 0 deletions src/Exceptions/DebugTraceIsDisabledException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php
namespace PHPJava\Exceptions;

class DebugTraceIsDisabledException extends \Exception
{
}
1 change: 1 addition & 0 deletions src/Kernel/Types/Type.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php
namespace PHPJava\Kernel\Types;

use PHPJava\Core\JVM\Cache\HeapCache;
use PHPJava\Exceptions\TypeException;

class Type
Expand Down
9 changes: 9 additions & 0 deletions tests/OutputDebugTraceTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php
namespace PHPJava\Tests;

use PHPJava\Core\JVM\Parameters\GlobalOptions;
use PHPUnit\Framework\TestCase;

class OutputDebugTraceTest extends Base
Expand All @@ -13,6 +14,12 @@ public function testCallMain()
{
ob_start();

GlobalOptions::set([
'operations' => [
'enable_trace' => true,
],
]);

$calculatedValue = $this->initiatedJavaClasses['OutputDebugTraceTest']
->getInvoker()
->getStatic()
Expand All @@ -30,5 +37,7 @@ public function testCallMain()
file_get_contents(__DIR__ . '/templates/DebugTraceTest.txt'),
$result
);

GlobalOptions::reset();
}
}