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)) {