Ejemplo n.º 1
0
 public function foo()
 {
     $x = 1 + 2 * (3 - 2 / 5);
     $y = -++$x % +10;
     $x -= $y++;
     $y /= --$z;
     $b = "hi" . " hello";
     $c .= "hey there";
     $n = $x-- | $y & ($z ^ ~$x >> 2) << 3;
     $yes = ((bool) \hacklib_cast_as_boolean($x) && $x > $y || !($x >= $z)) ^ $z < $y || $z <= $x;
     $maybe = \hacklib_equals($x, $y) && $x === $y && \hacklib_not_equals($z, $x) && $z !== $y;
 }
Ejemplo n.º 2
0
function boolean_expressions($c)
{
    $t = \hacklib_cast_as_boolean(is_array($c)) ? "Array" : $c->__toString();
    $isEmpty = \hacklib_cast_as_boolean(is_array($c)) ? !isset($c) || \hacklib_equals($c, false) : $c->isEmpty();
    $isEmptyStr = \hacklib_cast_as_boolean($isEmpty) ? "Empty" : "Not Empty";
    echo "\nTesting a {$t} that is {$isEmptyStr} \n";
    $b = (bool) \hacklib_cast_as_boolean($c);
    print_result("(bool) Cast", $b);
    $b = (bool) \hacklib_cast_as_boolean($c);
    print_result("(boolean) Cast", $b);
    print_result("&& left", \hacklib_cast_as_boolean($c) && true);
    print_result("&& right", true && \hacklib_cast_as_boolean($c));
    print_result("&& both", \hacklib_cast_as_boolean($c) && \hacklib_cast_as_boolean($c));
    print_result("|| left", \hacklib_cast_as_boolean($c) || false);
    print_result("|| right", false || \hacklib_cast_as_boolean($c));
    print_result("|| both", \hacklib_cast_as_boolean($c) || \hacklib_cast_as_boolean($c));
    print_result("! ", !\hacklib_cast_as_boolean($c));
    print_result("Eif", \hacklib_cast_as_boolean($c) ? true : false);
}
Ejemplo n.º 3
0
 public function readI64(&$value)
 {
     $data = $this->trans_->readAll(8);
     $arr = unpack("N2", $data);
     if (PHP_INT_SIZE == 4) {
         $hi = $arr[1];
         $lo = $arr[2];
         $isNeg = $hi < 0;
         if (\hacklib_cast_as_boolean($isNeg)) {
             $hi = ~$hi & (int) 0xffffffff;
             $lo = ~$lo & (int) 0xffffffff;
             if (\hacklib_equals($lo, (int) 0xffffffff)) {
                 $hi++;
                 $lo = 0;
             } else {
                 $lo++;
             }
         }
         if ($hi & (int) 0x80000000) {
             $hi &= (int) 0x7fffffff;
             $hi += 0x80000000;
         }
         if ($lo & (int) 0x80000000) {
             $lo &= (int) 0x7fffffff;
             $lo += 0x80000000;
         }
         $value = $hi * 4294967296 + $lo;
         if (\hacklib_cast_as_boolean($isNeg)) {
             $value = 0 - $value;
         }
     } else {
         if ($arr[2] & 0x80000000) {
             $arr[2] = $arr[2] & 0xffffffff;
         }
         if ($arr[1] & 0x80000000) {
             $arr[1] = $arr[1] & 0xffffffff;
             $arr[1] = $arr[1] ^ 0xffffffff;
             $arr[2] = $arr[2] ^ 0xffffffff;
             $value = 0 - $arr[1] * 4294967296 - $arr[2] - 1;
         } else {
             $value = $arr[1] * 4294967296 + $arr[2];
         }
     }
     return 8;
 }
