Keep boolean condition snapshots in native storage (#106)
* perf(compiler): keep boolean condition snapshots unboxed * fix(compiler): isolate static initializer temporary names * test: close PHP section in static initializer PHPTmaster
parent
f63eab17a3
commit
8332bfedd8
5 changed files with 82 additions and 2 deletions
@ -0,0 +1,25 @@ |
|||||||
|
<?php |
||||||
|
|
||||||
|
function nativeCondition(array $values): int |
||||||
|
{ |
||||||
|
if ($values[0] === 1) { |
||||||
|
return 1; |
||||||
|
} |
||||||
|
if ($values[0] === 2 || $values[1] === 3) { |
||||||
|
return 2; |
||||||
|
} |
||||||
|
return 0; |
||||||
|
} |
||||||
|
|
||||||
|
function dynamicConditionValue(mixed $value): mixed |
||||||
|
{ |
||||||
|
return $value; |
||||||
|
} |
||||||
|
|
||||||
|
function dynamicCondition(array $values): bool |
||||||
|
{ |
||||||
|
if (dynamicConditionValue($values[0])) { |
||||||
|
return true; |
||||||
|
} |
||||||
|
return false; |
||||||
|
} |
||||||
@ -0,0 +1,28 @@ |
|||||||
|
<?php |
||||||
|
|
||||||
|
use TypePhp\CompilerTest; |
||||||
|
|
||||||
|
final class NativeConditionTemporaryTest extends BaseTest |
||||||
|
{ |
||||||
|
public function testBooleanConditionsWithOperandCleanupRemainUnboxed(): void |
||||||
|
{ |
||||||
|
global $translator; |
||||||
|
$compiler = CompilerTest::create(TYPEPHP_ROOT_PATH); |
||||||
|
$translator = $compiler; |
||||||
|
$source = TYPEPHP_ROOT_PATH . '/phpunit/code/native-condition-temporaries.php'; |
||||||
|
$compiler->addFiles([$source]); |
||||||
|
$compiler->prepareFile($source); |
||||||
|
$code = file_get_contents($compiler->convertFile($source)); |
||||||
|
|
||||||
|
preg_match_all('/(tmp_var_\d+) = php::toBool\(php::same\(/', $code, $matches); |
||||||
|
self::assertCount(2, $matches[1]); |
||||||
|
foreach ($matches[1] as $temporary) { |
||||||
|
self::assertStringContainsString('php::Bool ' . $temporary . ' = 0;', $code); |
||||||
|
} |
||||||
|
|
||||||
|
$dynamicBody = explode('php::Bool php_dynamiccondition(', $code, 2)[1]; |
||||||
|
$dynamicBody = explode("\n}", $dynamicBody, 2)[0]; |
||||||
|
self::assertStringContainsString('php::Var tmp_var_1;', $dynamicBody); |
||||||
|
self::assertStringContainsString('tmp_var_1 = php_dynamicconditionvalue(', $dynamicBody); |
||||||
|
} |
||||||
|
} |
||||||
@ -0,0 +1,14 @@ |
|||||||
|
--TEST-- |
||||||
|
Static initializer temporaries do not reuse outer native condition snapshots |
||||||
|
--FILE-- |
||||||
|
<?php |
||||||
|
function conditionBeforeStatic(array $args): void { |
||||||
|
if ($args === []) { return; } |
||||||
|
static $table = [[[7, 'value']]]; |
||||||
|
echo $table[0][0][1], "\n"; |
||||||
|
} |
||||||
|
function main(): void { conditionBeforeStatic([1]); conditionBeforeStatic([2]); } |
||||||
|
?> |
||||||
|
--EXPECT-- |
||||||
|
value |
||||||
|
value |
||||||
Loading…
Reference in new issue