rtl.js 41 KB

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