Ejemplo n.º 4
0
 protected function _read($class, $spec, $input)
 {
     $xfer = 0;
     $fname = null;
     $ftype = 0;
     $fid = 0;
     $xfer += $input->readStructBegin($fname);
     while (true) {
         $xfer += $input->readFieldBegin($fname, $ftype, $fid);
         if (\hacklib_equals($ftype, TType::STOP)) {
             break;
         }
         if (\hacklib_cast_as_boolean(isset($spec[$fid]))) {
             $fspec = $spec[$fid];
             $var = $fspec[\hacklib_id('var')];
             if (\hacklib_equals($ftype, $fspec[\hacklib_id('type')])) {
                 $xfer = 0;
                 if (\hacklib_cast_as_boolean(isset(TBase::$tmethod[$ftype]))) {
                     $func = 'read' . TBase::$tmethod[$ftype];
                     $xfer += $input->{$func}($this->{$var});
                 } else {
                     switch ($ftype) {
                         case TType::STRUCT:
                             $class = $fspec[\hacklib_id('class')];
                             $this->{$var} = new $class();
                             $xfer += $this->{$var}->read($input);
                             break;
                         case TType::MAP:
                             $xfer += $this->_readMap($this->{$var}, $fspec, $input);
                             break;
                         case TType::LST:
                             $xfer += $this->_readList($this->{$var}, $fspec, $input, false);
                             break;
                         case TType::SET:
                             $xfer += $this->_readList($this->{$var}, $fspec, $input, true);
                             break;
                     }
                 }
             } else {
                 $xfer += $input->skip($ftype);
             }
         } else {
             $xfer += $input->skip($ftype);
         }
         $xfer += $input->readFieldEnd();
     }
     $xfer += $input->readStructEnd();
     return $xfer;
 }
 public static function checkVersion($v)
 {
     return \hacklib_equals($v, 1);
 }
Ejemplo n.º 6
0
 public function flush()
 {
     if (!\hacklib_cast_as_boolean($this->write_) || \hacklib_equals(strlen($this->wBuf_), 0)) {
         $this->transport_->flush();
         return;
     }
     $out = (string) pack("N", strlen($this->wBuf_));
     $out .= $this->wBuf_;
     $this->wBuf_ = "";
     $this->transport_->write($out);
     $this->transport_->flush();
 }
Ejemplo n.º 7
0
 public function open()
 {
     if (\hacklib_cast_as_boolean($this->ipV6_)) {
         $handle = socket_create(AF_INET6, SOCK_STREAM, SOL_TCP);
     } else {
         $handle = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
     }
     if ($handle === false) {
         $error = "TNonBlockingSocket: Could not create socket";
         throw new TTransportException($error);
     }
     $this->handle_ = $handle;
     if (!\hacklib_cast_as_boolean(socket_set_nonblock($this->handle_))) {
         $error = "TNonBlockingSocket: Could not set nonblocking.";
         throw new TTransportException($error);
     }
     $res = socket_connect($this->handle_, $this->host_, $this->port_);
     if (!\hacklib_cast_as_boolean($res)) {
         $errno = socket_last_error($this->handle_);
         $errstr = socket_strerror($errno);
         $error = "TNonBlockingSocket: socket_connect error (" . (string) $errstr . "[" . (string) $errno . "])";
         if (\hacklib_not_equals($errno, 115)) {
             if (\hacklib_cast_as_boolean($this->debug_)) {
                 call_user_func($this->debugHandler_, $error);
             }
         }
         throw new TTransportException($error);
     }
     $wBuf_ = "";
     $rBuf_ = "";
     $rBufPos_ = 0;
     $this->sockRecvCapacity_ = socket_get_option($this->handle_, SOL_SOCKET, SO_RCVBUF);
     if (\hacklib_equals($this->sockRecvCapacity_, false)) {
         $this->sockRecvCapacity_ = null;
     }
 }
Ejemplo n.º 8
0
 private static function inStreamName()
 {
     if (\hacklib_equals(php_sapi_name(), 'cli')) {
         return 'php://stdin';
     }
     return 'php://input';
 }
