Boot.hx 22 KB

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