rtl.js 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545
  1. var pas = { $libimports: {}};
  2. var rtl = {
  3. version: 30101,
  4. quiet: false,
  5. debug_load_units: false,
  6. debug_rtti: false,
  7. $res : {},
  8. debug: function(){
  9. if (rtl.quiet || !console || !console.log) return;
  10. console.log(arguments);
  11. },
  12. error: function(s){
  13. rtl.debug('Error: ',s);
  14. throw s;
  15. },
  16. warn: function(s){
  17. rtl.debug('Warn: ',s);
  18. },
  19. checkVersion: function(v){
  20. if (rtl.version != v) throw "expected rtl version "+v+", but found "+rtl.version;
  21. },
  22. hiInt: Math.pow(2,53),
  23. hasString: function(s){
  24. return rtl.isString(s) && (s.length>0);
  25. },
  26. isArray: function(a) {
  27. return Array.isArray(a);
  28. },
  29. isFunction: function(f){
  30. return typeof(f)==="function";
  31. },
  32. isModule: function(m){
  33. return rtl.isObject(m) && rtl.hasString(m.$name) && (pas[m.$name]===m);
  34. },
  35. isImplementation: function(m){
  36. return rtl.isObject(m) && rtl.isModule(m.$module) && (m.$module.$impl===m);
  37. },
  38. isNumber: function(n){
  39. return typeof(n)==="number";
  40. },
  41. isObject: function(o){
  42. var s=typeof(o);
  43. return (typeof(o)==="object") && (o!=null);
  44. },
  45. isString: function(s){
  46. return typeof(s)==="string";
  47. },
  48. getNumber: function(n){
  49. return typeof(n)==="number"?n:NaN;
  50. },
  51. getChar: function(c){
  52. return ((typeof(c)==="string") && (c.length===1)) ? c : "";
  53. },
  54. getObject: function(o){
  55. return ((typeof(o)==="object") || (typeof(o)==='function')) ? o : null;
  56. },
  57. isTRecord: function(type){
  58. return (rtl.isObject(type) && type.hasOwnProperty('$new') && (typeof(type.$new)==='function'));
  59. },
  60. isPasClass: function(type){
  61. return (rtl.isObject(type) && type.hasOwnProperty('$classname') && rtl.isObject(type.$module));
  62. },
  63. isPasClassInstance: function(type){
  64. return (rtl.isObject(type) && rtl.isPasClass(type.$class));
  65. },
  66. hexStr: function(n,digits){
  67. return ("000000000000000"+n.toString(16).toUpperCase()).slice(-digits);
  68. },
  69. m_loading: 0,
  70. m_loading_intf: 1,
  71. m_intf_loaded: 2,
  72. m_loading_impl: 3, // loading all used unit
  73. m_initializing: 4, // running initialization
  74. m_initialized: 5,
  75. module: function(module_name, intfuseslist, intfcode, impluseslist){
  76. if (rtl.debug_load_units) rtl.debug('rtl.module name="'+module_name+'" intfuses='+intfuseslist+' impluses='+impluseslist);
  77. if (!rtl.hasString(module_name)) rtl.error('invalid module name "'+module_name+'"');
  78. if (!rtl.isArray(intfuseslist)) rtl.error('invalid interface useslist of "'+module_name+'"');
  79. if (!rtl.isFunction(intfcode)) rtl.error('invalid interface code of "'+module_name+'"');
  80. if (!(impluseslist==undefined) && !rtl.isArray(impluseslist)) rtl.error('invalid implementation useslist of "'+module_name+'"');
  81. if (pas[module_name])
  82. rtl.error('module "'+module_name+'" is already registered');
  83. var r = Object.create(rtl.tSectionRTTI);
  84. var module = r.$module = pas[module_name] = {
  85. $name: module_name,
  86. $intfuseslist: intfuseslist,
  87. $impluseslist: impluseslist,
  88. $state: rtl.m_loading,
  89. $intfcode: intfcode,
  90. $implcode: null,
  91. $impl: null,
  92. $rtti: r
  93. };
  94. if (impluseslist) module.$impl = {
  95. $module: module,
  96. $rtti: r
  97. };
  98. },
  99. exitcode: 0,
  100. run: function(module_name){
  101. try {
  102. if (!rtl.hasString(module_name)) module_name='program';
  103. if (rtl.debug_load_units) rtl.debug('rtl.run module="'+module_name+'"');
  104. rtl.initRTTI();
  105. var module = pas[module_name];
  106. if (!module) rtl.error('rtl.run module "'+module_name+'" missing');
  107. rtl.loadintf(module);
  108. rtl.loadimpl(module);
  109. if ((module_name=='program') || (module_name=='library')){
  110. if (rtl.debug_load_units) rtl.debug('running $main');
  111. var r = pas[module_name].$main();
  112. if (rtl.isNumber(r)) rtl.exitcode = r;
  113. }
  114. } catch(re) {
  115. if (!rtl.showUncaughtExceptions) {
  116. throw re
  117. } else {
  118. if (!rtl.handleUncaughtException(re)) {
  119. rtl.showException(re);
  120. rtl.exitcode = 216;
  121. }
  122. }
  123. }
  124. return rtl.exitcode;
  125. },
  126. showException : function (re) {
  127. var errMsg = rtl.hasString(re.$classname) ? re.$classname : '';
  128. errMsg += ((errMsg) ? ': ' : '') + (re.hasOwnProperty('fMessage') ? re.fMessage : re);
  129. alert('Uncaught Exception : '+errMsg);
  130. },
  131. handleUncaughtException: function (e) {
  132. if (rtl.onUncaughtException) {
  133. try {
  134. rtl.onUncaughtException(e);
  135. return true;
  136. } catch (ee) {
  137. return false;
  138. }
  139. } else {
  140. return false;
  141. }
  142. },
  143. loadintf: function(module){
  144. if (module.$state>rtl.m_loading_intf) return; // already finished
  145. if (rtl.debug_load_units) rtl.debug('loadintf: "'+module.$name+'"');
  146. if (module.$state===rtl.m_loading_intf)
  147. rtl.error('unit cycle detected "'+module.$name+'"');
  148. module.$state=rtl.m_loading_intf;
  149. // load interfaces of interface useslist
  150. rtl.loaduseslist(module,module.$intfuseslist,rtl.loadintf);
  151. // run interface
  152. if (rtl.debug_load_units) rtl.debug('loadintf: run intf of "'+module.$name+'"');
  153. module.$intfcode(module.$intfuseslist);
  154. // success
  155. module.$state=rtl.m_intf_loaded;
  156. // Note: units only used in implementations are not yet loaded (not even their interfaces)
  157. },
  158. loaduseslist: function(module,useslist,f){
  159. if (useslist==undefined) return;
  160. var len = useslist.length;
  161. for (var i = 0; i<len; i++) {
  162. var unitname=useslist[i];
  163. if (rtl.debug_load_units) rtl.debug('loaduseslist of "'+module.$name+'" uses="'+unitname+'"');
  164. if (pas[unitname]==undefined)
  165. rtl.error('module "'+module.$name+'" misses "'+unitname+'"');
  166. f(pas[unitname]);
  167. }
  168. },
  169. loadimpl: function(module){
  170. if (module.$state>=rtl.m_loading_impl) return; // already processing
  171. if (module.$state<rtl.m_intf_loaded) rtl.error('loadimpl: interface not loaded of "'+module.$name+'"');
  172. if (rtl.debug_load_units) rtl.debug('loadimpl: load uses of "'+module.$name+'"');
  173. module.$state=rtl.m_loading_impl;
  174. // load interfaces of implementation useslist
  175. rtl.loaduseslist(module,module.$impluseslist,rtl.loadintf);
  176. // load implementation of interfaces useslist
  177. rtl.loaduseslist(module,module.$intfuseslist,rtl.loadimpl);
  178. // load implementation of implementation useslist
  179. rtl.loaduseslist(module,module.$impluseslist,rtl.loadimpl);
  180. // Note: At this point all interfaces used by this unit are loaded. If
  181. // there are implementation uses cycles some used units might not yet be
  182. // initialized. This is by design.
  183. // run implementation
  184. if (rtl.debug_load_units) rtl.debug('loadimpl: run impl of "'+module.$name+'"');
  185. if (rtl.isFunction(module.$implcode)) module.$implcode(module.$impluseslist);
  186. // run initialization
  187. if (rtl.debug_load_units) rtl.debug('loadimpl: run init of "'+module.$name+'"');
  188. module.$state=rtl.m_initializing;
  189. if (rtl.isFunction(module.$init)) module.$init();
  190. // unit initialized
  191. module.$state=rtl.m_initialized;
  192. },
  193. createCallback: function(scope, fn){
  194. var cb;
  195. if (typeof(fn)==='string'){
  196. if (!scope.hasOwnProperty('$events')) scope.$events = {};
  197. cb = scope.$events[fn];
  198. if (cb) return cb;
  199. scope.$events[fn] = cb = function(){
  200. return scope[fn].apply(scope,arguments);
  201. };
  202. } else {
  203. cb = function(){
  204. return fn.apply(scope,arguments);
  205. };
  206. };
  207. cb.scope = scope;
  208. cb.fn = fn;
  209. return cb;
  210. },
  211. createSafeCallback: function(scope, fn){
  212. var cb;
  213. if (typeof(fn)==='string'){
  214. if (!scope[fn]) return null;
  215. if (!scope.hasOwnProperty('$events')) scope.$events = {};
  216. cb = scope.$events[fn];
  217. if (cb) return cb;
  218. scope.$events[fn] = cb = function(){
  219. try{
  220. return scope[fn].apply(scope,arguments);
  221. } catch (err) {
  222. if (!rtl.handleUncaughtException(err)) throw err;
  223. }
  224. };
  225. } else if(!fn) {
  226. return null;
  227. } else {
  228. cb = function(){
  229. try{
  230. return fn.apply(scope,arguments);
  231. } catch (err) {
  232. if (!rtl.handleUncaughtException(err)) throw err;
  233. }
  234. };
  235. };
  236. cb.scope = scope;
  237. cb.fn = fn;
  238. return cb;
  239. },
  240. eqCallback: function(a,b){
  241. // can be a function or a function wrapper
  242. if (a===b){
  243. return true;
  244. } else {
  245. return (a!=null) && (b!=null) && (a.fn) && (a.scope===b.scope) && (a.fn===b.fn);
  246. }
  247. },
  248. initStruct: function(c,parent,name){
  249. if ((parent.$module) && (parent.$module.$impl===parent)) parent=parent.$module;
  250. c.$parent = parent;
  251. if (rtl.isModule(parent)){
  252. c.$module = parent;
  253. c.$name = name;
  254. } else {
  255. c.$module = parent.$module;
  256. c.$name = parent.$name+'.'+name;
  257. };
  258. return parent;
  259. },
  260. initClass: function(c,parent,name,initfn,rttiname){
  261. parent[name] = c;
  262. c.$class = c; // Note: o.$class === Object.getPrototypeOf(o)
  263. c.$classname = rttiname?rttiname:name;
  264. parent = rtl.initStruct(c,parent,name);
  265. c.$fullname = parent.$name+'.'+name;
  266. // rtti
  267. if (rtl.debug_rtti) rtl.debug('initClass '+c.$fullname);
  268. var t = c.$module.$rtti.$Class(c.$classname,{ "class": c });
  269. c.$rtti = t;
  270. if (rtl.isObject(c.$ancestor)) t.ancestor = c.$ancestor.$rtti;
  271. if (!t.ancestor) t.ancestor = null;
  272. // init members
  273. initfn.call(c);
  274. },
  275. createClass: function(parent,name,ancestor,initfn,rttiname){
  276. // create a normal class,
  277. // ancestor must be null or a normal class,
  278. // the root ancestor can be an external class
  279. var c = null;
  280. if (ancestor != null){
  281. c = Object.create(ancestor);
  282. c.$ancestor = ancestor;
  283. // Note:
  284. // if root is an "object" then c.$ancestor === Object.getPrototypeOf(c)
  285. // if root is a "function" then c.$ancestor === c.__proto__, Object.getPrototypeOf(c) returns the root
  286. } else {
  287. c = { $ancestor: null };
  288. c.$create = function(fn,args){
  289. if (args == undefined) args = [];
  290. var o = Object.create(this);
  291. o.$init();
  292. try{
  293. if (typeof(fn)==="string"){
  294. o[fn].apply(o,args);
  295. } else {
  296. fn.apply(o,args);
  297. };
  298. o.AfterConstruction();
  299. } catch($e){
  300. // do not call BeforeDestruction
  301. if (o.Destroy) o.Destroy();
  302. o.$final();
  303. throw $e;
  304. }
  305. return o;
  306. };
  307. c.$destroy = function(fnname){
  308. this.BeforeDestruction();
  309. if (this[fnname]) this[fnname]();
  310. this.$final();
  311. };
  312. };
  313. rtl.initClass(c,parent,name,initfn,rttiname);
  314. },
  315. createClassExt: function(parent,name,ancestor,newinstancefnname,initfn,rttiname){
  316. // Create a class using an external ancestor.
  317. // If newinstancefnname is given, use that function to create the new object.
  318. // If exist call BeforeDestruction and AfterConstruction.
  319. var isFunc = rtl.isFunction(ancestor);
  320. var c = null;
  321. if (isFunc){
  322. // create pascal class descendent from JS function
  323. c = Object.create(ancestor.prototype);
  324. c.$ancestorfunc = ancestor;
  325. c.$ancestor = null; // no pascal ancestor
  326. } else if (ancestor.$func){
  327. // create pascal class descendent from a pascal class descendent of a JS function
  328. isFunc = true;
  329. c = Object.create(ancestor);
  330. c.$ancestor = ancestor;
  331. } else {
  332. c = Object.create(ancestor);
  333. c.$ancestor = null; // no pascal ancestor
  334. }
  335. c.$create = function(fn,args){
  336. if (args == undefined) args = [];
  337. var o = null;
  338. if (newinstancefnname.length>0){
  339. o = this[newinstancefnname](fn,args);
  340. } else if(isFunc) {
  341. o = new this.$func(args);
  342. } else {
  343. o = Object.create(c);
  344. }
  345. if (o.$init) o.$init();
  346. try{
  347. if (typeof(fn)==="string"){
  348. this[fn].apply(o,args);
  349. } else {
  350. fn.apply(o,args);
  351. };
  352. if (o.AfterConstruction) o.AfterConstruction();
  353. } catch($e){
  354. // do not call BeforeDestruction
  355. if (o.Destroy) o.Destroy();
  356. if (o.$final) o.$final();
  357. throw $e;
  358. }
  359. return o;
  360. };
  361. c.$destroy = function(fnname){
  362. if (this.BeforeDestruction) this.BeforeDestruction();
  363. if (this[fnname]) this[fnname]();
  364. if (this.$final) this.$final();
  365. };
  366. rtl.initClass(c,parent,name,initfn,rttiname);
  367. if (isFunc){
  368. function f(){}
  369. f.prototype = c;
  370. c.$func = f;
  371. }
  372. },
  373. createHelper: function(parent,name,ancestor,initfn,rttiname){
  374. // create a helper,
  375. // ancestor must be null or a helper,
  376. var c = null;
  377. if (ancestor != null){
  378. c = Object.create(ancestor);
  379. c.$ancestor = ancestor;
  380. // c.$ancestor === Object.getPrototypeOf(c)
  381. } else {
  382. c = { $ancestor: null };
  383. };
  384. parent[name] = c;
  385. c.$class = c; // Note: o.$class === Object.getPrototypeOf(o)
  386. c.$classname = rttiname?rttiname:name;
  387. parent = rtl.initStruct(c,parent,name);
  388. c.$fullname = parent.$name+'.'+name;
  389. // rtti
  390. var t = c.$module.$rtti.$Helper(c.$classname,{ "helper": c });
  391. c.$rtti = t;
  392. if (rtl.isObject(ancestor)) t.ancestor = ancestor.$rtti;
  393. if (!t.ancestor) t.ancestor = null;
  394. // init members
  395. initfn.call(c);
  396. },
  397. tObjectDestroy: "Destroy",
  398. free: function(obj,name){
  399. if (obj[name]==null) return null;
  400. obj[name].$destroy(rtl.tObjectDestroy);
  401. obj[name]=null;
  402. },
  403. freeLoc: function(obj){
  404. if (obj==null) return null;
  405. obj.$destroy(rtl.tObjectDestroy);
  406. return null;
  407. },
  408. hideProp: function(o,p,v){
  409. Object.defineProperty(o,p, {
  410. enumerable: false,
  411. configurable: true,
  412. writable: true
  413. });
  414. if(arguments.length>2){ o[p]=v; }
  415. },
  416. recNewT: function(parent,name,initfn,full){
  417. // create new record type
  418. var t = {};
  419. if (parent) parent[name] = t;
  420. var h = rtl.hideProp;
  421. if (full){
  422. rtl.initStruct(t,parent,name);
  423. t.$record = t;
  424. h(t,'$record');
  425. h(t,'$name');
  426. h(t,'$parent');
  427. h(t,'$module');
  428. h(t,'$initSpec');
  429. }
  430. initfn.call(t);
  431. if (!t.$new){
  432. t.$new = function(){ return Object.create(t); };
  433. }
  434. t.$clone = function(r){ return t.$new().$assign(r); };
  435. h(t,'$new');
  436. h(t,'$clone');
  437. h(t,'$eq');
  438. h(t,'$assign');
  439. return t;
  440. },
  441. is: function(instance,type){
  442. return type.isPrototypeOf(instance) || (instance===type);
  443. },
  444. isExt: function(instance,type,mode){
  445. // mode===1 means instance must be a Pascal class instance
  446. // mode===2 means instance must be a Pascal class
  447. // Notes:
  448. // isPrototypeOf and instanceof return false on equal
  449. // isPrototypeOf does not work for Date.isPrototypeOf(new Date())
  450. // so if isPrototypeOf is false test with instanceof
  451. // instanceof needs a function on right side
  452. if (instance == null) return false; // Note: ==null checks for undefined too
  453. if ((typeof(type) !== 'object') && (typeof(type) !== 'function')) return false;
  454. if (instance === type){
  455. if (mode===1) return false;
  456. if (mode===2) return rtl.isPasClass(instance);
  457. return true;
  458. }
  459. if (type.isPrototypeOf && type.isPrototypeOf(instance)){
  460. if (mode===1) return rtl.isPasClassInstance(instance);
  461. if (mode===2) return rtl.isPasClass(instance);
  462. return true;
  463. }
  464. if ((typeof type == 'function') && (instance instanceof type)) return true;
  465. return false;
  466. },
  467. Exception: null,
  468. EInvalidCast: null,
  469. EAbstractError: null,
  470. ERangeError: null,
  471. EIntOverflow: null,
  472. EPropWriteOnly: null,
  473. raiseE: function(typename){
  474. var t = rtl[typename];
  475. if (t==null){
  476. var mod = pas.SysUtils;
  477. if (!mod) mod = pas.sysutils;
  478. if (mod){
  479. t = mod[typename];
  480. if (!t) t = mod[typename.toLowerCase()];
  481. if (!t) t = mod['Exception'];
  482. if (!t) t = mod['exception'];
  483. }
  484. }
  485. if (t){
  486. if (t.Create){
  487. throw t.$create("Create");
  488. } else if (t.create){
  489. throw t.$create("create");
  490. }
  491. }
  492. if (typename === "EInvalidCast") throw "invalid type cast";
  493. if (typename === "EAbstractError") throw "Abstract method called";
  494. if (typename === "ERangeError") throw "range error";
  495. throw typename;
  496. },
  497. as: function(instance,type){
  498. if((instance === null) || rtl.is(instance,type)) return instance;
  499. rtl.raiseE("EInvalidCast");
  500. },
  501. asExt: function(instance,type,mode){
  502. if((instance === null) || rtl.isExt(instance,type,mode)) return instance;
  503. rtl.raiseE("EInvalidCast");
  504. },
  505. createInterface: function(module, name, guid, fnnames, ancestor, initfn, rttiname){
  506. //console.log('createInterface name="'+name+'" guid="'+guid+'" names='+fnnames);
  507. var i = ancestor?Object.create(ancestor):{};
  508. module[name] = i;
  509. i.$module = module;
  510. i.$name = rttiname?rttiname:name;
  511. i.$fullname = module.$name+'.'+i.$name;
  512. i.$guid = guid;
  513. i.$guidr = null;
  514. i.$names = fnnames?fnnames:[];
  515. if (rtl.isFunction(initfn)){
  516. // rtti
  517. if (rtl.debug_rtti) rtl.debug('createInterface '+i.$fullname);
  518. var t = i.$module.$rtti.$Interface(i.$name,{ "interface": i, module: module });
  519. i.$rtti = t;
  520. if (ancestor) t.ancestor = ancestor.$rtti;
  521. if (!t.ancestor) t.ancestor = null;
  522. initfn.call(i);
  523. }
  524. return i;
  525. },
  526. strToGUIDR: function(s,g){
  527. var p = 0;
  528. function n(l){
  529. var h = s.substr(p,l);
  530. p+=l;
  531. return parseInt(h,16);
  532. }
  533. p+=1; // skip {
  534. g.D1 = n(8);
  535. p+=1; // skip -
  536. g.D2 = n(4);
  537. p+=1; // skip -
  538. g.D3 = n(4);
  539. p+=1; // skip -
  540. if (!g.D4) g.D4=[];
  541. g.D4[0] = n(2);
  542. g.D4[1] = n(2);
  543. p+=1; // skip -
  544. for(var i=2; i<8; i++) g.D4[i] = n(2);
  545. return g;
  546. },
  547. guidrToStr: function(g){
  548. if (g.$intf) return g.$intf.$guid;
  549. var h = rtl.hexStr;
  550. var s='{'+h(g.D1,8)+'-'+h(g.D2,4)+'-'+h(g.D3,4)+'-'+h(g.D4[0],2)+h(g.D4[1],2)+'-';
  551. for (var i=2; i<8; i++) s+=h(g.D4[i],2);
  552. s+='}';
  553. return s;
  554. },
  555. createTGUID: function(guid){
  556. var TGuid = (pas.System)?pas.System.TGuid:pas.system.tguid;
  557. var g = rtl.strToGUIDR(guid,TGuid.$new());
  558. return g;
  559. },
  560. getIntfGUIDR: function(intfTypeOrVar){
  561. if (!intfTypeOrVar) return null;
  562. if (!intfTypeOrVar.$guidr){
  563. var g = rtl.createTGUID(intfTypeOrVar.$guid);
  564. if (!intfTypeOrVar.hasOwnProperty('$guid')) intfTypeOrVar = Object.getPrototypeOf(intfTypeOrVar);
  565. g.$intf = intfTypeOrVar;
  566. intfTypeOrVar.$guidr = g;
  567. }
  568. return intfTypeOrVar.$guidr;
  569. },
  570. addIntf: function (aclass, intf, map){
  571. function jmp(fn){
  572. if (typeof(fn)==="function"){
  573. return function(){ return fn.apply(this.$o,arguments); };
  574. } else {
  575. return function(){ rtl.raiseE('EAbstractError'); };
  576. }
  577. }
  578. if(!map) map = {};
  579. var t = intf;
  580. var item = Object.create(t);
  581. if (!aclass.hasOwnProperty('$intfmaps')) aclass.$intfmaps = {};
  582. aclass.$intfmaps[intf.$guid] = item;
  583. do{
  584. var names = t.$names;
  585. if (!names) break;
  586. for (var i=0; i<names.length; i++){
  587. var intfname = names[i];
  588. var fnname = map[intfname];
  589. if (!fnname) fnname = intfname;
  590. //console.log('addIntf: intftype='+t.$name+' index='+i+' intfname="'+intfname+'" fnname="'+fnname+'" old='+typeof(item[intfname]));
  591. item[intfname] = jmp(aclass[fnname]);
  592. }
  593. t = Object.getPrototypeOf(t);
  594. }while(t!=null);
  595. },
  596. getIntfG: function (obj, guid, query){
  597. if (!obj) return null;
  598. //console.log('getIntfG: obj='+obj.$classname+' guid='+guid+' query='+query);
  599. // search
  600. var maps = obj.$intfmaps;
  601. if (!maps) return null;
  602. var item = maps[guid];
  603. if (!item) return null;
  604. // check delegation
  605. //console.log('getIntfG: obj='+obj.$classname+' guid='+guid+' query='+query+' item='+typeof(item));
  606. if (typeof item === 'function') return item.call(obj); // delegate. Note: COM contains _AddRef
  607. // check cache
  608. var intf = null;
  609. if (obj.$interfaces){
  610. intf = obj.$interfaces[guid];
  611. //console.log('getIntfG: obj='+obj.$classname+' guid='+guid+' cache='+typeof(intf));
  612. }
  613. if (!intf){ // intf can be undefined!
  614. intf = Object.create(item);
  615. intf.$o = obj;
  616. if (!obj.$interfaces) obj.$interfaces = {};
  617. obj.$interfaces[guid] = intf;
  618. }
  619. if (typeof(query)==='object'){
  620. // called by queryIntfT
  621. var o = null;
  622. if (intf.QueryInterface(rtl.getIntfGUIDR(query),
  623. {get:function(){ return o; }, set:function(v){ o=v; }}) === 0){
  624. return o;
  625. } else {
  626. return null;
  627. }
  628. } else if(query===2){
  629. // called by TObject.GetInterfaceByStr
  630. if (intf.$kind === 'com') intf._AddRef();
  631. }
  632. return intf;
  633. },
  634. getIntfT: function(obj,intftype){
  635. return rtl.getIntfG(obj,intftype.$guid);
  636. },
  637. queryIntfT: function(obj,intftype){
  638. return rtl.getIntfG(obj,intftype.$guid,intftype);
  639. },
  640. queryIntfIsT: function(obj,intftype){
  641. var i = rtl.getIntfG(obj,intftype.$guid);
  642. if (!i) return false;
  643. if (i.$kind === 'com') i._Release();
  644. return true;
  645. },
  646. asIntfT: function (obj,intftype){
  647. var i = rtl.getIntfG(obj,intftype.$guid);
  648. if (i!==null) return i;
  649. rtl.raiseEInvalidCast();
  650. },
  651. intfIsIntfT: function(intf,intftype){
  652. return (intf!==null) && rtl.queryIntfIsT(intf.$o,intftype);
  653. },
  654. intfAsIntfT: function (intf,intftype){
  655. if (!intf) return null;
  656. var i = rtl.getIntfG(intf.$o,intftype.$guid);
  657. if (i) return i;
  658. rtl.raiseEInvalidCast();
  659. },
  660. intfIsClass: function(intf,classtype){
  661. return (intf!=null) && (rtl.is(intf.$o,classtype));
  662. },
  663. intfAsClass: function(intf,classtype){
  664. if (intf==null) return null;
  665. return rtl.as(intf.$o,classtype);
  666. },
  667. intfToClass: function(intf,classtype){
  668. if ((intf!==null) && rtl.is(intf.$o,classtype)) return intf.$o;
  669. return null;
  670. },
  671. // interface reference counting
  672. intfRefs: { // base object for temporary interface variables
  673. ref: function(id,intf){
  674. // called for temporary interface references needing delayed release
  675. var old = this[id];
  676. //console.log('rtl.intfRefs.ref: id='+id+' old="'+(old?old.$name:'null')+'" intf="'+(intf?intf.$name:'null')+' $o='+(intf?intf.$o:'null'));
  677. if (old){
  678. // called again, e.g. in a loop
  679. delete this[id];
  680. old._Release(); // may fail
  681. }
  682. if(intf) {
  683. this[id]=intf;
  684. }
  685. return intf;
  686. },
  687. free: function(){
  688. //console.log('rtl.intfRefs.free...');
  689. for (var id in this){
  690. if (this.hasOwnProperty(id)){
  691. var intf = this[id];
  692. if (intf){
  693. //console.log('rtl.intfRefs.free: id='+id+' '+intf.$name+' $o='+intf.$o.$classname);
  694. intf._Release();
  695. }
  696. }
  697. }
  698. }
  699. },
  700. createIntfRefs: function(){
  701. //console.log('rtl.createIntfRefs');
  702. return Object.create(rtl.intfRefs);
  703. },
  704. setIntfP: function(path,name,value,skipAddRef){
  705. var old = path[name];
  706. //console.log('rtl.setIntfP path='+path+' name='+name+' old="'+(old?old.$name:'null')+'" value="'+(value?value.$name:'null')+'"');
  707. if (old === value) return;
  708. if (old !== null){
  709. path[name]=null;
  710. old._Release();
  711. }
  712. if (value !== null){
  713. if (!skipAddRef) value._AddRef();
  714. path[name]=value;
  715. }
  716. },
  717. setIntfL: function(old,value,skipAddRef){
  718. //console.log('rtl.setIntfL old="'+(old?old.$name:'null')+'" value="'+(value?value.$name:'null')+'"');
  719. if (old !== value){
  720. if (value!==null){
  721. if (!skipAddRef) value._AddRef();
  722. }
  723. if (old!==null){
  724. old._Release(); // Release after AddRef, to avoid double Release if Release creates an exception
  725. }
  726. } else if (skipAddRef){
  727. if (old!==null){
  728. old._Release(); // value has an AddRef
  729. }
  730. }
  731. return value;
  732. },
  733. _AddRef: function(intf){
  734. //if (intf) console.log('rtl._AddRef intf="'+(intf?intf.$name:'null')+'"');
  735. if (intf) intf._AddRef();
  736. return intf;
  737. },
  738. _Release: function(intf){
  739. //if (intf) console.log('rtl._Release intf="'+(intf?intf.$name:'null')+'"');
  740. if (intf) intf._Release();
  741. return intf;
  742. },
  743. _ReleaseArray: function(a,dim){
  744. if (!a) return null;
  745. for (var i=0; i<a.length; i++){
  746. if (dim<=1){
  747. if (a[i]) a[i]._Release();
  748. } else {
  749. rtl._ReleaseArray(a[i],dim-1);
  750. }
  751. }
  752. return null;
  753. },
  754. trunc: function(a){
  755. return a<0 ? Math.ceil(a) : Math.floor(a);
  756. },
  757. checkMethodCall: function(obj,type){
  758. if (rtl.isObject(obj) && rtl.is(obj,type)) return;
  759. rtl.raiseE("EInvalidCast");
  760. },
  761. oc: function(i){
  762. // overflow check integer
  763. if ((Math.floor(i)===i) && (i>=-0x1fffffffffffff) && (i<=0x1fffffffffffff)) return i;
  764. rtl.raiseE('EIntOverflow');
  765. },
  766. rc: function(i,minval,maxval){
  767. // range check integer
  768. if ((Math.floor(i)===i) && (i>=minval) && (i<=maxval)) return i;
  769. rtl.raiseE('ERangeError');
  770. },
  771. rcc: function(c,minval,maxval){
  772. // range check char
  773. if ((typeof(c)==='string') && (c.length===1)){
  774. var i = c.charCodeAt(0);
  775. if ((i>=minval) && (i<=maxval)) return c;
  776. }
  777. rtl.raiseE('ERangeError');
  778. },
  779. rcSetCharAt: function(s,index,c){
  780. // range check setCharAt
  781. if ((typeof(s)!=='string') || (index<0) || (index>=s.length)) rtl.raiseE('ERangeError');
  782. return rtl.setCharAt(s,index,c);
  783. },
  784. rcCharAt: function(s,index){
  785. // range check charAt
  786. if ((typeof(s)!=='string') || (index<0) || (index>=s.length)) rtl.raiseE('ERangeError');
  787. return s.charAt(index);
  788. },
  789. rcArrR: function(arr,index){
  790. // range check read array
  791. if (Array.isArray(arr) && (typeof(index)==='number') && (index>=0) && (index<arr.length)){
  792. if (arguments.length>2){
  793. // arr,index1,index2,...
  794. arr=arr[index];
  795. for (var i=2; i<arguments.length; i++) arr=rtl.rcArrR(arr,arguments[i]);
  796. return arr;
  797. }
  798. return arr[index];
  799. }
  800. rtl.raiseE('ERangeError');
  801. },
  802. rcArrW: function(arr,index,value){
  803. // range check write array
  804. // arr,index1,index2,...,value
  805. for (var i=3; i<arguments.length; i++){
  806. arr=rtl.rcArrR(arr,index);
  807. index=arguments[i-1];
  808. value=arguments[i];
  809. }
  810. if (Array.isArray(arr) && (typeof(index)==='number') && (index>=0) && (index<arr.length)){
  811. return arr[index]=value;
  812. }
  813. rtl.raiseE('ERangeError');
  814. },
  815. length: function(arr){
  816. return (arr == null) ? 0 : arr.length;
  817. },
  818. arrayRef: function(a){
  819. if (a!=null) rtl.hideProp(a,'$pas2jsrefcnt',1);
  820. return a;
  821. },
  822. arraySetLength: function(arr,defaultvalue,newlength){
  823. var stack = [];
  824. var s = 9999;
  825. for (var i=2; i<arguments.length; i++){
  826. var j = arguments[i];
  827. if (j==='s'){ s = i-2; }
  828. else {
  829. stack.push({ dim:j+0, a:null, i:0, src:null });
  830. }
  831. }
  832. var dimmax = stack.length-1;
  833. var depth = 0;
  834. var lastlen = 0;
  835. var item = null;
  836. var a = null;
  837. var src = arr;
  838. var srclen = 0, oldlen = 0;
  839. do{
  840. if (depth>0){
  841. item=stack[depth-1];
  842. src = (item.src && item.src.length>item.i)?item.src[item.i]:null;
  843. }
  844. if (!src){
  845. a = [];
  846. srclen = 0;
  847. oldlen = 0;
  848. } else if (src.$pas2jsrefcnt>0 || depth>=s){
  849. a = [];
  850. srclen = src.length;
  851. oldlen = srclen;
  852. } else {
  853. a = src;
  854. srclen = 0;
  855. oldlen = a.length;
  856. }
  857. lastlen = stack[depth].dim;
  858. a.length = lastlen;
  859. if (depth>0){
  860. item.a[item.i]=a;
  861. item.i++;
  862. if ((lastlen===0) && (item.i<item.a.length)) continue;
  863. }
  864. if (lastlen>0){
  865. if (depth<dimmax){
  866. item = stack[depth];
  867. item.a = a;
  868. item.i = 0;
  869. item.src = src;
  870. depth++;
  871. continue;
  872. } else {
  873. if (srclen>lastlen) srclen=lastlen;
  874. if (rtl.isArray(defaultvalue)){
  875. // array of dyn array
  876. for (var i=0; i<srclen; i++) a[i]=src[i];
  877. for (var i=oldlen; i<lastlen; i++) a[i]=[];
  878. } else if (rtl.isObject(defaultvalue)) {
  879. if (rtl.isTRecord(defaultvalue)){
  880. // array of record
  881. for (var i=0; i<srclen; i++) a[i]=defaultvalue.$clone(src[i]);
  882. for (var i=oldlen; i<lastlen; i++) a[i]=defaultvalue.$new();
  883. } else {
  884. // array of set
  885. for (var i=0; i<srclen; i++) a[i]=rtl.refSet(src[i]);
  886. for (var i=oldlen; i<lastlen; i++) a[i]={};
  887. }
  888. } else {
  889. for (var i=0; i<srclen; i++) a[i]=src[i];
  890. for (var i=oldlen; i<lastlen; i++) a[i]=defaultvalue;
  891. }
  892. }
  893. }
  894. // backtrack
  895. while ((depth>0) && (stack[depth-1].i>=stack[depth-1].dim)){
  896. depth--;
  897. };
  898. if (depth===0){
  899. if (dimmax===0) return a;
  900. return stack[0].a;
  901. }
  902. }while (true);
  903. },
  904. arrayEq: function(a,b){
  905. if (a===null) return b===null;
  906. if (b===null) return false;
  907. if (a.length!==b.length) return false;
  908. for (var i=0; i<a.length; i++) if (a[i]!==b[i]) return false;
  909. return true;
  910. },
  911. arrayClone: function(type,src,srcpos,endpos,dst,dstpos){
  912. // type: 0 for references, "refset" for calling refSet(), a function for new type()
  913. // src must not be null
  914. // This function does not range check.
  915. if(type === 'refSet') {
  916. for (; srcpos<endpos; srcpos++) dst[dstpos++] = rtl.refSet(src[srcpos]); // ref set
  917. } else if (type === 'slice'){
  918. for (; srcpos<endpos; srcpos++) dst[dstpos++] = src[srcpos].slice(0); // clone static array of simple types
  919. } else if (typeof(type)==='function'){
  920. for (; srcpos<endpos; srcpos++) dst[dstpos++] = type(src[srcpos]); // clone function
  921. } else if (rtl.isTRecord(type)){
  922. for (; srcpos<endpos; srcpos++) dst[dstpos++] = type.$clone(src[srcpos]); // clone record
  923. } else {
  924. for (; srcpos<endpos; srcpos++) dst[dstpos++] = src[srcpos]; // reference
  925. };
  926. },
  927. arrayConcat: function(type){
  928. // type: see rtl.arrayClone
  929. var a = [];
  930. var l = 0;
  931. for (var i=1; i<arguments.length; i++){
  932. var src = arguments[i];
  933. if (src !== null) l+=src.length;
  934. };
  935. a.length = l;
  936. l=0;
  937. for (var i=1; i<arguments.length; i++){
  938. var src = arguments[i];
  939. if (src === null) continue;
  940. rtl.arrayClone(type,src,0,src.length,a,l);
  941. l+=src.length;
  942. };
  943. return a;
  944. },
  945. arrayConcatN: function(){
  946. var a = null;
  947. for (var i=0; i<arguments.length; i++){
  948. var src = arguments[i];
  949. if (src === null) continue;
  950. if (a===null){
  951. a=rtl.arrayRef(src); // Note: concat(a) does not clone
  952. } else if (a['$pas2jsrefcnt']){
  953. a=a.concat(src); // clone a and append src
  954. } else {
  955. for (var i=0; i<src.length; i++){
  956. a.push(src[i]);
  957. }
  958. }
  959. };
  960. return a;
  961. },
  962. arrayPush: function(type,a){
  963. if(a===null){
  964. a=[];
  965. } else if (a['$pas2jsrefcnt']){
  966. a=rtl.arrayCopy(type,a,0,a.length);
  967. }
  968. rtl.arrayClone(type,arguments,2,arguments.length,a,a.length);
  969. return a;
  970. },
  971. arrayPushN: function(a){
  972. if(a===null){
  973. a=[];
  974. } else if (a['$pas2jsrefcnt']){
  975. a=a.concat();
  976. }
  977. for (var i=1; i<arguments.length; i++){
  978. a.push(arguments[i]);
  979. }
  980. return a;
  981. },
  982. arrayCopy: function(type, srcarray, index, count){
  983. // type: see rtl.arrayClone
  984. // if count is missing, use srcarray.length
  985. if (srcarray === null) return [];
  986. if (index < 0) index = 0;
  987. if (count === undefined) count=srcarray.length;
  988. var end = index+count;
  989. if (end>srcarray.length) end = srcarray.length;
  990. if (index>=end) return [];
  991. if (type===0){
  992. return srcarray.slice(index,end);
  993. } else {
  994. var a = [];
  995. a.length = end-index;
  996. rtl.arrayClone(type,srcarray,index,end,a,0);
  997. return a;
  998. }
  999. },
  1000. arrayInsert: function(item, arr, index){
  1001. if (arr){
  1002. arr.splice(index,0,item);
  1003. return arr;
  1004. } else {
  1005. return [item];
  1006. }
  1007. },
  1008. setCharAt: function(s,index,c){
  1009. return s.substr(0,index)+c+s.substr(index+1);
  1010. },
  1011. getResStr: function(mod,name){
  1012. var rs = mod.$resourcestrings[name];
  1013. return rs.current?rs.current:rs.org;
  1014. },
  1015. createSet: function(){
  1016. var s = {};
  1017. for (var i=0; i<arguments.length; i++){
  1018. if (arguments[i]!=null){
  1019. s[arguments[i]]=true;
  1020. } else {
  1021. var first=arguments[i+=1];
  1022. var last=arguments[i+=1];
  1023. for(var j=first; j<=last; j++) s[j]=true;
  1024. }
  1025. }
  1026. return s;
  1027. },
  1028. cloneSet: function(s){
  1029. var r = {};
  1030. for (var key in s) r[key]=true;
  1031. return r;
  1032. },
  1033. refSet: function(s){
  1034. rtl.hideProp(s,'$shared',true);
  1035. return s;
  1036. },
  1037. includeSet: function(s,enumvalue){
  1038. if (s.$shared) s = rtl.cloneSet(s);
  1039. s[enumvalue] = true;
  1040. return s;
  1041. },
  1042. excludeSet: function(s,enumvalue){
  1043. if (s.$shared) s = rtl.cloneSet(s);
  1044. delete s[enumvalue];
  1045. return s;
  1046. },
  1047. diffSet: function(s,t){
  1048. var r = {};
  1049. for (var key in s) if (!t[key]) r[key]=true;
  1050. return r;
  1051. },
  1052. unionSet: function(s,t){
  1053. var r = {};
  1054. for (var key in s) r[key]=true;
  1055. for (var key in t) r[key]=true;
  1056. return r;
  1057. },
  1058. intersectSet: function(s,t){
  1059. var r = {};
  1060. for (var key in s) if (t[key]) r[key]=true;
  1061. return r;
  1062. },
  1063. symDiffSet: function(s,t){
  1064. var r = {};
  1065. for (var key in s) if (!t[key]) r[key]=true;
  1066. for (var key in t) if (!s[key]) r[key]=true;
  1067. return r;
  1068. },
  1069. eqSet: function(s,t){
  1070. for (var key in s) if (!t[key]) return false;
  1071. for (var key in t) if (!s[key]) return false;
  1072. return true;
  1073. },
  1074. neSet: function(s,t){
  1075. return !rtl.eqSet(s,t);
  1076. },
  1077. leSet: function(s,t){
  1078. for (var key in s) if (!t[key]) return false;
  1079. return true;
  1080. },
  1081. geSet: function(s,t){
  1082. for (var key in t) if (!s[key]) return false;
  1083. return true;
  1084. },
  1085. strSetLength: function(s,newlen){
  1086. var oldlen = s.length;
  1087. if (oldlen > newlen){
  1088. return s.substring(0,newlen);
  1089. } else if (s.repeat){
  1090. // Note: repeat needs ECMAScript6!
  1091. return s+' '.repeat(newlen-oldlen);
  1092. } else {
  1093. while (oldlen<newlen){
  1094. s+=' ';
  1095. oldlen++;
  1096. };
  1097. return s;
  1098. }
  1099. },
  1100. spaceLeft: function(s,width){
  1101. var l=s.length;
  1102. if (l>=width) return s;
  1103. if (s.repeat){
  1104. // Note: repeat needs ECMAScript6!
  1105. return ' '.repeat(width-l) + s;
  1106. } else {
  1107. while (l<width){
  1108. s=' '+s;
  1109. l++;
  1110. };
  1111. return s;
  1112. };
  1113. },
  1114. floatToStr: function(d,w,p){
  1115. // input 1-3 arguments: double, width, precision
  1116. if (arguments.length>2){
  1117. return rtl.spaceLeft(d.toFixed(p),w);
  1118. } else {
  1119. // exponent width
  1120. var pad = "";
  1121. var ad = Math.abs(d);
  1122. if (((ad>1) && (ad<1.0e+10)) || ((ad>1.e-10) && (ad<1))) {
  1123. pad='00';
  1124. } else if ((ad>1) && (ad<1.0e+100) || (ad<1.e-10)) {
  1125. pad='0';
  1126. }
  1127. if (arguments.length<2) {
  1128. w=24;
  1129. } else if (w<9) {
  1130. w=9;
  1131. }
  1132. var p = w-8;
  1133. var s=(d>0 ? " " : "" ) + d.toExponential(p);
  1134. s=s.replace(/e(.)/,'E$1'+pad);
  1135. return rtl.spaceLeft(s,w);
  1136. }
  1137. },
  1138. valEnum: function(s, enumType, setCodeFn){
  1139. s = s.toLowerCase();
  1140. for (var key in enumType){
  1141. if((typeof(key)==='string') && (key.toLowerCase()===s)){
  1142. setCodeFn(0);
  1143. return enumType[key];
  1144. }
  1145. }
  1146. setCodeFn(1);
  1147. return 0;
  1148. },
  1149. lw: function(l){
  1150. // fix longword bitwise operation
  1151. return l<0?l+0x100000000:l;
  1152. },
  1153. and: function(a,b){
  1154. var hi = 0x80000000;
  1155. var low = 0x7fffffff;
  1156. var h = (a / hi) & (b / hi);
  1157. var l = (a & low) & (b & low);
  1158. return h*hi + l;
  1159. },
  1160. or: function(a,b){
  1161. var hi = 0x80000000;
  1162. var low = 0x7fffffff;
  1163. var h = (a / hi) | (b / hi);
  1164. var l = (a & low) | (b & low);
  1165. return h*hi + l;
  1166. },
  1167. xor: function(a,b){
  1168. var hi = 0x80000000;
  1169. var low = 0x7fffffff;
  1170. var h = (a / hi) ^ (b / hi);
  1171. var l = (a & low) ^ (b & low);
  1172. return h*hi + l;
  1173. },
  1174. shr: function(a,b){
  1175. if (a<0) a += rtl.hiInt;
  1176. if (a<0x80000000) return a >> b;
  1177. if (b<=0) return a;
  1178. if (b>54) return 0;
  1179. return Math.floor(a / Math.pow(2,b));
  1180. },
  1181. shl: function(a,b){
  1182. if (a<0) a += rtl.hiInt;
  1183. if (b<=0) return a;
  1184. if (b>54) return 0;
  1185. var r = a * Math.pow(2,b);
  1186. if (r <= rtl.hiInt) return r;
  1187. return r % rtl.hiInt;
  1188. },
  1189. initRTTI: function(){
  1190. if (rtl.debug_rtti) rtl.debug('initRTTI');
  1191. // base types
  1192. rtl.tTypeInfo = { name: "tTypeInfo", kind: 0, $module: null, attr: null };
  1193. function newBaseTI(name,kind,ancestor){
  1194. if (!ancestor) ancestor = rtl.tTypeInfo;
  1195. if (rtl.debug_rtti) rtl.debug('initRTTI.newBaseTI "'+name+'" '+kind+' ("'+ancestor.name+'")');
  1196. var t = Object.create(ancestor);
  1197. t.name = name;
  1198. t.kind = kind;
  1199. rtl[name] = t;
  1200. return t;
  1201. };
  1202. function newBaseInt(name,minvalue,maxvalue,ordtype){
  1203. var t = newBaseTI(name,1 /* tkInteger */,rtl.tTypeInfoInteger);
  1204. t.minvalue = minvalue;
  1205. t.maxvalue = maxvalue;
  1206. t.ordtype = ordtype;
  1207. return t;
  1208. };
  1209. newBaseTI("tTypeInfoInteger",1 /* tkInteger */);
  1210. newBaseInt("shortint",-0x80,0x7f,0);
  1211. newBaseInt("byte",0,0xff,1);
  1212. newBaseInt("smallint",-0x8000,0x7fff,2);
  1213. newBaseInt("word",0,0xffff,3);
  1214. newBaseInt("longint",-0x80000000,0x7fffffff,4);
  1215. newBaseInt("longword",0,0xffffffff,5);
  1216. newBaseInt("nativeint",-0x10000000000000,0xfffffffffffff,6);
  1217. newBaseInt("nativeuint",0,0xfffffffffffff,7);
  1218. newBaseTI("char",2 /* tkChar */);
  1219. newBaseTI("string",3 /* tkString */);
  1220. newBaseTI("tTypeInfoEnum",4 /* tkEnumeration */,rtl.tTypeInfoInteger);
  1221. newBaseTI("tTypeInfoSet",5 /* tkSet */);
  1222. newBaseTI("double",6 /* tkDouble */);
  1223. newBaseTI("boolean",7 /* tkBool */);
  1224. newBaseTI("tTypeInfoProcVar",8 /* tkProcVar */);
  1225. newBaseTI("tTypeInfoMethodVar",9 /* tkMethod */,rtl.tTypeInfoProcVar);
  1226. newBaseTI("tTypeInfoArray",10 /* tkArray */);
  1227. newBaseTI("tTypeInfoDynArray",11 /* tkDynArray */);
  1228. newBaseTI("tTypeInfoPointer",15 /* tkPointer */);
  1229. var t = newBaseTI("pointer",15 /* tkPointer */,rtl.tTypeInfoPointer);
  1230. t.reftype = null;
  1231. newBaseTI("jsvalue",16 /* tkJSValue */);
  1232. newBaseTI("tTypeInfoRefToProcVar",17 /* tkRefToProcVar */,rtl.tTypeInfoProcVar);
  1233. // member kinds
  1234. rtl.tTypeMember = { attr: null };
  1235. function newMember(name,kind){
  1236. var m = Object.create(rtl.tTypeMember);
  1237. m.name = name;
  1238. m.kind = kind;
  1239. rtl[name] = m;
  1240. };
  1241. newMember("tTypeMemberField",1); // tmkField
  1242. newMember("tTypeMemberMethod",2); // tmkMethod
  1243. newMember("tTypeMemberProperty",3); // tmkProperty
  1244. // base object for storing members: a simple object
  1245. rtl.tTypeMembers = {};
  1246. // tTypeInfoStruct - base object for tTypeInfoClass, tTypeInfoRecord, tTypeInfoInterface
  1247. var tis = newBaseTI("tTypeInfoStruct",0);
  1248. tis.$addMember = function(name,ancestor,options){
  1249. if (rtl.debug_rtti){
  1250. if (!rtl.hasString(name) || (name.charAt()==='$')) throw 'invalid member "'+name+'", this="'+this.name+'"';
  1251. if (!rtl.is(ancestor,rtl.tTypeMember)) throw 'invalid ancestor "'+ancestor+':'+ancestor.name+'", "'+this.name+'.'+name+'"';
  1252. if ((options!=undefined) && (typeof(options)!='object')) throw 'invalid options "'+options+'", "'+this.name+'.'+name+'"';
  1253. };
  1254. var t = Object.create(ancestor);
  1255. t.name = name;
  1256. this.members[name] = t;
  1257. this.names.push(name);
  1258. if (rtl.isObject(options)){
  1259. for (var key in options) if (options.hasOwnProperty(key)) t[key] = options[key];
  1260. };
  1261. return t;
  1262. };
  1263. tis.addField = function(name,type,options){
  1264. var t = this.$addMember(name,rtl.tTypeMemberField,options);
  1265. if (rtl.debug_rtti){
  1266. if (!rtl.is(type,rtl.tTypeInfo)) throw 'invalid type "'+type+'", "'+this.name+'.'+name+'"';
  1267. };
  1268. t.typeinfo = type;
  1269. this.fields.push(name);
  1270. return t;
  1271. };
  1272. tis.addFields = function(){
  1273. var i=0;
  1274. while(i<arguments.length){
  1275. var name = arguments[i++];
  1276. var type = arguments[i++];
  1277. if ((i<arguments.length) && (typeof(arguments[i])==='object')){
  1278. this.addField(name,type,arguments[i++]);
  1279. } else {
  1280. this.addField(name,type);
  1281. };
  1282. };
  1283. };
  1284. tis.addMethod = function(name,methodkind,params,result,flags,options){
  1285. var t = this.$addMember(name,rtl.tTypeMemberMethod,options);
  1286. t.methodkind = methodkind;
  1287. t.procsig = rtl.newTIProcSig(params,result,flags);
  1288. this.methods.push(name);
  1289. return t;
  1290. };
  1291. tis.addProperty = function(name,flags,result,getter,setter,options){
  1292. var t = this.$addMember(name,rtl.tTypeMemberProperty,options);
  1293. t.flags = flags;
  1294. t.typeinfo = result;
  1295. t.getter = getter;
  1296. t.setter = setter;
  1297. // Note: in options: params, stored, defaultvalue
  1298. t.params = rtl.isArray(t.params) ? rtl.newTIParams(t.params) : null;
  1299. this.properties.push(name);
  1300. if (!rtl.isString(t.stored)) t.stored = "";
  1301. return t;
  1302. };
  1303. tis.getField = function(index){
  1304. return this.members[this.fields[index]];
  1305. };
  1306. tis.getMethod = function(index){
  1307. return this.members[this.methods[index]];
  1308. };
  1309. tis.getProperty = function(index){
  1310. return this.members[this.properties[index]];
  1311. };
  1312. newBaseTI("tTypeInfoRecord",12 /* tkRecord */,rtl.tTypeInfoStruct);
  1313. newBaseTI("tTypeInfoClass",13 /* tkClass */,rtl.tTypeInfoStruct);
  1314. newBaseTI("tTypeInfoClassRef",14 /* tkClassRef */);
  1315. newBaseTI("tTypeInfoInterface",18 /* tkInterface */,rtl.tTypeInfoStruct);
  1316. newBaseTI("tTypeInfoHelper",19 /* tkHelper */,rtl.tTypeInfoStruct);
  1317. newBaseTI("tTypeInfoExtClass",20 /* tkExtClass */,rtl.tTypeInfoClass);
  1318. },
  1319. tSectionRTTI: {
  1320. $module: null,
  1321. $inherited: function(name,ancestor,o){
  1322. if (rtl.debug_rtti){
  1323. rtl.debug('tSectionRTTI.newTI "'+(this.$module?this.$module.$name:"(no module)")
  1324. +'"."'+name+'" ('+ancestor.name+') '+(o?'init':'forward'));
  1325. };
  1326. var t = this[name];
  1327. if (t){
  1328. if (!t.$forward) throw 'duplicate type "'+name+'"';
  1329. if (!ancestor.isPrototypeOf(t)) throw 'typeinfo ancestor mismatch "'+name+'" ancestor="'+ancestor.name+'" t.name="'+t.name+'"';
  1330. } else {
  1331. t = Object.create(ancestor);
  1332. t.name = name;
  1333. t.$module = this.$module;
  1334. this[name] = t;
  1335. }
  1336. if (o){
  1337. delete t.$forward;
  1338. for (var key in o) if (o.hasOwnProperty(key)) t[key]=o[key];
  1339. } else {
  1340. t.$forward = true;
  1341. }
  1342. return t;
  1343. },
  1344. $Scope: function(name,ancestor,o){
  1345. var t=this.$inherited(name,ancestor,o);
  1346. t.members = {};
  1347. t.names = [];
  1348. t.fields = [];
  1349. t.methods = [];
  1350. t.properties = [];
  1351. return t;
  1352. },
  1353. $TI: function(name,kind,o){ var t=this.$inherited(name,rtl.tTypeInfo,o); t.kind = kind; return t; },
  1354. $Int: function(name,o){ return this.$inherited(name,rtl.tTypeInfoInteger,o); },
  1355. $Enum: function(name,o){ return this.$inherited(name,rtl.tTypeInfoEnum,o); },
  1356. $Set: function(name,o){ return this.$inherited(name,rtl.tTypeInfoSet,o); },
  1357. $StaticArray: function(name,o){ return this.$inherited(name,rtl.tTypeInfoArray,o); },
  1358. $DynArray: function(name,o){ return this.$inherited(name,rtl.tTypeInfoDynArray,o); },
  1359. $ProcVar: function(name,o){ return this.$inherited(name,rtl.tTypeInfoProcVar,o); },
  1360. $RefToProcVar: function(name,o){ return this.$inherited(name,rtl.tTypeInfoRefToProcVar,o); },
  1361. $MethodVar: function(name,o){ return this.$inherited(name,rtl.tTypeInfoMethodVar,o); },
  1362. $Record: function(name,o){ return this.$Scope(name,rtl.tTypeInfoRecord,o); },
  1363. $Class: function(name,o){ return this.$Scope(name,rtl.tTypeInfoClass,o); },
  1364. $ClassRef: function(name,o){ return this.$inherited(name,rtl.tTypeInfoClassRef,o); },
  1365. $Pointer: function(name,o){ return this.$inherited(name,rtl.tTypeInfoPointer,o); },
  1366. $Interface: function(name,o){ return this.$Scope(name,rtl.tTypeInfoInterface,o); },
  1367. $Helper: function(name,o){ return this.$Scope(name,rtl.tTypeInfoHelper,o); },
  1368. $ExtClass: function(name,o){ return this.$Scope(name,rtl.tTypeInfoExtClass,o); }
  1369. },
  1370. newTIParam: function(param){
  1371. // param is an array, 0=name, 1=type, 2=optional flags
  1372. var t = {
  1373. name: param[0],
  1374. typeinfo: param[1],
  1375. flags: (rtl.isNumber(param[2]) ? param[2] : 0)
  1376. };
  1377. return t;
  1378. },
  1379. newTIParams: function(list){
  1380. // list: optional array of [paramname,typeinfo,optional flags]
  1381. var params = [];
  1382. if (rtl.isArray(list)){
  1383. for (var i=0; i<list.length; i++) params.push(rtl.newTIParam(list[i]));
  1384. };
  1385. return params;
  1386. },
  1387. newTIProcSig: function(params,result,flags){
  1388. var s = {
  1389. params: rtl.newTIParams(params),
  1390. resulttype: result?result:null,
  1391. flags: flags?flags:0
  1392. };
  1393. return s;
  1394. },
  1395. addResource: function(aRes){
  1396. rtl.$res[aRes.name]=aRes;
  1397. },
  1398. getResource: function(aName){
  1399. var res = rtl.$res[aName];
  1400. if (res !== undefined) {
  1401. return res;
  1402. } else {
  1403. return null;
  1404. }
  1405. },
  1406. getResourceList: function(){
  1407. return Object.keys(rtl.$res);
  1408. }
  1409. }