rtl.js 42 KB

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