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 PHPUnit\Framework\TestCase;
use PhpAot\Php\CompilerTest; use PhpAot\Php\CompilerTest;
use PhpAot\Php\CompilerBase; use PhpAot\Php\CompilerBase;
use PhpAot\Php\Platform\Windows;
class CompilerBaseApiTest extends TestCase class CompilerBaseApiTest extends TestCase
{ {
@ -153,6 +154,25 @@ class CompilerBaseApiTest extends TestCase
$this->assertNotEquals($name1, $name3); $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 // genAnonClassName
// ======================================================================== // ========================================================================

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

@ -13,9 +13,23 @@ use PhpAot\Php\Constants;
trait Utils 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 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; return $value;
} }
if (is_bool($value)) { if (is_bool($value)) {

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

Loading…
Cancel
Save