fix(php): 修复类名处理逻辑和静态属性列表结构

- 修复 getNativeName 方法中函数名为空时的处理逻辑
- 添加调试追踪用于非Test类的原生类检查
- 使用 getNamespacedClassName 替代直接获取类名进行查找
- 修改静态属性列表为关联数组以支持完整的属性标识符
pull/1/head
韩天峰 6 months ago
parent 4d0a44fdbd
commit a54985897f
  1. 17
      src/Php/CompilerBase.php
  2. 10
      src/Php/Translator.php

@ -643,13 +643,16 @@ class CompilerBase extends \PhpAot\Core\Translator
protected function getNativeName(string $fn, string $ns = '', string $class = ''): string protected function getNativeName(string $fn, string $ns = '', string $class = ''): string
{ {
$names = [];
if ($ns) { if ($ns) {
$names[] = $this->escapeNamespace($ns); $names[] = $this->escapeNamespace($ns);
} }
if ($class) { if ($class) {
$names[] = $this->escapeClass($class); $names[] = $this->escapeClass($class);
} }
$names[] = $this->escapeName($fn); if ($fn) {
$names[] = $this->escapeName($fn);
}
return implode(self::NAMESPACE_SEPARATOR, $names); return implode(self::NAMESPACE_SEPARATOR, $names);
} }
@ -1452,6 +1455,9 @@ class CompilerBase extends \PhpAot\Core\Translator
*/ */
protected function hasNativeClass(string $name): bool protected function hasNativeClass(string $name): bool
{ {
if (!str_starts_with($name, 'Test')) {
debug_print_backtrace();
}
return array_key_exists($this->escapeClass($name), $this->classes); return array_key_exists($this->escapeClass($name), $this->classes);
} }
@ -3360,7 +3366,7 @@ class CompilerBase extends \PhpAot\Core\Translator
return $id; return $id;
} }
if ($id === 'self') { if ($id === 'self') {
$id = $this->classDef->getNamespacedName(false); $id = $this->getNamespacedClassName($this->class);
} }
if ($this->isNameExpr($node) or $this->isIdExpr($node)) { if ($this->isNameExpr($node) or $this->isIdExpr($node)) {
return $this->genCharPtr($id, true); return $this->genCharPtr($id, true);
@ -3441,11 +3447,12 @@ class CompilerBase extends \PhpAot\Core\Translator
$class = $this->class; $class = $this->class;
} }
if (!$this->hasNativeClass($class)) { $fullName = $this->getNamespacedClassName($class);
if (!$this->hasNativeClass($fullName)) {
return null; return null;
} }
$classDef = $this->classes[$class]; $classDef = $this->getClassDef($fullName);
$namespace = $classDef->namespace; $namespace = $classDef->namespace;
if ($classDef->hasProperty($prop)) { if ($classDef->hasProperty($prop)) {
$propDef = $classDef->getProperty($prop); $propDef = $classDef->getProperty($prop);
@ -3500,7 +3507,7 @@ class CompilerBase extends \PhpAot\Core\Translator
{ {
$nativeProp = $this->findNativeStaticProperty($expr, $class, $namespace); $nativeProp = $this->findNativeStaticProperty($expr, $class, $namespace);
if ($nativeProp) { if ($nativeProp) {
$classPtr = $this->getClassEntryPtr($class); $classPtr = $this->getClassEntryPtr($this->getNamespacedClassName($class));
$propOffset = self::PREFIX . $this->getPropertyOffset($nativeProp->name, $class, $namespace); $propOffset = self::PREFIX . $this->getPropertyOffset($nativeProp->name, $class, $namespace);
return 'php::getStaticProperty(' . $classPtr . ', ' . $propOffset . ')'; return 'php::getStaticProperty(' . $classPtr . ', ' . $propOffset . ')';
} }

@ -690,12 +690,12 @@ class Translator extends Preprocessor
$extends = $class->extends; $extends = $class->extends;
} }
$nativeName = $this->getNativeName('', $this->class, $this->namespace); $fullName = $this->getNamespacedClassName($this->class);
if ($this->hasNativeClass($nativeName)) { if ($this->hasNativeClass($fullName)) {
$this->classDef = $this->classes[$nativeName]; $this->classDef = $this->classes[$fullName];
} else { } else {
$this->classDef = new ClassDef($this->class, $flags, $this->namespace); $this->classDef = new ClassDef($this->class, $flags, $this->namespace);
$this->addClass($nativeName, $this->classDef); $this->addClass($fullName, $this->classDef);
} }
if ($class instanceof Node\Stmt\Enum_) { if ($class instanceof Node\Stmt\Enum_) {
@ -817,7 +817,7 @@ class Translator extends Preprocessor
$prop->class = $classDef->getNamespacedName(false); $prop->class = $classDef->getNamespacedName(false);
$prop->name = $property->name; $prop->name = $property->name;
$prop->default = $property->default; $prop->default = $property->default;
$this->staticPropertyList[] = $prop; $this->staticPropertyList[$name . '::' . $property->name] = $prop;
} else { } else {
$propOffset = self::PREFIX . $this->getPropertyOffset($property->name, $classDef->name, $classDef->namespace); $propOffset = self::PREFIX . $this->getPropertyOffset($property->name, $classDef->name, $classDef->namespace);
$cppCode .= $this->getIndent() . 'this_.attr(' . $propOffset . ') = ' . $property->default . ';' . PHP_EOL; $cppCode .= $this->getIndent() . 'this_.attr(' . $propOffset . ') = ' . $property->default . ';' . PHP_EOL;

Loading…
Cancel
Save