Boot.hx 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851
  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 (!_hx_is_numeric($a) || !_hx_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_is_numeric($v)
  328. {
  329. return is_numeric($v) && !is_string($v);
  330. }
  331. function _hx_last_index_of($s, $value, $startIndex = null) {
  332. $x = strrpos($s, $value, $startIndex === null ? null : strlen($s) - $startIndex);
  333. if($x === false)
  334. return -1;
  335. else
  336. return $x;
  337. }
  338. function _hx_len($o) {
  339. return is_string($o) ? strlen($o) : $o->length;
  340. }
  341. class _hx_list_iterator implements Iterator {
  342. private $»h;
  343. private $»list;
  344. private $»counter;
  345. public function __construct($list) {
  346. $this->»list = $list;
  347. $this->rewind();
  348. }
  349. public function next() {
  350. if($this->»h == null) return null;
  351. $this->»counter++;
  352. $x = $this->»h[0];
  353. $this->»h = $this->»h[1];
  354. return $x;
  355. }
  356. public function hasNext() {
  357. return $this->»h != null;
  358. }
  359. public function current() {
  360. if (!$this->hasNext()) return null;
  361. return $this->»h[0];
  362. }
  363. public function key() {
  364. return $this->»counter;
  365. }
  366. public function valid() {
  367. return $this->current() !== null;
  368. }
  369. public function rewind() {
  370. $this->»counter = -1;
  371. $this->»h = $this->»list->h;
  372. }
  373. public function size() {
  374. return $this->»list->length;
  375. }
  376. }
  377. function _hx_null() { return null; }
  378. class _hx_nullob {
  379. function _throw() { throw new HException('Null object'); }
  380. function __call($f, $a) { $this->_throw(); }
  381. function __get($f) { $this->_throw(); }
  382. function __set($f, $v) { $this->_throw(); }
  383. function __isset($f) { $this->_throw(); }
  384. function __unset($f) { $this->_throw(); }
  385. function __toString() { return 'null'; }
  386. static $inst;
  387. }
  388. _hx_nullob::$inst = new _hx_nullob();
  389. function _hx_nullob() { return _hx_nullob::$inst; }
  390. function _hx_qtype($n) {
  391. return isset(php_Boot::$qtypes[$n]) ? php_Boot::$qtypes[$n] : null;
  392. }
  393. function _hx_register_type($t) {
  394. php_Boot::$qtypes[$t->__qname__] = $t;
  395. php_Boot::$ttypes[$t->__tname__] = $t;
  396. if($t->__path__ !== null)
  397. php_Boot::$tpaths[$t->__tname__] = $t->__path__;
  398. }
  399. function _hx_set_method($o, $field, $func) {
  400. $value[0]->scope = $o;
  401. $o->$field = $func;
  402. }
  403. function _hx_shift_right($v, $n) {
  404. $z = 0x80000000;
  405. if ($z & $v) {
  406. $v = ($v>>1);
  407. $v &= (~$z);
  408. $v |= 0x40000000;
  409. $v = ($v>>($n-1));
  410. } else $v = ($v>>$n);
  411. return $v;
  412. }
  413. function _hx_string_call($s, $method, $params) {
  414. if(!is_string($s)) return call_user_func_array(array($s, $method), $params);
  415. switch($method) {
  416. case 'toUpperCase': return strtoupper($s);
  417. case 'toLowerCase': return strtolower($s);
  418. case 'charAt' : return substr($s, $params[0], 1);
  419. case 'charCodeAt' : return _hx_char_code_at($s, $params[0]);
  420. case 'indexOf' : return _hx_index_of($s, $params[0], (count($params) > 1 ? $params[1] : null));
  421. case 'lastIndexOf': return _hx_last_index_of($s, (count($params) > 1 ? $params[1] : null), null);
  422. case 'split' : return _hx_explode($params[0], $s);
  423. case 'substr' : return _hx_substr($s, $params[0], (count($params) > 1 ? $params[1] : null));
  424. case 'toString' : return $s;
  425. default : throw new HException('Invalid Operation: ' . $method);
  426. }
  427. }
  428. function _hx_string_rec($o, $s) {
  429. if($o === null) return 'null';
  430. if(strlen($s) >= 5) return '<...>';
  431. if(is_int($o) || is_float($o)) return '' . $o;
  432. if(is_bool($o)) return $o ? 'true' : 'false';
  433. if(is_object($o)) {
  434. $c = get_class($o);
  435. if($o instanceof Enum) {
  436. $b = $o->tag;
  437. if(!empty($o->params)) {
  438. $s .= \"\t\";
  439. $b .= '(';
  440. for($i = 0; $i < count($o->params); $i++) {
  441. if($i > 0)
  442. $b .= ', ' . _hx_string_rec($o->params[$i], $s);
  443. else
  444. $b .= _hx_string_rec($o->params[$i], $s);
  445. }
  446. $b .= ')';
  447. }
  448. return $b;
  449. } else {
  450. if($o instanceof _hx_anonymous) {
  451. $rfl = new ReflectionObject($o);
  452. $b2 = \"{\n\";
  453. $s .= \"\t\";
  454. $properties = $rfl->getProperties();
  455. for($i = 0; $i < count($properties); $i++) {
  456. $prop = $properties[$i];
  457. $f = $prop->getName();
  458. if($i > 0)
  459. $b2 .= \", \n\";
  460. $b2 .= $s . $f . ' : ' . _hx_string_rec($o->$f, $s);
  461. }
  462. $s = substr($s, 1);
  463. $b2 .= \"\n\" . $s . '}';
  464. return $b2;
  465. } else {
  466. if($o instanceof _hx_type)
  467. return $o->__qname__;
  468. else {
  469. if(is_callable(array($o, 'toString')))
  470. return $o->toString();
  471. else {
  472. if(is_callable(array($o, '__toString')))
  473. return $o->__toString();
  474. else
  475. return '[' . _hx_ttype($c) . ']';
  476. }
  477. }
  478. }
  479. }
  480. }
  481. if(is_string($o)) {
  482. if(_hx_is_lambda($o)) return '«function»';
  483. if(strlen($s) > 0) return '\"' . str_replace('\"', '\\\"', $o) . '\"';
  484. else return $o;
  485. }
  486. if(is_array($o)) {
  487. if(is_callable($o)) return '«function»';
  488. $str = '[';
  489. $s .= \"\t\";
  490. for($i = 0; $i < count($o); $i++)
  491. $str .= ($i > 0 ? ', ' : '') . _hx_string_rec($o[$i], $s);
  492. $str .= ']';
  493. return $str;
  494. }
  495. return '';
  496. }
  497. function _hx_substr($s, $pos, $len) {
  498. if($pos !== null && $pos !== 0 && $len !== null && $len < 0) return '';
  499. if($len === null) $len = strlen($s);
  500. if($pos < 0) {
  501. $pos = strlen($s) + $pos;
  502. if($pos < 0) $pos = 0;
  503. } else if($len < 0 )
  504. $len = strlen($s) + $len - $pos;
  505. $s = substr($s, $pos, $len);
  506. if($s === false)
  507. return '';
  508. else
  509. return $s;
  510. }
  511. function _hx_trace($v, $i) {
  512. $msg = $i !== null ? $i->fileName.':'.$i->lineNumber.': ' : '';
  513. echo $msg._hx_string_rec($v, '').\"\n\";
  514. }
  515. function _hx_ttype($n) {
  516. return isset(php_Boot::$ttypes[$n]) ? php_Boot::$ttypes[$n] : null;
  517. }
  518. function _hx_make_var_args() {
  519. $args = func_get_args();
  520. $f = array_shift($args);
  521. return call_user_func($f, new _hx_array($args));
  522. }
  523. class _hx_anonymous extends stdClass {
  524. public function __call($m, $a) {
  525. return call_user_func_array($this->$m, $a);
  526. }
  527. public function __set($n, $v) {
  528. $this->$n = $v;
  529. }
  530. public function &__get($n) {
  531. if(isset($this->$n))
  532. return $this->$n;
  533. $null = null;
  534. return $null;
  535. }
  536. public function __isset($n) {
  537. return isset($this->$n);
  538. }
  539. public function __unset($n) {
  540. unset($this->$n);
  541. }
  542. public function __toString() {
  543. $rfl = new ReflectionObject($this);
  544. $b = '{ ';
  545. $properties = $rfl->getProperties();
  546. $first = true;
  547. while(list(, $prop) = each($properties)) {
  548. if($first)
  549. $first = false;
  550. else
  551. $b .= ', ';
  552. $f = $prop->getName();
  553. $b .= $f . ' => ' . $this->$f;
  554. }
  555. $b .= ' }';
  556. return $b;
  557. }
  558. }
  559. class _hx_type {
  560. public $__tname__;
  561. public $__qname__;
  562. public $__path__;
  563. public function __construct($cn, $qn, $path = null) {
  564. $this->__tname__ = $cn;
  565. $this->__qname__ = $qn;
  566. $this->__path__ = $path;
  567. if(property_exists($cn, '__meta__'))
  568. $this->__meta__ = eval($cn.'::$__meta__');
  569. }
  570. public function toString() { return $this->__toString(); }
  571. public function __toString() {
  572. return $this->__qname__;
  573. }
  574. private $rfl = false;
  575. public function __rfl__() {
  576. if($this->rfl !== false) return $this->rfl;
  577. if(class_exists($this->__tname__) || interface_exists($this->__tname__))
  578. $this->rfl = new ReflectionClass($this->__tname__);
  579. else
  580. $this->rfl = null;
  581. return $this->rfl;
  582. }
  583. public function __call($n, $a) {
  584. return call_user_func_array(array($this->__tname__, $n), $a);
  585. }
  586. public function __get($n) {
  587. if(($r = $this->__rfl__())==null) return null;
  588. if($r->hasProperty($n))
  589. return $r->getStaticPropertyValue($n);
  590. else if($r->hasMethod($n))
  591. return array($r, $n);
  592. else
  593. return null;
  594. }
  595. public function __set($n, $v) {
  596. if(($r = $this->__rfl__())==null) return null;
  597. return $r->setStaticPropertyValue($n, $v);
  598. }
  599. public function __isset($n) {
  600. if(($r = $this->__rfl__())==null) return null;
  601. return $r->hasProperty($n) || $r->hasMethod($n);
  602. }
  603. }
  604. class _hx_class extends _hx_type {}
  605. class _hx_enum extends _hx_type {}
  606. class _hx_interface extends _hx_type {}
  607. class HException extends Exception {
  608. public function __construct($e, $message = null, $code = null, $p = null) {
  609. $message = _hx_string_rec($e, '') . $message;
  610. parent::__construct($message,$code);
  611. $this->e = $e;
  612. $this->p = $p;
  613. }
  614. public $e;
  615. public $p;
  616. public function setLine($l) {
  617. $this->line = $l;
  618. }
  619. public function setFile($f) {
  620. $this->file = $f;
  621. }
  622. }
  623. class _hx_lambda {
  624. public function __construct($locals, $func) {
  625. $this->locals = $locals;
  626. $this->func = $func;
  627. }
  628. public $locals;
  629. public $func;
  630. public function execute() {
  631. // if use $this->locals directly in array_merge it works only if I make the assignement loop,
  632. // so I've decided to reference $arr
  633. $arr = array();
  634. for ($i = 0; $i<count($this->locals);$i++)
  635. $arr[] = & $this->locals[$i];
  636. $args = func_get_args();
  637. return call_user_func_array($this->func, array_merge($arr, $args));
  638. }
  639. }
  640. class Enum {
  641. public function __construct($tag, $index, $params = null) { $this->tag = $tag; $this->index = $index; $this->params = $params; }
  642. public $tag;
  643. public $index;
  644. public $params;
  645. public function __toString() {
  646. return $this->tag;
  647. }
  648. }
  649. error_reporting(E_ALL & ~E_STRICT);
  650. set_error_handler('_hx_error_handler', E_ALL);
  651. set_exception_handler('_hx_exception_handler');
  652. php_Boot::$qtypes = array();
  653. php_Boot::$ttypes = array();
  654. php_Boot::$tpaths = array();
  655. _hx_register_type(new _hx_class('String', 'String'));
  656. _hx_register_type(new _hx_class('_hx_array', 'Array'));
  657. _hx_register_type(new _hx_class('Int', 'Int'));
  658. _hx_register_type(new _hx_class('Float', 'Float'));
  659. _hx_register_type(new _hx_class('Class', 'Class'));
  660. _hx_register_type(new _hx_class('Enum', 'Enum'));
  661. _hx_register_type(new _hx_class('Dynamic', 'Dynamic'));
  662. _hx_register_type(new _hx_enum('Bool', 'Bool'));
  663. _hx_register_type(new _hx_enum('Void', 'Void'));
  664. $_hx_libdir = dirname(__FILE__) . '/..';
  665. $_hx_autload_cache_file = $_hx_libdir . '/../cache/haxe_autoload.php';
  666. if(!file_exists($_hx_autload_cache_file)) {
  667. function _hx_build_paths($d, &$_hx_types_array, $pack) {
  668. $h = opendir($d);
  669. while(false !== ($f = readdir($h))) {
  670. $p = $d.'/'.$f;
  671. if($f == '.' || $f == '..')
  672. continue;
  673. if(is_file($p) && substr($f, -4) == '.php') {
  674. $bn = basename($f, '.php');
  675. if(substr($bn, -6) == '.class') {
  676. $bn = substr($bn, 0, -6);
  677. $t = 0;
  678. } else if(substr($bn, -5) == '.enum') {
  679. $bn = substr($bn, 0, -5);
  680. $t = 1;
  681. } else if(substr($bn, -10) == '.interface') {
  682. $bn = substr($bn, 0, -10);
  683. $t = 2;
  684. } else if(substr($bn, -7) == '.extern') {
  685. $bn = substr($bn, 0, -7);
  686. $t = 3;
  687. } else
  688. continue;
  689. $qname = ($bn == 'HList' && empty($pack)) ? 'List' : join(array_merge($pack, array($bn)), '.');
  690. $_hx_types_array[] = array(
  691. 'path' => $p,
  692. 'name' => $bn,
  693. 'type' => $t,
  694. 'qname' => $qname,
  695. 'phpname' => join(array_merge($pack, array($bn)), '_')
  696. );
  697. } else if(is_dir($p))
  698. _hx_build_paths($p, $_hx_types_array, array_merge($pack, array($f)));
  699. }
  700. closedir($h);
  701. }
  702. $_hx_cache_content = '<?php\n\n';
  703. $_hx_types_array = array();
  704. _hx_build_paths($_hx_libdir, $_hx_types_array, array());
  705. for($i=0;$i<count($_hx_types_array);$i++) {
  706. $_hx_cache_content .= '_hx_register_type(new ';
  707. $t = null;
  708. if($_hx_types_array[$i]['type'] == 0) {
  709. $t = new _hx_class($_hx_types_array[$i]['phpname'], $_hx_types_array[$i]['qname'], $_hx_types_array[$i]['path']);
  710. $_hx_cache_content .= '_hx_class';
  711. } else if($_hx_types_array[$i]['type'] == 1) {
  712. $t = new _hx_enum($_hx_types_array[$i]['phpname'], $_hx_types_array[$i]['qname'], $_hx_types_array[$i]['path']);
  713. $_hx_cache_content .= '_hx_enum';
  714. } else if($_hx_types_array[$i]['type'] == 2) {
  715. $t = new _hx_interface($_hx_types_array[$i]['phpname'], $_hx_types_array[$i]['qname'], $_hx_types_array[$i]['path']);
  716. $_hx_cache_content .= '_hx_interface';
  717. } else if($_hx_types_array[$i]['type'] == 3) {
  718. $t = new _hx_class($_hx_types_array[$i]['name'], $_hx_types_array[$i]['qname'], $_hx_types_array[$i]['path']);
  719. $_hx_cache_content .= '_hx_class';
  720. }
  721. _hx_register_type($t);
  722. $_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';
  723. }
  724. try {
  725. file_put_contents($_hx_autload_cache_file, $_hx_cache_content);
  726. } catch(Exception $e) {}
  727. unset($_hx_types_array);
  728. unset($_hx_cache_content);
  729. } else {
  730. require($_hx_autload_cache_file);
  731. }
  732. function _hx_autoload($name) {
  733. if(!isset(php_Boot::$tpaths[$name])) return false;
  734. require_once(php_Boot::$tpaths[$name]);
  735. return true;
  736. }
  737. if(!ini_get('date.timezone'))
  738. date_default_timezone_set('UTC');
  739. spl_autoload_register('_hx_autoload')");
  740. }
  741. }