diff --git a/src/Php/CompilerBase.php b/src/Php/CompilerBase.php index 11db8417..959f1683 100644 --- a/src/Php/CompilerBase.php +++ b/src/Php/CompilerBase.php @@ -248,7 +248,7 @@ class CompilerBase extends \PhpAot\Core\Translator protected array $linkPaths = []; // --link-path / -L: user-specified library search paths protected int $floatPrecision = 17; protected bool $debug = false; - protected bool $formatCode = false; // --format: enable clang-format (disabled by default) + protected bool $formatCode = true; // --format: enable clang-format (disabled by default) protected bool $printBacktraceOnError = true; protected bool $noLiteralStrings = false; protected bool $noConsole = false; // Windows: hide console window @@ -334,6 +334,7 @@ class CompilerBase extends \PhpAot\Core\Translator '_SESSION' => self::TYPE_ARRAY, '_REQUEST' => self::TYPE_ARRAY, '_ENV' => self::TYPE_ARRAY, + 'GLOBALS' => self::TYPE_ARRAY, ]; protected array $globalVars = []; protected bool $nativeTypes = false; diff --git a/src/Php/Optimizer/FuncCallOptimizer.php b/src/Php/Optimizer/FuncCallOptimizer.php index b13baa84..2863d24d 100644 --- a/src/Php/Optimizer/FuncCallOptimizer.php +++ b/src/Php/Optimizer/FuncCallOptimizer.php @@ -200,6 +200,14 @@ trait FuncCallOptimizer return false; } + // 检测参数中使用的变量是否已定义,若变量不存在则回退到动态调用路径 + // 动态路径中的 parseCallArgs() 会给出明确的错误信息 + foreach ($expr->args as $arg) { + if ($this->isVarExpr($arg->value) && is_string($arg->value->name) && !$this->hasVar($arg->value->name)) { + return false; + } + } + if (is_string($config)) { $targetName = $config; $config = ['target' => $targetName]; diff --git a/src/Php/Translator.php b/src/Php/Translator.php index 92e8c1c5..3432cabc 100644 --- a/src/Php/Translator.php +++ b/src/Php/Translator.php @@ -674,7 +674,7 @@ class Translator extends Preprocessor $lines[] = '#include '; $lines[] = PHP_EOL; foreach ($this->globalVars as $name => $type) { - $lines[] = 'extern ' . self::TYPE_VAR . ' ' . $this->escapeGlobalVar($name) . ';'; + $lines[] = 'extern THREAD_LOCAL ' . self::TYPE_VAR . ' ' . $this->escapeGlobalVar($name) . ';'; } if ($this->literalStrings) { @@ -732,7 +732,7 @@ class Translator extends Preprocessor $code .= "// global vars \n"; foreach ($this->globalVars as $name => $type) { - $code .= self::TYPE_VAR . ' ' . $this->escapeGlobalVar($name) . ';' . PHP_EOL; + $code .= 'THREAD_LOCAL ' . self::TYPE_VAR . ' ' . $this->escapeGlobalVar($name) . ';' . PHP_EOL; } $code .= "// class register functions \n"; @@ -852,6 +852,8 @@ CODE; $code .= '}' . PHP_EOL . PHP_EOL; // minit end + $code .= 'THREAD_LOCAL zval globals_array;' . PHP_EOL; + // php_app_init begin $code .= 'void php_app_init() {' . PHP_EOL; $code .= '// register constants' . PHP_EOL; @@ -861,7 +863,13 @@ CODE; } $code .= '// global vars ' . PHP_EOL; foreach ($this->globalVars as $name => $type) { - $code .= 'php::initGlobal(' . $this->genCharPtr($name) . ', ' . $this->escapeGlobalVar($name) . ');' . PHP_EOL; + if ($name == 'GLOBALS') { + $var = $this->escapeGlobalVar($name); + $code .= 'ZVAL_ARR(&globals_array, &EG(symbol_table));' . PHP_EOL; + $code .= 'ZVAL_INDIRECT(' . $var. '.ptr(), &globals_array);' . PHP_EOL; + } else { + $code .= 'php::initGlobal(' . $this->genCharPtr($name) . ', ' . $this->escapeGlobalVar($name) . ');' . PHP_EOL; + } } $code .= '// static property ' . PHP_EOL; @@ -886,8 +894,10 @@ CODE; // php_app_clean begin $code .= 'void php_app_clean() {' . PHP_EOL; foreach ($this->globalVars as $name => $type) { - $code .= $this->escapeGlobalVar($name) . '.unset();' . PHP_EOL; - $code .= 'php::unsetGlobal("' . $name . '");' . PHP_EOL; + if ($name != 'GLOBALS') { + $code .= $this->escapeGlobalVar($name) . '.unset();' . PHP_EOL; + $code .= 'php::unsetGlobal("' . $name . '");' . PHP_EOL; + } } foreach ($this->constants as $name => $const) { if ($const->type !== self::TYPE_VAR) { diff --git a/tests/aot/basic/global-003.phpt b/tests/aot/basic/global-003.phpt new file mode 100644 index 00000000..8dca2499 --- /dev/null +++ b/tests/aot/basic/global-003.phpt @@ -0,0 +1,16 @@ +--TEST-- +global vars +--FILE-- + +--EXPECTF--