Boot.hx 24 KB

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