Ejemplo n.º 9
0
    echo $name . " : " . (\hacklib_cast_as_boolean($c) ? " TRUE " : " FALSE ") . "\n";
}
$o1 = 10;
$o2 = "10";
print_res("straightforward equality", 10 == "10");
print_res("simple equality", \hacklib_equals($o1, $o2));
print_res("simple inequality", \hacklib_not_equals($o1, $o2));
print_res("vec and immvec", \hacklib_equals(new \HH\Vector(array()), new \HH\ImmVector(array())));
$m1 = \HH\Map::hacklib_new(array("t", 1), array("tt", 2));
$m2 = \HH\Map::hacklib_new(array(1, "t"), array(2, "tt"));
print_res("map key order", \hacklib_equals($m1, $m2));
$p1 = \HH\Pair::hacklib_new(1, 2);
$p2 = \HH\Pair::hacklib_new(1, 2);
$p1->getIterator();
print_res("collections with additional state", \hacklib_equals($p1, $p2));
print_res("Pairs can be unequal", \hacklib_not_equals(\HH\Pair::hacklib_new("high", "tea"), \HH\Pair::hacklib_new("high", "tee")));
print_res("Vector uses ==", \hacklib_equals(new \HH\Vector(array(1)), new \HH\ImmVector(array("1"))));
print_res("Even if Empty", \hacklib_equals(new \HH\Vector(array()), new \HH\ImmVector(array())));
print_res("Number of Items", \hacklib_equals(new \HH\Set(array(1)), new \HH\Set(array(1, 2))));
print_res("Sets use === on keys", \hacklib_equals(new \HH\Set(array(1)), new \HH\Set(array("1"))));
print_res("Sets can equal ImmSets", \hacklib_equals(new \HH\Set(array("a", 1)), new \HH\ImmSet(array(1, "a"))));
print_res("Maps do both", \hacklib_equals(\HH\Map::hacklib_new(array("1", 1), array("zork", "mindy")), \HH\Map::hacklib_new(array(1, "1"), array("mindy", "zork"))));
print_res("Maps can equal ImmMaps", \hacklib_equals(\HH\ImmMap::hacklib_new(array("o", 1), array("mork", 1)), \HH\Map::hacklib_new(array(1, "o"), array(1, "mork"))));
print_res("Check for false", \hacklib_equals(new \HH\Vector(array()), false));
print_res("Check for true", \hacklib_equals(true, new \HH\Vector(array())));
print_res("Using not equals", \hacklib_not_equals(\HH\Map::hacklib_new(array(), array()), true));
print_res("Nested Comparison True", \hacklib_equals(new \HH\Vector(array(\HH\Map::hacklib_new(array(1), array("zoom")), new \HH\Vector(array("33")))), new \HH\Vector(array(\HH\Map::hacklib_new(array(1), array("zoom")), new \HH\ImmVector(array(33))))));
print_res("Nested Comparison False", \hacklib_equals(new \HH\Vector(array(\HH\Map::hacklib_new(array(1), array("zoom")), new \HH\Set(array("33")))), new \HH\Vector(array(\HH\Map::hacklib_new(array(1), array("zoom")), new \HH\Set(array(33))))));
print_res("Compare with null", \hacklib_equals(null, new \HH\Vector(array())));
print_res("Compare values that are null", \hacklib_equals(new \HH\Vector(array(false)), new \HH\Vector(array(null))));
Ejemplo n.º 10
0
<?php

