From 1e47ac0f538e14b22d7e072f942ce90b7d3f2a8b Mon Sep 17 00:00:00 2001 From: tianfenghan Date: Wed, 15 Apr 2026 22:01:46 +0800 Subject: [PATCH] =?UTF-8?q?fix(parser):=20=E8=A7=A3=E5=86=B3=E5=B1=9E?= =?UTF-8?q?=E6=80=A7=E9=BB=98=E8=AE=A4=E5=80=BC=E5=A4=84=E7=90=86=E4=B8=AD?= =?UTF-8?q?=E7=9A=84=E7=B1=BB=E5=9E=8B=E8=BD=AC=E6=8D=A2=E5=92=8C=E9=87=8D?= =?UTF-8?q?=E5=A4=8D=E5=AE=9A=E4=B9=89=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 添加对布尔类型默认值的正确处理和转换 - 修复常量表达式在字符串类型检查中的逻辑错误 - 增加对重复属性定义的检测和错误报告 - 优化数值类型默认值的处理流程 - 添加测试用例验证各种类型的属性默认值行为 --- src/Php/Generator/Utils.php | 3 +++ src/Php/Preprocessor.php | 9 ++++++++- src/Php/Translator.php | 2 ++ src/gen_stub.php | 10 ++++++---- tests/aot/object_property/002.phpt | 26 ++++++++++++++++++++++++++ 5 files changed, 45 insertions(+), 5 deletions(-) create mode 100644 tests/aot/object_property/002.phpt diff --git a/src/Php/Generator/Utils.php b/src/Php/Generator/Utils.php index 4d2752ee..6aa19d8d 100644 --- a/src/Php/Generator/Utils.php +++ b/src/Php/Generator/Utils.php @@ -18,6 +18,9 @@ trait Utils if (is_int($value) or is_float($value)) { return $value; } + if (is_bool($value)) { + return $value ? 1 : 0; + } if (is_string($value)) { return $this->genCharPtr($value); } else { diff --git a/src/Php/Preprocessor.php b/src/Php/Preprocessor.php index bfd2911b..6f2e3c4d 100644 --- a/src/Php/Preprocessor.php +++ b/src/Php/Preprocessor.php @@ -251,6 +251,9 @@ class Preprocessor extends CompilerBase $nullable = false; } $default = $this->parseParamDefaultValue($param->default); + if ($this->classDef->hasProperty($name)) { + $this->fatalError($param, "Duplicate property `{$name}`"); + } $propertyDef = new PropertyDef($name, $param->flags, $type, $default, $nullable); $this->classDef->properties[$name] = $propertyDef; } @@ -542,7 +545,11 @@ class Preprocessor extends CompilerBase $type = $this->parseTypeDecl($v->type, self::DECL_TYPE_OF_PROPERTY, $class); foreach ($v->props as $prop) { - $propDef = new PropertyDef($this->parseIdentifier($prop->name), $flags, $type); + $propName = $this->parseIdentifier($prop->name); + if ($this->classDef->hasProperty($propName)) { + $this->fatalError($v, "Duplicate property `{$propName}`"); + } + $propDef = new PropertyDef($propName, $flags, $type); if ($prop->default) { $propDef->default = $this->parseIdentifier($prop->default); if ($prop->default->getType() == 'Expr_Array') { diff --git a/src/Php/Translator.php b/src/Php/Translator.php index fbd1a73d..4c9e2a50 100644 --- a/src/Php/Translator.php +++ b/src/Php/Translator.php @@ -591,6 +591,8 @@ class Translator extends Preprocessor } } elseif (is_float($value)) { return $value; + } else if (is_bool($value)) { + return $value ? 1 : 0; } elseif (is_string($value)) { return $this->genCharPtr($value); } else { diff --git a/src/gen_stub.php b/src/gen_stub.php index a9239092..9527117c 100755 --- a/src/gen_stub.php +++ b/src/gen_stub.php @@ -2437,6 +2437,7 @@ class EvaluatedValue // $this->expr has all its PHP constants replaced by C constants $prettyPrinter = new Standard; $expr = $prettyPrinter->prettyPrintExpr($this->expr); + // PHP single-quote to C double-quote string if ($this->type->isString()) { if ( @@ -2449,15 +2450,16 @@ class EvaluatedValue } else { return $this->value; } + } elseif ($this->expr instanceof Expr\ConstFetch) { + return getTranslator()->getConstValue($this->expr->name->toString()); } elseif (!($this->expr instanceof String_)) { - if ($this->expr instanceof Expr\ConstFetch) { - return getTranslator()->getConstValue($this->expr->name->toString()); - } throw new Exception("Expression at line " . $this->expr->getStartLine() . " must be a scalar string"); } $expr = preg_replace("/(^'|'$)/", '"', $expr); - } elseif ($this->type->isInt() or $this->type->isFloat() or $this->expr instanceof Expr\ConstFetch) { + } elseif ($this->type->isInt() or $this->type->isFloat()) { return strval($this->value); + } elseif ($this->type->isBool()) { + return $this->value ? 'true' : 'false'; } return $expr[0] == '"' ? $expr : preg_replace('(\bnull\b)', 'NULL', str_replace('\\', '', $expr)); } diff --git a/tests/aot/object_property/002.phpt b/tests/aot/object_property/002.phpt new file mode 100644 index 00000000..94614e3b --- /dev/null +++ b/tests/aot/object_property/002.phpt @@ -0,0 +1,26 @@ +--TEST-- +default array property +--FILE-- +attr1, $this->attr2, $this->attr3, $this->attr4); + } +} + +function main() { + $obj = new Test; + $obj->bar(); +} +?> +--EXPECT-- +bool(false) +string(5) "hello" +int(1232) +float(3.1415) \ No newline at end of file