Boot.hx 24 KB

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