require_once $GLOBALS["HACKLIB_ROOT"];
function print_res($c)
{
    var_dump($c);
    echo "\n";
}
$v = new \HH\Vector(array());
print_res(!isset($non_existent) || \hacklib_equals($non_existent, false));
print_res(!isset($v) || \hacklib_equals($v, false));
Ejemplo n.º 11
0
 public static function skipBinary($itrans, $type)
 {
     switch ($type) {
         case TType::BOOL:
             return $itrans->readAll(1);
         case TType::BYTE:
             return $itrans->readAll(1);
         case TType::I16:
             return $itrans->readAll(2);
         case TType::I32:
             return $itrans->readAll(4);
         case TType::I64:
             return $itrans->readAll(8);
         case TType::DOUBLE:
             return $itrans->readAll(8);
         case TType::FLOAT:
             return $itrans->readAll(4);
         case TType::STRING:
             $len = unpack("N", $itrans->readAll(4));
             $len = $len[1];
             if ($len > 0x7fffffff) {
                 $len = 0 - ($len - 1 ^ 0xffffffff);
             }
             return 4 + $itrans->readAll($len);
         case TType::STRUCT:
             $result = 0;
             while (true) {
                 $ftype = 0;
                 $fid = 0;
                 $data = $itrans->readAll(1);
                 $arr = unpack("c", $data);
                 $ftype = $arr[1];
                 if (\hacklib_equals($ftype, TType::STOP)) {
                     break;
                 }
                 $result += $itrans->readAll(2);
                 $result += self::skipBinary($itrans, $ftype);
             }
             return $result;
         case TType::MAP:
             $data = $itrans->readAll(1);
             $arr = unpack("c", $data);
             $ktype = $arr[1];
             $data = $itrans->readAll(1);
             $arr = unpack("c", $data);
             $vtype = $arr[1];
             $data = $itrans->readAll(4);
             $arr = unpack("N", $data);
             $size = $arr[1];
             if ($size > 0x7fffffff) {
                 $size = 0 - ($size - 1 ^ 0xffffffff);
             }
             $result = 6;
             for ($i = 0; $i < $size; $i++) {
                 $result += self::skipBinary($itrans, $ktype);
                 $result += self::skipBinary($itrans, $vtype);
             }
             return $result;
         case TType::SET:
         case TType::LST:
             $data = $itrans->readAll(1);
             $arr = unpack("c", $data);
             $vtype = $arr[1];
             $data = $itrans->readAll(4);
             $arr = unpack("N", $data);
             $size = $arr[1];
             if ($size > 0x7fffffff) {
                 $size = 0 - ($size - 1 ^ 0xffffffff);
             }
             $result = 5;
             for ($i = 0; $i < $size; $i++) {
                 $result += self::skipBinary($itrans, $vtype);
             }
             return $result;
         default:
             throw new TProtocolException("Unknown field type: " . $type, TProtocolException::INVALID_DATA);
     }
 }
Ejemplo n.º 12
0
 private function flushImpl($oneway)
 {
     if (\hacklib_equals(strlen($this->wBuf_), 0)) {
         if (\hacklib_cast_as_boolean($oneway)) {
             $this->transport_->onewayFlush();
         } else {
             $this->transport_->flush();
         }
         return;
     }
     $out = $this->transform($this->wBuf_);
     $this->wBuf_ = '';
     if ($this->protoId_ === 1 && $this->clientType_ !== self::HTTP_CLIENT_TYPE) {
         throw new TTransportException('Trying to send JSON encoding over binary', TTransportException::INVALID_CLIENT);
     }
     $buf = '';
     if ($this->clientType_ === self::HEADER_CLIENT_TYPE) {
         $transformData = '';
         $num_headers = 0;
         foreach ($this->writeTrans_ as $trans) {
             ++$num_headers;
             $transformData .= $this->getVarint($trans);
         }
         if ($this->identity !== null) {
             $this->writeHeaders[self::ID_VERSION_HEADER] = (string) self::ID_VERSION;
             $this->writeHeaders[self::IDENTITY_HEADER] = $this->identity;
         }
         $infoData = '';
         if (\hacklib_cast_as_boolean($this->writeHeaders) || \hacklib_cast_as_boolean($this->persistentWriteHeaders)) {
             $infoData .= $this->getVarint(self::INFO_KEYVALUE);
             $infoData .= $this->getVarint(count($this->writeHeaders) + count($this->persistentWriteHeaders));
             foreach ($this->persistentWriteHeaders as $str_key => $str_value) {
                 $infoData .= $this->writeString($str_key);
                 $infoData .= $this->writeString($str_value);
             }
             foreach ($this->writeHeaders as $str_key => $str_value) {
                 $infoData .= $this->writeString($str_key);
                 $infoData .= $this->writeString($str_value);
             }
         }
         $this->writeHeaders = \HH\Map::hacklib_new(array(), array());
         $headerData = $this->getVarint($this->protoId_) . $this->getVarint($num_headers);
         $header_size = strlen($transformData) + strlen($infoData) + strlen($headerData);
         $paddingSize = 4 - $header_size % 4;
         $header_size += $paddingSize;
         $buf = (string) pack('nn', self::HEADER_MAGIC, $this->flags_);
         $buf .= (string) pack('Nn', $this->seqId_, $header_size / 4);
         $buf .= $headerData . $transformData;
         $buf .= $infoData;
         for ($i = 0; $i < $paddingSize; $i++) {
             $buf .= (string) pack('C', '\\0');
         }
         $buf .= $out;
         $buf = (string) pack('N', strlen($buf)) . $buf;
     } else {
         if ($this->clientType_ === self::FRAMED_DEPRECATED) {
             $buf = (string) pack('N', strlen($out));
             $buf .= $out;
         } else {
             if ($this->clientType_ === self::UNFRAMED_DEPRECATED) {
                 $buf = $out;
             } else {
                 if ($this->clientType_ === self::HTTP_CLIENT_TYPE) {
                     throw new TTransportException('HTTP not implemented', TTransportException::INVALID_CLIENT);
                 } else {
                     throw new TTransportException('Unknown client type', TTransportException::INVALID_CLIENT);
                 }
             }
         }
     }
     if (strlen($buf) > self::MAX_FRAME_SIZE) {
         throw new TTransportException('Attempting to send oversize frame', TTransportException::INVALID_FRAME_SIZE);
     }
     $this->transport_->write($buf);
     if (\hacklib_cast_as_boolean($oneway)) {
         $this->transport_->onewayFlush();
     } else {
         $this->transport_->flush();
     }
 }
