diff --git a/src/HotSwap/HotSwap.php b/src/HotSwap/HotSwap.php index df89b02..e9cbf99 100644 --- a/src/HotSwap/HotSwap.php +++ b/src/HotSwap/HotSwap.php @@ -207,6 +207,10 @@ private static function compileDonorClass( self::unpublishClassEntry($lowerName); } self::publishClassEntry($lowerName, $liveClass); + // Linking the donor (a class with a parent or interfaces) memoized it in the + // engine's class-name cache. Repoint that slot at the live entry, or every later + // lookup by name resolves the donor, which is destroyed on apply or discard. + $liveClass->bindClassNameCache($donorClass); } if ($compileError !== null) { diff --git a/src/Reflection/ReflectionClass.php b/src/Reflection/ReflectionClass.php index 56087fd..522690c 100644 --- a/src/Reflection/ReflectionClass.php +++ b/src/Reflection/ReflectionClass.php @@ -782,6 +782,36 @@ public function copyOutOfSharedMemory(): void } } + /** + * Points the engine's fast class-name cache at this class entry + * + * A class lookup by an interned name reads a memoized per-request slot BEFORE the + * class table, and the engine fills that slot itself whenever it links a class + * (zend_do_link_class). Code that swaps the entry a name resolves to behind the + * engine's back must repoint the slot too, or lookups keep returning the old entry. + * + * @param ReflectionClass|null $namesake Another entry declared under the same name + * whose name string must stop resolving to it + * + * @internal shared with the hot-swap machinery (HotSwap) + */ + public function bindClassNameCache(?ReflectionClass $namesake = null): void + { + $nameStrings = [$this->pointer->name]; + if ($namesake !== null) { + // Usually the same interned string, but nothing guarantees it + $nameStrings[] = $namesake->pointer->name; + } + foreach ($nameStrings as $nameString) { + // Engine invariant: every class entry carries a name + assert($nameString !== null); + $nameEntry = StringEntry::fromCData($nameString); + if ($nameEntry->hasClassEntryCache()) { + $nameEntry->setCachedClassEntry($this->pointer); + } + } + } + /** * Returns the raw zend_class_entry pointer this reflection wraps * diff --git a/src/Type/StringEntry.php b/src/Type/StringEntry.php index 59dac5b..a662ae2 100644 --- a/src/Type/StringEntry.php +++ b/src/Type/StringEntry.php @@ -16,6 +16,7 @@ use FFI\CData; use ReflectionClass; use ZEngine\Core; +use ZEngine\Generated\zend_class_entry; use ZEngine\Generated\zend_refcounted_h; use ZEngine\Generated\zend_string; use ZEngine\Memory\Allocator; @@ -335,7 +336,7 @@ public function hasClassEntryCache(): bool * sites compiled into cached scripts read the memoized entry, not the table, so without * this refresh they would keep dispatching the shared-memory class entry. * - * @param CData $classEntry zend_class_entry the name must resolve to from now on + * @param CData|zend_class_entry $classEntry zend_class_entry the name must resolve to from now on * * @see zend_types.h:ZSTR_SET_CE_CACHE */ diff --git a/tests/HotSwap/ClassDeltaTest.php b/tests/HotSwap/ClassDeltaTest.php index d28bcd4..4c39e00 100644 --- a/tests/HotSwap/ClassDeltaTest.php +++ b/tests/HotSwap/ClassDeltaTest.php @@ -20,6 +20,7 @@ use ZEngine\ClassExtension\ObjectCreateTrait; use ZEngine\OpCache\SharedMemoryException; use ZEngine\Reflection\ReflectionClass; +use ZEngine\Type\ObjectEntry; class ClassDeltaTest extends TestCase { @@ -139,6 +140,24 @@ public function testRemovedOverrideRestoresInheritedMethod(): void $this->assertSame('base', self::callMethod($restoredInstance, 'speak')); } + public function testNameLookupResolvesLiveClassAfterSwapOfLinkedClass(): void + { + $baseName = 'HotSwapBaseForNameCache'; + $className = 'HotSwapNameCacheTarget'; + eval("class {$baseName} {}"); + eval("class {$className} extends {$baseName} { public function speak(): string { return \"live\"; } }"); + $liveAddress = (new ReflectionClass($className))->getAddress(); + + // Linking a donor with a parent stores it in the engine's class-name cache. A lookup + // by name must still reach the live entry once the donor is destroyed, on discard + // as well as on apply. + HotSwap::prepare($className, "class {$className} extends {$baseName} {}")->discard(); + $this->assertSame($liveAddress, (new ObjectEntry(self::newInstance($className)))->getClass()->getAddress()); + + HotSwap::prepare($className, "class {$className} extends {$baseName} {}")->apply(); + $this->assertSame($liveAddress, (new ObjectEntry(self::newInstance($className)))->getClass()->getAddress()); + } + public function testConstantChangeAndAddition(): void { $className = $this->declareClass('Const', '{ public const VERSION = 1; }');