3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?hh class Guard { private static bool $guarded = false; public static function acquire(): void { self::$guarded = true; } public static function release(): void { self::$guarded = false; } public static function isGuarded(): bool { return self::$guarded; } } class BatchedWriter { const int BATCH_SIZE = 2; private Set<string> $queue = Set { }; private ?Awaitable<ImmMap<string, string>> $waitHandle = null; public async function write(string $id): Awaitable<string> { $this->queue[] = $id; $all_results = await $this->writeAll(); return $all_results->at($id); } private function writeAll(): Awaitable<ImmMap<string, string>> { $wh = $this->waitHandle; if ($wh === null || HH\Asio\has_finished($wh)) { $wh = $this->writeAllImpl(); $this->waitHandle = $wh; } return $wh; } private async function writeAllImpl(): Awaitable<ImmMap<string, string>> { while (count($this->queue) < self::BATCH_SIZE) { await HH\Asio\later(); } $results = Map { }; foreach ($this->queue as $id) { $results[$id] = $id.' is '.(Guard::isGuarded() ? 'guarded' : 'unguarded'); } $this->queue = Set { }; return $results->immutable(); } <<__Memoize>> public static function get(): BatchedWriter { return new BatchedWriter(); } } function stop_eager_execution(): Awaitable<void> { return RescheduleWaitHandle::create(RescheduleWaitHandle::QUEUE_DEFAULT, 0); } function join_for_guard<T>( (function(): Awaitable<T>) $callback, ): T { $awaitable = async { await stop_eager_execution(); return await $callback(); }; return HH\Asio\join($awaitable); } async function do_unguarded_write(): Awaitable<void> { $result = await BatchedWriter::get()->write(__FUNCTION__); var_dump(__FUNCTION__.': '.$result); } async function do_guarded_write(): Awaitable<void> { $id = __FUNCTION__; await HH\Asio\later(); Guard::acquire(); $result = join_for_guard(async () ==> await BatchedWriter::get()->write($id)); var_dump($id.': '.$result); Guard::release(); } HH\Asio\join(HH\Asio\va( do_unguarded_write(), do_guarded_write(), ));
based on S5YIY
Output for 5.6.0 - 5.6.30, 7.0.0 - 7.1.4
Parse error: syntax error, unexpected 'class' (T_CLASS) in /in/2hgn6 on line 3
Process exited with code 255.
Output for hhvm-3.17.3
string(45) "do_guarded_write: do_guarded_write is guarded" string(49) "do_unguarded_write: do_unguarded_write is guarded"
Output for hhvm-3.12.14 - 3.15.4
Fatal error: Uncaught Error: Call to undefined function HH\Asio\va() in /in/2hgn6:87 Stack trace: #0 {main}
Process exited with code 255.