Boot.hx 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846
  1. package php;
  2. class Boot {
  3. static var qtypes;
  4. static var ttypes;
  5. static var tpaths;
  6. static var skip_constructor = false;
  7. static function __init__() : Void {
  8. untyped __php__("
  9. function _hx_add($a, $b) {
  10. if(is_numeric($a) && is_numeric($b)) {
  11. return $a + $b;
  12. } else {
  13. return $a . $b;
  14. }
  15. }
  16. function _hx_anonymous($arr = array()) {
  17. $o = new _hx_anonymous();
  18. reset($arr);
  19. while(list($k, $v) = each($arr))
  20. $o->$k = $v;
  21. return $o;
  22. }
  23. class _hx_array implements ArrayAccess, IteratorAggregate {
  24. var $»a;
  25. var $length;
  26. function __construct($a = array()) {
  27. $this->»a = $a;
  28. $this->length = count($a);
  29. }
  30. function concat($a) {
  31. return new _hx_array(array_merge($this->»a, $a->»a));
  32. }
  33. function copy() {
  34. return new _hx_array($this->»a);
  35. }
  36. function &get($index) {
  37. if(isset($this->»a[$index])) return $this->»a[$index];
  38. return null;
  39. }
  40. function insert($pos, $x) {
  41. array_splice($this->»a, $pos, 0, array($x));
  42. $this->length++;
  43. }
  44. function iterator() {
  45. return new _hx_array_iterator($this->»a);
  46. }
  47. function getIterator() {
  48. return $this->iterator();
  49. }
  50. function join($sep) {
  51. return implode($this->»a, $sep);
  52. }
  53. function pop() {
  54. $r = array_pop($this->»a);
  55. $this->length = count($this->»a);
  56. return $r;
  57. }
  58. function push($x) {
  59. $this->»a[] = $x;
  60. return ++$this->length;
  61. }
  62. function remove($x) {
  63. for($i = 0; $i < count($this->»a); $i++)
  64. if($this->»a[$i] === $x) {
  65. unset($this->»a[$i]);
  66. $this->»a = array_values($this->»a);
  67. $this->length--;
  68. return true;
  69. }
  70. return false;
  71. }
  72. function removeAt($pos) {
  73. if(array_key_exists($pos, $this->»a)) {
  74. unset($this->»a[$pos]);
  75. $this->length--;
  76. return true;
  77. } else
  78. return false;
  79. }
  80. function reverse() {
  81. $this->»a = array_reverse($this->»a, false);
  82. }
  83. function shift() {
  84. $r = array_shift($this->»a);
  85. $this->length = count($this->»a);
  86. return $r;
  87. }
  88. function slice($pos, $end) {
  89. if($end == null)
  90. return new _hx_array(array_slice($this->»a, $pos));
  91. else
  92. return new _hx_array(array_slice($this->»a, $pos, $end-$pos));
  93. }
  94. function sort($f) {
  95. usort($this->»a, $f);
  96. }
  97. function splice($pos, $len) {
  98. if($len < 0) $len = 0;
  99. $nh = new _hx_array(array_splice($this->»a, $pos, $len));
  100. $this->length = count($this->»a);
  101. return $nh;
  102. }
  103. function toString() {
  104. return '['.implode($this->»a, ', ').']';
  105. }
  106. function __toString() {
  107. return $this->toString();
  108. }
  109. function unshift($x) {
  110. array_unshift($this->»a, $x);
  111. $this->length++;
  112. }
  113. // ArrayAccess methods:
  114. function offsetExists($offset) {
  115. return isset($this->»a[$offset]);
  116. }
  117. function offsetGet($offset) {
  118. if(isset($this->»a[$offset])) return $this->»a[$offset];
  119. return null;
  120. }
  121. function offsetSet($offset, $value) {
  122. if($this->length <= $offset) {
  123. $this->»a = array_merge($this->»a, array_fill(0, $offset+1-$this->length, null));
  124. $this->length = $offset+1;
  125. }
  126. return $this->»a[$offset] = $value;
  127. }
  128. function offsetUnset($offset) {
  129. return $this->removeAt($offset);
  130. }
  131. }
  132. class _hx_array_iterator implements Iterator {
  133. private $»a;
  134. private $»i;
  135. public function __construct($a) {
  136. $this->»a = $a;
  137. $this->»i = 0;
  138. }
  139. public function next() {
  140. if(!$this->hasNext()) return null;
  141. return $this->»a[$this->»i++];
  142. }
  143. public function hasNext() {
  144. return $this->»i < count($this->»a);
  145. }
  146. public function current() {
  147. if (!$this->hasNext()) return false;
  148. return $this->»a[$this->»i];
  149. }
  150. public function key() {
  151. return $this->»i;
  152. }
  153. public function valid() {
  154. return $this->current() !== false;
  155. }
  156. public function rewind() {
  157. $this->»i = 0;
  158. }
  159. public function size() {
  160. return count($this->»a);
  161. }
  162. }
  163. function _hx_array_get($a, $pos) { return $a[$pos]; }
  164. function _hx_array_assign($a, $i, $v) { return $a[$i] = $v; }
  165. class _hx_break_exception extends Exception {}
  166. function _hx_cast($v, $type) {
  167. if(Std::is($v, $type)) {
  168. return $v;
  169. } else {
  170. throw new HException('Class cast error');
  171. }
  172. }
  173. function _hx_char_at($o, $i) { $c = substr($o, $i, 1); return FALSE === $c ? '' : $c; }
  174. function _hx_char_code_at($s, $pos) {
  175. if($pos < 0 || $pos >= strlen($s)) return null;
  176. return ord($s{$pos});
  177. }
  178. function _hx_deref($o) { return $o; }
  179. function _hx_equal($x, $y) {
  180. if(is_null($x)) {
  181. return is_null($y);
  182. } else {
  183. if(is_null($y)) {
  184. return false;
  185. } else {
  186. if((is_float($x) || is_int($x)) && (is_float($y) || is_int($y))) {
  187. return $x == $y;
  188. } else {
  189. return $x === $y;
  190. }
  191. }
  192. }
  193. }
  194. function _hx_error_handler($errno, $errmsg, $filename, $linenum, $vars) {
  195. $msg = $errmsg . ' (errno: ' . $errno . ') in ' . $filename . ' at line #' . $linenum;
  196. $e = new HException($msg, $errmsg, $errno, _hx_anonymous(array('fileName' => 'Boot.hx', 'lineNumber' => __LINE__, 'className' => 'php.Boot', 'methodName' => '_hx_error_handler')));
  197. $e->setFile($filename);
  198. $e->setLine($linenum);
  199. throw $e;
  200. return null;
  201. }
  202. function _hx_exception_handler($e) {
  203. if(0 == strncasecmp(PHP_SAPI, 'cli', 3)) {
  204. $msg = $e-> getMessage();
  205. $nl = \"\\n\";
  206. $pre = '';
  207. $post = '';
  208. } else {
  209. $msg = '<b>' . $e-> getMessage() . '</b>';
  210. $nl = \"<br />\";
  211. $pre = '<pre>';
  212. $post = '</pre>';
  213. }
  214. if(isset($GLOBALS['%s'])) {
  215. $stack = '';
  216. $i = $GLOBALS['%s']->length;
  217. while(--$i >= 0)
  218. $stack .= 'Called from '.$GLOBALS['%s'][$i].$nl;
  219. die($pre.'uncaught exception: '.$msg.$nl.$nl.$stack.$post);
  220. } else
  221. die($pre.'uncaught exception: '.$msg.$nl.$nl.'in file: '.$e->getFile().' line '.$e->getLine().$nl.$e->getTraceAsString().$post);
  222. }
  223. function _hx_explode($delimiter, $s) {
  224. if($delimiter == '')
  225. return new _hx_array(str_split($s, 1));
  226. return new _hx_array(explode($delimiter, $s));
  227. }
  228. function _hx_explode2($s, $delimiter) {
  229. if($delimiter == '')
  230. return new _hx_array(str_split($s, 1));
  231. return new _hx_array(explode($delimiter, $s));
  232. }
  233. function _hx_field($o, $field) {
  234. if(_hx_has_field($o, $field)) {
  235. if($o instanceof _hx_type) {
  236. if(is_callable(array($o->__tname__, $field))) {
  237. return array($o->__tname__, $field);
  238. } else {
  239. $name = $o->__tname__;
  240. return eval('return '.$name.'::$'.$field.';');
  241. }
  242. } else {
  243. if(is_string($o)) {
  244. if($field == 'length') {
  245. return strlen($o);
  246. } else {
  247. switch($field) {
  248. case 'charAt' : return array(new _hx_lambda(array(&$o), '_hx_char_at'), 'execute');
  249. case 'charCodeAt' : return array(new _hx_lambda(array(&$o), '_hx_char_code_at'), 'execute');
  250. case 'indexOf' : return array(new _hx_lambda(array(&$o), '_hx_index_of'), 'execute');
  251. case 'lastIndexOf': return array(new _hx_lambda(array(&$o), '_hx_last_index_of'), 'execute');
  252. case 'split' : return array(new _hx_lambda(array(&$o), '_hx_explode2'), 'execute');
  253. case 'substr' : return array(new _hx_lambda(array(&$o), '_hx_substr'), 'execute');
  254. case 'toUpperCase': return array(new _hx_lambda(array(&$o), 'strtoupper'), 'execute');
  255. case 'toLowerCase': return array(new _hx_lambda(array(&$o), 'strtolower'), 'execute');
  256. case 'toString' : return array(new _hx_lambda(array(&$o), '_hx_deref'), 'execute');
  257. }
  258. return null;
  259. }
  260. } else {
  261. if(property_exists($o, $field)) {
  262. if(is_array($o->$field) && is_callable($o->$field)) {
  263. return $o->$field;
  264. } else {
  265. if(is_string($o->$field) && _hx_is_lambda($o->$field)) {
  266. return array($o, $field);
  267. } else {
  268. return $o->$field;
  269. }
  270. }
  271. } else if(isset($o->»dynamics[$field])) {
  272. return $o->»dynamics[$field];
  273. } else {
  274. return array($o, $field);
  275. }
  276. }
  277. }
  278. } else {
  279. return null;
  280. }
  281. }
  282. function _hx_get_object_vars($o) {
  283. $a = array_keys(get_object_vars($o));
  284. if(isset($o->»dynamics))
  285. $a = array_merge($a, array_keys($o->»dynamics));
  286. $arr = array();
  287. while($k = current($a)) {
  288. if(substr($k, 0, 1) != '»')
  289. $arr[] = $k;
  290. next($a);
  291. }
  292. return $arr;
  293. }
  294. function _hx_has_field($o, $field) {
  295. return
  296. (is_object($o) && (method_exists($o, $field) || isset($o->$field) || property_exists($o, $field) || isset($o->»dynamics[$field])))
  297. ||
  298. (is_string($o) && (in_array($field, array('toUpperCase', 'toLowerCase', 'charAt', 'charCodeAt', 'indexOf', 'lastIndexOf', 'split', 'substr', 'toString', 'length'))))
  299. ;
  300. }
  301. function _hx_index_of($s, $value, $startIndex = null) {
  302. $x = strpos($s, $value, $startIndex);
  303. if($x === false)
  304. return -1;
  305. else
  306. return $x;
  307. }
  308. function _hx_instanceof($v, $t) {
  309. if($t === null) {
  310. return false;
  311. }
  312. switch($t->__tname__) {
  313. case 'Array' : return is_array($v);
  314. case 'String' : return is_string($v) && !_hx_is_lambda($v);
  315. case 'Bool' : return is_bool($v);
  316. case 'Int' : return is_int($v);
  317. case 'Float' : return is_float($v) || is_int($v);
  318. case 'Dynamic': return true;
  319. case 'Class' : return ($v instanceof _hx_class || $v instanceof _hx_interface) && $v->__tname__ != 'Enum';
  320. case 'Enum' : return $v instanceof _hx_enum;
  321. default : return is_a($v, $t->__tname__);
  322. }
  323. }
  324. function _hx_is_lambda($s) {
  325. 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')));
  326. }
  327. function _hx_last_index_of($s, $value, $startIndex = null) {
  328. $x = strrpos($s, $value, $startIndex === null ? null : strlen($s) - $startIndex);
  329. if($x === false)
  330. return -1;
  331. else
  332. return $x;
  333. }
  334. function _hx_len($o) {
  335. return is_string($o) ? strlen($o) : $o->length;
  336. }
  337. class _hx_list_iterator implements Iterator {
  338. private $»h;
  339. private $»list;
  340. private $»counter;
  341. public function __construct($list) {
  342. $this->»list = $list;
  343. $this->rewind();
  344. }
  345. public function next() {
  346. if($this->»h == null) return null;
  347. $this->»counter++;
  348. $x = $this->»h[0];
  349. $this->»h = $this->»h[1];
  350. return $x;
  351. }
  352. public function hasNext() {
  353. return $this->»h != null;
  354. }
  355. public function current() {
  356. if (!$this->hasNext()) return null;
  357. return $this->»h[0];
  358. }
  359. public function key() {
  360. return $this->»counter;
  361. }
  362. public function valid() {
  363. return $this->current() !== null;
  364. }
  365. public function rewind() {
  366. $this->»counter = -1;
  367. $this->»h = $this->»list->h;
  368. }
  369. public function size() {
  370. return $this->»list->length;
  371. }
  372. }
  373. function _hx_null() { return null; }
  374. class _hx_nullob {
  375. function _throw() { throw new HException('Null object'); }
  376. function __call($f, $a) { $this->_throw(); }
  377. function __get($f) { $this->_throw(); }
  378. function __set($f, $v) { $this->_throw(); }
  379. function __isset($f) { $this->_throw(); }
  380. function __unset($f) { $this->_throw(); }
  381. function __toString() { return 'null'; }
  382. static $inst;
  383. }
  384. _hx_nullob::$inst = new _hx_nullob();
  385. function _hx_nullob() { return _hx_nullob::$inst; }
  386. function _hx_qtype($n) {
  387. return isset(php_Boot::$qtypes[$n]) ? php_Boot::$qtypes[$n] : null;
  388. }
  389. function _hx_register_type($t) {
  390. php_Boot::$qtypes[$t->__qname__] = $t;
  391. php_Boot::$ttypes[$t->__tname__] = $t;
  392. if($t->__path__ !== null)
  393. php_Boot::$tpaths[$t->__tname__] = $t->__path__;
  394. }
  395. function _hx_set_method($o, $field, $func) {
  396. $value[0]->scope = $o;
  397. $o->$field = $func;
  398. }
  399. function _hx_shift_right($v, $n) {
  400. $z = 0x80000000;
  401. if ($z & $v) {
  402. $v = ($v>>1);
  403. $v &= (~$z);
  404. $v |= 0x40000000;
  405. $v = ($v>>($n-1));
  406. } else $v = ($v>>$n);
  407. return $v;
  408. }
  409. function _hx_string_call($s, $method, $params) {
  410. if(!is_string($s)) return call_user_func_array(array($s, $method), $params);
  411. switch($method) {
  412. case 'toUpperCase': return strtoupper($s);
  413. case 'toLowerCase': return strtolower($s);
  414. case 'charAt' : return substr($s, $params[0], 1);
  415. case 'charCodeAt' : return _hx_char_code_at($s, $params[0]);
  416. case 'indexOf' : return _hx_index_of($s, $params[0], (count($params) > 1 ? $params[1] : null));
  417. case 'lastIndexOf': return _hx_last_index_of($s, (count($params) > 1 ? $params[1] : null), null);
  418. case 'split' : return _hx_explode($params[0], $s);
  419. case 'substr' : return _hx_substr($s, $params[0], (count($params) > 1 ? $params[1] : null));
  420. case 'toString' : return $s;
  421. default : throw new HException('Invalid Operation: ' . $method);
  422. }
  423. }
  424. function _hx_string_rec($o, $s) {
  425. if($o === null) return 'null';
  426. if(strlen($s) >= 5) return '<...>';
  427. if(is_int($o) || is_float($o)) return '' . $o;
  428. if(is_bool($o)) return $o ? 'true' : 'false';
  429. if(is_object($o)) {
  430. $c = get_class($o);
  431. if($o instanceof Enum) {
  432. $b = $o->tag;
  433. if(!empty($o->params)) {
  434. $s .= \"\t\";
  435. $b .= '(';
  436. for($i = 0; $i < count($o->params); $i++) {
  437. if($i > 0)
  438. $b .= ', ' . _hx_string_rec($o->params[$i], $s);
  439. else
  440. $b .= _hx_string_rec($o->params[$i], $s);
  441. }
  442. $b .= ')';
  443. }
  444. return $b;
  445. } else {
  446. if($o instanceof _hx_anonymous) {
  447. $rfl = new ReflectionObject($o);
  448. $b2 = \"{\n\";
  449. $s .= \"\t\";
  450. $properties = $rfl->getProperties();
  451. for($i = 0; $i < count($properties); $i++) {
  452. $prop = $properties[$i];
  453. $f = $prop->getName();
  454. if($i > 0)
  455. $b2 .= \", \n\";
  456. $b2 .= $s . $f . ' : ' . _hx_string_rec($o->$f, $s);
  457. }
  458. $s = substr($s, 1);
  459. $b2 .= \"\n\" . $s . '}';
  460. return $b2;
  461. } else {
  462. if($o instanceof _hx_type)
  463. return $o->__qname__;
  464. else {
  465. if(is_callable(array($o, 'toString')))
  466. return $o->toString();
  467. else {
  468. if(is_callable(array($o, '__toString')))
  469. return $o->__toString();
  470. else
  471. return '[' . _hx_ttype($c) . ']';
  472. }
  473. }
  474. }
  475. }
  476. }
  477. if(is_string($o)) {
  478. if(_hx_is_lambda($o)) return '«function»';
  479. if(strlen($s) > 0) return '\"' . str_replace('\"', '\\\"', $o) . '\"';
  480. else return $o;
  481. }
  482. if(is_array($o)) {
  483. if(is_callable($o)) return '«function»';
  484. $str = '[';
  485. $s .= \"\t\";
  486. for($i = 0; $i < count($o); $i++)
  487. $str .= ($i > 0 ? ', ' : '') . _hx_string_rec($o[$i], $s);
  488. $str .= ']';
  489. return $str;
  490. }
  491. return '';
  492. }
  493. function _hx_substr($s, $pos, $len) {
  494. if($pos !== null && $pos !== 0 && $len !== null && $len < 0) return '';
  495. if($len === null) $len = strlen($s);
  496. if($pos < 0) {
  497. $pos = strlen($s) + $pos;
  498. if($pos < 0) $pos = 0;
  499. } else if($len < 0 )
  500. $len = strlen($s) + $len - $pos;
  501. $s = substr($s, $pos, $len);
  502. if($s === false)
  503. return '';
  504. else
  505. return $s;
  506. }
  507. function _hx_trace($v, $i) {
  508. $msg = $i !== null ? $i->fileName.':'.$i->lineNumber.': ' : '';
  509. echo $msg._hx_string_rec($v, '').\"\n\";
  510. }
  511. function _hx_ttype($n) {
  512. return isset(php_Boot::$ttypes[$n]) ? php_Boot::$ttypes[$n] : null;
  513. }
  514. function _hx_make_var_args() {
  515. $args = func_get_args();
  516. $f = array_shift($args);
  517. return call_user_func($f, new _hx_array($args));
  518. }
  519. class _hx_anonymous extends stdClass {
  520. public function __call($m, $a) {
  521. return call_user_func_array($this->$m, $a);
  522. }
  523. public function __set($n, $v) {
  524. $this->$n = $v;
  525. }
  526. public function &__get($n) {
  527. if(isset($this->$n))
  528. return $this->$n;
  529. $null = null;
  530. return $null;
  531. }
  532. public function __isset($n) {
  533. return isset($this->$n);
  534. }
  535. public function __unset($n) {
  536. unset($this->$n);
  537. }
  538. public function __toString() {
  539. $rfl = new ReflectionObject($this);
  540. $b = '{ ';
  541. $properties = $rfl->getProperties();
  542. $first = true;
  543. while(list(, $prop) = each($properties)) {
  544. if($first)
  545. $first = false;
  546. else
  547. $b .= ', ';
  548. $f = $prop->getName();
  549. $b .= $f . ' => ' . $this->$f;
  550. }
  551. $b .= ' }';
  552. return $b;
  553. }
  554. }
  555. class _hx_type {
  556. public $__tname__;
  557. public $__qname__;
  558. public $__path__;
  559. public function __construct($cn, $qn, $path = null) {
  560. $this->__tname__ = $cn;
  561. $this->__qname__ = $qn;
  562. $this->__path__ = $path;
  563. if(property_exists($cn, '__meta__'))
  564. $this->__meta__ = eval($cn.'::$__meta__');
  565. }
  566. public function toString() { return $this->__toString(); }
  567. public function __toString() {
  568. return $this->__qname__;
  569. }
  570. private $rfl = false;
  571. public function __rfl__() {
  572. if($this->rfl !== false) return $this->rfl;
  573. if(class_exists($this->__tname__) || interface_exists($this->__tname__))
  574. $this->rfl = new ReflectionClass($this->__tname__);
  575. else
  576. $this->rfl = null;
  577. return $this->rfl;
  578. }
  579. public function __call($n, $a) {
  580. return call_user_func_array(array($this->__tname__, $n), $a);
  581. }
  582. public function __get($n) {
  583. if(($r = $this->__rfl__())==null) return null;
  584. if($r->hasProperty($n))
  585. return $r->getStaticPropertyValue($n);
  586. else if($r->hasMethod($n))
  587. return array($r, $n);
  588. else
  589. return null;
  590. }
  591. public function __set($n, $v) {
  592. if(($r = $this->__rfl__())==null) return null;
  593. return $r->setStaticPropertyValue($n, $v);
  594. }
  595. public function __isset($n) {
  596. if(($r = $this->__rfl__())==null) return null;
  597. return $r->hasProperty($n) || $r->hasMethod($n);
  598. }
  599. }
  600. class _hx_class extends _hx_type {}
  601. class _hx_enum extends _hx_type {}
  602. class _hx_interface extends _hx_type {}
  603. class HException extends Exception {
  604. public function __construct($e, $message = null, $code = null, $p = null) {
  605. $message = _hx_string_rec($e, '') . $message;
  606. parent::__construct($message,$code);
  607. $this->e = $e;
  608. $this->p = $p;
  609. }
  610. public $e;
  611. public $p;
  612. public function setLine($l) {
  613. $this->line = $l;
  614. }
  615. public function setFile($f) {
  616. $this->file = $f;
  617. }
  618. }
  619. class _hx_lambda {
  620. public function __construct($locals, $func) {
  621. $this->locals = $locals;
  622. $this->func = $func;
  623. }
  624. public $locals;
  625. public $func;
  626. public function execute() {
  627. // if use $this->locals directly in array_merge it works only if I make the assignement loop,
  628. // so I've decided to reference $arr
  629. $arr = array();
  630. for ($i = 0; $i<count($this->locals);$i++)
  631. $arr[] = & $this->locals[$i];
  632. $args = func_get_args();
  633. return call_user_func_array($this->func, array_merge($arr, $args));
  634. }
  635. }
  636. class Enum {
  637. public function __construct($tag, $index, $params = null) { $this->tag = $tag; $this->index = $index; $this->params = $params; }
  638. public $tag;
  639. public $index;
  640. public $params;
  641. public function __toString() {
  642. return $this->tag;
  643. }
  644. }
  645. error_reporting(E_ALL & ~E_STRICT);
  646. set_error_handler('_hx_error_handler', E_ALL);
  647. set_exception_handler('_hx_exception_handler');
  648. php_Boot::$qtypes = array();
  649. php_Boot::$ttypes = array();
  650. php_Boot::$tpaths = array();
  651. _hx_register_type(new _hx_class('String', 'String'));
  652. _hx_register_type(new _hx_class('_hx_array', 'Array'));
  653. _hx_register_type(new _hx_class('Int', 'Int'));
  654. _hx_register_type(new _hx_class('Float', 'Float'));
  655. _hx_register_type(new _hx_class('Class', 'Class'));
  656. _hx_register_type(new _hx_class('Enum', 'Enum'));
  657. _hx_register_type(new _hx_class('Dynamic', 'Dynamic'));
  658. _hx_register_type(new _hx_enum('Bool', 'Bool'));
  659. _hx_register_type(new _hx_enum('Void', 'Void'));
  660. $_hx_libdir = dirname(__FILE__) . '/..';
  661. $_hx_autload_cache_file = $_hx_libdir . '/../cache/haxe_autoload.php';
  662. if(!file_exists($_hx_autload_cache_file)) {
  663. function _hx_build_paths($d, &$_hx_types_array, $pack) {
  664. $h = opendir($d);
  665. while(false !== ($f = readdir($h))) {
  666. $p = $d.'/'.$f;
  667. if($f == '.' || $f == '..')
  668. continue;
  669. if(is_file($p) && substr($f, -4) == '.php') {
  670. $bn = basename($f, '.php');
  671. if(substr($bn, -6) == '.class') {
  672. $bn = substr($bn, 0, -6);
  673. $t = 0;
  674. } else if(substr($bn, -5) == '.enum') {
  675. $bn = substr($bn, 0, -5);
  676. $t = 1;
  677. } else if(substr($bn, -10) == '.interface') {
  678. $bn = substr($bn, 0, -10);
  679. $t = 2;
  680. } else if(substr($bn, -7) == '.extern') {
  681. $bn = substr($bn, 0, -7);
  682. $t = 3;
  683. } else
  684. continue;
  685. $qname = ($bn == 'HList' && empty($pack)) ? 'List' : join(array_merge($pack, array($bn)), '.');
  686. $_hx_types_array[] = array(
  687. 'path' => $p,
  688. 'name' => $bn,
  689. 'type' => $t,
  690. 'qname' => $qname,
  691. 'phpname' => join(array_merge($pack, array($bn)), '_')
  692. );
  693. } else if(is_dir($p))
  694. _hx_build_paths($p, $_hx_types_array, array_merge($pack, array($f)));
  695. }
  696. closedir($h);
  697. }
  698. $_hx_cache_content = '<?php\n\n';
  699. $_hx_types_array = array();
  700. _hx_build_paths($_hx_libdir, $_hx_types_array, array());
  701. for($i=0;$i<count($_hx_types_array);$i++) {
  702. $_hx_cache_content .= '_hx_register_type(new ';
  703. $t = null;
  704. if($_hx_types_array[$i]['type'] == 0) {
  705. $t = new _hx_class($_hx_types_array[$i]['phpname'], $_hx_types_array[$i]['qname'], $_hx_types_array[$i]['path']);
  706. $_hx_cache_content .= '_hx_class';
  707. } else if($_hx_types_array[$i]['type'] == 1) {
  708. $t = new _hx_enum($_hx_types_array[$i]['phpname'], $_hx_types_array[$i]['qname'], $_hx_types_array[$i]['path']);
  709. $_hx_cache_content .= '_hx_enum';
  710. } else if($_hx_types_array[$i]['type'] == 2) {
  711. $t = new _hx_interface($_hx_types_array[$i]['phpname'], $_hx_types_array[$i]['qname'], $_hx_types_array[$i]['path']);
  712. $_hx_cache_content .= '_hx_interface';
  713. } else if($_hx_types_array[$i]['type'] == 3) {
  714. $t = new _hx_class($_hx_types_array[$i]['name'], $_hx_types_array[$i]['qname'], $_hx_types_array[$i]['path']);
  715. $_hx_cache_content .= '_hx_class';
  716. }
  717. _hx_register_type($t);
  718. $_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'].'\\'));\n';
  719. }
  720. try {
  721. file_put_contents($_hx_autload_cache_file, $_hx_cache_content);
  722. } catch(Exception $e) {}
  723. unset($_hx_types_array);
  724. unset($_hx_cache_content);
  725. } else {
  726. require($_hx_autload_cache_file);
  727. }
  728. function _hx_autoload($name) {
  729. if(!isset(php_Boot::$tpaths[$name])) return false;
  730. require_once(php_Boot::$tpaths[$name]);
  731. return true;
  732. }
  733. if(!ini_get('date.timezone'))
  734. date_default_timezone_set('UTC');
  735. spl_autoload_register('_hx_autoload')");
  736. }
  737. }