function test_all_expression_types()
{
    $v = new \HH\Vector(array());
    $f = new Foo();
    test_cast("clone", (bool) \hacklib_cast_as_boolean(clone $v));
    test_cast("obj_get", (bool) \hacklib_cast_as_boolean($f->prop));
    test_cast("array_get", (bool) \hacklib_cast_as_boolean(array($v)[0]));
    test_cast("class_get", (bool) \hacklib_cast_as_boolean(Foo::$s_prop));
    test_cast("cast", (bool) \hacklib_cast_as_boolean((object) $v));
    test_cast("cast", (bool) (array) \hacklib_cast_as_array($v));
    for ($v_in = new \HH\Vector(array(1)), $i = 1; $i > 0, \hacklib_cast_as_boolean($v_in); $i++, $v_in->pop()) {
        echo "in here at " . $i . "\n";
        if ($i > 5) {
            break;
        }
    }
    test_cast("Eif", (bool) \hacklib_cast_as_boolean(true ? $v : $f));
    test_cast("unsafe_expr", (bool) \hacklib_cast_as_boolean($v));
    test_cast("call", (bool) \hacklib_cast_as_boolean(Foo::s_func()));
    test_cast("Collection", (bool) (!\hacklib_id(new \HH\Vector(array()))->isEmpty()));
    test_cast("New", (bool) new Foo());
    test_cast("New", (bool) (!\hacklib_id(new \HH\Vector(array()))->isEmpty()));
    test_cast("Eq", (bool) ($y = false));
    test_cast("Eq", (bool) \hacklib_cast_as_boolean($z = $f->i_func()));
    echo $z . "\n";
}
Exemplo n.º 2
0
function verify_set($s)
{
    echo \hacklib_cast_as_boolean($s->isEmpty()) ? "empty\n" : "not empty\n";
    echo $s->count() . "\n";
    echo \hacklib_cast_as_boolean($s->contains("25")) ? "contains 25\n" : "does not contain 25\n";
    echo \hacklib_cast_as_boolean($s->contains("truman")) ? "contains truman\n" : "does not contain truman\n";
    echo \hacklib_cast_as_boolean(isset($s[\hacklib_id("25")])) ? "contains 25\n" : "does not contain 25\n";
    echo gettype($s[\hacklib_id("25")]) . "\n";
    echo gettype($s[\hacklib_id('25')]) . "\n";
    echo gettype($s[25]) . "\n";
    try {
        $s[\hacklib_id("truman")];
        echo "should not see this";
    } catch (OutOfBoundsException $e) {
        echo $e->getMessage() . "\n";
    }
    foreach ($s as $i => $val) {
        $out = var_export($val, true);
        $t = gettype($i);
        echo "({$t}) {$i} : {$out}\n";
    }
    $i = $s->getIterator();
    $i->next();
    $i->next();
    $i->next();
    $i->next();
    try {
        $i->current();
        echo "should not see this";
    } catch (InvalidOperationException $e) {
        echo $e->getMessage() . "\n";
    }
}
Exemplo n.º 3
0
function verify_map($m)
{
    echo \hacklib_cast_as_boolean($m->isEmpty()) ? "empty\n" : "not empty\n";
    echo $m->count() . "\n";
    echo $m->at("25") . "\n";
    echo $m[\hacklib_id("25")] . "\n";
    echo $m->get("25") . "\n";
    try {
        $m->at(25);
        echo "should not see this";
    } catch (OutOfBoundsException $e) {
        echo $e->getMessage() . "\n";
    }
    try {
        $m[25];
        echo "should not see this";
    } catch (OutOfBoundsException $e) {
        echo $e->getMessage() . "\n";
    }
    echo var_export($m->get(25), true) . "\n";
    echo \hacklib_cast_as_boolean(isset($m[\hacklib_id("25")])) ? "is set\n" : "not set\n";
    echo \hacklib_cast_as_boolean($m->containsKey("25")) ? "contains Key\n" : "does not contain Key\n";
    echo \hacklib_cast_as_boolean($m->containsKey(25)) ? "contains Key\n" : "does not contain Key\n";
    foreach ($m as $i => $mal) {
        $out = var_export($mal, true);
        $t = gettype($i);
        echo "({$t}) {$i} : {$out}\n";
    }
    $i = $m->getIterator();
    $i->next();
    $i->next();
    $i->next();
    $i->next();
    try {
        $i->current();
        echo "should not see this";
    } catch (InvalidOperationException $e) {
        echo $e->getMessage() . "\n";
    }
}
Exemplo n.º 4
0
 protected function handle($client)
 {
     $trans = $this->transportFactory->getTransport($client);
     $prot = $this->protocolFactory->getProtocol($trans);
     $this->_clientBegin($prot);
     try {
         if (!$trans instanceof TTransportStatus || \hacklib_cast_as_boolean($trans->isReadable())) {
             $this->processor->process($prot, $prot);
         }
     } catch (Exception $x) {
         $md = $client->getMetaData();
         if (\hacklib_cast_as_boolean($md[\hacklib_id("timed_out")])) {
         } else {
             if (\hacklib_cast_as_boolean($md[\hacklib_id("eof")])) {
                 \HH\invariant($trans instanceof TTransport, "Need to make Hack happy");
                 $trans->close();
                 return false;
             } else {
                 echo "Handle caught transport exception: " . $x->getMessage() . "\n";
             }
         }
     }
     return true;
 }
