Boot.hx 24 KB

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