|
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\JoinedOpcode; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Class Vm |
|
23
|
|
|
*/ |
|
24
|
|
|
class Vm implements LoggerAwareInterface |
|
25
|
|
|
{ |
|
26
|
|
|
use LoggerAwareTrait; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @var Reflection |
|
30
|
|
|
*/ |
|
31
|
|
|
private $root; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Process constructor. |
|
35
|
|
|
* @param Reflection $root |
|
36
|
|
|
*/ |
|
37
|
|
|
public function __construct(Reflection $root) |
|
38
|
|
|
{ |
|
39
|
|
|
$this->root = $root; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @param Readable $file |
|
44
|
|
|
* @param iterable $opcodes |
|
45
|
|
|
* @return Document|DocumentInterface |
|
46
|
|
|
* @throws \Railt\Io\Exception\ExternalFileException |
|
47
|
|
|
*/ |
|
48
|
|
|
public function run(Readable $file, iterable $opcodes): Document |
|
49
|
|
|
{ |
|
50
|
|
|
$runtime = new Runtime($this->root, $file); |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @var JoinedOpcode $opcode |
|
54
|
|
|
* @var mixed $result |
|
55
|
|
|
*/ |
|
56
|
|
|
foreach ($runtime->execute($opcodes) as $opcode => $result) { |
|
57
|
|
|
if ($this->logger) { |
|
58
|
|
|
$value = \get_class($result) . '#' . \spl_object_hash($result); |
|
59
|
|
|
$message = \sprintf('%4s = %s', '#' . $opcode->getId(), $value); |
|
|
|
|
|
|
60
|
|
|
|
|
61
|
|
|
$this->logger->debug($message); |
|
62
|
|
|
} |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
$result = $runtime->get(0); |
|
66
|
|
|
|
|
67
|
|
|
if ($result instanceof DocumentInterface) { |
|
68
|
|
|
return $result; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
throw new VmException($opcode, 'Zero VM Stack index contains a non-compatible with %s value', |
|
72
|
|
|
DocumentInterface::class); |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
|
Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.