fix(php): 修复字面量字符串为空时的编译错误

- 在声明字面量字符串数组前检查数组是否为空
- 避免当没有字面量字符串时生成无效的 extern 声明
- 添加空数组条件判断以防止编译器报错
- 为函数默认值相关的字符串字面量添加相同保护
- 确保代码在各种情况下都能正确编译
pull/1/head
韩天峰 2 months ago
parent 91a710844c
commit 5ed79e9451
  1. 24
      src/Php/Translator.php
  2. 9
      tests/aot/basic/main-func.phpt

@ -507,8 +507,10 @@ class Translator extends Preprocessor
$lines[] = 'extern ' . self::TYPE_VAR . ' ' . $this->escapeGlobalVar($name) . ';';
}
$literalStringsCount = count($this->literalStrings);
$lines[] = 'extern ' . self::TYPE_STR . ' ' . self::LITERAL_STRINGS . '[' . $literalStringsCount . '];' . PHP_EOL;
if ($this->literalStrings) {
$literalStringsCount = count($this->literalStrings);
$lines[] = 'extern ' . self::TYPE_STR . ' ' . self::LITERAL_STRINGS . '[' . $literalStringsCount . '];' . PHP_EOL;
}
// 确保数组大小至少为 1,避免 C/C++ 编译错误
$classCount = max(1, count($this->classMap));
@ -613,11 +615,15 @@ CODE;
$code .= "\n\n";
$code .= "// literal strings \n";
$code .= self::TYPE_STR . ' ' . self::LITERAL_STRINGS . '[] = {' . PHP_EOL;
foreach ($this->literalStrings as $str => $index) {
$code .= self::TYPE_STR . '{ZEND_STRL("' . $this->escapeString($str) . '"), true}, // [' . $index . ']' . PHP_EOL;
if ($this->literalStrings) {
$code .= self::TYPE_STR . ' ' . self::LITERAL_STRINGS . '[] = {' . PHP_EOL;
foreach ($this->literalStrings as $str => $index) {
$code .= self::TYPE_STR . '{ZEND_STRL("' . $this->escapeString($str) . '"), true}, // [' . $index . ']' . PHP_EOL;
}
$code .= '};' . PHP_EOL . PHP_EOL;
} else {
$code .= PHP_EOL;
}
$code .= '};' . PHP_EOL . PHP_EOL;
$code .= "// constants \n";
foreach ($this->constants as $name => $const) {
@ -1386,8 +1392,10 @@ CODE;
$code = '#include <phpx.h>' . PHP_EOL;
// 函数的默认值可能会使用字符串字面量,需要提前声明
$literalStringsCount = count($this->literalStrings);
$code .= 'extern ' . self::TYPE_STR . ' ' . self::LITERAL_STRINGS . '[' . $literalStringsCount . '];' . PHP_EOL;
if ($this->literalStrings) {
$literalStringsCount = count($this->literalStrings);
$code .= 'extern ' . self::TYPE_STR . ' ' . self::LITERAL_STRINGS . '[' . $literalStringsCount . '];' . PHP_EOL;
}
foreach ($this->functions as $name => $func) {
$code .= 'extern ' . $func->returnType . ' ' . self::PREFIX . $name . '(';

@ -0,0 +1,9 @@
--TEST--
main function
--FILE--
<?php
function main()
{
}
?>
--EXPECT--
Loading…
Cancel
Save