Boot.hx 22 KB

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