From fc5eeb3c0fbc1a7894e9943687f6121da4b52070 Mon Sep 17 00:00:00 2001 From: tianfenghan Date: Wed, 8 Jul 2026 17:49:02 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=E7=BB=9F=E4=B8=80=E6=95=B4=E6=95=B0?= =?UTF-8?q?=E5=AD=97=E9=9D=A2=E9=87=8F=E7=94=9F=E6=88=90=E5=B9=B6=E6=B7=BB?= =?UTF-8?q?=E5=8A=A0=E5=B9=B3=E5=8F=B0=E5=90=8E=E7=BC=80=E6=94=AF=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- phpunit/src/CompilerBaseApiTest.php | 20 ++++++++++++++++++++ src/Php/CompilerBase.php | 8 +------- src/Php/Generator/Utils.php | 16 +++++++++++++++- src/Php/Translator.php | 9 +-------- 4 files changed, 37 insertions(+), 16 deletions(-) diff --git a/phpunit/src/CompilerBaseApiTest.php b/phpunit/src/CompilerBaseApiTest.php index 40ae077e..240939bf 100644 --- a/phpunit/src/CompilerBaseApiTest.php +++ b/phpunit/src/CompilerBaseApiTest.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 // ======================================================================== diff --git a/src/Php/CompilerBase.php b/src/Php/CompilerBase.php index e70e77c5..05263242 100644 --- a/src/Php/CompilerBase.php +++ b/src/Php/CompilerBase.php @@ -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)) { diff --git a/src/Php/Generator/Utils.php b/src/Php/Generator/Utils.php index 45d3fb17..4a5f7e89 100644 --- a/src/Php/Generator/Utils.php +++ b/src/Php/Generator/Utils.php @@ -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)) { diff --git a/src/Php/Translator.php b/src/Php/Translator.php index eff272fa..8ae59339 100644 --- a/src/Php/Translator.php +++ b/src/Php/Translator.php @@ -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)) {