Ejemplo n.º 13
0
 public function peek($len, $start = 0)
 {
     $bytes_needed = $len + $start;
     if (\hacklib_equals(strlen($this->rBuf_), 0)) {
         $this->rBuf_ = $this->transport_->readAll($bytes_needed);
     } else {
         if ($bytes_needed > strlen($this->rBuf_)) {
             $this->rBuf_ .= $this->transport_->readAll($bytes_needed - strlen($this->rBuf_));
         }
     }
     $ret = substr($this->rBuf_, $start, $len);
     return $ret;
 }
Ejemplo n.º 14
0
 public function readI64(&$value)
 {
     $hi = 0;
     $lo = 0;
     $idx = 0;
     $shift = 0;
     $arr = array();
     while (true) {
         $x = $this->trans_->readAll(1);
         $byte = ord($x);
         $idx += 1;
         if ($shift < 32) {
             $lo |= ($byte & 0x7f) << $shift & 0xffffffff;
         }
         if ($shift >= 32) {
             $hi |= ($byte & 0x7f) << $shift - 32;
         } else {
             if ($shift > 24) {
                 $hi |= ($byte & 0x7f) >> $shift - 24;
             }
         }
         if ($byte >> 7 === 0) {
             break;
         }
         $shift += 7;
     }
     $xorer = 0;
     if ($lo & 1) {
         $xorer = 0xffffffff;
     }
     $lo = $lo >> 1 & 0x7fffffff;
     $lo = $lo | ($hi & 1) << 31;
     $hi = $hi >> 1 ^ $xorer;
     $lo = $lo ^ $xorer;
     if (true) {
         $isNeg = $hi < 0;
         if (\hacklib_cast_as_boolean($isNeg)) {
             $hi = ~$hi & (int) 0xffffffff;
             $lo = ~$lo & (int) 0xffffffff;
             if (\hacklib_equals($lo, (int) 0xffffffff)) {
                 $hi++;
                 $lo = 0;
             } else {
                 $lo++;
             }
         }
         if ($hi & (int) 0x80000000) {
             $hi &= (int) 0x7fffffff;
             $hi += 0x80000000;
         }
         if ($lo & (int) 0x80000000) {
             $lo &= (int) 0x7fffffff;
             $lo += 0x80000000;
         }
         $value = $hi * 4294967296 + $lo;
         if (\hacklib_cast_as_boolean($isNeg)) {
             $value = 0 - $value;
         }
     } else {
         if ($arr[2] & 0x80000000) {
             $arr[2] = $arr[2] & 0xffffffff;
         }
         if ($arr[1] & 0x80000000) {
             $arr[1] = $arr[1] & 0xffffffff;
             $arr[1] = $arr[1] ^ 0xffffffff;
             $arr[2] = $arr[2] ^ 0xffffffff;
             $value = 0 - $arr[1] * 4294967296 - $arr[2] - 1;
         } else {
             $value = $arr[1] * 4294967296 + $arr[2];
         }
     }
     return $idx;
 }
