for/foreach 的 key/value 变量作为 scope var 而不是 local var

pull/1/head
韩天峰 8 months ago
parent ecbb00542e
commit f7132da93b
  1. 18
      src/Php/CompilerBase.php

@ -1077,10 +1077,8 @@ class CompilerBase extends \PhpAot\Core\Translator
if ($this->hasGlobalVar($name)) { if ($this->hasGlobalVar($name)) {
$this->fatalError($left, 'Cannot assign to global variable in for loop'); $this->fatalError($left, 'Cannot assign to global variable in for loop');
} }
if (!$this->hasVar($name)) { $type = $this->detectExprType($expr->expr);
$type = $this->detectExprType($expr->expr); $code .= $type . ' ' . $name . ' = ' . '(' . $this->parseIdentifier($expr->expr) . ');';
$this->addLocalVar($name, $type);
}
} }
$list_cond[] = $this->parseExpr($expr); $list_cond[] = $this->parseExpr($expr);
} }
@ -1259,7 +1257,7 @@ class CompilerBase extends \PhpAot\Core\Translator
*/ */
protected function findNativeFunction(string $fname): string|false protected function findNativeFunction(string $fname): string|false
{ {
$possibleFunctionNames = [$fname,]; $possibleFunctionNames = [strtolower($fname),];
if ($this->namespace) { if ($this->namespace) {
$possibleFunctionNames[] = $this->namespace . self::NAMESPACE_SEPARATOR . $fname; $possibleFunctionNames[] = $this->namespace . self::NAMESPACE_SEPARATOR . $fname;
} }
@ -1967,10 +1965,7 @@ class CompilerBase extends \PhpAot\Core\Translator
if ($this->hasGlobalVar($keyVar)) { if ($this->hasGlobalVar($keyVar)) {
$this->fatalError($node->keyVar, 'Cannot redefine key variable: ' . $this->unescapeVarName($keyVar)); $this->fatalError($node->keyVar, 'Cannot redefine key variable: ' . $this->unescapeVarName($keyVar));
} }
$code .= $this->getIndent() . ' ' . $keyVar . ' = iter.key();' . PHP_EOL; $code .= self::TYPE_VAR . ' ' . $this->getIndent() . ' ' . $keyVar . ' = iter.key();' . PHP_EOL;
if (!$this->hasVar($keyVar)) {
$this->addLocalVar($keyVar, self::TYPE_VAR);
}
} }
if ($node->valueVar->getType() == self::EXPR_ARRAY_DIM_FETCH) { if ($node->valueVar->getType() == self::EXPR_ARRAY_DIM_FETCH) {
@ -1985,10 +1980,7 @@ class CompilerBase extends \PhpAot\Core\Translator
if ($this->hasGlobalVar($valueVar)) { if ($this->hasGlobalVar($valueVar)) {
$this->fatalError($node->valueVar, 'Cannot redefine value variable: ' . $this->unescapeVarName($valueVar)); $this->fatalError($node->valueVar, 'Cannot redefine value variable: ' . $this->unescapeVarName($valueVar));
} }
$code .= $this->getIndent() . ' ' . $valueVar . ' = iter.value();' . PHP_EOL; $code .= self::TYPE_VAR . ' ' . $this->getIndent() . ' ' . $valueVar . ' = iter.value();' . PHP_EOL;
if (!$this->hasVar($valueVar)) {
$this->addLocalVar($valueVar, self::TYPE_VAR);
}
} }
$code .= $this->parseStmts($stmts); $code .= $this->parseStmts($stmts);
$this->indentLevel--; $this->indentLevel--;

Loading…
Cancel
Save