Boot.hx 22 KB

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