Exemplo n.º 5
0
 public function write($buf)
 {
     if ($this->handle_ === null) {
         throw new TException('TSocket: handle_ is null');
     }
     $this->onWrite(strlen($buf));
     if (!\hacklib_cast_as_boolean($this->sendTimeoutSet_)) {
         stream_set_timeout($this->handle_, 0, $this->sendTimeout_ * 1000);
         $this->sendTimeoutSet_ = true;
     }
     while (strlen($buf) > 0) {
         $buflen = strlen($buf);
         $t_start = microtime(true);
         $got = fwrite($this->handle_, $buf);
         $write_time = microtime(true) - $t_start;
         if ($got === 0 || !\hacklib_cast_as_boolean(is_int($got))) {
             $read_err_detail = sprintf('%d bytes from %s:%d to localhost:%d. Spent %2.2f ms.', $buflen, $this->host_, $this->port_, $this->lport_, $write_time * 1000);
             $md = stream_get_meta_data($this->handle_);
             if (\hacklib_cast_as_boolean($md[\hacklib_id('timed_out')])) {
                 throw new TTransportException('TSocket: timeout while writing ' . $read_err_detail, TTransportException::TIMED_OUT);
             } else {
                 $md_str = str_replace("\n", " ", print_r($md, true));
                 throw new TTransportException('TSocket: could not write ' . $read_err_detail, TTransportException::COULD_NOT_WRITE);
             }
         }
         $buf = substr($buf, $got);
     }
     $this->writeAttemptStart_ = null;
 }
