Boot.hx 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972
  1. /*
  2. * Copyright (C)2005-2015 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 indexOf($x, $fromIndex) {
  95. $i = ($fromIndex === null) ? 0 : $fromIndex;
  96. $len = $this->length;
  97. $a = $this->a;
  98. if ($i < 0) {
  99. $i += $len;
  100. if ($i < 0) $i = 0;
  101. }
  102. while ($i < $len) {
  103. if ($a[$i] === $x)
  104. return $i;
  105. $i++;
  106. }
  107. return -1;
  108. }
  109. function lastIndexOf($x, $fromIndex) {
  110. $len = $this->length;
  111. $i = ($fromIndex === null) ? $len - 1 : $fromIndex;
  112. $a = $this->a;
  113. if ($i >= $len)
  114. $i = $len - 1;
  115. else if ($i < 0)
  116. $i += $len;
  117. while ($i >= 0) {
  118. if ($a[$i] === $x)
  119. return $i;
  120. $i--;
  121. }
  122. return -1;
  123. }
  124. function removeAt($pos) {
  125. if(array_key_exists($pos, $this->a)) {
  126. unset($this->a[$pos]);
  127. $this->length--;
  128. return true;
  129. } else
  130. return false;
  131. }
  132. function reverse() {
  133. $this->a = array_reverse($this->a, false);
  134. }
  135. function shift() {
  136. $r = array_shift($this->a);
  137. $this->length = count($this->a);
  138. return $r;
  139. }
  140. function slice($pos, $end) {
  141. if($end === null)
  142. return new _hx_array(array_slice($this->a, $pos));
  143. else
  144. return new _hx_array(array_slice($this->a, $pos, $end-$pos));
  145. }
  146. function sort($f) {
  147. usort($this->a, $f);
  148. }
  149. function splice($pos, $len) {
  150. if($len < 0) $len = 0;
  151. $nh = new _hx_array(array_splice($this->a, $pos, $len));
  152. $this->length = count($this->a);
  153. return $nh;
  154. }
  155. function toString() {
  156. return '['.implode(',', array_map('_hx_string_rec',$this->a,array())).']';
  157. }
  158. function __toString() {
  159. return $this->toString();
  160. }
  161. function unshift($x) {
  162. array_unshift($this->a, $x);
  163. $this->length++;
  164. }
  165. function map($f) {
  166. return new _hx_array(array_map($f, $this->a));
  167. }
  168. function filter($f) {
  169. return new _hx_array(array_values(array_filter($this->a,$f)));
  170. }
  171. // ArrayAccess methods:
  172. function offsetExists($offset) {
  173. return isset($this->a[$offset]);
  174. }
  175. function offsetGet($offset) {
  176. if(isset($this->a[$offset])) return $this->a[$offset];
  177. return null;
  178. }
  179. function offsetSet($offset, $value) {
  180. if($this->length <= $offset) {
  181. $this->a = array_merge($this->a, array_fill(0, $offset+1-$this->length, null));
  182. $this->length = $offset+1;
  183. }
  184. return $this->a[$offset] = $value;
  185. }
  186. function offsetUnset($offset) {
  187. return $this->removeAt($offset);
  188. }
  189. }
  190. class _hx_array_iterator implements Iterator {
  191. private $a;
  192. private $i;
  193. public function __construct($a) {
  194. $this->a = $a;
  195. $this->i = 0;
  196. }
  197. public function next() {
  198. if(!$this->hasNext()) return null;
  199. return $this->a[$this->i++];
  200. }
  201. public function hasNext() {
  202. return $this->i < count($this->a);
  203. }
  204. public function current() {
  205. if (!$this->hasNext()) return false;
  206. return $this->a[$this->i];
  207. }
  208. public function key() {
  209. return $this->i;
  210. }
  211. public function valid() {
  212. return $this->current() !== false;
  213. }
  214. public function rewind() {
  215. $this->i = 0;
  216. }
  217. public function size() {
  218. return count($this->a);
  219. }
  220. }
  221. function _hx_array_get($a, $pos) { return $a[$pos]; }
  222. function _hx_array_increment($a, $pos) { return $a[$pos] += 1; }
  223. function _hx_array_decrement($a, $pos) { return $a[$pos] -= 1; }
  224. function _hx_array_assign($a, $i, $v) { return $a[$i] = $v; }
  225. class _hx_break_exception extends Exception {}
  226. function _hx_cast($v, $type) {
  227. if(Std::is($v, $type)) {
  228. return $v;
  229. } else {
  230. throw new HException('Class cast error');
  231. }
  232. }
  233. function _hx_char_at($o, $i) {
  234. if ($i < 0)
  235. return '';
  236. $c = substr($o, $i, 1);
  237. return FALSE === $c ? '' : $c;
  238. }
  239. function _hx_char_code_at($s, $pos) {
  240. if($pos < 0 || $pos >= strlen($s)) return null;
  241. return ord($s{$pos});
  242. }
  243. function _hx_deref($o) { return $o; }
  244. function _hx_equal($x, $y) {
  245. if(is_null($x)) {
  246. return is_null($y);
  247. } else {
  248. if(is_null($y)) {
  249. return false;
  250. } else {
  251. if((is_float($x) || is_int($x)) && (is_float($y) || is_int($y))) {
  252. return $x == $y;
  253. } else {
  254. return $x === $y;
  255. }
  256. }
  257. }
  258. }
  259. function _hx_mod($x, $y) {
  260. if (is_int($x) && is_int($y)) {
  261. if ($y == 0) return 0;
  262. return $x % $y;
  263. }
  264. if (!is_nan($x) && !is_nan($y) && !is_finite($y) && is_finite($x)) {
  265. return $x;
  266. }
  267. return fmod($x, $y);
  268. }
  269. function _hx_error_handler($errno, $errmsg, $filename, $linenum, $vars) {
  270. if (!(error_reporting() & $errno)) {
  271. return false;
  272. }
  273. $msg = $errmsg . ' (errno: ' . $errno . ') in ' . $filename . ' at line #' . $linenum;
  274. $e = new HException($msg, $errmsg, $errno, _hx_anonymous(array('fileName' => 'Boot.hx', 'lineNumber' => __LINE__, 'className' => 'php.Boot', 'methodName' => '_hx_error_handler')));
  275. $e->setFile($filename);
  276. $e->setLine($linenum);
  277. throw $e;
  278. return null;
  279. }
  280. function _hx_exception_handler($e) {
  281. if(0 == strncasecmp(PHP_SAPI, 'cli', 3)) {
  282. $msg = $e-> getMessage();
  283. $nl = \"\\n\";
  284. $pre = '';
  285. $post = '';
  286. } else {
  287. $msg = '<b>' . $e-> getMessage() . '</b>';
  288. $nl = \"<br />\";
  289. $pre = '<pre>';
  290. $post = '</pre>';
  291. }
  292. if(isset($GLOBALS['%s'])) {
  293. $stack = '';
  294. $i = $GLOBALS['%s']->length;
  295. while(--$i >= 0)
  296. $stack .= 'Called from '.$GLOBALS['%s'][$i].$nl;
  297. die($pre.'uncaught exception: '.$msg.$nl.$nl.$stack.$post);
  298. } else
  299. die($pre.'uncaught exception: '.$msg.$nl.$nl.'in file: '.$e->getFile().' line '.$e->getLine().$nl.$e->getTraceAsString().$post);
  300. }
  301. function _hx_explode($delimiter, $s) {
  302. if($delimiter == '')
  303. return new _hx_array(str_split($s, 1));
  304. return new _hx_array(explode($delimiter, $s));
  305. }
  306. function _hx_explode2($s, $delimiter) {
  307. if($delimiter == '')
  308. return new _hx_array(str_split($s, 1));
  309. return new _hx_array(explode($delimiter, $s));
  310. }
  311. function _hx_field($o, $field) {
  312. if(_hx_has_field($o, $field)) {
  313. if($o instanceof _hx_type) {
  314. if(is_callable($c = array($o->__tname__, $field)) && !property_exists($o->__tname__, $field)) {
  315. return $c;
  316. } else {
  317. $name = $o->__tname__;
  318. return eval('return '.$name.'::$'.$field.';');
  319. }
  320. } else {
  321. if(is_string($o)) {
  322. if($field == 'length') {
  323. return strlen($o);
  324. } else {
  325. switch($field) {
  326. case 'charAt' : return array(new _hx_lambda(array(&$o), '_hx_char_at'), 'execute');
  327. case 'charCodeAt' : return array(new _hx_lambda(array(&$o), '_hx_char_code_at'), 'execute');
  328. case 'indexOf' : return array(new _hx_lambda(array(&$o), '_hx_index_of'), 'execute');
  329. case 'lastIndexOf': return array(new _hx_lambda(array(&$o), '_hx_last_index_of'), 'execute');
  330. case 'split' : return array(new _hx_lambda(array(&$o), '_hx_explode2'), 'execute');
  331. case 'substr' : return array(new _hx_lambda(array(&$o), '_hx_substr'), 'execute');
  332. case 'toUpperCase': return array(new _hx_lambda(array(&$o), 'strtoupper'), 'execute');
  333. case 'toLowerCase': return array(new _hx_lambda(array(&$o), 'strtolower'), 'execute');
  334. case 'toString' : return array(new _hx_lambda(array(&$o), '_hx_deref'), 'execute');
  335. }
  336. return null;
  337. }
  338. } else {
  339. if(property_exists($o, $field)) {
  340. if(is_array($o->$field) && is_callable($o->$field)) {
  341. return $o->$field;
  342. } else {
  343. if(is_string($o->$field) && _hx_is_lambda($o->$field)) {
  344. return array($o, $field);
  345. } else {
  346. return $o->$field;
  347. }
  348. }
  349. } else if(isset($o->__dynamics) && isset($o->__dynamics[$field])) {
  350. return $o->__dynamics[$field];
  351. } else {
  352. return array($o, $field);
  353. }
  354. }
  355. }
  356. } else {
  357. return null;
  358. }
  359. }
  360. function _hx_get_object_vars($o) {
  361. $a = array_keys(get_object_vars($o));
  362. if(isset($o->__dynamics))
  363. $a = array_merge($a, array_keys($o->__dynamics));
  364. $arr = array();
  365. for($i=0;$i<count($a); $i++)
  366. {
  367. $arr[] = '' . $a[$i];
  368. }
  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. for($i = 0; $i < count($o->params); $i++) {
  514. if($i > 0)
  515. $b .= ',' . _hx_string_rec($o->params[$i], $s);
  516. else
  517. $b .= _hx_string_rec($o->params[$i], $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. for($i = 0; $i < count($properties); $i++) {
  532. $prop = $properties[$i];
  533. $f = $prop->getName();
  534. if($i > 0)
  535. $b2 .= \", \n\";
  536. $b2 .= $s . $f . ' : ' . _hx_string_rec($o->$f, $s);
  537. }
  538. $s = substr($s, 1);
  539. $b2 .= \"\n\" . $s . '}';
  540. return $b2;
  541. } else {
  542. if($o instanceof _hx_type)
  543. return $o->__qname__;
  544. else {
  545. if(is_callable(array($o, 'toString')))
  546. return $o->toString();
  547. else {
  548. if(is_callable(array($o, '__toString')))
  549. return $o->__toString();
  550. else
  551. return '[' . _hx_ttype($c) . ']';
  552. }
  553. }
  554. }
  555. }
  556. }
  557. if(is_string($o)) {
  558. if(_hx_is_lambda($o)) return '<function>';
  559. // if(strlen($s) > 0) return '\"' . str_replace('\"', '\\\"', $o) . '\"';
  560. else return $o;
  561. }
  562. if(is_array($o)) {
  563. if(is_callable($o)) return '<function>';
  564. $str = '[';
  565. $s .= \"\t\";
  566. $first = true;
  567. $assoc = true;
  568. foreach($o as $k => $v)
  569. {
  570. if ($first && $k === 0)
  571. $assoc = false;
  572. $str .= ($first ? '' : ',') . ($assoc
  573. ? _hx_string_rec($k, $s) . '=>' . _hx_string_rec($o[$k], $s)
  574. : _hx_string_rec($o[$k], $s)
  575. );
  576. $first = false;
  577. }
  578. $str .= ']';
  579. return $str;
  580. }
  581. return '';
  582. }
  583. function _hx_substr($s, $pos, $len) {
  584. if($pos !== null && $pos !== 0 && $len !== null && $len < 0) return '';
  585. if($len === null) $len = strlen($s);
  586. if($pos < 0) {
  587. $pos = strlen($s) + $pos;
  588. if($pos < 0) $pos = 0;
  589. } else if($len < 0 )
  590. $len = strlen($s) + $len - $pos;
  591. $s = substr($s, $pos, $len);
  592. if($s === false)
  593. return '';
  594. else
  595. return $s;
  596. }
  597. function _hx_substring($s, $startIndex, $endIndex) {
  598. $len = strlen($s);
  599. if ($endIndex === null)
  600. $endIndex = $len;
  601. else if ($endIndex < 0)
  602. $endIndex = 0;
  603. else if ($endIndex > $len)
  604. $endIndex = $len;
  605. if ($startIndex < 0)
  606. $startIndex = 0;
  607. else if ($startIndex > $len)
  608. $startIndex = $len;
  609. if ($startIndex > $endIndex) {
  610. $tmp = $startIndex;
  611. $startIndex = $endIndex;
  612. $endIndex = $tmp;
  613. }
  614. return _hx_substr($s, $startIndex, $endIndex - $startIndex);
  615. }
  616. function _hx_trace($v, $i) {
  617. $msg = $i !== null ? $i->fileName.':'.$i->lineNumber.': ' : '';
  618. echo $msg._hx_string_rec($v, '').\"\n\";
  619. }
  620. function _hx_ttype($n) {
  621. return isset(php_Boot::$ttypes[$n]) ? php_Boot::$ttypes[$n] : null;
  622. }
  623. function _hx_make_var_args() {
  624. $args = func_get_args();
  625. $f = array_shift($args);
  626. return call_user_func($f, new _hx_array($args));
  627. }
  628. class _hx_anonymous extends stdClass {
  629. public function __call($m, $a) {
  630. return call_user_func_array($this->$m, $a);
  631. }
  632. public function __set($n, $v) {
  633. $this->$n = $v;
  634. }
  635. public function &__get($n) {
  636. if(isset($this->$n))
  637. return $this->$n;
  638. $null = null;
  639. return $null;
  640. }
  641. public function __isset($n) {
  642. return isset($this->$n);
  643. }
  644. public function __unset($n) {
  645. unset($this->$n);
  646. }
  647. public function __toString() {
  648. $rfl = new ReflectionObject($this);
  649. $b = '{ ';
  650. $properties = $rfl->getProperties();
  651. $first = true;
  652. while(list(, $prop) = each($properties)) {
  653. if($first)
  654. $first = false;
  655. else
  656. $b .= ', ';
  657. $f = $prop->getName();
  658. $b .= $f . ' => ' . $this->$f;
  659. }
  660. $b .= ' }';
  661. return $b;
  662. }
  663. }
  664. class _hx_type {
  665. public $__tname__;
  666. public $__qname__;
  667. public $__path__;
  668. public function __construct($cn, $qn, $path = null) {
  669. $this->__tname__ = $cn;
  670. $this->__qname__ = $qn;
  671. $this->__path__ = $path;
  672. if(property_exists($cn, '__meta__'))
  673. $this->__meta__ = eval($cn.'::$__meta__');
  674. }
  675. public function toString() { return $this->__toString(); }
  676. public function __toString() {
  677. return $this->__qname__;
  678. }
  679. private $rfl = false;
  680. public function __rfl__() {
  681. if($this->rfl !== false) return $this->rfl;
  682. if(class_exists($this->__tname__) || interface_exists($this->__tname__))
  683. $this->rfl = new ReflectionClass($this->__tname__);
  684. else
  685. $this->rfl = null;
  686. return $this->rfl;
  687. }
  688. public function __call($n, $a) {
  689. return call_user_func_array(array($this->__tname__, $n), $a);
  690. }
  691. public function __get($n) {
  692. if(($r = $this->__rfl__())==null) return null;
  693. if($r->hasProperty($n))
  694. return $r->getStaticPropertyValue($n);
  695. else if($r->hasMethod($n))
  696. return array($r->name, $n);
  697. else
  698. return null;
  699. }
  700. public function __set($n, $v) {
  701. if(($r = $this->__rfl__())==null) return null;
  702. return $r->setStaticPropertyValue($n, $v);
  703. }
  704. public function __isset($n) {
  705. if(($r = $this->__rfl__())==null) return null;
  706. return $r->hasProperty($n) || $r->hasMethod($n);
  707. }
  708. }
  709. class _hx_class extends _hx_type {}
  710. class _hx_enum extends _hx_type {}
  711. class _hx_interface extends _hx_type {}
  712. class HException extends Exception {
  713. public function __construct($e, $message = null, $code = null, $p = null) {
  714. $message = _hx_string_rec($e, '') . $message;
  715. parent::__construct($message,$code);
  716. $this->e = $e;
  717. $this->p = $p;
  718. }
  719. public $e;
  720. public $p;
  721. public function setLine($l) {
  722. $this->line = $l;
  723. }
  724. public function setFile($f) {
  725. $this->file = $f;
  726. }
  727. }
  728. class _hx_lambda {
  729. public function __construct($locals, $func) {
  730. $this->locals = $locals;
  731. $this->func = $func;
  732. }
  733. public $locals;
  734. public $func;
  735. public function execute() {
  736. // if use $this->locals directly in array_merge it works only if I make the assignement loop,
  737. // so I've decided to reference $arr
  738. $arr = array();
  739. for ($i = 0; $i<count($this->locals);$i++)
  740. $arr[] = & $this->locals[$i];
  741. $args = func_get_args();
  742. return call_user_func_array($this->func, array_merge($arr, $args));
  743. }
  744. }
  745. class Enum {
  746. public function __construct($tag, $index, $params = null) { $this->tag = $tag; $this->index = $index; $this->params = $params; }
  747. public $tag;
  748. public $index;
  749. public $params;
  750. public function __toString() {
  751. return $this->tag;
  752. }
  753. }
  754. error_reporting(E_ALL & ~E_STRICT);
  755. set_error_handler('_hx_error_handler', E_ALL & ~E_STRICT);
  756. set_exception_handler('_hx_exception_handler');
  757. php_Boot::$qtypes = array();
  758. php_Boot::$ttypes = array();
  759. php_Boot::$tpaths = array();
  760. _hx_register_type(new _hx_class('String', 'String'));
  761. _hx_register_type(new _hx_class('_hx_array', 'Array'));
  762. _hx_register_type(new _hx_class('Int', 'Int'));
  763. _hx_register_type(new _hx_class('Float', 'Float'));
  764. _hx_register_type(new _hx_class('Class', 'Class'));
  765. _hx_register_type(new _hx_class('Enum', 'Enum'));
  766. _hx_register_type(new _hx_class('Dynamic', 'Dynamic'));
  767. _hx_register_type(new _hx_enum('Bool', 'Bool'));
  768. _hx_register_type(new _hx_enum('Void', 'Void'));
  769. $_hx_libdir = dirname(__FILE__) . '/..';
  770. $_hx_autload_cache_file = $_hx_libdir . '/../cache/haxe_autoload.php';
  771. if(!file_exists($_hx_autload_cache_file)) {
  772. function _hx_build_paths($d, &$_hx_types_array, $pack, $prefix) {
  773. $h = opendir($d);
  774. while(false !== ($f = readdir($h))) {
  775. $p = $d.'/'.$f;
  776. if($f == '.' || $f == '..')
  777. continue;
  778. if (is_file($p) && substr($f, -4) == '.php') {
  779. $bn = basename($f, '.php');
  780. if ($prefix)
  781. {
  782. if ($prefix != substr($bn, 0, $lenprefix = strlen($prefix)))
  783. continue;
  784. $bn = substr($bn, $lenprefix);
  785. }
  786. if(substr($bn, -6) == '.class') {
  787. $bn = substr($bn, 0, -6);
  788. $t = 0;
  789. } else if(substr($bn, -5) == '.enum') {
  790. $bn = substr($bn, 0, -5);
  791. $t = 1;
  792. } else if(substr($bn, -10) == '.interface') {
  793. $bn = substr($bn, 0, -10);
  794. $t = 2;
  795. } else if(substr($bn, -7) == '.extern') {
  796. $bn = substr($bn, 0, -7);
  797. $t = 3;
  798. } else
  799. continue;
  800. $qname = ($bn == 'HList' && empty($pack)) ? 'List' : join(array_merge($pack, array($bn)), '.');
  801. $_hx_types_array[] = array(
  802. 'path' => $p,
  803. 'name' => $prefix . $bn,
  804. 'type' => $t,
  805. 'qname' => $qname,
  806. 'phpname' => join(array_merge($pack, array($prefix . $bn)), '_')
  807. );
  808. } else if(is_dir($p))
  809. _hx_build_paths($p, $_hx_types_array, array_merge($pack, array($f)), $prefix);
  810. }
  811. closedir($h);
  812. }
  813. $_hx_cache_content = '<?php\n\n';
  814. $_hx_types_array = array();
  815. _hx_build_paths($_hx_libdir, $_hx_types_array, array(), $_hx_class_prefix);
  816. for($i=0;$i<count($_hx_types_array);$i++) {
  817. $_hx_cache_content .= '_hx_register_type(new ';
  818. $t = null;
  819. if($_hx_types_array[$i]['type'] == 0) {
  820. $t = new _hx_class($_hx_types_array[$i]['phpname'], $_hx_types_array[$i]['qname'], $_hx_types_array[$i]['path']);
  821. $_hx_cache_content .= '_hx_class';
  822. } else if($_hx_types_array[$i]['type'] == 1) {
  823. $t = new _hx_enum($_hx_types_array[$i]['phpname'], $_hx_types_array[$i]['qname'], $_hx_types_array[$i]['path']);
  824. $_hx_cache_content .= '_hx_enum';
  825. } else if($_hx_types_array[$i]['type'] == 2) {
  826. $t = new _hx_interface($_hx_types_array[$i]['phpname'], $_hx_types_array[$i]['qname'], $_hx_types_array[$i]['path']);
  827. $_hx_cache_content .= '_hx_interface';
  828. } else if($_hx_types_array[$i]['type'] == 3) {
  829. $t = new _hx_class($_hx_types_array[$i]['name'], $_hx_types_array[$i]['qname'], $_hx_types_array[$i]['path']);
  830. $_hx_cache_content .= '_hx_class';
  831. }
  832. _hx_register_type($t);
  833. $_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';
  834. }
  835. try {
  836. file_put_contents($_hx_autload_cache_file, $_hx_cache_content);
  837. } catch(Exception $e) {}
  838. unset($_hx_types_array);
  839. unset($_hx_cache_content);
  840. } else {
  841. require($_hx_autload_cache_file);
  842. }
  843. function _hx_autoload($name) {
  844. if(!isset(php_Boot::$tpaths[$name])) return false;
  845. require_once(php_Boot::$tpaths[$name]);
  846. return true;
  847. }
  848. if(!ini_get('date.timezone'))
  849. date_default_timezone_set('UTC');
  850. spl_autoload_register('_hx_autoload')");
  851. }
  852. }