fix(generator): 修复只读属性的默认值初始化问题

- 在生成 PHP stub 时为只读属性使用 ZVAL_UNDEF 初始化
- 防止对只读属性进行类型默认值初始化
- 添加空指针检查以避免访问可能不存在的变量名称
pull/1/head
韩天峰 3 months ago
parent 9eec3e7483
commit 981a8613b5
  1. 4
      src/Php/Analysis/SsaBuilder.php
  2. 4
      src/gen_stub.php

@ -984,7 +984,7 @@ class SsaBuilder
$defs[] = $stmt->valueVar->name;
}
} elseif ($stmt instanceof Stmt\Catch_) {
if (is_string($stmt->var->name)) {
if ($stmt->var && is_string($stmt->var->name)) {
$defs[] = $stmt->var->name;
}
}
@ -1223,7 +1223,7 @@ class SsaBuilder
// Handle catch variables
if ($stmt instanceof Stmt\Catch_) {
if (is_string($stmt->var->name)) {
if ($stmt->var && is_string($stmt->var->name)) {
$varName = $stmt->var->name;
$ssaId = $this->allocateSsaId();
$ssaVar = new SsaVar($ssaId, $varName, 0);

@ -3258,7 +3258,11 @@ class PropertyInfo extends VariableLike
$zvalName = "property_{$propertyName}_default_value";
if ($this->defaultValue === null && $this->type !== null) {
$code .= "\tzval $zvalName;\n";
if ($this->flags & Modifiers::READONLY || $this->classFlags & Modifiers::READONLY) {
$code .= "\tZVAL_UNDEF(&$zvalName);\n";
} else {
$code .= $this->getTypeDefaultValueCode($zvalName);
}
} else {
$code .= $defaultValue->initializeZval($zvalName, varName: $this->name->__toString());
}

Loading…
Cancel
Save