Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
VennDev authored Jul 30, 2023
1 parent b56f313 commit 99348e8
Show file tree
Hide file tree
Showing 27 changed files with 571 additions and 924 deletions.
11 changes: 4 additions & 7 deletions src/vennv/vapm/AssumptionFailedError.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,19 @@

use TypeError;

final class AssumptionFailedError extends TypeError
{
final class AssumptionFailedError extends TypeError {

public function __construct(
protected string $errorMessage,
protected int $errorCode = 0
)
{
protected int $errorCode = 0
) {
parent::__construct(
$this->errorMessage,
$this->errorCode
);
}

public function __toString() : string
{
public function __toString() : string {
return __CLASS__ . ": [$this->errorCode]: $this->errorMessage\n";
}

Expand Down
28 changes: 10 additions & 18 deletions src/vennv/vapm/Async.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,59 +22,51 @@
use Throwable;
use function is_callable;

interface AsyncInterface
{
interface AsyncInterface {

public function getId(): int;
public function getId() : int;

/**
* @throws Throwable
*/
public static function await(Promise|Async|callable $await): mixed;
public static function await(Promise|Async|callable $await) : mixed;

}

final class Async implements AsyncInterface
{
final class Async implements AsyncInterface {

private int $id;

/**
* @throws Throwable
*/
public function __construct(callable $callback)
{
public function __construct(callable $callback) {
$promise = new Promise($callback, true);
$this->id = $promise->getId();
}

public function getId(): int
{
public function getId() : int {
return $this->id;
}

/**
* @throws Throwable
*/
public static function await(Promise|Async|callable $await): mixed
{
public static function await(Promise|Async|callable $await) : mixed {
$result = null;

if (is_callable($await))
{
if (is_callable($await)) {
$await = new Async($await);
}

$return = EventLoop::getReturn($await->getId());

while ($return === null)
{
while ($return === null) {
$return = EventLoop::getReturn($await->getId());
FiberManager::wait();
}

if ($return instanceof Promise)
{
if ($return instanceof Promise) {
$result = $return->getResult();
}

Expand Down
34 changes: 13 additions & 21 deletions src/vennv/vapm/ChildCoroutine.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,58 +22,50 @@
use Generator;
use Exception;

interface ChildCoroutineInterface
{
interface ChildCoroutineInterface {

public function getId(): int;
public function getId() : int;

public function setException(Exception $exception): void;
public function setException(Exception $exception) : void;

public function run(): void;
public function run() : void;

public function isFinished(): bool;
public function isFinished() : bool;

public function getReturn(): mixed;
public function getReturn() : mixed;

}

final class ChildCoroutine implements ChildCoroutineInterface
{
final class ChildCoroutine implements ChildCoroutineInterface {

protected int $id;

protected Generator $coroutine;

protected Exception $exception;

public function __construct(int $id, Generator $coroutine)
{
public function __construct(int $id, Generator $coroutine) {
$this->id = $id;
$this->coroutine = $coroutine;
}

public function getId(): int
{
public function getId() : int {
return $this->id;
}

public function setException(Exception $exception): void
{
public function setException(Exception $exception) : void {
$this->exception = $exception;
}

public function run(): void
{
public function run() : void {
$this->coroutine->send($this->coroutine->current());
}

public function isFinished(): bool
{
public function isFinished() : bool {
return !$this->coroutine->valid();
}

public function getReturn(): mixed
{
public function getReturn() : mixed {
return $this->coroutine->getReturn();
}

Expand Down
45 changes: 16 additions & 29 deletions src/vennv/vapm/CoroutineGen.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,46 +22,39 @@
use SplQueue;
use Generator;

interface CoroutineGenInterface
{
interface CoroutineGenInterface {

/**
* @param Generator|callable ...$coroutines
* @return void
*/
public static function runBlocking(Generator|callable ...$coroutines): void;
public static function runBlocking(Generator|callable ...$coroutines) : void;

public static function launch(callable $callback): Generator;
public static function launch(callable $callback) : Generator;

}

final class CoroutineGen implements CoroutineGenInterface
{
final class CoroutineGen implements CoroutineGenInterface {

protected static int $maxTaskId = 0;

protected static ?SplQueue $taskQueue = null;

/**
* @param Generator|callable ...$coroutines
* @return void
*/
public static function runBlocking(Generator|callable ...$coroutines): void
{
if (self::$taskQueue === null)
{
public static function runBlocking(Generator|callable ...$coroutines) : void {
if (self::$taskQueue === null) {
self::$taskQueue = new SplQueue();
}

foreach ($coroutines as $coroutine)
{
if (is_callable($coroutine))
{
foreach ($coroutines as $coroutine) {
if (is_callable($coroutine)) {
$coroutine = call_user_func($coroutine);
}

if ($coroutine instanceof Generator)
{
if ($coroutine instanceof Generator) {
$tid = ++self::$maxTaskId;
self::schedule(new ChildCoroutine($tid, $coroutine));
}
Expand All @@ -70,30 +63,24 @@ public static function runBlocking(Generator|callable ...$coroutines): void
self::run();
}

public static function launch(callable $callback): Generator
{
public static function launch(callable $callback) : Generator {
return yield $callback;
}

private static function schedule(ChildCoroutine $childCoroutine): void
{
private static function schedule(ChildCoroutine $childCoroutine) : void {
self::$taskQueue?->enqueue($childCoroutine);
}

private static function run(): void
{
if (self::$taskQueue !== null)
{
while (!self::$taskQueue->isEmpty())
{
private static function run() : void {
if (self::$taskQueue !== null) {
while (!self::$taskQueue->isEmpty()) {
/**
* @var ChildCoroutine $childCoroutine
*/
$childCoroutine = self::$taskQueue->dequeue();
$childCoroutine->run();

if (!$childCoroutine->isFinished())
{
if (!$childCoroutine->isFinished()) {
self::schedule($childCoroutine);
}
}
Expand Down
24 changes: 8 additions & 16 deletions src/vennv/vapm/Deferred.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,39 +21,31 @@

use Generator;

interface DeferredInterface
{
interface DeferredInterface {

/**
* This method is used to get the result of the deferred.
*/
public function await(): mixed;
public function await() : mixed;

}

final class Deferred implements DeferredInterface
{
final class Deferred implements DeferredInterface {

protected ChildCoroutine $childCoroutine;

public function __construct(callable $callback)
{
public function __construct(callable $callback) {
$generator = call_user_func($callback);

if ($generator instanceof Generator)
{
if ($generator instanceof Generator) {
$this->childCoroutine = new ChildCoroutine(0, $generator);
}
else
{
} else {
throw new DeferredException(Error::DEFERRED_CALLBACK_MUST_RETURN_GENERATOR);
}
}

public function await(): mixed
{
while (!$this->childCoroutine->isFinished())
{
public function await() : mixed {
while (!$this->childCoroutine->isFinished()) {
$this->childCoroutine->run();
}

Expand Down
11 changes: 4 additions & 7 deletions src/vennv/vapm/DeferredException.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,19 @@

use TypeError;

final class DeferredException extends TypeError
{
final class DeferredException extends TypeError {

public function __construct(
protected string $errorMessage,
protected int $errorCode = 0
)
{
protected int $errorCode = 0
) {
parent::__construct(
$this->errorMessage,
$this->errorCode
);
}

public function __toString() : string
{
public function __toString() : string {
return __CLASS__ . ": [$this->errorCode]: $this->errorMessage\n";
}

Expand Down
3 changes: 1 addition & 2 deletions src/vennv/vapm/DescriptorSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@

namespace vennv\vapm;

final class DescriptorSpec
{
final class DescriptorSpec {

public const BASIC = [
0 => ['pipe', 'r'],
Expand Down
3 changes: 1 addition & 2 deletions src/vennv/vapm/Error.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@

namespace vennv\vapm;

final class Error
{
final class Error {

public const FAILED_IN_FETCHING_DATA = "Error in fetching data";

Expand Down
Loading

0 comments on commit 99348e8

Please sign in to comment.