Make all helper functions static, and add support for ArrayAccess in coroutines

This commit is contained in:
Daniil Gentili 2019-06-06 19:52:02 +02:00
parent c16328396d
commit d18300a631
3 changed files with 74 additions and 48 deletions

2
docs

@ -1 +1 @@
Subproject commit dc05dc5cebfcec90ac7851928c522a4d635dbab6 Subproject commit 9754f506d36c41260096b384ed82677faf2ab5bf

View File

@ -41,7 +41,7 @@ use Amp\Success;
* value is sent into the generator, while a failure reason is thrown into the generator. Using a coroutine, * value is sent into the generator, while a failure reason is thrown into the generator. Using a coroutine,
* asynchronous code can be written without callbacks and be structured like synchronous code. * asynchronous code can be written without callbacks and be structured like synchronous code.
*/ */
final class Coroutine implements Promise final class Coroutine implements Promise, \ArrayAccess
{ {
use Internal\Placeholder; use Internal\Placeholder;
/** @var \Generator */ /** @var \Generator */
@ -181,4 +181,29 @@ final class Coroutine implements Promise
}; };
$yielded->onResolve($this->onResolve); $yielded->onResolve($this->onResolve);
} }
public function offsetExists($offset): bool
{
throw new Exception('Not supported!');
}
public function offsetGet($offset)
{
return Tools::call((function () use ($offset) {
return (yield $this)[$offset];
})());
}
public function offsetSet($offset, $value)
{
return Tools::call((function () use ($offset, $value) {
$result = yield $this;
return $result[$offset] = value;
})());
}
public function offsetUnset($offset)
{
return Tools::call((function () use ($offset) {
$result = yield $this;
unset($result[$offset]);
})());
}
} }

View File