Exemplo n.º 6
0
class Bar
{
    public $s;
    public function __construct($s = \HACKLIB_UNINIT)
    {
        if ($s === \HACKLIB_UNINIT) {
            $s = new \HH\Set(array("witches", "wizards"));
        }
        $this->s = $s;
    }
}
foo();
foo(new \HH\Set(array(1, "hi", 1)), " : ");
$b = new Bar();
foo($b->s, " - ");
foo(\hacklib_id(new Bar(\HH\Map::hacklib_new(array("x", "p"), array("y", "q"))))->s, " = ");
interface Boo
{
    public function fizz($v = \HACKLIB_UNINIT);
}
abstract class Booze
{
    public abstract function buzz($s = \HACKLIB_UNINIT);
}
class BooImpl extends Booze implements Boo
{
    public function fizz($v = \HACKLIB_UNINIT)
    {
        if ($v === \HACKLIB_UNINIT) {
            $v = new \HH\Vector(array(1, 2));
        }
Exemplo n.º 7
0
 protected function _write($class, $spec, $output)
 {
     $xfer = 0;
     $xfer += $output->writeStructBegin($class);
     foreach ($spec as $fid => $fspec) {
         $var = $fspec[\hacklib_id('var')];
         if ($this->{$var} !== null) {
             $ftype = $fspec[\hacklib_id('type')];
             $xfer += $output->writeFieldBegin($var, $ftype, $fid);
             if (\hacklib_cast_as_boolean(isset(TBase::$tmethod[$ftype]))) {
                 $func = 'write' . TBase::$tmethod[$ftype];
                 $xfer += $output->{$func}($this->{$var});
             } else {
                 switch ($ftype) {
                     case TType::STRUCT:
                         $xfer += $this->{$var}->write($output);
                         break;
                     case TType::MAP:
                         $xfer += $this->_writeMap($this->{$var}, $fspec, $output);
                         break;
                     case TType::LST:
                         $xfer += $this->_writeList($this->{$var}, $fspec, $output, false);
                         break;
                     case TType::SET:
                         $xfer += $this->_writeList($this->{$var}, $fspec, $output, true);
                         break;
                 }
             }
             $xfer += $output->writeFieldEnd();
         }
     }
     $xfer += $output->writeFieldStop();
     $xfer += $output->writeStructEnd();
     return $xfer;
 }
Exemplo n.º 8
0
 public function flush()
 {
     $host = $this->host_ . ":" . $this->port_;
     $user_agent = "PHP/THttpClient";
     $script = (string) urlencode(basename($_SERVER[\hacklib_id("SCRIPT_FILENAME")]));
     if (\hacklib_cast_as_boolean($script)) {
         $user_agent .= " (" . $script . ")";
     }
     $curl = curl_init($this->scheme_ . "://" . $host . $this->uri_);
     $headers = array("Host: " . $host, "Accept: application/x-thrift", "User-Agent: " . $user_agent, "Content-Type: application/x-thrift", "Content-Length: " . (string) strlen($this->buf_));
     if ($this->custom_headers_ !== null) {
         foreach ($this->custom_headers_ as $header => $value) {
             $headers[] = $header . ": " . $value;
         }
     }
     curl_setopt($curl, CURLOPT_PROXY, "");
     curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
     curl_setopt($curl, CURLOPT_POST, true);
     curl_setopt($curl, CURLOPT_MAXREDIRS, 1);
     curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
     curl_setopt($curl, CURLOPT_POSTFIELDS, $this->buf_);
     curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
     curl_setopt($curl, CURLOPT_BINARYTRANSFER, true);
     curl_setopt($curl, CURLOPT_TIMEOUT, $this->timeout_);
     if ($this->caInfo_ !== null) {
         curl_setopt($curl, CURLOPT_CAINFO, $this->caInfo_);
     }
     $this->buf_ = "";
     $this->data_ = curl_exec($curl);
     $this->errstr_ = curl_error($curl);
     curl_close($curl);
     if ($this->data_ === false) {
         $error = "THttpClient: Could not connect to " . $host . $this->uri_;
         throw new TTransportException($error, TTransportException::NOT_OPEN);
     }
 }
Exemplo n.º 9
0
<?php

namespace foo\bar\baz;

require_once $GLOBALS["HACKLIB_ROOT"];
function display($x)
{
    echo $x;
}
namespace cdef;

class C
{
    public function foo()
    {
        return 5;
    }
}
class D
{
    public $state = "";
    public function __construct($foo, $bar)
    {
        return $this->state = $foo . $bar;
    }
}
namespace zork;

\foo\bar\baz\display(\hacklib_id(new \cdef\C())->foo());
\foo\bar\baz\display(\hacklib_id(new \cdef\D("rin tin ", "tin"))->state);
\foo\bar\baz\display("\n");
Exemplo n.º 10
0
<?php

require_once $GLOBALS["HACKLIB_ROOT"];
class C
{
    public function foo()
    {
        return 5;
    }
}
class D
{
    public $state = "";
    public function __construct($foo, $bar)
    {
        return $this->state = $foo . $bar;
    }
}
echo \hacklib_id(new C())->foo();
echo \hacklib_id(new D("rin tin ", "tin"))->state;
echo "\n";
Exemplo n.º 11
0
        echo $t . " is an Iterator\n";
    }
    if ($c instanceof \HH\Iterator) {
        echo $t . " is an \\HH\\Iterator\n";
    }
    if ($c instanceof \Iterator) {
        echo $t . " is an Iterator\n";
    }
    if ($c instanceof \HH\Vector) {
        echo $t . " is a Vector\n";
    }
    $y = "HH\\Traversable";
    if (\hacklib_instanceof($c, $y)) {
        echo $t . " is a \"HH\\Traversable\"\n";
    }
    if ($c instanceof Foo) {
        echo $t . " is a Foo\n";
    }
    $y = "\\Boo\\Foo";
    if (\hacklib_instanceof($c, $y)) {
        echo $t . " is a \"\\Boo\\Foo\"\n";
    }
    echo "\n\n";
}
class Bar implements Foo
{
}
somedata(new \HH\Vector(array("c`est", "la", "vie")));
somedata(\hacklib_id(new \HH\Vector(array()))->getIterator());
somedata(array("so", "it", "goes"));
somedata(new Bar());
Exemplo n.º 12
0
 function foo($d)
 {
     $d[\hacklib_id('hits')]++;
     echo "{$d['name']} : {$d['hits']}\n";
 }
Exemplo n.º 13
0
 function foo($d)
 {
     $d[\hacklib_id("hits")]++;
     echo $d[\hacklib_id("name")] . " : " . $d[\hacklib_id("hits")] . "\n";
 }