2
0

Boot.hx 24 KB

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