ownership.php (942B)
1 <?php 2 declare(strict_types=1); 3 require __DIR__ . '/../src/ffi.php'; 4 5 $ctx = Arboricx\ctx_init(getenv('ARBORICX_LIB')); 6 $other = Arboricx\ctx_init(getenv('ARBORICX_LIB')); 7 try { 8 $h = Arboricx\ofString($ctx, 'owned'); 9 $alias = Arboricx\retain($ctx, $h); 10 Arboricx\release($ctx, $h); 11 Arboricx\collect($ctx); 12 if (Arboricx\toString($ctx, $alias) !== 'owned') throw new RuntimeException('lost root'); 13 foreach ([fn() => Arboricx\release($ctx, $h), fn() => Arboricx\retain($other, $alias)] as $invalid) { 14 try { 15 $invalid(); 16 throw new LogicException('invalid handle accepted'); 17 } catch (RuntimeException $error) { 18 if (!str_contains($error->getMessage(), 'status 1')) throw $error; 19 } 20 } 21 Arboricx\release($ctx, $alias); 22 Arboricx\collect($ctx); 23 echo "PHP ownership checks passed\n"; 24 } finally { 25 Arboricx\ctx_free($other); 26 Arboricx\ctx_free($ctx); 27 }