1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of Railt package. |
4
|
|
|
* |
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
6
|
|
|
* file that was distributed with this source code. |
7
|
|
|
*/ |
8
|
|
|
declare(strict_types=1); |
9
|
|
|
|
10
|
|
|
namespace Railt\SDL\Backend; |
11
|
|
|
|
12
|
|
|
use Psr\Log\LoggerAwareInterface; |
13
|
|
|
use Psr\Log\LoggerAwareTrait; |
14
|
|
|
use Railt\Io\Readable; |
15
|
|
|
use Railt\Reflection\Contracts\Document as DocumentInterface; |
16
|
|
|
use Railt\Reflection\Contracts\Reflection; |
17
|
|
|
use Railt\Reflection\Document; |
18
|
|
|
use Railt\SDL\Exception\VmException; |
19
|
|
|
use Railt\SDL\Frontend\IR\Opcode; |
20
|
|
|
use Railt\SDL\Frontend\IR\OpcodeInterface; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Class Vm |
24
|
|
|
*/ |
25
|
|
|
class Vm implements LoggerAwareInterface |
26
|
|
|
{ |
27
|
|
|
use LoggerAwareTrait; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var Reflection |
31
|
|
|
*/ |
32
|
|
|
private $root; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Process constructor. |
36
|
|
|
* @param Reflection $root |
37
|
|
|
*/ |
38
|
|
|
public function __construct(Reflection $root) |
39
|
|
|
{ |
40
|
|
|
$this->root = $root; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @param Readable $file |
45
|
|
|
* @param iterable $opcodes |
46
|
|
|
* @return Document|DocumentInterface |
47
|
|
|
* @throws \Railt\Io\Exception\ExternalFileException |
48
|
|
|
*/ |
49
|
|
|
public function run(Readable $file, iterable $opcodes): Document |
|
|
|
|
50
|
|
|
{ |
51
|
|
|
foreach ($opcodes as $opcode) { |
|
|
|
|
52
|
|
|
//$opcode->exec(); |
|
|
|
|
53
|
|
|
} |
54
|
|
|
die(42); |
55
|
|
|
|
56
|
|
|
if ($result instanceof DocumentInterface) { |
|
|
|
|
57
|
|
|
return $result; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
$message = 'Zero VM Stack index contains a non-compatible with %s value'; |
61
|
|
|
throw new VmException($opcode, $message, DocumentInterface::class); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @param OpcodeInterface $opcode |
66
|
|
|
* @param mixed $result |
67
|
|
|
*/ |
68
|
|
|
private function log(OpcodeInterface $opcode, $result): void |
|
|
|
|
69
|
|
|
{ |
70
|
|
|
$value = \gettype($result); |
71
|
|
|
|
72
|
|
|
if (\is_object($result)) { |
73
|
|
|
$value = \get_class($result) . '#' . \spl_object_hash($result); |
74
|
|
|
|
75
|
|
|
if (\method_exists($result, '__toString')) { |
76
|
|
|
$value = (string)$result; |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
$message = \sprintf('%4s = %s', '#' . $opcode->getId(), $value); |
81
|
|
|
|
82
|
|
|
$this->logger->debug($message); |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.