rtl.js 40 KB

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