Ejemplo n.º 15
0
 public function open()
 {
     if (\hacklib_cast_as_boolean($this->randomize_)) {
         $n = count($this->servers_);
         $s = $this->servers_;
         for ($i = 1; $i < $n; $i++) {
             $j = mt_rand(0, $i);
             $tmp = $s[$i];
             $s[$i] = $s[$j];
             $s[$j] = $tmp;
         }
         $this->servers_ = $s;
     }
     $numServers = count($this->servers_);
     $has_conn_errors = false;
     $fail_reason = array();
     for ($i = 0; $i < $numServers; ++$i) {
         list($host, $port) = $this->servers_[$i];
         $failtimeKey = TSocketPool::getAPCFailtimeKey($host, $port);
         $lastFailtime = (int) $this->apcFetch($failtimeKey);
         $this->apcLog("TSocketPool: host {$host}:{$port} last fail time: " . $lastFailtime);
         $retryIntervalPassed = false;
         if ($lastFailtime > 0) {
             $elapsed = time() - $lastFailtime;
             if ($elapsed > $this->retryInterval_) {
                 $retryIntervalPassed = true;
                 if (\hacklib_cast_as_boolean($this->debug_) && $this->debugHandler_ !== null) {
                     $dh = $this->debugHandler_;
                     $dh('TSocketPool: retryInterval ' . '(' . $this->retryInterval_ . ') ' . 'has passed for host ' . $host . ':' . $port);
                 }
             }
         }
         $isLastServer = false;
         if (\hacklib_cast_as_boolean($this->alwaysTryLast_)) {
             $isLastServer = \hacklib_equals($i, $numServers - 1);
         }
         if ($lastFailtime === 0 || \hacklib_cast_as_boolean($isLastServer) || $lastFailtime > 0 && \hacklib_cast_as_boolean($retryIntervalPassed)) {
             $this->host_ = $host;
             $this->port_ = $port;
             for ($attempt = 0; $attempt < $this->numRetries_; $attempt++) {
                 try {
                     parent::open();
                     if ($lastFailtime > 0) {
                         $this->apcStore($failtimeKey, 0);
                     }
                     return;
                 } catch (TException $tx) {
                     $errstr = $this->getErrStr();
                     $errno = $this->getErrNo();
                     if ($errstr !== null || $errno !== null) {
                         $fail_reason[$i] = '(' . $errstr . '[' . $errno . '])';
                     } else {
                         $fail_reason[$i] = '(?)';
                     }
                 }
             }
             if (\hacklib_cast_as_boolean($this->alwaysRetryForTransientFailure_) && \hacklib_cast_as_boolean($this->isTransientConnectFailure($this->getErrNo()))) {
                 continue;
             }
             $dh = \hacklib_cast_as_boolean($this->debug_) ? $this->debugHandler_ : null;
             $has_conn_errors = $this->recordFailure($host, $port, $this->maxConsecutiveFailures_, $this->retryInterval_, $dh);
         } else {
             $fail_reason[$i] = '(cached-down)';
         }
     }
     $error = 'TSocketPool: All hosts in pool are down. ';
     $hosts = array();
     foreach ($this->servers_ as $i => $server) {
         list($host, $port) = $server;
         $h = $host . ':' . $port;
         if (\hacklib_cast_as_boolean(array_key_exists($i, $fail_reason))) {
             $h .= (string) $fail_reason[$i];
         }
         $hosts[] = $h;
     }
     $hostlist = implode(',', $hosts);
     $error .= '(' . $hostlist . ')';
     if (\hacklib_cast_as_boolean($this->debug_) && $this->debugHandler_ !== null) {
         $dh = $this->debugHandler_;
         $dh($error);
     }
     throw new TTransportException($error);
 }
Ejemplo n.º 16
0
 private static function inStreamName()
 {
     if (\hacklib_equals(php_sapi_name(), "cli")) {
         return "php://stdin";
     }
     return "php://input";
 }