@ -38,7 +38,7 @@ use function Amp\ByteStream\getOutputBufferStream;
*/ */
trait Tools trait Tools
{ {
public function gen_vector_hash($ints) public static function gen_vector_hash($ints)
{ {
//sort($ints, SORT_NUMERIC); //sort($ints, SORT_NUMERIC);
if (\danog\MadelineProto\Magic::$bigint) { if (\danog\MadelineProto\Magic::$bigint) {
@ -46,7 +46,7 @@ trait Tools
foreach ($ints as $int) { foreach ($ints as $int) {
$hash = $hash->multiply(\danog\MadelineProto\Magic::$twozerotwosixone)->add(\danog\MadelineProto\Magic::$zeroeight)->add(new \phpseclib\Math\BigInteger($int))->divide(\danog\MadelineProto\Magic::$zeroeight)[1]; $hash = $hash->multiply(\danog\MadelineProto\Magic::$twozerotwosixone)->add(\danog\MadelineProto\Magic::$zeroeight)->add(new \phpseclib\Math\BigInteger($int))->divide(\danog\MadelineProto\Magic::$zeroeight)[1];
} }
$hash = $this->unpack_signed_int(strrev(str_pad($hash->toBytes(), 4, "\0", STR_PAD_LEFT))); $hash = self::unpack_signed_int(strrev(str_pad($hash->toBytes(), 4, "\0", STR_PAD_LEFT)));
} else { } else {
$hash = 0; $hash = 0;
foreach ($ints as $int) { foreach ($ints as $int) {
@ -57,7 +57,7 @@ trait Tools
return $hash; return $hash;
} }
public function random_int($modulus = false) public static function random_int($modulus = false)
{ {
if ($modulus === false) { if ($modulus === false) {
$modulus = PHP_INT_MAX; $modulus = PHP_INT_MAX;
@ -77,15 +77,15 @@ trait Tools
} }
if (Magic::$bigint) { if (Magic::$bigint) {
$number = $this->unpack_signed_int($this->random(4)); $number = self::unpack_signed_int(self::random(4));
} else { } else {
$number = $this->unpack_signed_long($this->random(8)); $number = self::unpack_signed_long(self::random(8));
} }
return ($number & PHP_INT_MAX) % $modulus; return ($number & PHP_INT_MAX) % $modulus;
} }
public function random($length) public static function random($length)
{ {
return $length === 0 ? '' : \phpseclib\Crypt\Random::string($length); return $length === 0 ? '' : \phpseclib\Crypt\Random::string($length);
} }
@ -94,14 +94,14 @@ trait Tools
* posmod(numeric,numeric) : numeric * posmod(numeric,numeric) : numeric
* Works just like the % (modulus) operator, only returns always a postive number. * Works just like the % (modulus) operator, only returns always a postive number.
*/ */
public function posmod($a, $b) public static function posmod($a, $b)
{ {
$resto = $a % $b; $resto = $a % $b;
return $resto < 0 ? $resto + abs($b) : $resto; return $resto < 0 ? $resto + abs($b) : $resto;
} }
public function unpack_signed_int($value) public static function unpack_signed_int($value)
{ {
if (strlen($value) !== 4) { if (strlen($value) !== 4) {
throw new TL\Exception(\danog\MadelineProto\Lang::$current_lang['length_not_4']); throw new TL\Exception(\danog\MadelineProto\Lang::$current_lang['length_not_4']);
@ -110,7 +110,7 @@ trait Tools
return unpack('l', \danog\MadelineProto\Magic::$BIG_ENDIAN ? strrev($value) : $value)[1]; return unpack('l', \danog\MadelineProto\Magic::$BIG_ENDIAN ? strrev($value) : $value)[1];
} }
public function unpack_signed_long($value) public static function unpack_signed_long($value)
{ {
if (strlen($value) !== 8) { if (strlen($value) !== 8) {
throw new TL\Exception(\danog\MadelineProto\Lang::$current_lang['length_not_8']); throw new TL\Exception(\danog\MadelineProto\Lang::$current_lang['length_not_8']);
@ -119,7 +119,7 @@ trait Tools
return unpack('q', \danog\MadelineProto\Magic::$BIG_ENDIAN ? strrev($value) : $value)[1]; return unpack('q', \danog\MadelineProto\Magic::$BIG_ENDIAN ? strrev($value) : $value)[1];
} }
public function pack_signed_int($value) public static function pack_signed_int($value)
{ {
if ($value > 2147483647) { if ($value > 2147483647) {
throw new TL\Exception(sprintf(\danog\MadelineProto\Lang::$current_lang['value_bigger_than_2147483647'], $value)); throw new TL\Exception(sprintf(\danog\MadelineProto\Lang::$current_lang['value_bigger_than_2147483647'], $value));
@ -132,7 +132,7 @@ trait Tools
return \danog\MadelineProto\Magic::$BIG_ENDIAN ? strrev($res) : $res; return \danog\MadelineProto\Magic::$BIG_ENDIAN ? strrev($res) : $res;
} }
public function pack_signed_long($value) public static function pack_signed_long($value)
{ {
if ($value > 9223372036854775807) { if ($value > 9223372036854775807) {
throw new TL\Exception(sprintf(\danog\MadelineProto\Lang::$current_lang['value_bigger_than_9223372036854775807'], $value)); throw new TL\Exception(sprintf(\danog\MadelineProto\Lang::$current_lang['value_bigger_than_9223372036854775807'], $value));
@ -140,12 +140,12 @@ trait Tools
if ($value < -9.223372036854776E+18) { if ($value < -9.223372036854776E+18) {
throw new TL\Exception(sprintf(\danog\MadelineProto\Lang::$current_lang['value_smaller_than_9223372036854775808'], $value)); throw new TL\Exception(sprintf(\danog\MadelineProto\Lang::$current_lang['value_smaller_than_9223372036854775808'], $value));
} }
$res = \danog\MadelineProto\Magic::$bigint ? $this->pack_signed_int($value)."\0\0\0\0" : (\danog\MadelineProto\Magic::$BIG_ENDIAN ? strrev(pack('q', $value)) : pack('q', $value)); $res = \danog\MadelineProto\Magic::$bigint ? self::pack_signed_int($value)."\0\0\0\0" : (\danog\MadelineProto\Magic::$BIG_ENDIAN ? strrev(pack('q', $value)) : pack('q', $value));
return $res; return $res;
} }
public function pack_unsigned_int($value) public static function pack_unsigned_int($value)
{ {
if ($value > 4294967295) { if ($value > 4294967295) {
throw new TL\Exception(sprintf(\danog\MadelineProto\Lang::$current_lang['value_bigger_than_4294967296'], $value)); throw new TL\Exception(sprintf(\danog\MadelineProto\Lang::$current_lang['value_bigger_than_4294967296'], $value));
@ -157,7 +157,7 @@ trait Tools
return pack('V', $value); return pack('V', $value);
} }
public function pack_double($value) public static function pack_double($value)
{ {
$res = pack('d', $value); $res = pack('d', $value);
if (strlen($res) !== 8) { if (strlen($res) !== 8) {
@ -167,7 +167,7 @@ trait Tools
return \danog\MadelineProto\Magic::$BIG_ENDIAN ? strrev($res) : $res; return \danog\MadelineProto\Magic::$BIG_ENDIAN ? strrev($res) : $res;
} }
public function unpack_double($value) public static function unpack_double($value)
{ {
if (strlen($value) !== 8) { if (strlen($value) !== 8) {
throw new TL\Exception(\danog\MadelineProto\Lang::$current_lang['length_not_8']); throw new TL\Exception(\danog\MadelineProto\Lang::$current_lang['length_not_8']);
@ -176,7 +176,7 @@ trait Tools
return unpack('d', \danog\MadelineProto\Magic::$BIG_ENDIAN ? strrev($value) : $value)[1]; return unpack('d', \danog\MadelineProto\Magic::$BIG_ENDIAN ? strrev($value) : $value)[1];
} }
public function wait($promise) public static function wait($promise)
{ {
if ($promise instanceof \Generator) { if ($promise instanceof \Generator) {
$promise = new Coroutine($promise); $promise = new Coroutine($promise);
@ -209,48 +209,48 @@ trait Tools
return $value; return $value;
} }
public function all($promises) public static function all($promises)
{ {
foreach ($promises as &$promise) { foreach ($promises as &$promise) {
$promise = $this->call($promise); $promise = self::call($promise);
} }
return all($promises); return all($promises);
} }
public function any($promises) public static function any($promises)
{ {
foreach ($promises as &$promise) { foreach ($promises as &$promise) {
$promise = $this->call($promise); $promise = self::call($promise);
} }
return any($promises); return any($promises);
} }
public function some($promises) public static function some($promises)
{ {
foreach ($promises as &$promise) { foreach ($promises as &$promise) {
$promise = $this->call($promise); $promise = self::call($promise);
} }
return some($promises); return some($promises);
} }
public function first($promises) public static function first($promises)
{ {
foreach ($promises as &$promise) { foreach ($promises as &$promise) {
$promise = $this->call($promise); $promise = self::call($promise);
} }
return first($promises); return first($promises);
} }
public function timeout($promise, $timeout) public static function timeout($promise, $timeout)
{ {
return timeout($this->call($promise), $timeout); return timeout(self::call($promise), $timeout);
} }
public function call($promise) public static function call($promise)
{ {
if ($promise instanceof \Generator) { if ($promise instanceof \Generator) {
$promise = new Coroutine($promise); $promise = new Coroutine($promise);
@ -261,7 +261,7 @@ trait Tools
return $promise; return $promise;
} }
public function callFork($promise, $actual = null, $file = '') public static function callFork($promise, $actual = null, $file = '')
{ {
if ($actual) { if ($actual) {
$promise = $actual; $promise = $actual;
@ -281,7 +281,7 @@ trait Tools
if ($promise instanceof Promise) { if ($promise instanceof Promise) {
$promise->onResolve(function ($e, $res) use ($file) { $promise->onResolve(function ($e, $res) use ($file) {
if ($e) { if ($e) {
$this->rethrow($e, $file); self::rethrow($e, $file);
} }
}); });
} }
@ -289,39 +289,40 @@ trait Tools
return $promise; return $promise;
} }
public function callForkDefer($promise) public static function callForkDefer($promise)
{ {
Loop::defer([$this, 'callFork'], $promise); Loop::defer([__CLASS__, 'callFork'], $promise);
} }
public function rethrow($e, $file = '') public static function rethrow($e, $file = '')
{ {
$logger = isset($this->logger) ? $this->logger : Logger::$default; $zis = isset($this) ? $this : null;
$logger = isset($zis->logger) ? $zis->logger : Logger::$default;
if ($file) { if ($file) {
$file = " started @ $file"; $file = " started @ $file";
} }
$logger->logger("Got the following exception within a forked strand$file, trying to rethrow"); $logger->logger("Got the following exception within a forked strand$file, trying to rethrow");
if ($e->getMessage() === "Cannot get return value of a generator that hasn't returned") { if ($e->getMessage() === "Cannot get return value of a generator that hasn't returned") {
$logger->logger("Well you know, this might actually not be the actual exception, scroll up in the logs to see the actual exception"); $logger->logger("Well you know, this might actually not be the actual exception, scroll up in the logs to see the actual exception");
if (!isset($this->destructing)) Promise\rethrow(new Failure($e)); if (!isset($zis->destructing)) Promise\rethrow(new Failure($e));
} else { } else {
$logger->logger($e); $logger->logger($e);
Promise\rethrow(new Failure($e)); Promise\rethrow(new Failure($e));
} }
} }
public function after($a, $b) public static function after($a, $b)
{ {
$a = $this->call($a()); $a = self::call($a());
$deferred = new Deferred(); $deferred = new Deferred();
$a->onResolve(function ($e, $res) use ($b, $deferred) { $a->onResolve(static function ($e, $res) use ($b, $deferred) {
if ($e) { if ($e) {
return $this->rethrow($e); return self::rethrow($e);
} }
$b = $this->call($b()); $b = self::call($b());
$b->onResolve(static function ($e, $res) use ($deferred) { $b->onResolve(static function ($e, $res) use ($deferred) {
if ($e) { if ($e) {
return $this->rethrow($e); return self::rethrow($e);
} }
$deferred->resolve($res); $deferred->resolve($res);
}); });
@ -330,15 +331,15 @@ trait Tools
return $deferred->promise(); return $deferred->promise();
} }
public function sleep($time) public static function sleep($time)
{ {
return new \Amp\Delayed($time * 1000); return new \Amp\Delayed($time * 1000);
} }
public function readLine($prompt = '') public static function readLine($prompt = '')
{ {
return $this->call($this->readLineAsync($prompt)); return self::call(self::readLineAsync($prompt));
} }
public function readLineAsync($prompt = '') public static function readLineAsync($prompt = '')
{ {
$stdin = getStdin(); $stdin = getStdin();
$stdout = getStdout(); $stdout = getStdout();
@ -355,11 +356,11 @@ trait Tools
return array_shift($lines); return array_shift($lines);
} }
public function echo($string) public static function echo($string)
{ {
return getOutputBufferStream()->write($string); return getOutputBufferStream()->write($string);
} }
public function is_array_or_alike($var) public static function is_array_or_alike($var)
{ {
return is_array($var) || return is_array($var) ||
($var instanceof ArrayAccess && ($var instanceof ArrayAccess &&