refactor(compiler): simplify object property access in compiler base

- Removed redundant zval macro handling for property access
- Replaced Z_LVAL_P and Z_DVAL_P calls with direct attribute accessors
- Updated property getter logic to use attrInt and attrFloat methods
- Modified tests to match new property access pattern
- Added comprehensive test case for inherited property reference handling
master
韩天峰 1 month ago
parent 011cb31882
commit 38d828cbbe
  1. 8
      phpunit/src/NativePropertyTest.php
  2. 3
      src/CompilerBase.php
  3. 5
      src/Parser/PropertyAccessTrait.php
  4. 31
      tests/compiler/optimizations/objprop-hoist-this-inherited-reference.phpt

@ -59,11 +59,11 @@ class NativePropertyTest extends \BaseTest
}
$code = file_get_contents($outputFile);
$this->assertStringContainsString('php::Int &_object_prop_this___value = Z_LVAL_P(this_.attr(', $code);
$this->assertStringContainsString('php::Int &_object_prop_this___value = this_.attrInt(', $code);
$this->assertStringContainsString('php::Int &_object_prop_box__value = Z_LVAL_P(box.attr(', $code);
$this->assertStringContainsString('_object_prop_this___value += (2L);', $code);
$this->assertStringContainsString('_object_prop_box__value += (2L);', $code);
$this->assertSame(2, substr_count($code, 'Z_LVAL_P('));
$this->assertSame(1, substr_count($code, 'Z_LVAL_P('));
$this->assertStringNotContainsString('typephp_write_property_scoped(', $code);
}
@ -92,7 +92,7 @@ class NativePropertyTest extends \BaseTest
}
$code = file_get_contents($outputFile);
$this->assertStringContainsString('php::Int &_object_prop_this___flags = Z_LVAL_P(this_.attr(', $code);
$this->assertStringContainsString('php::Int &_object_prop_this___flags = this_.attrInt(', $code);
// A TypePHP class constant is available during conversion and is
// folded before the native property operation is emitted.
$this->assertStringContainsString('_object_prop_this___flags &= (~php::toInt(1L));', $code);
@ -125,7 +125,7 @@ class NativePropertyTest extends \BaseTest
}
$code = file_get_contents($outputFile);
$this->assertStringContainsString('php::Int &_object_prop_this___value = Z_LVAL_P(this_.attr(', $code);
$this->assertStringContainsString('php::Int &_object_prop_this___value = this_.attrInt(', $code);
$this->assertStringContainsString('_object_prop_this___value = php::toIntExact(dynamicValue, "NativePropertyThisWriteConversionBox::$value");', $code);
}

@ -5340,8 +5340,7 @@ class CompilerBase implements PropertyAccessContext
if (($info['kind'] ?? 'zval') === 'var') {
$code .= $this->getIndent() . Type::VAR . ' ' . $name . ' = ' . $info['getter'] . ';' . PHP_EOL;
} else {
$zvalMacro = ($info['type'] === Type::FLOAT) ? 'Z_DVAL_P' : 'Z_LVAL_P';
$code .= $this->getIndent() . $info['type'] . ' &' . $name . ' = ' . $zvalMacro . '(' . $info['getter'] . '.unwrap_ptr());' . PHP_EOL;
$code .= $this->getIndent() . $info['type'] . ' &' . $name . ' = ' . $info['getter'] . ';' . PHP_EOL;
}
}
foreach ($this->context->staticPropRefs as $info) {

@ -1382,6 +1382,11 @@ trait PropertyAccessTrait
if (!$this->canHoistObjectProp($objectVar, $propName, $def)) {
return null;
}
if ($def->type === Type::INT) {
$getter = $objectVar . '.attrInt(' . $propertyId . ')';
} elseif ($def->type === Type::FLOAT) {
$getter = $objectVar . '.attrFloat(' . $propertyId . ')';
}
$this->registerHoistedObjectPropVar($propVar, $def->type, $getter);
$this->setNativePropertyVar($expr, $propVar);
$this->setNativePropertyValueSource($expr, self::NATIVE_PROPERTY_VALUE_VAR);

@ -0,0 +1,31 @@
--TEST--
Hoisted this numeric slots preserve inherited properties and existing references
--FILE--
<?php
class SlotBase {
public int $count = 2;
public float $weight = 1.5;
}
class SlotChild extends SlotBase {
public function update(): void {
$this->count += 3;
$this->weight += 0.5;
}
}
function main(): void {
$object = new SlotChild();
$count =& $object->count;
$weight =& $object->weight;
$object->update();
var_dump($count, $weight);
$count = 10;
$weight = 3.5;
$object->update();
var_dump($count, $weight);
}
?>
--EXPECT--
int(5)
float(2)
int(13)
float(4)
Loading…
Cancel
Save