PageRenderTime 27ms CodeModel.GetById 11ms RepoModel.GetById 1ms app.codeStats 0ms

/library/tools/trm/lib/Type.class.php

https://code.google.com/p/haquery/
PHP | 515 lines | 512 code | 3 blank | 0 comment | 71 complexity | ba5f7d225ed3efeae148aefc577bd2f0 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. class Type {
  3. public function __construct(){}
  4. static function getClass($o) {
  5. $GLOBALS['%s']->push("Type::getClass");
  6. $?spos = $GLOBALS['%s']->length;
  7. if($o === null) {
  8. $GLOBALS['%s']->pop();
  9. return null;
  10. }
  11. if(is_array($o)) {
  12. if(count($o) === 2 && is_callable($o)) {
  13. $GLOBALS['%s']->pop();
  14. return null;
  15. }
  16. {
  17. $?tmp = _hx_ttype("Array");
  18. $GLOBALS['%s']->pop();
  19. return $?tmp;
  20. }
  21. }
  22. if(is_string($o)) {
  23. if(_hx_is_lambda($o)) {
  24. $GLOBALS['%s']->pop();
  25. return null;
  26. }
  27. {
  28. $?tmp = _hx_ttype("String");
  29. $GLOBALS['%s']->pop();
  30. return $?tmp;
  31. }
  32. }
  33. if(!is_object($o)) {
  34. $GLOBALS['%s']->pop();
  35. return null;
  36. }
  37. $c = get_class($o);
  38. if($c === false || $c === "_hx_anonymous" || is_subclass_of($c, "enum")) {
  39. $GLOBALS['%s']->pop();
  40. return null;
  41. } else {
  42. $?tmp = _hx_ttype($c);
  43. $GLOBALS['%s']->pop();
  44. return $?tmp;
  45. }
  46. $GLOBALS['%s']->pop();
  47. }
  48. static function getEnum($o) {
  49. $GLOBALS['%s']->push("Type::getEnum");
  50. $?spos = $GLOBALS['%s']->length;
  51. if(!$o instanceof Enum) {
  52. $GLOBALS['%s']->pop();
  53. return null;
  54. } else {
  55. $?tmp = _hx_ttype(get_class($o));
  56. $GLOBALS['%s']->pop();
  57. return $?tmp;
  58. }
  59. $GLOBALS['%s']->pop();
  60. }
  61. static function getSuperClass($c) {
  62. $GLOBALS['%s']->push("Type::getSuperClass");
  63. $?spos = $GLOBALS['%s']->length;
  64. $s = get_parent_class($c->__tname__);
  65. if($s === false) {
  66. $GLOBALS['%s']->pop();
  67. return null;
  68. } else {
  69. $?tmp = _hx_ttype($s);
  70. $GLOBALS['%s']->pop();
  71. return $?tmp;
  72. }
  73. $GLOBALS['%s']->pop();
  74. }
  75. static function getClassName($c) {
  76. $GLOBALS['%s']->push("Type::getClassName");
  77. $?spos = $GLOBALS['%s']->length;
  78. if($c === null) {
  79. $GLOBALS['%s']->pop();
  80. return null;
  81. }
  82. {
  83. $?tmp = $c->__qname__;
  84. $GLOBALS['%s']->pop();
  85. return $?tmp;
  86. }
  87. $GLOBALS['%s']->pop();
  88. }
  89. static function getEnumName($e) {
  90. $GLOBALS['%s']->push("Type::getEnumName");
  91. $?spos = $GLOBALS['%s']->length;
  92. {
  93. $?tmp = $e->__qname__;
  94. $GLOBALS['%s']->pop();
  95. return $?tmp;
  96. }
  97. $GLOBALS['%s']->pop();
  98. }
  99. static function resolveClass($name) {
  100. $GLOBALS['%s']->push("Type::resolveClass");
  101. $?spos = $GLOBALS['%s']->length;
  102. $c = _hx_qtype($name);
  103. if($c instanceof _hx_class) {
  104. $GLOBALS['%s']->pop();
  105. return $c;
  106. } else {
  107. $GLOBALS['%s']->pop();
  108. return null;
  109. }
  110. $GLOBALS['%s']->pop();
  111. }
  112. static function resolveEnum($name) {
  113. $GLOBALS['%s']->push("Type::resolveEnum");
  114. $?spos = $GLOBALS['%s']->length;
  115. $e = _hx_qtype($name);
  116. if($e instanceof _hx_enum) {
  117. $GLOBALS['%s']->pop();
  118. return $e;
  119. } else {
  120. $GLOBALS['%s']->pop();
  121. return null;
  122. }
  123. $GLOBALS['%s']->pop();
  124. }
  125. static function createInstance($cl, $args) {
  126. $GLOBALS['%s']->push("Type::createInstance");
  127. $?spos = $GLOBALS['%s']->length;
  128. if($cl->__qname__ === "Array") {
  129. $?tmp = new _hx_array(array());
  130. $GLOBALS['%s']->pop();
  131. return $?tmp;
  132. }
  133. if($cl->__qname__ === "String") {
  134. $?tmp = $args[0];
  135. $GLOBALS['%s']->pop();
  136. return $?tmp;
  137. }
  138. $c = $cl->__rfl__();
  139. if($c === null) {
  140. $GLOBALS['%s']->pop();
  141. return null;
  142. }
  143. {
  144. $?tmp = $inst = $c->getConstructor() ? $c->newInstanceArgs($args->?a) : $c->newInstanceArgs();
  145. $GLOBALS['%s']->pop();
  146. return $?tmp;
  147. }
  148. $GLOBALS['%s']->pop();
  149. }
  150. static function createEmptyInstance($cl) {
  151. $GLOBALS['%s']->push("Type::createEmptyInstance");
  152. $?spos = $GLOBALS['%s']->length;
  153. if($cl->__qname__ === "Array") {
  154. $?tmp = new _hx_array(array());
  155. $GLOBALS['%s']->pop();
  156. return $?tmp;
  157. }
  158. if($cl->__qname__ === "String") {
  159. $GLOBALS['%s']->pop();
  160. return "";
  161. }
  162. try {
  163. php_Boot::$skip_constructor = true;
  164. $rfl = $cl->__rfl__();
  165. if($rfl === null) {
  166. $GLOBALS['%s']->pop();
  167. return null;
  168. }
  169. $m = $rfl->getConstructor();
  170. $nargs = $m->getNumberOfRequiredParameters();
  171. $i = null;
  172. if($nargs > 0) {
  173. $args = array_fill(0, $m->getNumberOfRequiredParameters(), null);
  174. $i = $rfl->newInstanceArgs($args);
  175. } else {
  176. $i = $rfl->newInstanceArgs(array());
  177. }
  178. php_Boot::$skip_constructor = false;
  179. {
  180. $GLOBALS['%s']->pop();
  181. return $i;
  182. }
  183. }catch(Exception $?e) {
  184. $_ex_ = ($?e instanceof HException) ? $?e->e : $?e;
  185. $e = $_ex_;
  186. {
  187. $GLOBALS['%e'] = new _hx_array(array());
  188. while($GLOBALS['%s']->length >= $?spos) {
  189. $GLOBALS['%e']->unshift($GLOBALS['%s']->pop());
  190. }
  191. $GLOBALS['%s']->push($GLOBALS['%e'][0]);
  192. php_Boot::$skip_constructor = false;
  193. throw new HException("Unable to instantiate " . Std::string($cl));
  194. }
  195. }
  196. {
  197. $GLOBALS['%s']->pop();
  198. return null;
  199. }
  200. $GLOBALS['%s']->pop();
  201. }
  202. static function createEnum($e, $constr, $params) {
  203. $GLOBALS['%s']->push("Type::createEnum");
  204. $?spos = $GLOBALS['%s']->length;
  205. $f = Reflect::field($e, $constr);
  206. if($f === null) {
  207. throw new HException("No such constructor " . $constr);
  208. }
  209. if(Reflect::isFunction($f)) {
  210. if($params === null) {
  211. throw new HException("Constructor " . $constr . " need parameters");
  212. }
  213. {
  214. $?tmp = Reflect::callMethod($e, $f, $params);
  215. $GLOBALS['%s']->pop();
  216. return $?tmp;
  217. }
  218. }
  219. if($params !== null && $params->length !== 0) {
  220. throw new HException("Constructor " . $constr . " does not need parameters");
  221. }
  222. {
  223. $GLOBALS['%s']->pop();
  224. return $f;
  225. }
  226. $GLOBALS['%s']->pop();
  227. }
  228. static function createEnumIndex($e, $index, $params) {
  229. $GLOBALS['%s']->push("Type::createEnumIndex");
  230. $?spos = $GLOBALS['%s']->length;
  231. $c = _hx_array_get(Type::getEnumConstructs($e), $index);
  232. if($c === null) {
  233. throw new HException($index . " is not a valid enum constructor index");
  234. }
  235. {
  236. $?tmp = Type::createEnum($e, $c, $params);
  237. $GLOBALS['%s']->pop();
  238. return $?tmp;
  239. }
  240. $GLOBALS['%s']->pop();
  241. }
  242. static function getInstanceFields($c) {
  243. $GLOBALS['%s']->push("Type::getInstanceFields");
  244. $?spos = $GLOBALS['%s']->length;
  245. if($c->__qname__ === "String") {
  246. $?tmp = new _hx_array(array("substr", "charAt", "charCodeAt", "indexOf", "lastIndexOf", "split", "toLowerCase", "toUpperCase", "toString", "length"));
  247. $GLOBALS['%s']->pop();
  248. return $?tmp;
  249. }
  250. if($c->__qname__ === "Array") {
  251. $?tmp = new _hx_array(array("push", "concat", "join", "pop", "reverse", "shift", "slice", "sort", "splice", "toString", "copy", "unshift", "insert", "remove", "iterator", "length"));
  252. $GLOBALS['%s']->pop();
  253. return $?tmp;
  254. }
  255. $rfl = $c->__rfl__();
  256. if($rfl === null) return new _hx_array(array());
  257. $r = array();
  258. $internals = array('__construct', '__call', '__get', '__set', '__isset', '__unset', '__toString');
  259. $ms = $rfl->getMethods();
  260. while(list(, $m) = each($ms)) {
  261. $n = $m->getName();
  262. if(!$m->isStatic() && ! in_array($n, $internals)) $r[] = $n;
  263. }
  264. $ps = $rfl->getProperties();
  265. while(list(, $p) = each($ps))
  266. if(!$p->isStatic()) $r[] = $p->getName();
  267. {
  268. $?tmp = new _hx_array(array_values(array_unique($r)));
  269. $GLOBALS['%s']->pop();
  270. return $?tmp;
  271. }
  272. $GLOBALS['%s']->pop();
  273. }
  274. static function getClassFields($c) {
  275. $GLOBALS['%s']->push("Type::getClassFields");
  276. $?spos = $GLOBALS['%s']->length;
  277. if($c->__qname__ === "String") {
  278. $?tmp = new _hx_array(array("fromCharCode"));
  279. $GLOBALS['%s']->pop();
  280. return $?tmp;
  281. }
  282. if($c->__qname__ === "Array") {
  283. $?tmp = new _hx_array(array());
  284. $GLOBALS['%s']->pop();
  285. return $?tmp;
  286. }
  287. $rfl = $c->__rfl__();
  288. if($rfl === null) return new _hx_array(array());
  289. $ms = $rfl->getMethods();
  290. $r = array();
  291. while(list(, $m) = each($ms))
  292. if($m->isStatic()) $r[] = $m->getName();
  293. $ps = $rfl->getProperties();
  294. while(list(, $p) = each($ps))
  295. if($p->isStatic()) $r[] = $p->getName();
  296. ;
  297. {
  298. $?tmp = new _hx_array(array_unique($r));
  299. $GLOBALS['%s']->pop();
  300. return $?tmp;
  301. }
  302. $GLOBALS['%s']->pop();
  303. }
  304. static function getEnumConstructs($e) {
  305. $GLOBALS['%s']->push("Type::getEnumConstructs");
  306. $?spos = $GLOBALS['%s']->length;
  307. if($e->__tname__ == 'Bool') {
  308. $?tmp = new _hx_array(array("true", "false"));
  309. $GLOBALS['%s']->pop();
  310. return $?tmp;
  311. }
  312. if($e->__tname__ == 'Void') {
  313. $?tmp = new _hx_array(array());
  314. $GLOBALS['%s']->pop();
  315. return $?tmp;
  316. }
  317. {
  318. $?tmp = new _hx_array($e->__constructors);
  319. $GLOBALS['%s']->pop();
  320. return $?tmp;
  321. }
  322. $GLOBALS['%s']->pop();
  323. }
  324. static function typeof($v) {
  325. $GLOBALS['%s']->push("Type::typeof");
  326. $?spos = $GLOBALS['%s']->length;
  327. if($v === null) {
  328. $?tmp = ValueType::$TNull;
  329. $GLOBALS['%s']->pop();
  330. return $?tmp;
  331. }
  332. if(is_array($v)) {
  333. if(is_callable($v)) {
  334. $?tmp = ValueType::$TFunction;
  335. $GLOBALS['%s']->pop();
  336. return $?tmp;
  337. }
  338. {
  339. $?tmp = ValueType::TClass(_hx_qtype("Array"));
  340. $GLOBALS['%s']->pop();
  341. return $?tmp;
  342. }
  343. }
  344. if(is_string($v)) {
  345. if(_hx_is_lambda($v)) {
  346. $?tmp = ValueType::$TFunction;
  347. $GLOBALS['%s']->pop();
  348. return $?tmp;
  349. }
  350. {
  351. $?tmp = ValueType::TClass(_hx_qtype("String"));
  352. $GLOBALS['%s']->pop();
  353. return $?tmp;
  354. }
  355. }
  356. if(is_bool($v)) {
  357. $?tmp = ValueType::$TBool;
  358. $GLOBALS['%s']->pop();
  359. return $?tmp;
  360. }
  361. if(is_int($v)) {
  362. $?tmp = ValueType::$TInt;
  363. $GLOBALS['%s']->pop();
  364. return $?tmp;
  365. }
  366. if(is_float($v)) {
  367. $?tmp = ValueType::$TFloat;
  368. $GLOBALS['%s']->pop();
  369. return $?tmp;
  370. }
  371. if($v instanceof _hx_anonymous) {
  372. $?tmp = ValueType::$TObject;
  373. $GLOBALS['%s']->pop();
  374. return $?tmp;
  375. }
  376. if($v instanceof _hx_enum) {
  377. $?tmp = ValueType::$TObject;
  378. $GLOBALS['%s']->pop();
  379. return $?tmp;
  380. }
  381. if($v instanceof _hx_class) {
  382. $?tmp = ValueType::$TObject;
  383. $GLOBALS['%s']->pop();
  384. return $?tmp;
  385. }
  386. $c = _hx_ttype(get_class($v));
  387. if($c instanceof _hx_enum) {
  388. $?tmp = ValueType::TEnum($c);
  389. $GLOBALS['%s']->pop();
  390. return $?tmp;
  391. }
  392. if($c instanceof _hx_class) {
  393. $?tmp = ValueType::TClass($c);
  394. $GLOBALS['%s']->pop();
  395. return $?tmp;
  396. }
  397. {
  398. $?tmp = ValueType::$TUnknown;
  399. $GLOBALS['%s']->pop();
  400. return $?tmp;
  401. }
  402. $GLOBALS['%s']->pop();
  403. }
  404. static function enumEq($a, $b) {
  405. $GLOBALS['%s']->push("Type::enumEq");
  406. $?spos = $GLOBALS['%s']->length;
  407. if($a === $b) {
  408. $GLOBALS['%s']->pop();
  409. return true;
  410. }
  411. try {
  412. if(!_hx_equal($a->index, $b->index)) {
  413. $GLOBALS['%s']->pop();
  414. return false;
  415. }
  416. {
  417. $_g1 = 0; $_g = count($a->params);
  418. while($_g1 < $_g) {
  419. $i = $_g1++;
  420. if(Type::getEnum($a->params[$i]) !== null) {
  421. if(!Type::enumEq($a->params[$i], $b->params[$i])) {
  422. $GLOBALS['%s']->pop();
  423. return false;
  424. }
  425. } else {
  426. if(!_hx_equal($a->params[$i], $b->params[$i])) {
  427. $GLOBALS['%s']->pop();
  428. return false;
  429. }
  430. }
  431. unset($i);
  432. }
  433. }
  434. }catch(Exception $?e) {
  435. $_ex_ = ($?e instanceof HException) ? $?e->e : $?e;
  436. $e = $_ex_;
  437. {
  438. $GLOBALS['%e'] = new _hx_array(array());
  439. while($GLOBALS['%s']->length >= $?spos) {
  440. $GLOBALS['%e']->unshift($GLOBALS['%s']->pop());
  441. }
  442. $GLOBALS['%s']->push($GLOBALS['%e'][0]);
  443. {
  444. $GLOBALS['%s']->pop();
  445. return false;
  446. }
  447. }
  448. }
  449. {
  450. $GLOBALS['%s']->pop();
  451. return true;
  452. }
  453. $GLOBALS['%s']->pop();
  454. }
  455. static function enumConstructor($e) {
  456. $GLOBALS['%s']->push("Type::enumConstructor");
  457. $?spos = $GLOBALS['%s']->length;
  458. {
  459. $?tmp = $e->tag;
  460. $GLOBALS['%s']->pop();
  461. return $?tmp;
  462. }
  463. $GLOBALS['%s']->pop();
  464. }
  465. static function enumParameters($e) {
  466. $GLOBALS['%s']->push("Type::enumParameters");
  467. $?spos = $GLOBALS['%s']->length;
  468. if(_hx_field($e, "params") === null) {
  469. $?tmp = new _hx_array(array());
  470. $GLOBALS['%s']->pop();
  471. return $?tmp;
  472. } else {
  473. $?tmp = new _hx_array($e->params);
  474. $GLOBALS['%s']->pop();
  475. return $?tmp;
  476. }
  477. $GLOBALS['%s']->pop();
  478. }
  479. static function enumIndex($e) {
  480. $GLOBALS['%s']->push("Type::enumIndex");
  481. $?spos = $GLOBALS['%s']->length;
  482. {
  483. $?tmp = $e->index;
  484. $GLOBALS['%s']->pop();
  485. return $?tmp;
  486. }
  487. $GLOBALS['%s']->pop();
  488. }
  489. static function allEnums($e) {
  490. $GLOBALS['%s']->push("Type::allEnums");
  491. $?spos = $GLOBALS['%s']->length;
  492. $all = new _hx_array(array());
  493. {
  494. $_g = 0; $_g1 = Type::getEnumConstructs($e);
  495. while($_g < $_g1->length) {
  496. $c = $_g1[$_g];
  497. ++$_g;
  498. $v = Reflect::field($e, $c);
  499. if(!Reflect::isFunction($v)) {
  500. $all->push($v);
  501. }
  502. unset($v,$c);
  503. }
  504. }
  505. {
  506. $GLOBALS['%s']->pop();
  507. return $all;
  508. }
  509. $GLOBALS['%s']->pop();
  510. }
  511. function __toString() { return 'Type'; }
  512. }