refactor:统一整数字面量生成并添加平台后缀支持

pull/15/head
韩天峰 2 months ago
parent c3f0151e87
commit fc5eeb3c0f
  1. 20
      phpunit/src/CompilerBaseApiTest.php
  2. 8
      src/Php/CompilerBase.php
  3. 16
      src/Php/Generator/Utils.php
  4. 9
      src/Php/Translator.php

@ -5,6 +5,7 @@ namespace PhpAot\Tests;
use PHPUnit\Framework\TestCase;
use PhpAot\Php\CompilerTest;
use PhpAot\Php\CompilerBase;
use PhpAot\Php\Platform\Windows;
class CompilerBaseApiTest extends TestCase
{
@ -153,6 +154,25 @@ class CompilerBaseApiTest extends TestCase
$this->assertNotEquals($name1, $name3);
}
public function testWindowsIntegerLiteralSuffixForGeneratedCValues(): void
{
$this->setPropertyValue('platform', new Windows());
$this->assertSame('42LL', $this->invokeMethod('genCValue', 42));
$this->assertSame('-42LL', $this->invokeMethod('genCValue', -42));
$this->assertSame('ZEND_LONG_MAX', $this->invokeMethod('genCValue', PHP_INT_MAX));
$this->assertSame('ZEND_LONG_MIN', $this->invokeMethod('genCValue', PHP_INT_MIN));
}
public function testWindowsIntegerLiteralSuffixForInternalConstants(): void
{
$this->setPropertyValue('platform', new Windows());
$this->assertSame(PHP_INT_SIZE . 'LL', $this->compiler->getConstValue('PHP_INT_SIZE'));
$this->assertSame('ZEND_LONG_MAX', $this->compiler->getConstValue('PHP_INT_MAX'));
$this->assertSame('ZEND_LONG_MIN', $this->compiler->getConstValue('PHP_INT_MIN'));
}
// ========================================================================
// genAnonClassName
// ========================================================================

@ -5651,13 +5651,7 @@ class CompilerBase extends \PhpAot\Core\Translator implements PropertyAccessCont
{
$value = $this->internalConstants[$name];
if (is_int($value)) {
if ($value === PHP_INT_MIN) {
return 'LONG_MIN';
}
if ($value === PHP_INT_MAX) {
return 'LONG_MAX';
}
return $value . 'L';
return $this->genIntegerLiteral($value);
}
if (is_float($value)) {
if (is_nan($value)) {

@ -13,9 +13,23 @@ use PhpAot\Php\Constants;
trait Utils
{
protected function genIntegerLiteral(int $value): string
{
if ($value === PHP_INT_MIN) {
return 'ZEND_LONG_MIN';
}
if ($value === PHP_INT_MAX) {
return 'ZEND_LONG_MAX';
}
return $value . $this->getPlatform()->getIntegerLiteralSuffix();
}
protected function genCValue(mixed $value): mixed
{
if (is_int($value) or is_float($value)) {
if (is_int($value)) {
return $this->genIntegerLiteral($value);
}
if (is_float($value)) {
return $value;
}
if (is_bool($value)) {

@ -1862,14 +1862,7 @@ CODE;
if ($this->isInternalConstant($name)) {
$value = $this->internalConstants[$name];
if (is_int($value)) {
$expr = strval($value);
if ($value === PHP_INT_MIN) {
$expr = 'LONG_MIN';
} elseif ($value === PHP_INT_MAX) {
$expr = 'LONG_MAX';
} else {
$expr = $expr . 'L';
}
$expr = $this->genIntegerLiteral($value);
} elseif (is_float($value)) {
return $value;
} elseif (is_bool($value)) {

Loading…
Cancel
Save