Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/HotSwap/HotSwap.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
30 changes: 30 additions & 0 deletions src/Reflection/ReflectionClass.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down
3 changes: 2 additions & 1 deletion src/Type/StringEntry.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
*/
Expand Down
19 changes: 19 additions & 0 deletions tests/HotSwap/ClassDeltaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use ZEngine\ClassExtension\ObjectCreateTrait;
use ZEngine\OpCache\SharedMemoryException;
use ZEngine\Reflection\ReflectionClass;
use ZEngine\Type\ObjectEntry;

class ClassDeltaTest extends TestCase
{
Expand Down Expand Up @@ -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; }');
Expand Down
Loading