packages/deriver/tests/Unit/Deriver/Evaluator/Memory/SlotsTest.php

1<?php
2
3declare (strict_types=1);
4
5namespace Tests\Unit\Deriver\Evaluator\Memory;
6
7use Deriver\Deriver\Contributions;
8use Deriver\Deriver\DeriverConfig;
9use Deriver\Deriver\Evaluator\Call\BoundInstruction;
10use Deriver\Deriver\Evaluator\Call\Evaluators;
11use Deriver\Deriver\Evaluator\Call\Registrations;
12use Deriver\Deriver\Evaluator\Call\State\StateSlot;
13use Deriver\Deriver\Evaluator\Evaluator;
14use Deriver\Deriver\Evaluator\EvaluatorConfig;
15use Deriver\Deriver\Evaluator\Index\DefinitionIndex;
16use Deriver\Deriver\Evaluator\Memory\Slots;
17use Deriver\Deriver\Evaluator\Request\Budget;
18use Deriver\Deriver\Evaluator\Scope\Context;
19use Deriver\Deriver\Evaluator\Scope\Frame;
20use Deriver\Deriver\Evaluator\TargetProfile;
21use Deriver\Deriver\Evaluator\Term;
22use Deriver\Deriver\Evaluator\TermKind;
23use Deriver\Deriver\Lowerer\Declaration\Index;
24use Deriver\Deriver\MicroEvaluatorConversion;
25use Deriver\Deriver\Parser\Node\File;
26use Deriver\Deriver\Parser\Parser;
27use Deriver\Deriver\Parser\ParserConfig;
28use Deriver\Deriver\Providers\ProviderExpansion;
29use Deriver\Deriver\Resolver\Resolver;
30use Deriver\Deriver\Sources\Path;
31use Deriver\Deriver\Sources\Source;
32use Deriver\Deriver\Sources\Sources;
33use PHPUnit\Framework\Attributes\CoversNothing;
34use PHPUnit\Framework\TestCase;
35
36#[CoversNothing]
37final class SlotsTest extends TestCase
38{
39    public function testReadRetainsAnExternalStateReference(): void
40    {
41        $source = 'function target($x){return $x;}';
42        $configuration = new DeriverConfig();
43        $budget = new Budget();
44        $contributions = new Contributions();
45        $target = new TargetProfile($configuration->phpVersion, $configuration->integerBits, $configuration->floatPrecision);
46        $providers = new ProviderExpansion(new Sources([new Source('candidate.php', '<?php ' . $source)]), array_map(Term::fromNative(...), $configuration->environment), $contributions);
47        $contributions1 = $providers->contributions;
48        $evaluators = new Evaluators(new TargetProfile($configuration->phpVersion, $configuration->integerBits, $configuration->floatPrecision), new Registrations($contributions1->evaluators, $contributions1->providers, $contributions1->domains, $contributions1->intrinsics, $contributions1->stateSlots), $configuration->standardEvaluators);
49        $settings = new EvaluatorConfig($target, $providers->environment, $configuration->standardEvaluators, array_map((new MicroEvaluatorConversion())->convert(...), $configuration->evaluators));
50        $snapshotId = 'test';
51        $input = $providers->input;
52        $parser = new Parser(new ParserConfig($target->version));
53        $trees = array_map(static fn (Source $source1): File => (new Resolver())->resolve($parser->parse(new Source(Path::normalize($source1->path), $source1->contents, $source1->declarationsOnly))), iterator_to_array($input, false));
54        $program = new Index($snapshotId, $trees);
55        $e = new Evaluator(new Context(new DefinitionIndex($program), $settings, $evaluators, $budget));
56        $symbol = 'target';
57        $graph = $e->context->index->graph($symbol);
58        self::assertNotNull($graph);
59        $f = new Frame($graph, 'source:' . $symbol);
60        $operation = 'read';
61        $instruction = array_values(array_filter($f->graph->definitions, static fn ($instruction): bool => $instruction->operation === $operation))[0] ?? null;
62        $local = $instruction;
63        self::assertNotNull($local);
64        $a = new BoundInstruction('slot', 'evaluator-state-address', $f->graph->body->source, operands: [$local->result], name: 'example.state');
65        $v = (new Slots($e))->read($f, $a, 64);
66        self::assertSame(TermKind::StateRead, $v->kind);
67        self::assertSame('EXTERNAL_STATE', $v->operands[1]->attributes['reason']);
68    }
69    public function testBeforeUsesOnlyTheRequestedAllocatedDefault(): void
70    {
71        $source = 'function target(){return 1;}';
72        $configuration = new DeriverConfig();
73        $budget = new Budget();
74        $contributions = new Contributions(stateSlots: [new StateSlot('example.count', 'int', Term::constant(3))]);
75        $target = new TargetProfile($configuration->phpVersion, $configuration->integerBits, $configuration->floatPrecision);
76        $providers = new ProviderExpansion(new Sources([new Source('candidate.php', '<?php ' . $source)]), array_map(Term::fromNative(...), $configuration->environment), $contributions);
77        $contributions1 = $providers->contributions;
78        $evaluators = new Evaluators(new TargetProfile($configuration->phpVersion, $configuration->integerBits, $configuration->floatPrecision), new Registrations($contributions1->evaluators, $contributions1->providers, $contributions1->domains, $contributions1->intrinsics, $contributions1->stateSlots), $configuration->standardEvaluators);
79        $settings = new EvaluatorConfig($target, $providers->environment, $configuration->standardEvaluators, array_map((new MicroEvaluatorConversion())->convert(...), $configuration->evaluators));
80        $snapshotId = 'test';
81        $input = $providers->input;
82        $parser = new Parser(new ParserConfig($target->version));
83        $trees = array_map(static fn (Source $source1): File => (new Resolver())->resolve($parser->parse(new Source(Path::normalize($source1->path), $source1->contents, $source1->declarationsOnly))), iterator_to_array($input, false));
84        $program = new Index($snapshotId, $trees);
85        $e = new Evaluator(new Context(new DefinitionIndex($program), $settings, $evaluators, $budget));
86        $symbol = 'target';
87        $graph = $e->context->index->graph($symbol);
88        self::assertNotNull($graph);
89        $v = (new Slots($e))->before(new Frame($graph, 'source:' . $symbol), new Term(TermKind::Object, 'allocation'), 'example.count', 0, 0, 64);
90        self::assertSame(3, $v->native());
91    }
92    public function testEffectIgnoresUnmodeledUnrelatedSlots(): void
93    {
94        $source = 'function target($x){return $x->missing();}';
95        $configuration = new DeriverConfig();
96        $budget = new Budget();
97        $contributions = new Contributions();
98        $target = new TargetProfile($configuration->phpVersion, $configuration->integerBits, $configuration->floatPrecision);
99        $providers = new ProviderExpansion(new Sources([new Source('candidate.php', '<?php ' . $source)]), array_map(Term::fromNative(...), $configuration->environment), $contributions);
100        $contributions1 = $providers->contributions;
101        $evaluators = new Evaluators(new TargetProfile($configuration->phpVersion, $configuration->integerBits, $configuration->floatPrecision), new Registrations($contributions1->evaluators, $contributions1->providers, $contributions1->domains, $contributions1->intrinsics, $contributions1->stateSlots), $configuration->standardEvaluators);
102        $settings = new EvaluatorConfig($target, $providers->environment, $configuration->standardEvaluators, array_map((new MicroEvaluatorConversion())->convert(...), $configuration->evaluators));
103        $snapshotId = 'test';
104        $input = $providers->input;
105        $parser = new Parser(new ParserConfig($target->version));
106        $trees = array_map(static fn (Source $source1): File => (new Resolver())->resolve($parser->parse(new Source(Path::normalize($source1->path), $source1->contents, $source1->declarationsOnly))), iterator_to_array($input, false));
107        $program = new Index($snapshotId, $trees);
108        $e = new Evaluator(new Context(new DefinitionIndex($program), $settings, $evaluators, $budget));
109        $symbol = 'target';
110        $graph = $e->context->index->graph($symbol);
111        self::assertNotNull($graph);
112        $f = new Frame($graph, 'source:' . $symbol);
113        $operation = 'invoke-method';
114        $instruction = array_values(array_filter($f->graph->definitions, static fn ($instruction): bool => $instruction->operation === $operation))[0] ?? null;
115        self::assertNotNull($instruction);
116        self::assertNull((new Slots($e))->effect($f, $instruction, 'example.slot', 64));
117    }
118    public function testInitialRetainsTheExternalReceiver(): void
119    {
120        $source = 'function target($x){return $x+2;}';
121        $configuration = new DeriverConfig();
122        $budget = new Budget();
123        $contributions = new Contributions();
124        $target = new TargetProfile($configuration->phpVersion, $configuration->integerBits, $configuration->floatPrecision);
125        $providers = new ProviderExpansion(new Sources([new Source('candidate.php', '<?php ' . $source)]), array_map(Term::fromNative(...), $configuration->environment), $contributions);
126        $contributions1 = $providers->contributions;
127        $evaluators = new Evaluators(new TargetProfile($configuration->phpVersion, $configuration->integerBits, $configuration->floatPrecision), new Registrations($contributions1->evaluators, $contributions1->providers, $contributions1->domains, $contributions1->intrinsics, $contributions1->stateSlots), $configuration->standardEvaluators);
128        $settings = new EvaluatorConfig($target, $providers->environment, $configuration->standardEvaluators, array_map((new MicroEvaluatorConversion())->convert(...), $configuration->evaluators));
129        $snapshotId = 'test';
130        $input = $providers->input;
131        $parser = new Parser(new ParserConfig($target->version));
132        $trees = array_map(static fn (Source $source1): File => (new Resolver())->resolve($parser->parse(new Source(Path::normalize($source1->path), $source1->contents, $source1->declarationsOnly))), iterator_to_array($input, false));
133        $program = new Index($snapshotId, $trees);
134        $e = new Evaluator(new Context(new DefinitionIndex($program), $settings, $evaluators, $budget));
135        $receiver = Term::parameter('external', 'Box');
136        $symbol = 'target';
137        $graph = $e->context->index->graph($symbol);
138        self::assertNotNull($graph);
139        $v = (new Slots($e))->initial(new Frame($graph, 'source:' . $symbol), $receiver, 'example.slot');
140        self::assertSame($receiver, $v->operands[0]);
141    }
142}
143