4 public function __construct(){}
8 static $skip_constructor = false;
9 function __toString() { return 'php.Boot'; }
12 $_hx_class_prefix = null;
14 function _hx_add($a, $b) {
15 if (!_hx_is_numeric($a) || !_hx_is_numeric($b)) {
22 function _hx_anonymous($arr = array()) {
23 $o = new _hx_anonymous();
24 foreach($arr as $k => $v)
29 class _hx_array implements ArrayAccess, IteratorAggregate {
32 function __construct($a = array()) {
34 $this->length = count($a);
38 return new _hx_array(array_merge($this->»a, $a->»a));
42 return new _hx_array($this->»a);
45 function &get($index) {
46 if(isset($this->»a[$index])) return $this->»a[$index];
50 function insert($pos, $x) {
51 array_splice($this->»a, $pos, 0, array($x));
56 return new _hx_array_iterator($this->»a);
59 function getIterator() {
60 return $this->iterator();
64 return implode($sep, $this->»a);
68 $r = array_pop($this->»a);
69 $this->length = count($this->»a);
75 return ++$this->length;
79 for($i = 0; $i < count($this->»a); $i++)
80 if($this->»a[$i] === $x) {
82 $this->»a = array_values($this->»a);
89 function removeAt($pos) {
90 if(array_key_exists($pos, $this->»a)) {
91 unset($this->»a[$pos]);
99 $this->»a = array_reverse($this->»a, false);
103 $r = array_shift($this->»a);
104 $this->length = count($this->»a);
108 function slice($pos, $end) {
110 return new _hx_array(array_slice($this->»a, $pos));
112 return new _hx_array(array_slice($this->»a, $pos, $end-$pos));
116 usort($this->»a, $f);
119 function splice($pos, $len) {
120 if($len < 0) $len = 0;
121 $nh = new _hx_array(array_splice($this->»a, $pos, $len));
122 $this->length = count($this->»a);
126 function toString() {
127 return '['.implode(',', array_map('_hx_string_rec',$this->»a,array())).']';
130 function __toString() {
131 return $this->toString();
134 function unshift($x) {
135 array_unshift($this->»a, $x);
139 // ArrayAccess methods:
140 function offsetExists($offset) {
141 return isset($this->»a[$offset]);
144 function offsetGet($offset) {
145 if(isset($this->»a[$offset])) return $this->»a[$offset];
149 function offsetSet($offset, $value) {
150 if($this->length <= $offset) {
151 $this->»a = array_merge($this->»a, array_fill(0, $offset+1-$this->length, null));
152 $this->length = $offset+1;
154 return $this->»a[$offset] = $value;
157 function offsetUnset($offset) {
158 return $this->removeAt($offset);
162 class _hx_array_iterator implements Iterator {
165 public function __construct($a) {
170 public function next() {
171 if(!$this->hasNext()) return null;
172 return $this->»a[$this->»i++];
175 public function hasNext() {
176 return $this->»i < count($this->»a);
179 public function current() {
180 if (!$this->hasNext()) return false;
181 return $this->»a[$this->»i];
184 public function key() {
188 public function valid() {
189 return $this->current() !== false;
192 public function rewind() {
195 public function size() {
196 return count($this->»a);
200 function _hx_array_get($a, $pos) { return $a[$pos]; }
202 function _hx_array_increment($a, $pos) { return $a[$pos] += 1; }
203 function _hx_array_decrement($a, $pos) { return $a[$pos] -= 1; }
205 function _hx_array_assign($a, $i, $v) { return $a[$i] = $v; }
207 class _hx_break_exception extends Exception {}
209 function _hx_cast($v, $type) {
210 if(Std::is($v, $type)) {
213 throw new HException('Class cast error');
217 function _hx_char_at($o, $i) { $c = substr($o, $i, 1); return FALSE === $c ? '' : $c; }
219 function _hx_char_code_at($s, $pos) {
220 if($pos < 0 || $pos >= strlen($s)) return null;
221 return ord($s{$pos});
224 function _hx_deref($o) { return $o; }
226 function _hx_equal($x, $y) {
233 if((is_float($x) || is_int($x)) && (is_float($y) || is_int($y))) {
242 function _hx_mod($x, $y) {
243 if (is_int($x) && is_int($y)) {
244 if ($y == 0) return 0;
247 if (!is_nan($x) && !is_nan($y) && !is_finite($y) && is_finite($x)) {
253 function _hx_error_handler($errno, $errmsg, $filename, $linenum, $vars) {
254 $msg = $errmsg . ' (errno: ' . $errno . ') in ' . $filename . ' at line #' . $linenum;
255 $e = new HException($msg, $errmsg, $errno, _hx_anonymous(array('fileName' => 'Boot.hx', 'lineNumber' => __LINE__, 'className' => 'php.Boot', 'methodName' => '_hx_error_handler')));
256 $e->setFile($filename);
257 $e->setLine($linenum);
262 function _hx_exception_handler($e) {
263 if(0 == strncasecmp(PHP_SAPI, 'cli', 3)) {
264 $msg = $e-> getMessage();
269 $msg = '<b>' . $e-> getMessage() . '</b>';
274 if(isset($GLOBALS['%s'])) {
276 $i = $GLOBALS['%s']->length;
278 $stack .= 'Called from '.$GLOBALS['%s'][$i].$nl;
279 die($pre.'uncaught exception: '.$msg.$nl.$nl.$stack.$post);
281 die($pre.'uncaught exception: '.$msg.$nl.$nl.'in file: '.$e->getFile().' line '.$e->getLine().$nl.$e->getTraceAsString().$post);
284 function _hx_explode($delimiter, $s) {
286 return new _hx_array(str_split($s, 1));
287 return new _hx_array(explode($delimiter, $s));
290 function _hx_explode2($s, $delimiter) {
292 return new _hx_array(str_split($s, 1));
293 return new _hx_array(explode($delimiter, $s));
296 function _hx_field($o, $field) {
297 if(_hx_has_field($o, $field)) {
298 if($o instanceof _hx_type) {
299 if(is_callable($c = array($o->__tname__, $field)) && !property_exists($o->__tname__, $field)) {
302 $name = $o->__tname__;
303 return eval('return '.$name.'::$'.$field.';');
307 if($field == 'length') {
311 case 'charAt' : return array(new _hx_lambda(array(&$o), '_hx_char_at'), 'execute');
312 case 'charCodeAt' : return array(new _hx_lambda(array(&$o), '_hx_char_code_at'), 'execute');
313 case 'indexOf' : return array(new _hx_lambda(array(&$o), '_hx_index_of'), 'execute');
314 case 'lastIndexOf': return array(new _hx_lambda(array(&$o), '_hx_last_index_of'), 'execute');
315 case 'split' : return array(new _hx_lambda(array(&$o), '_hx_explode2'), 'execute');
316 case 'substr' : return array(new _hx_lambda(array(&$o), '_hx_substr'), 'execute');
317 case 'toUpperCase': return array(new _hx_lambda(array(&$o), 'strtoupper'), 'execute');
318 case 'toLowerCase': return array(new _hx_lambda(array(&$o), 'strtolower'), 'execute');
319 case 'toString' : return array(new _hx_lambda(array(&$o), '_hx_deref'), 'execute');
324 if(property_exists($o, $field)) {
325 if(is_array($o->$field) && is_callable($o->$field)) {
328 if(is_string($o->$field) && _hx_is_lambda($o->$field)) {
329 return array($o, $field);
334 } else if(isset($o->»dynamics[$field])) {
335 return $o->»dynamics[$field];
337 return array($o, $field);
346 function _hx_get_object_vars($o) {
347 $a = array_keys(get_object_vars($o));
348 if(isset($o->»dynamics))
349 $a = array_merge($a, array_keys($o->»dynamics));
351 for($i=0;$i<count($a); $i++)
354 if(substr($k, 0, 1) != '»')
360 function _hx_has_field($o, $field) {
362 (is_object($o) && (method_exists($o, $field) || isset($o->$field) || property_exists($o, $field) || isset($o->»dynamics[$field])))
364 (is_string($o) && (in_array($field, array('toUpperCase', 'toLowerCase', 'charAt', 'charCodeAt', 'indexOf', 'lastIndexOf', 'split', 'substr', 'toString', 'length'))))
368 function _hx_index_of($s, $value, $startIndex = null) {
369 $x = strpos($s, $value, $startIndex);
376 function _hx_instanceof($v, $t) {
380 switch($t->__tname__) {
381 case 'Array' : return is_array($v);
382 case 'String' : return is_string($v) && !_hx_is_lambda($v);
383 case 'Bool' : return is_bool($v);
384 case 'Int' : return is_int($v) || (is_float($v) && intval($v) == $v && !is_nan($v));
385 case 'Float' : return is_float($v) || is_int($v);
386 case 'Dynamic': return true;
387 case 'Class' : return ($v instanceof _hx_class || $v instanceof _hx_interface) && $v->__tname__ != 'Enum';
388 case 'Enum' : return $v instanceof _hx_enum;
389 default : return is_a($v, $t->__tname__);
393 function _hx_is_lambda($s) {
394 return (is_string($s) && substr($s, 0, 8) == chr(0).'lambda_') || (is_array($s) && count($s) > 0 && (is_a($s[0], '_hx_lambda') || is_a($s[0], '_hx_lambda2')));
397 function _hx_is_numeric($v)
399 return is_numeric($v) && !is_string($v);
402 function _hx_last_index_of($s, $value, $startIndex = null) {
403 $x = strrpos($s, $value, $startIndex === null ? null : strlen($s) - $startIndex);
410 function _hx_len($o) {
411 return is_string($o) ? strlen($o) : $o->length;
414 class _hx_list_iterator implements Iterator {
418 public function __construct($list) {
419 $this->»list = $list;
423 public function next() {
424 if($this->»h == null) return null;
427 $this->»h = $this->»h[1];
431 public function hasNext() {
432 return $this->»h != null;
435 public function current() {
436 if (!$this->hasNext()) return null;
440 public function key() {
441 return $this->»counter;
444 public function valid() {
445 return $this->current() !== null;
448 public function rewind() {
449 $this->»counter = -1;
450 $this->»h = $this->»list->h;
453 public function size() {
454 return $this->»list->length;
458 function _hx_null() { return null; }
461 function _throw() { throw new HException('Null object'); }
462 function __call($f, $a) { $this->_throw(); }
463 function __get($f) { $this->_throw(); }
464 function __set($f, $v) { $this->_throw(); }
465 function __isset($f) { $this->_throw(); }
466 function __unset($f) { $this->_throw(); }
467 function __toString() { return 'null'; }
471 _hx_nullob::$inst = new _hx_nullob();
473 function _hx_nullob() { return _hx_nullob::$inst; }
475 function _hx_qtype($n) {
476 return isset(php_Boot::$qtypes[$n]) ? php_Boot::$qtypes[$n] : null;
479 function _hx_register_type($t) {
480 php_Boot::$qtypes[$t->__qname__] = $t;
481 php_Boot::$ttypes[$t->__tname__] = $t;
482 if($t->__path__ !== null)
483 php_Boot::$tpaths[$t->__tname__] = $t->__path__;
486 function _hx_set_method($o, $field, $func) {
487 $value[0]->scope = $o;
491 function _hx_shift_right($v, $n) {
498 } else $v = ($v>>$n);
502 function _hx_string_call($s, $method, $params) {
503 if(!is_string($s)) return call_user_func_array(array($s, $method), $params);
505 case 'toUpperCase': return strtoupper($s);
506 case 'toLowerCase': return strtolower($s);
507 case 'charAt' : return substr($s, $params[0], 1);
508 case 'charCodeAt' : return _hx_char_code_at($s, $params[0]);
509 case 'indexOf' : return _hx_index_of($s, $params[0], (count($params) > 1 ? $params[1] : null));
510 case 'lastIndexOf': return _hx_last_index_of($s, (count($params) > 1 ? $params[1] : null), null);
511 case 'split' : return _hx_explode($params[0], $s);
512 case 'substr' : return _hx_substr($s, $params[0], (count($params) > 1 ? $params[1] : null));
513 case 'toString' : return $s;
514 default : throw new HException('Invalid Operation: ' . $method);
518 function _hx_string_rec($o, $s) {
519 if($o === null) return 'null';
520 if(strlen($s) >= 5) return '<...>';
521 if(is_int($o) || is_float($o)) return '' . $o;
522 if(is_bool($o)) return $o ? 'true' : 'false';
525 if($o instanceof Enum) {
527 if(!empty($o->params)) {
530 for($i = 0; $i < count($o->params); $i++) {
532 $b .= ',' . _hx_string_rec($o->params[$i], $s);
534 $b .= _hx_string_rec($o->params[$i], $s);
540 if ($o instanceof _hx_anonymous) {
541 if ($o->toString && is_callable($o->toString)) {
542 return call_user_func($o->toString);
544 $rfl = new ReflectionObject($o);
548 $properties = $rfl->getProperties();
550 for($i = 0; $i < count($properties); $i++) {
551 $prop = $properties[$i];
552 $f = $prop->getName();
556 $b2 .= $s . $f . ' : ' . _hx_string_rec($o->$f, $s);
563 if($o instanceof _hx_type)
564 return $o->__qname__;
566 if(is_callable(array($o, 'toString')))
567 return $o->toString();
569 if(is_callable(array($o, '__toString')))
570 return $o->__toString();
572 return '[' . _hx_ttype($c) . ']';
579 if(_hx_is_lambda($o)) return '«function»';
580 // if(strlen($s) > 0) return '"' . str_replace('"', '\"', $o) . '"';
584 if(is_callable($o)) return '«function»';
589 foreach($o as $k => $v)
591 if ($first && $k === 0)
593 $str .= ($first ? '' : ',') . ($assoc
594 ? _hx_string_rec($k, $s) . '=>' . _hx_string_rec($o[$k], $s)
595 : _hx_string_rec($o[$k], $s)
605 function _hx_substr($s, $pos, $len) {
606 if($pos !== null && $pos !== 0 && $len !== null && $len < 0) return '';
607 if($len === null) $len = strlen($s);
609 $pos = strlen($s) + $pos;
610 if($pos < 0) $pos = 0;
612 $len = strlen($s) + $len - $pos;
613 $s = substr($s, $pos, $len);
620 function _hx_trace($v, $i) {
621 $msg = $i !== null ? $i->fileName.':'.$i->lineNumber.': ' : '';
622 echo $msg._hx_string_rec($v, '')."
626 function _hx_ttype($n) {
627 return isset(php_Boot::$ttypes[$n]) ? php_Boot::$ttypes[$n] : null;
630 function _hx_make_var_args() {
631 $args = func_get_args();
632 $f = array_shift($args);
633 return call_user_func($f, new _hx_array($args));
636 class _hx_anonymous extends stdClass {
637 public function __call($m, $a) {
638 return call_user_func_array($this->$m, $a);
641 public function __set($n, $v) {
645 public function &__get($n) {
652 public function __isset($n) {
653 return isset($this->$n);
656 public function __unset($n) {
660 public function __toString() {
661 $rfl = new ReflectionObject($this);
663 $properties = $rfl->getProperties();
665 while(list(, $prop) = each($properties)) {
670 $f = $prop->getName();
671 $b .= $f . ' => ' . $this->$f;
682 public function __construct($cn, $qn, $path = null) {
683 $this->__tname__ = $cn;
684 $this->__qname__ = $qn;
685 $this->__path__ = $path;
686 if(property_exists($cn, '__meta__'))
687 $this->__meta__ = eval($cn.'::$__meta__');
690 public function toString() { return $this->__toString(); }
692 public function __toString() {
693 return $this->__qname__;
696 private $rfl = false;
697 public function __rfl__() {
698 if($this->rfl !== false) return $this->rfl;
699 if(class_exists($this->__tname__) || interface_exists($this->__tname__))
700 $this->rfl = new ReflectionClass($this->__tname__);
706 public function __call($n, $a) {
707 return call_user_func_array(array($this->__tname__, $n), $a);
710 public function __get($n) {
711 if(($r = $this->__rfl__())==null) return null;
712 if($r->hasProperty($n))
713 return $r->getStaticPropertyValue($n);
714 else if($r->hasMethod($n))
715 return array($r, $n);
720 public function __set($n, $v) {
721 if(($r = $this->__rfl__())==null) return null;
722 return $r->setStaticPropertyValue($n, $v);
725 public function __isset($n) {
726 if(($r = $this->__rfl__())==null) return null;
727 return $r->hasProperty($n) || $r->hasMethod($n);
731 class _hx_class extends _hx_type {}
733 class _hx_enum extends _hx_type {}
735 class _hx_interface extends _hx_type {}
737 class HException extends Exception {
738 public function __construct($e, $message = null, $code = null, $p = null) {
739 $message = _hx_string_rec($e, '') . $message;
740 parent::__construct($message,$code);
746 public function setLine($l) {
749 public function setFile($f) {
755 public function __construct($locals, $func) {
756 $this->locals = $locals;
762 public function execute() {
763 // if use $this->locals directly in array_merge it works only if I make the assignement loop,
764 // so I've decided to reference $arr
766 for ($i = 0; $i<count($this->locals);$i++)
767 $arr[] = & $this->locals[$i];
768 $args = func_get_args();
769 return call_user_func_array($this->func, array_merge($arr, $args));
774 public function __construct($tag, $index, $params = null) { $this->tag = $tag; $this->index = $index; $this->params = $params; }
779 public function __toString() {
784 error_reporting(E_ALL & ~E_STRICT);
785 set_error_handler('_hx_error_handler', E_ALL);
786 set_exception_handler('_hx_exception_handler');
788 php_Boot::$qtypes = array();
789 php_Boot::$ttypes = array();
790 php_Boot::$tpaths = array();
792 _hx_register_type(new _hx_class('String', 'String'));
793 _hx_register_type(new _hx_class('_hx_array', 'Array'));
794 _hx_register_type(new _hx_class('Int', 'Int'));
795 _hx_register_type(new _hx_class('Float', 'Float'));
796 _hx_register_type(new _hx_class('Class', 'Class'));
797 _hx_register_type(new _hx_class('Enum', 'Enum'));
798 _hx_register_type(new _hx_class('Dynamic', 'Dynamic'));
799 _hx_register_type(new _hx_enum('Bool', 'Bool'));
800 _hx_register_type(new _hx_enum('Void', 'Void'));
803 $_hx_libdir = dirname(__FILE__) . '/..';
804 $_hx_autload_cache_file = $_hx_libdir . '/../cache/haxe_autoload.php';
805 if(!file_exists($_hx_autload_cache_file)) {
806 function _hx_build_paths($d, &$_hx_types_array, $pack, $prefix) {
808 while(false !== ($f = readdir($h))) {
810 if($f == '.' || $f == '..')
812 if (is_file($p) && substr($f, -4) == '.php') {
813 $bn = basename($f, '.php');
816 if ($prefix != substr($bn, 0, $lenprefix = strlen($prefix)))
818 $bn = substr($bn, $lenprefix);
820 if(substr($bn, -6) == '.class') {
821 $bn = substr($bn, 0, -6);
823 } else if(substr($bn, -5) == '.enum') {
824 $bn = substr($bn, 0, -5);
826 } else if(substr($bn, -10) == '.interface') {
827 $bn = substr($bn, 0, -10);
829 } else if(substr($bn, -7) == '.extern') {
830 $bn = substr($bn, 0, -7);
834 $qname = ($bn == 'HList' && empty($pack)) ? 'List' : join(array_merge($pack, array($bn)), '.');
835 $_hx_types_array[] = array(
837 'name' => $prefix . $bn,
840 'phpname' => join(array_merge($pack, array($prefix . $bn)), '_')
842 } else if(is_dir($p))
843 _hx_build_paths($p, $_hx_types_array, array_merge($pack, array($f)), $prefix);
848 $_hx_cache_content = '<?php
851 $_hx_types_array = array();
853 _hx_build_paths($_hx_libdir, $_hx_types_array, array(), $_hx_class_prefix);
855 for($i=0;$i<count($_hx_types_array);$i++) {
856 $_hx_cache_content .= '_hx_register_type(new ';
858 if($_hx_types_array[$i]['type'] == 0) {
859 $t = new _hx_class($_hx_types_array[$i]['phpname'], $_hx_types_array[$i]['qname'], $_hx_types_array[$i]['path']);
860 $_hx_cache_content .= '_hx_class';
861 } else if($_hx_types_array[$i]['type'] == 1) {
862 $t = new _hx_enum($_hx_types_array[$i]['phpname'], $_hx_types_array[$i]['qname'], $_hx_types_array[$i]['path']);
863 $_hx_cache_content .= '_hx_enum';
864 } else if($_hx_types_array[$i]['type'] == 2) {
865 $t = new _hx_interface($_hx_types_array[$i]['phpname'], $_hx_types_array[$i]['qname'], $_hx_types_array[$i]['path']);
866 $_hx_cache_content .= '_hx_interface';
867 } else if($_hx_types_array[$i]['type'] == 3) {
868 $t = new _hx_class($_hx_types_array[$i]['name'], $_hx_types_array[$i]['qname'], $_hx_types_array[$i]['path']);
869 $_hx_cache_content .= '_hx_class';
871 _hx_register_type($t);
872 $_hx_cache_content .= '(\''.($_hx_types_array[$i]['type'] == 3 ? $_hx_types_array[$i]['name'] : $_hx_types_array[$i]['phpname']).'\', \''.$_hx_types_array[$i]['qname'].'\', \''.$_hx_types_array[$i]['path'].'\'));
876 file_put_contents($_hx_autload_cache_file, $_hx_cache_content);
877 } catch(Exception $e) {}
878 unset($_hx_types_array);
879 unset($_hx_cache_content);
881 require($_hx_autload_cache_file);
884 function _hx_autoload($name) {
885 if(!isset(php_Boot::$tpaths[$name])) return false;
886 require_once(php_Boot::$tpaths[$name]);
890 if(!ini_get('date.timezone'))
891 date_default_timezone_set('UTC');
893 spl_autoload_register('_hx_autoload');