Boot.hx 24 KB

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