Boot.hx 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986
  1. /*
  2. * Copyright (C)2005-2017 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(_hx_instanceof($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. case 'php.NativeArray': return is_array($v);
  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. if(!isset(php_Boot::$qtypes[$n])) {
  470. php_Boot::$qtypes[$n] = new _hx_type($n, null);
  471. }
  472. return php_Boot::$qtypes[$n];
  473. }
  474. function _hx_register_type($t) {
  475. php_Boot::$qtypes[$t->__qname__] = $t;
  476. php_Boot::$ttypes[$t->__tname__] = $t;
  477. if($t->__path__ !== null)
  478. php_Boot::$tpaths[$t->__tname__] = $t->__path__;
  479. }
  480. function _hx_set_method($o, $field, $func) {
  481. $value[0]->scope = $o;
  482. $o->$field = $func;
  483. }
  484. function _hx_shift_right($v, $n) {
  485. return ($n == 0) ? $v : ($v >= 0) ? ($v >> $n) : ($v >> $n) & (0x7fffffff >> ($n-1));
  486. }
  487. function _hx_string_call($s, $method, $params) {
  488. if(!is_string($s)) return call_user_func_array(array($s, $method), $params);
  489. switch($method) {
  490. case 'toUpperCase': return strtoupper($s);
  491. case 'toLowerCase': return strtolower($s);
  492. case 'charAt' : return substr($s, $params[0], 1);
  493. case 'charCodeAt' : return _hx_char_code_at($s, $params[0]);
  494. case 'indexOf' : return _hx_index_of($s, $params[0], (count($params) > 1 ? $params[1] : null));
  495. case 'lastIndexOf': return _hx_last_index_of($s, $params[0], (count($params) > 1 ? $params[1] : null));
  496. case 'split' : return _hx_explode($params[0], $s);
  497. case 'substr' : return _hx_substr($s, $params[0], (count($params) > 1 ? $params[1] : null));
  498. case 'toString' : return $s;
  499. default : throw new HException('Invalid Operation: ' . $method);
  500. }
  501. }
  502. function _hx_string_or_null($s) {
  503. return $s === null ? 'null' : $s;
  504. }
  505. function _hx_string_rec($o, $s) {
  506. if($o === null) return 'null';
  507. if(strlen($s) >= 5) return '<...>';
  508. if(is_int($o) || is_float($o)) return '' . $o;
  509. if(is_bool($o)) return $o ? 'true' : 'false';
  510. if(is_object($o)) {
  511. $c = get_class($o);
  512. if($o instanceof Enum) {
  513. $b = $o->tag;
  514. if(!empty($o->params)) {
  515. $s .= \"\t\";
  516. $b .= '(';
  517. foreach($o->params as $i => $val) {
  518. if($i > 0)
  519. $b .= ',' . _hx_string_rec($val, $s);
  520. else
  521. $b .= _hx_string_rec($val, $s);
  522. }
  523. $b .= ')';
  524. }
  525. return $b;
  526. } else {
  527. if ($o instanceof _hx_anonymous) {
  528. if ($o->toString && is_callable($o->toString)) {
  529. return call_user_func($o->toString);
  530. }
  531. $rfl = new ReflectionObject($o);
  532. $b2 = \"{\n\";
  533. $s .= \"\t\";
  534. $properties = $rfl->getProperties();
  535. foreach($properties as $i => $prop) {
  536. $f = $prop->getName();
  537. if($i > 0)
  538. $b2 .= \", \n\";
  539. $b2 .= $s . $f . ' : ' . _hx_string_rec($o->$f, $s);
  540. }
  541. $s = substr($s, 1);
  542. $b2 .= \"\n\" . $s . '}';
  543. return $b2;
  544. } else {
  545. if($o instanceof _hx_type)
  546. return $o->__qname__;
  547. else {
  548. if(is_callable(array($o, 'toString')))
  549. return $o->toString();
  550. else {
  551. if(is_callable(array($o, '__toString')))
  552. return $o->__toString();
  553. else
  554. return '[' . _hx_ttype($c) . ']';
  555. }
  556. }
  557. }
  558. }
  559. }
  560. if(is_string($o)) {
  561. if(_hx_is_lambda($o)) return '<function>';
  562. // if(strlen($s) > 0) return '\"' . str_replace('\"', '\\\"', $o) . '\"';
  563. else return $o;
  564. }
  565. if(is_array($o)) {
  566. if(is_callable($o)) return '<function>';
  567. $str = '[';
  568. $s .= \"\t\";
  569. $first = true;
  570. $assoc = true;
  571. foreach($o as $k => $v)
  572. {
  573. if ($first && $k === 0)
  574. $assoc = false;
  575. $str .= ($first ? '' : ',') . ($assoc
  576. ? _hx_string_rec($k, $s) . '=>' . _hx_string_rec($o[$k], $s)
  577. : _hx_string_rec($o[$k], $s)
  578. );
  579. $first = false;
  580. }
  581. $str .= ']';
  582. return $str;
  583. }
  584. return '';
  585. }
  586. function _hx_substr($s, $pos, $len) {
  587. if($pos !== null && $pos !== 0 && $len !== null && $len < 0) return '';
  588. if($len === null) $len = strlen($s);
  589. if($pos < 0) {
  590. $pos = strlen($s) + $pos;
  591. if($pos < 0) $pos = 0;
  592. } else if($len < 0 )
  593. $len = strlen($s) + $len - $pos;
  594. $s = substr($s, $pos, $len);
  595. if($s === false)
  596. return '';
  597. else
  598. return $s;
  599. }
  600. function _hx_substring($s, $startIndex, $endIndex) {
  601. $len = strlen($s);
  602. if ($endIndex === null)
  603. $endIndex = $len;
  604. else if ($endIndex < 0)
  605. $endIndex = 0;
  606. else if ($endIndex > $len)
  607. $endIndex = $len;
  608. if ($startIndex < 0)
  609. $startIndex = 0;
  610. else if ($startIndex > $len)
  611. $startIndex = $len;
  612. if ($startIndex > $endIndex) {
  613. $tmp = $startIndex;
  614. $startIndex = $endIndex;
  615. $endIndex = $tmp;
  616. }
  617. return _hx_substr($s, $startIndex, $endIndex - $startIndex);
  618. }
  619. function _hx_trace($v, $i) {
  620. $msg = $i !== null ? $i->fileName.':'.$i->lineNumber.': ' : '';
  621. echo $msg._hx_string_rec($v, '').\"\n\";
  622. }
  623. function _hx_ttype($n) {
  624. return isset(php_Boot::$ttypes[$n]) ? php_Boot::$ttypes[$n] : null;
  625. }
  626. function _hx_make_var_args() {
  627. $args = func_get_args();
  628. $f = array_shift($args);
  629. return call_user_func($f, new _hx_array($args));
  630. }
  631. class _hx_anonymous extends stdClass {
  632. public function __call($m, $a) {
  633. return call_user_func_array($this->$m, $a);
  634. }
  635. public function __set($n, $v) {
  636. $this->$n = $v;
  637. }
  638. public function &__get($n) {
  639. if(isset($this->$n))
  640. return $this->$n;
  641. $null = null;
  642. return $null;
  643. }
  644. public function __isset($n) {
  645. return isset($this->$n);
  646. }
  647. public function __unset($n) {
  648. unset($this->$n);
  649. }
  650. public function __toString() {
  651. $rfl = new ReflectionObject($this);
  652. $b = '{ ';
  653. $properties = $rfl->getProperties();
  654. $first = true;
  655. while(list(, $prop) = each($properties)) {
  656. if($first)
  657. $first = false;
  658. else
  659. $b .= ', ';
  660. $f = $prop->getName();
  661. $b .= $f . ' => ' . $this->$f;
  662. }
  663. $b .= ' }';
  664. return $b;
  665. }
  666. }
  667. class _hx_type {
  668. public $__tname__;
  669. public $__qname__;
  670. public $__path__;
  671. public function __construct($cn, $qn, $path = null) {
  672. $this->__tname__ = $cn;
  673. $this->__qname__ = $qn;
  674. $this->__path__ = $path;
  675. if(property_exists($cn, '__meta__'))
  676. $this->__meta__ = eval($cn.'::$__meta__');
  677. }
  678. public function __ensureMeta__() {
  679. if(property_exists($this->__tname__, '__meta__') && !$this->__meta__) {
  680. $this->__meta__ = eval($this->__tname__.'::$__meta__');
  681. }
  682. }
  683. public function toString() { return $this->__toString(); }
  684. public function __toString() {
  685. return $this->__qname__;
  686. }
  687. private $rfl = false;
  688. public function __rfl__() {
  689. if($this->rfl !== false) return $this->rfl;
  690. if(class_exists($this->__tname__) || interface_exists($this->__tname__))
  691. $this->rfl = new ReflectionClass($this->__tname__);
  692. else
  693. $this->rfl = null;
  694. return $this->rfl;
  695. }
  696. public function __call($n, $a) {
  697. return call_user_func_array(array($this->__tname__, $n), $a);
  698. }
  699. public function __get($n) {
  700. if(($r = $this->__rfl__())==null) return null;
  701. if($r->hasProperty($n)) {
  702. try {
  703. return $r->getStaticPropertyValue($n);
  704. } catch(Exception $e) {
  705. return null;
  706. }
  707. } else if($r->hasMethod($n))
  708. return array($r->name, $n);
  709. else
  710. return null;
  711. }
  712. public function __set($n, $v) {
  713. if(($r = $this->__rfl__())==null) return null;
  714. return $r->setStaticPropertyValue($n, $v);
  715. }
  716. public function __isset($n) {
  717. if(($r = $this->__rfl__())==null) return null;
  718. return array_key_exists($n, $r->getStaticProperties()) || $r->hasMethod($n);
  719. }
  720. }
  721. class _hx_class extends _hx_type {}
  722. class _hx_enum extends _hx_type {}
  723. class _hx_interface extends _hx_type {}
  724. class HException extends Exception {
  725. public function __construct($e, $message = null, $code = null, $p = null) {
  726. $message = _hx_string_rec($e, '') . $message;
  727. parent::__construct($message,$code);
  728. $this->e = $e;
  729. $this->p = $p;
  730. }
  731. public $e;
  732. public $p;
  733. public function setLine($l) {
  734. $this->line = $l;
  735. }
  736. public function setFile($f) {
  737. $this->file = $f;
  738. }
  739. }
  740. class _hx_lambda {
  741. public function __construct($locals, $func) {
  742. $this->locals = $locals;
  743. $this->func = $func;
  744. }
  745. public $locals;
  746. public $func;
  747. public function execute() {
  748. // if use $this->locals directly in array_merge it works only if I make the assignement loop,
  749. // so I've decided to reference $arr
  750. $arr = array();
  751. foreach($this->locals as $i => $val)
  752. $arr[] = & $this->locals[$i];
  753. $args = func_get_args();
  754. return call_user_func_array($this->func, array_merge($arr, $args));
  755. }
  756. }
  757. class Enum {
  758. public function __construct($tag, $index, $params = null) { $this->tag = $tag; $this->index = $index; $this->params = $params; }
  759. public $tag;
  760. public $index;
  761. public $params;
  762. public function __toString() {
  763. return $this->tag;
  764. }
  765. }
  766. error_reporting(E_ALL & ~E_STRICT);
  767. set_error_handler('_hx_error_handler', E_ALL & ~E_STRICT);
  768. set_exception_handler('_hx_exception_handler');
  769. php_Boot::$qtypes = array();
  770. php_Boot::$ttypes = array();
  771. php_Boot::$tpaths = array();
  772. _hx_register_type(new _hx_class('String', 'String'));
  773. _hx_register_type(new _hx_class('_hx_array', 'Array'));
  774. _hx_register_type(new _hx_class('Int', 'Int'));
  775. _hx_register_type(new _hx_class('Float', 'Float'));
  776. _hx_register_type(new _hx_class('Class', 'Class'));
  777. _hx_register_type(new _hx_class('Enum', 'Enum'));
  778. _hx_register_type(new _hx_class('Dynamic', 'Dynamic'));
  779. _hx_register_type(new _hx_enum('Bool', 'Bool'));
  780. _hx_register_type(new _hx_enum('Void', 'Void'));
  781. $_hx_libdir = dirname(__FILE__) . '/..';
  782. $_hx_autload_cache_file = $_hx_libdir . '/../cache/haxe_autoload.php';
  783. if(!file_exists($_hx_autload_cache_file)) {
  784. function _hx_build_paths($d, &$_hx_types_array, $pack, $prefix) {
  785. $h = opendir($d);
  786. while(false !== ($f = readdir($h))) {
  787. $p = $d.'/'.$f;
  788. if($f == '.' || $f == '..')
  789. continue;
  790. if (is_file($p) && substr($f, -4) == '.php') {
  791. $bn = basename($f, '.php');
  792. if ($prefix)
  793. {
  794. if ($prefix != substr($bn, 0, $lenprefix = strlen($prefix)))
  795. continue;
  796. $bn = substr($bn, $lenprefix);
  797. }
  798. if(substr($bn, -6) == '.class') {
  799. $bn = substr($bn, 0, -6);
  800. $t = 0;
  801. } else if(substr($bn, -5) == '.enum') {
  802. $bn = substr($bn, 0, -5);
  803. $t = 1;
  804. } else if(substr($bn, -10) == '.interface') {
  805. $bn = substr($bn, 0, -10);
  806. $t = 2;
  807. } else if(substr($bn, -7) == '.extern') {
  808. $bn = substr($bn, 0, -7);
  809. $t = 3;
  810. } else
  811. continue;
  812. $qname = ($bn == 'HList' && empty($pack)) ? 'List' : join(array_merge($pack, array($bn)), '.');
  813. $_hx_types_array[] = array(
  814. 'path' => $p,
  815. 'name' => $prefix . $bn,
  816. 'type' => $t,
  817. 'qname' => $qname,
  818. 'phpname' => join(array_merge($pack, array($prefix . $bn)), '_')
  819. );
  820. } else if(is_dir($p))
  821. _hx_build_paths($p, $_hx_types_array, array_merge($pack, array($f)), $prefix);
  822. }
  823. closedir($h);
  824. }
  825. $_hx_cache_content = '<?php\n\n';
  826. $_hx_types_array = array();
  827. _hx_build_paths($_hx_libdir, $_hx_types_array, array(), $_hx_class_prefix);
  828. foreach($_hx_types_array as $val) {
  829. $_hx_cache_content .= '_hx_register_type(new ';
  830. $t = null;
  831. if($val['type'] == 0) {
  832. $t = new _hx_class($val['phpname'], $val['qname'], $val['path']);
  833. $_hx_cache_content .= '_hx_class';
  834. } else if($val['type'] == 1) {
  835. $t = new _hx_enum($val['phpname'], $val['qname'], $val['path']);
  836. $_hx_cache_content .= '_hx_enum';
  837. } else if($val['type'] == 2) {
  838. $t = new _hx_interface($val['phpname'], $val['qname'], $val['path']);
  839. $_hx_cache_content .= '_hx_interface';
  840. } else if($val['type'] == 3) {
  841. $t = new _hx_class($val['name'], $val['qname'], $val['path']);
  842. $_hx_cache_content .= '_hx_class';
  843. }
  844. _hx_register_type($t);
  845. $_hx_cache_content .= '(\\''.($val['type'] == 3 ? $val['name'] : $val['phpname']).'\\', \\''.$val['qname'].'\\', \\''.$val['path'].'\\'));\n';
  846. }
  847. try {
  848. file_put_contents($_hx_autload_cache_file, $_hx_cache_content);
  849. } catch(Exception $e) {}
  850. unset($_hx_types_array);
  851. unset($_hx_cache_content);
  852. } else {
  853. require($_hx_autload_cache_file);
  854. }
  855. function _hx_autoload($name) {
  856. if(!isset(php_Boot::$tpaths[$name])) return false;
  857. require_once(php_Boot::$tpaths[$name]);
  858. return true;
  859. }
  860. if(!ini_get('date.timezone'))
  861. date_default_timezone_set('UTC');
  862. spl_autoload_register('_hx_autoload')");
  863. }
  864. }