refactor(php): 优化迭代器处理逻辑以使用类入口指针和字面量字符串

- 使用 getClassEntryPtr 获取 IteratorAggregate 和 Iterator 类入口指针
- 使用 getLiteralString 替换硬编码的方法名字符串
- 将 instanceOf 检查从字符串比较改为类入口指针比较
- 将方法调用从字符串参数改为字面量字符串参数
- 使用 getFuncPtr 替换 get_object_vars 函数调用的硬编码字符串
pull/1/head
韩天峰 2 months ago
parent 0c7464b4b3
commit 8fa49e078f
  1. 29
      src/Php/Translator.php

@ -2638,37 +2638,46 @@ CODE;
$tmpArrayVar = $this->genTmpVarName();
$this->addLocalVar($tmpArrayVar, self::TYPE_ARRAY);
$code = 'if (' . $obj . '.instanceOf("IteratorAggregate")) {' . PHP_EOL;
$code .= $this->getIndent() . $tmpVar . ' = ' . $obj . '.call("getIterator");' . PHP_EOL . '}' . PHP_EOL;
$code .= 'else if (' . $obj . '.instanceOf("Iterator")) {' . PHP_EOL;
$IteratorAggregateCe = $this->getClassEntryPtr('IteratorAggregate');
$IteratorCe = $this->getClassEntryPtr('Iterator');
$getIteratorStr = $this->getLiteralString('getIterator');
$validStr = $this->getLiteralString('valid');
$currentStr = $this->getLiteralString('current');
$keyStr = $this->getLiteralString('key');
$nextStr = $this->getLiteralString('next');
$rewindStr = $this->getLiteralString('rewind');
$code = 'if (' . $obj . '.instanceOf(' . $IteratorAggregateCe . ')) {' . PHP_EOL;
$code .= $this->getIndent() . $tmpVar . ' = ' . $obj . '.call(' . $getIteratorStr . ');' . PHP_EOL . '}' . PHP_EOL;
$code .= 'else if (' . $obj . '.instanceOf(' . $IteratorCe . ')) {' . PHP_EOL;
$code .= $this->getIndent() . $tmpVar . ' = ' . $obj . ';' . PHP_EOL . '}' . PHP_EOL;
$code .= 'if (' . $tmpVar . ') {' . PHP_EOL;
$this->indentLevel++;
$code .= $this->getIndent() . $tmpVar . '.call("rewind");' . PHP_EOL;
$code .= $this->getIndent() . 'for (;' . $tmpVar . '.call("valid"); ' . $tmpVar . '.call("next")) {' . PHP_EOL;
$code .= $this->getIndent() . $tmpVar . '.call(' . $rewindStr . ');' . PHP_EOL;
$code .= $this->getIndent() . 'for (;' . $tmpVar . '.call(' . $validStr . '); ' . $tmpVar . '.call(' . $nextStr . ')) {' . PHP_EOL;
$this->indentLevel++;
if ($node->valueVar instanceof List_) {
$listTmpVar = $this->genTmpVarName();
$this->addLocalVar($listTmpVar, self::TYPE_VAR);
$code .= $this->getIndent() . ' ' . $listTmpVar . ' = ' . $tmpVar . '.call("current");' . PHP_EOL;
$code .= $this->getIndent() . ' ' . $listTmpVar . ' = ' . $tmpVar . '.call(' . $currentStr . ');' . PHP_EOL;
if ($node->keyVar) {
$keyVar = $this->parseIdentifier($node->keyVar);
$this->checkVar($node, $keyVar);
$code .= $this->getIndent() . ' ' . $keyVar . ' = ' . $tmpVar . '.call("key");' . PHP_EOL;
$code .= $this->getIndent() . ' ' . $keyVar . ' = ' . $tmpVar . '.call(' . $keyStr . ');' . PHP_EOL;
}
$code .= $this->parseForeachItemAsList($listTmpVar, $node->valueVar->items);
} else {
$valueVar = $this->parseIdentifier($node->valueVar);
$this->checkVar($node, $valueVar);
$code .= $this->getIndent() . ' ' . $valueVar . ' = ' . $tmpVar . '.call("current");' . PHP_EOL;
$code .= $this->getIndent() . ' ' . $valueVar . ' = ' . $tmpVar . '.call(' . $currentStr . ');' . PHP_EOL;
if ($node->keyVar) {
$keyVar = $this->parseIdentifier($node->keyVar);
$this->checkVar($node, $keyVar);
$code .= $this->getIndent() . ' ' . $keyVar . ' = ' . $tmpVar . '.call("key");' . PHP_EOL;
$code .= $this->getIndent() . ' ' . $keyVar . ' = ' . $tmpVar . '.call(' . $keyStr . ');' . PHP_EOL;
}
}
$code .= $this->parseStmts($node->stmts);
@ -2676,7 +2685,7 @@ CODE;
$code .= '}' . PHP_EOL;
$this->indentLevel--;
$code .= $this->getIndent() . '} else {' . PHP_EOL;
$code .= $this->getIndent() . $tmpArrayVar . ' = php::call("get_object_vars", {' . $obj . '});' . PHP_EOL;
$code .= $this->getIndent() . $tmpArrayVar . ' = php::call(' . $this->getFuncPtr('get_object_vars') . ', {' . $obj . '});' . PHP_EOL;
$code .= $this->parseForeachArray($node, $tmpArrayVar);
$this->indentLevel--;
$code .= '}' . PHP_EOL;

Loading…
Cancel
Save