-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathCompiler.php
More file actions
119 lines (98 loc) Β· 3.88 KB
/
Compiler.php
File metadata and controls
119 lines (98 loc) Β· 3.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
<?php
declare(strict_types=1);
namespace PHPJava\Compiler;
use PHPJava\Compiler\Builder\Segments\AccessFlags;
use PHPJava\Compiler\Builder\Segments\Attributes\Attributes;
use PHPJava\Compiler\Builder\Segments\Attributes\AttributesCount;
use PHPJava\Compiler\Builder\Segments\ConstantPool;
use PHPJava\Compiler\Builder\Segments\ConstantPoolCount;
use PHPJava\Compiler\Builder\Segments\Fields;
use PHPJava\Compiler\Builder\Segments\FieldsCount;
use PHPJava\Compiler\Builder\Segments\Interfaces;
use PHPJava\Compiler\Builder\Segments\InterfacesCount;
use PHPJava\Compiler\Builder\Segments\MagicByte;
use PHPJava\Compiler\Builder\Segments\MajorVersion;
use PHPJava\Compiler\Builder\Segments\Methods;
use PHPJava\Compiler\Builder\Segments\MethodsCount;
use PHPJava\Compiler\Builder\Segments\MinorVersion;
use PHPJava\Compiler\Builder\Segments\SuperClass;
use PHPJava\Compiler\Builder\Segments\ThisClass;
use PHPJava\Compiler\Builder\Structures\ClassFileStructure;
use PHPJava\Core\JVM\Stream\BinaryWriter;
use PHPJava\Exceptions\CompilerException;
class Compiler
{
/**
* @var ClassFileStructure
*/
private $classFileStructureBuilder;
public function __construct(ClassFileStructure $classFileStructureBuilder)
{
$this->classFileStructureBuilder = $classFileStructureBuilder;
}
public function compile($stream): void
{
if (!is_resource($stream)) {
throw new CompilerException('The stream is not a resource.');
}
$binaryWriter = new BinaryWriter($stream);
if (!$binaryWriter->isWritable()) {
throw new CompilerException('The stream is not writable.');
}
$binaryWriter->enableBuffer(true);
// Write magic
MagicByte::init($this->classFileStructureBuilder, $binaryWriter)
->build();
// Write minor_version
MinorVersion::init($this->classFileStructureBuilder, $binaryWriter)
->build();
// Write major_version
MajorVersion::init($this->classFileStructureBuilder, $binaryWriter)
->build();
// Write constant_pool_count
ConstantPoolCount::init($this->classFileStructureBuilder, $binaryWriter)
->build();
// Write constant_pool
ConstantPool::init($this->classFileStructureBuilder, $binaryWriter)
->build();
// Write access_flags
AccessFlags::init($this->classFileStructureBuilder, $binaryWriter)
->build();
// Write this_class
ThisClass::init($this->classFileStructureBuilder, $binaryWriter)
->build();
// Write super_class
SuperClass::init($this->classFileStructureBuilder, $binaryWriter)
->build();
// Write interfaces_count
InterfacesCount::init($this->classFileStructureBuilder, $binaryWriter)
->build();
// Write interfaces
Interfaces::init($this->classFileStructureBuilder, $binaryWriter)
->build();
// Write fields_count
FieldsCount::init($this->classFileStructureBuilder, $binaryWriter)
->build();
// Write fields
Fields::init($this->classFileStructureBuilder, $binaryWriter)
->build();
// Write methods_count
MethodsCount::init($this->classFileStructureBuilder, $binaryWriter)
->build();
// Write methods
Methods::init($this->classFileStructureBuilder, $binaryWriter)
->build();
// Build attributes_count
AttributesCount::init(
$this->classFileStructureBuilder->getAttributes(),
$this->classFileStructureBuilder->getConstantPool(),
$binaryWriter
)->build();
// Build attributes
Attributes::init(
$this->classFileStructureBuilder->getAttributes(),
$this->classFileStructureBuilder->getConstantPool(),
$binaryWriter
)->build();
}
}