rtl.js 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349
  1. var pas = {};
  2. var rtl = {
  3. version: 10410,
  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 = {};
  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 = {};
  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;
  334. obj[name].$destroy(rtl.tObjectDestroy);
  335. obj[name]=null;
  336. },
  337. freeLoc: function(obj){
  338. if (obj==null) return;
  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. EPropWriteOnly: null,
  399. raiseE: function(typename){
  400. var t = rtl[typename];
  401. if (t==null){
  402. var mod = pas.SysUtils;
  403. if (!mod) mod = pas.sysutils;
  404. if (mod){
  405. t = mod[typename];
  406. if (!t) t = mod[typename.toLowerCase()];
  407. if (!t) t = mod['Exception'];
  408. if (!t) t = mod['exception'];
  409. }
  410. }
  411. if (t){
  412. if (t.Create){
  413. throw t.$create("Create");
  414. } else if (t.create){
  415. throw t.$create("create");
  416. }
  417. }
  418. if (typename === "EInvalidCast") throw "invalid type cast";
  419. if (typename === "EAbstractError") throw "Abstract method called";
  420. if (typename === "ERangeError") throw "range error";
  421. throw typename;
  422. },
  423. as: function(instance,type){
  424. if((instance === null) || rtl.is(instance,type)) return instance;
  425. rtl.raiseE("EInvalidCast");
  426. },
  427. asExt: function(instance,type,mode){
  428. if((instance === null) || rtl.isExt(instance,type,mode)) return instance;
  429. rtl.raiseE("EInvalidCast");
  430. },
  431. createInterface: function(module, name, guid, fnnames, ancestor, initfn){
  432. //console.log('createInterface name="'+name+'" guid="'+guid+'" names='+fnnames);
  433. var i = ancestor?Object.create(ancestor):{};
  434. module[name] = i;
  435. i.$module = module;
  436. i.$name = name;
  437. i.$fullname = module.$name+'.'+name;
  438. i.$guid = guid;
  439. i.$guidr = null;
  440. i.$names = fnnames?fnnames:[];
  441. if (rtl.isFunction(initfn)){
  442. // rtti
  443. if (rtl.debug_rtti) rtl.debug('createInterface '+i.$fullname);
  444. var t = i.$module.$rtti.$Interface(name,{ "interface": i, module: module });
  445. i.$rtti = t;
  446. if (ancestor) t.ancestor = ancestor.$rtti;
  447. if (!t.ancestor) t.ancestor = null;
  448. initfn.call(i);
  449. }
  450. return i;
  451. },
  452. strToGUIDR: function(s,g){
  453. var p = 0;
  454. function n(l){
  455. var h = s.substr(p,l);
  456. p+=l;
  457. return parseInt(h,16);
  458. }
  459. p+=1; // skip {
  460. g.D1 = n(8);
  461. p+=1; // skip -
  462. g.D2 = n(4);
  463. p+=1; // skip -
  464. g.D3 = n(4);
  465. p+=1; // skip -
  466. if (!g.D4) g.D4=[];
  467. g.D4[0] = n(2);
  468. g.D4[1] = n(2);
  469. p+=1; // skip -
  470. for(var i=2; i<8; i++) g.D4[i] = n(2);
  471. return g;
  472. },
  473. guidrToStr: function(g){
  474. if (g.$intf) return g.$intf.$guid;
  475. var h = rtl.hexStr;
  476. var s='{'+h(g.D1,8)+'-'+h(g.D2,4)+'-'+h(g.D3,4)+'-'+h(g.D4[0],2)+h(g.D4[1],2)+'-';
  477. for (var i=2; i<8; i++) s+=h(g.D4[i],2);
  478. s+='}';
  479. return s;
  480. },
  481. createTGUID: function(guid){
  482. var TGuid = (pas.System)?pas.System.TGuid:pas.system.tguid;
  483. var g = rtl.strToGUIDR(guid,TGuid.$new());
  484. return g;
  485. },
  486. getIntfGUIDR: function(intfTypeOrVar){
  487. if (!intfTypeOrVar) return null;
  488. if (!intfTypeOrVar.$guidr){
  489. var g = rtl.createTGUID(intfTypeOrVar.$guid);
  490. if (!intfTypeOrVar.hasOwnProperty('$guid')) intfTypeOrVar = Object.getPrototypeOf(intfTypeOrVar);
  491. g.$intf = intfTypeOrVar;
  492. intfTypeOrVar.$guidr = g;
  493. }
  494. return intfTypeOrVar.$guidr;
  495. },
  496. addIntf: function (aclass, intf, map){
  497. function jmp(fn){
  498. if (typeof(fn)==="function"){
  499. return function(){ return fn.apply(this.$o,arguments); };
  500. } else {
  501. return function(){ rtl.raiseE('EAbstractError'); };
  502. }
  503. }
  504. if(!map) map = {};
  505. var t = intf;
  506. var item = Object.create(t);
  507. if (!aclass.hasOwnProperty('$intfmaps')) aclass.$intfmaps = {};
  508. aclass.$intfmaps[intf.$guid] = item;
  509. do{
  510. var names = t.$names;
  511. if (!names) break;
  512. for (var i=0; i<names.length; i++){
  513. var intfname = names[i];
  514. var fnname = map[intfname];
  515. if (!fnname) fnname = intfname;
  516. //console.log('addIntf: intftype='+t.$name+' index='+i+' intfname="'+intfname+'" fnname="'+fnname+'" old='+typeof(item[intfname]));
  517. item[intfname] = jmp(aclass[fnname]);
  518. }
  519. t = Object.getPrototypeOf(t);
  520. }while(t!=null);
  521. },
  522. getIntfG: function (obj, guid, query){
  523. if (!obj) return null;
  524. //console.log('getIntfG: obj='+obj.$classname+' guid='+guid+' query='+query);
  525. // search
  526. var maps = obj.$intfmaps;
  527. if (!maps) return null;
  528. var item = maps[guid];
  529. if (!item) return null;
  530. // check delegation
  531. //console.log('getIntfG: obj='+obj.$classname+' guid='+guid+' query='+query+' item='+typeof(item));
  532. if (typeof item === 'function') return item.call(obj); // delegate. Note: COM contains _AddRef
  533. // check cache
  534. var intf = null;
  535. if (obj.$interfaces){
  536. intf = obj.$interfaces[guid];
  537. //console.log('getIntfG: obj='+obj.$classname+' guid='+guid+' cache='+typeof(intf));
  538. }
  539. if (!intf){ // intf can be undefined!
  540. intf = Object.create(item);
  541. intf.$o = obj;
  542. if (!obj.$interfaces) obj.$interfaces = {};
  543. obj.$interfaces[guid] = intf;
  544. }
  545. if (typeof(query)==='object'){
  546. // called by queryIntfT
  547. var o = null;
  548. if (intf.QueryInterface(rtl.getIntfGUIDR(query),
  549. {get:function(){ return o; }, set:function(v){ o=v; }}) === 0){
  550. return o;
  551. } else {
  552. return null;
  553. }
  554. } else if(query===2){
  555. // called by TObject.GetInterfaceByStr
  556. if (intf.$kind === 'com') intf._AddRef();
  557. }
  558. return intf;
  559. },
  560. getIntfT: function(obj,intftype){
  561. return rtl.getIntfG(obj,intftype.$guid);
  562. },
  563. queryIntfT: function(obj,intftype){
  564. return rtl.getIntfG(obj,intftype.$guid,intftype);
  565. },
  566. queryIntfIsT: function(obj,intftype){
  567. var i = rtl.queryIntfG(obj,intftype.$guid);
  568. if (!i) return false;
  569. if (i.$kind === 'com') i._Release();
  570. return true;
  571. },
  572. asIntfT: function (obj,intftype){
  573. var i = rtl.getIntfG(obj,intftype.$guid);
  574. if (i!==null) return i;
  575. rtl.raiseEInvalidCast();
  576. },
  577. intfIsClass: function(intf,classtype){
  578. return (intf!=null) && (rtl.is(intf.$o,classtype));
  579. },
  580. intfAsClass: function(intf,classtype){
  581. if (intf==null) return null;
  582. return rtl.as(intf.$o,classtype);
  583. },
  584. intfToClass: function(intf,classtype){
  585. if ((intf!==null) && rtl.is(intf.$o,classtype)) return intf.$o;
  586. return null;
  587. },
  588. // interface reference counting
  589. intfRefs: { // base object for temporary interface variables
  590. ref: function(id,intf){
  591. // called for temporary interface references needing delayed release
  592. var old = this[id];
  593. //console.log('rtl.intfRefs.ref: id='+id+' old="'+(old?old.$name:'null')+'" intf="'+(intf?intf.$name:'null')+' $o='+(intf?intf.$o:'null'));
  594. if (old){
  595. // called again, e.g. in a loop
  596. delete this[id];
  597. old._Release(); // may fail
  598. }
  599. this[id]=intf;
  600. return intf;
  601. },
  602. free: function(){
  603. //console.log('rtl.intfRefs.free...');
  604. for (var id in this){
  605. if (this.hasOwnProperty(id)){
  606. //console.log('rtl.intfRefs.free: id='+id+' '+this[id].$name+' $o='+this[id].$o.$classname);
  607. this[id]._Release();
  608. }
  609. }
  610. }
  611. },
  612. createIntfRefs: function(){
  613. //console.log('rtl.createIntfRefs');
  614. return Object.create(rtl.intfRefs);
  615. },
  616. setIntfP: function(path,name,value,skipAddRef){
  617. var old = path[name];
  618. //console.log('rtl.setIntfP path='+path+' name='+name+' old="'+(old?old.$name:'null')+'" value="'+(value?value.$name:'null')+'"');
  619. if (old === value) return;
  620. if (old !== null){
  621. path[name]=null;
  622. old._Release();
  623. }
  624. if (value !== null){
  625. if (!skipAddRef) value._AddRef();
  626. path[name]=value;
  627. }
  628. },
  629. setIntfL: function(old,value,skipAddRef){
  630. //console.log('rtl.setIntfL old="'+(old?old.$name:'null')+'" value="'+(value?value.$name:'null')+'"');
  631. if (old !== value){
  632. if (value!==null){
  633. if (!skipAddRef) value._AddRef();
  634. }
  635. if (old!==null){
  636. old._Release(); // Release after AddRef, to avoid double Release if Release creates an exception
  637. }
  638. } else if (skipAddRef){
  639. if (old!==null){
  640. old._Release(); // value has an AddRef
  641. }
  642. }
  643. return value;
  644. },
  645. _AddRef: function(intf){
  646. //if (intf) console.log('rtl._AddRef intf="'+(intf?intf.$name:'null')+'"');
  647. if (intf) intf._AddRef();
  648. return intf;
  649. },
  650. _Release: function(intf){
  651. //if (intf) console.log('rtl._Release intf="'+(intf?intf.$name:'null')+'"');
  652. if (intf) intf._Release();
  653. return intf;
  654. },
  655. checkMethodCall: function(obj,type){
  656. if (rtl.isObject(obj) && rtl.is(obj,type)) return;
  657. rtl.raiseE("EInvalidCast");
  658. },
  659. rc: function(i,minval,maxval){
  660. // range check integer
  661. if ((Math.floor(i)===i) && (i>=minval) && (i<=maxval)) return i;
  662. rtl.raiseE('ERangeError');
  663. },
  664. rcc: function(c,minval,maxval){
  665. // range check char
  666. if ((typeof(c)==='string') && (c.length===1)){
  667. var i = c.charCodeAt(0);
  668. if ((i>=minval) && (i<=maxval)) return c;
  669. }
  670. rtl.raiseE('ERangeError');
  671. },
  672. rcSetCharAt: function(s,index,c){
  673. // range check setCharAt
  674. if ((typeof(s)!=='string') || (index<0) || (index>=s.length)) rtl.raiseE('ERangeError');
  675. return rtl.setCharAt(s,index,c);
  676. },
  677. rcCharAt: function(s,index){
  678. // range check charAt
  679. if ((typeof(s)!=='string') || (index<0) || (index>=s.length)) rtl.raiseE('ERangeError');
  680. return s.charAt(index);
  681. },
  682. rcArrR: function(arr,index){
  683. // range check read array
  684. if (Array.isArray(arr) && (typeof(index)==='number') && (index>=0) && (index<arr.length)){
  685. if (arguments.length>2){
  686. // arr,index1,index2,...
  687. arr=arr[index];
  688. for (var i=2; i<arguments.length; i++) arr=rtl.rcArrR(arr,arguments[i]);
  689. return arr;
  690. }
  691. return arr[index];
  692. }
  693. rtl.raiseE('ERangeError');
  694. },
  695. rcArrW: function(arr,index,value){
  696. // range check write array
  697. // arr,index1,index2,...,value
  698. for (var i=3; i<arguments.length; i++){
  699. arr=rtl.rcArrR(arr,index);
  700. index=arguments[i-1];
  701. value=arguments[i];
  702. }
  703. if (Array.isArray(arr) && (typeof(index)==='number') && (index>=0) && (index<arr.length)){
  704. return arr[index]=value;
  705. }
  706. rtl.raiseE('ERangeError');
  707. },
  708. length: function(arr){
  709. return (arr == null) ? 0 : arr.length;
  710. },
  711. arraySetLength: function(arr,defaultvalue,newlength){
  712. // multi dim: (arr,defaultvalue,dim1,dim2,...)
  713. var p = arguments;
  714. function setLength(src,argNo){
  715. var newlen = p[argNo];
  716. var a = [];
  717. a.length = newlength;
  718. if (argNo === p.length-1){
  719. var oldlen = src?src.length:0;
  720. if (rtl.isArray(defaultvalue)){
  721. for (var i=0; i<newlen; i++) a[i]=(i<oldlen)?src[i]:[]; // array of dyn array
  722. } else if (rtl.isObject(defaultvalue)) {
  723. if (rtl.isTRecord(defaultvalue)){
  724. for (var i=0; i<newlen; i++)
  725. a[i]=(i<oldlen)?defaultvalue.$clone(src[i]):defaultvalue.$new(); // e.g. record
  726. } else {
  727. for (var i=0; i<newlen; i++) a[i]=(i<oldlen)?rtl.refSet(src[i]):{}; // e.g. set
  728. }
  729. } else {
  730. for (var i=0; i<newlen; i++)
  731. a[i]=(i<oldlen)?src[i]:defaultvalue;
  732. }
  733. } else {
  734. // multi dim array
  735. for (var i=0; i<newlen; i++) a[i]=setLength(src?src[i]:null,argNo+1);
  736. }
  737. return a;
  738. }
  739. return setLength(arr,2);
  740. },
  741. /*arrayChgLength: function(arr,defaultvalue,newlength){
  742. // multi dim: (arr,defaultvalue,dim1,dim2,...)
  743. if (arr == null) arr = [];
  744. var p = arguments;
  745. function setLength(a,argNo){
  746. var oldlen = a.length;
  747. var newlen = p[argNo];
  748. if (oldlen!==newlength){
  749. a.length = newlength;
  750. if (argNo === p.length-1){
  751. if (rtl.isArray(defaultvalue)){
  752. for (var i=oldlen; i<newlen; i++) a[i]=[]; // nested array
  753. } else if (rtl.isObject(defaultvalue)) {
  754. if (rtl.isTRecord(defaultvalue)){
  755. for (var i=oldlen; i<newlen; i++) a[i]=defaultvalue.$new(); // e.g. record
  756. } else {
  757. for (var i=oldlen; i<newlen; i++) a[i]={}; // e.g. set
  758. }
  759. } else {
  760. for (var i=oldlen; i<newlen; i++) a[i]=defaultvalue;
  761. }
  762. } else {
  763. for (var i=oldlen; i<newlen; i++) a[i]=[]; // nested array
  764. }
  765. }
  766. if (argNo < p.length-1){
  767. // multi argNo
  768. for (var i=0; i<newlen; i++) a[i]=setLength(a[i],argNo+1);
  769. }
  770. return a;
  771. }
  772. return setLength(arr,2);
  773. },*/
  774. arrayEq: function(a,b){
  775. if (a===null) return b===null;
  776. if (b===null) return false;
  777. if (a.length!==b.length) return false;
  778. for (var i=0; i<a.length; i++) if (a[i]!==b[i]) return false;
  779. return true;
  780. },
  781. arrayClone: function(type,src,srcpos,endpos,dst,dstpos){
  782. // type: 0 for references, "refset" for calling refSet(), a function for new type()
  783. // src must not be null
  784. // This function does not range check.
  785. if(type === 'refSet') {
  786. for (; srcpos<endpos; srcpos++) dst[dstpos++] = rtl.refSet(src[srcpos]); // ref set
  787. } else if (rtl.isTRecord(type)){
  788. for (; srcpos<endpos; srcpos++) dst[dstpos++] = type.$clone(src[srcpos]); // clone record
  789. } else {
  790. for (; srcpos<endpos; srcpos++) dst[dstpos++] = src[srcpos]; // reference
  791. };
  792. },
  793. arrayConcat: function(type){
  794. // type: see rtl.arrayClone
  795. var a = [];
  796. var l = 0;
  797. for (var i=1; i<arguments.length; i++){
  798. var src = arguments[i];
  799. if (src !== null) l+=src.length;
  800. };
  801. a.length = l;
  802. l=0;
  803. for (var i=1; i<arguments.length; i++){
  804. var src = arguments[i];
  805. if (src === null) continue;
  806. rtl.arrayClone(type,src,0,src.length,a,l);
  807. l+=src.length;
  808. };
  809. return a;
  810. },
  811. arrayConcatN: function(){
  812. var a = null;
  813. for (var i=1; i<arguments.length; i++){
  814. var src = arguments[i];
  815. if (src === null) continue;
  816. if (a===null){
  817. a=src; // Note: concat(a) does not clone
  818. } else {
  819. a=a.concat(src);
  820. }
  821. };
  822. return a;
  823. },
  824. arrayCopy: function(type, srcarray, index, count){
  825. // type: see rtl.arrayClone
  826. // if count is missing, use srcarray.length
  827. if (srcarray === null) return [];
  828. if (index < 0) index = 0;
  829. if (count === undefined) count=srcarray.length;
  830. var end = index+count;
  831. if (end>srcarray.length) end = srcarray.length;
  832. if (index>=end) return [];
  833. if (type===0){
  834. return srcarray.slice(index,end);
  835. } else {
  836. var a = [];
  837. a.length = end-index;
  838. rtl.arrayClone(type,srcarray,index,end,a,0);
  839. return a;
  840. }
  841. },
  842. setCharAt: function(s,index,c){
  843. return s.substr(0,index)+c+s.substr(index+1);
  844. },
  845. getResStr: function(mod,name){
  846. var rs = mod.$resourcestrings[name];
  847. return rs.current?rs.current:rs.org;
  848. },
  849. createSet: function(){
  850. var s = {};
  851. for (var i=0; i<arguments.length; i++){
  852. if (arguments[i]!=null){
  853. s[arguments[i]]=true;
  854. } else {
  855. var first=arguments[i+=1];
  856. var last=arguments[i+=1];
  857. for(var j=first; j<=last; j++) s[j]=true;
  858. }
  859. }
  860. return s;
  861. },
  862. cloneSet: function(s){
  863. var r = {};
  864. for (var key in s) r[key]=true;
  865. return r;
  866. },
  867. refSet: function(s){
  868. Object.defineProperty(s, '$shared', {
  869. enumerable: false,
  870. configurable: true,
  871. writable: true,
  872. value: true
  873. });
  874. return s;
  875. },
  876. includeSet: function(s,enumvalue){
  877. if (s.$shared) s = rtl.cloneSet(s);
  878. s[enumvalue] = true;
  879. return s;
  880. },
  881. excludeSet: function(s,enumvalue){
  882. if (s.$shared) s = rtl.cloneSet(s);
  883. delete s[enumvalue];
  884. return s;
  885. },
  886. diffSet: function(s,t){
  887. var r = {};
  888. for (var key in s) if (!t[key]) r[key]=true;
  889. return r;
  890. },
  891. unionSet: function(s,t){
  892. var r = {};
  893. for (var key in s) r[key]=true;
  894. for (var key in t) r[key]=true;
  895. return r;
  896. },
  897. intersectSet: function(s,t){
  898. var r = {};
  899. for (var key in s) if (t[key]) r[key]=true;
  900. return r;
  901. },
  902. symDiffSet: function(s,t){
  903. var r = {};
  904. for (var key in s) if (!t[key]) r[key]=true;
  905. for (var key in t) if (!s[key]) r[key]=true;
  906. return r;
  907. },
  908. eqSet: function(s,t){
  909. for (var key in s) if (!t[key]) return false;
  910. for (var key in t) if (!s[key]) return false;
  911. return true;
  912. },
  913. neSet: function(s,t){
  914. return !rtl.eqSet(s,t);
  915. },
  916. leSet: function(s,t){
  917. for (var key in s) if (!t[key]) return false;
  918. return true;
  919. },
  920. geSet: function(s,t){
  921. for (var key in t) if (!s[key]) return false;
  922. return true;
  923. },
  924. strSetLength: function(s,newlen){
  925. var oldlen = s.length;
  926. if (oldlen > newlen){
  927. return s.substring(0,newlen);
  928. } else if (s.repeat){
  929. // Note: repeat needs ECMAScript6!
  930. return s+' '.repeat(newlen-oldlen);
  931. } else {
  932. while (oldlen<newlen){
  933. s+=' ';
  934. oldlen++;
  935. };
  936. return s;
  937. }
  938. },
  939. spaceLeft: function(s,width){
  940. var l=s.length;
  941. if (l>=width) return s;
  942. if (s.repeat){
  943. // Note: repeat needs ECMAScript6!
  944. return ' '.repeat(width-l) + s;
  945. } else {
  946. while (l<width){
  947. s=' '+s;
  948. l++;
  949. };
  950. };
  951. },
  952. floatToStr: function(d,w,p){
  953. // input 1-3 arguments: double, width, precision
  954. if (arguments.length>2){
  955. return rtl.spaceLeft(d.toFixed(p),w);
  956. } else {
  957. // exponent width
  958. var pad = "";
  959. var ad = Math.abs(d);
  960. if (ad<1.0e+10) {
  961. pad='00';
  962. } else if (ad<1.0e+100) {
  963. pad='0';
  964. }
  965. if (arguments.length<2) {
  966. w=9;
  967. } else if (w<9) {
  968. w=9;
  969. }
  970. var p = w-8;
  971. var s=(d>0 ? " " : "" ) + d.toExponential(p);
  972. s=s.replace(/e(.)/,'E$1'+pad);
  973. return rtl.spaceLeft(s,w);
  974. }
  975. },
  976. valEnum: function(s, enumType, setCodeFn){
  977. s = s.toLowerCase();
  978. for (var key in enumType){
  979. if((typeof(key)==='string') && (key.toLowerCase()===s)){
  980. setCodeFn(0);
  981. return enumType[key];
  982. }
  983. }
  984. setCodeFn(1);
  985. return 0;
  986. },
  987. and: function(a,b){
  988. var hi = 0x80000000;
  989. var low = 0x7fffffff;
  990. var h = (a / hi) & (b / hi);
  991. var l = (a & low) & (b & low);
  992. return h*hi + l;
  993. },
  994. or: function(a,b){
  995. var hi = 0x80000000;
  996. var low = 0x7fffffff;
  997. var h = (a / hi) | (b / hi);
  998. var l = (a & low) | (b & low);
  999. return h*hi + l;
  1000. },
  1001. xor: function(a,b){
  1002. var hi = 0x80000000;
  1003. var low = 0x7fffffff;
  1004. var h = (a / hi) ^ (b / hi);
  1005. var l = (a & low) ^ (b & low);
  1006. return h*hi + l;
  1007. },
  1008. shr: function(a,b){
  1009. if (a<0) a += rtl.hiInt;
  1010. if (a<0x80000000) return a >> b;
  1011. if (b<=0) return a;
  1012. if (b>54) return 0;
  1013. return Math.floor(a / Math.pow(2,b));
  1014. },
  1015. shl: function(a,b){
  1016. if (a<0) a += rtl.hiInt;
  1017. if (b<=0) return a;
  1018. if (b>54) return 0;
  1019. var r = a * Math.pow(2,b);
  1020. if (r <= rtl.hiInt) return r;
  1021. return r % rtl.hiInt;
  1022. },
  1023. initRTTI: function(){
  1024. if (rtl.debug_rtti) rtl.debug('initRTTI');
  1025. // base types
  1026. rtl.tTypeInfo = { name: "tTypeInfo" };
  1027. function newBaseTI(name,kind,ancestor){
  1028. if (!ancestor) ancestor = rtl.tTypeInfo;
  1029. if (rtl.debug_rtti) rtl.debug('initRTTI.newBaseTI "'+name+'" '+kind+' ("'+ancestor.name+'")');
  1030. var t = Object.create(ancestor);
  1031. t.name = name;
  1032. t.kind = kind;
  1033. rtl[name] = t;
  1034. return t;
  1035. };
  1036. function newBaseInt(name,minvalue,maxvalue,ordtype){
  1037. var t = newBaseTI(name,1 /* tkInteger */,rtl.tTypeInfoInteger);
  1038. t.minvalue = minvalue;
  1039. t.maxvalue = maxvalue;
  1040. t.ordtype = ordtype;
  1041. return t;
  1042. };
  1043. newBaseTI("tTypeInfoInteger",1 /* tkInteger */);
  1044. newBaseInt("shortint",-0x80,0x7f,0);
  1045. newBaseInt("byte",0,0xff,1);
  1046. newBaseInt("smallint",-0x8000,0x7fff,2);
  1047. newBaseInt("word",0,0xffff,3);
  1048. newBaseInt("longint",-0x80000000,0x7fffffff,4);
  1049. newBaseInt("longword",0,0xffffffff,5);
  1050. newBaseInt("nativeint",-0x10000000000000,0xfffffffffffff,6);
  1051. newBaseInt("nativeuint",0,0xfffffffffffff,7);
  1052. newBaseTI("char",2 /* tkChar */);
  1053. newBaseTI("string",3 /* tkString */);
  1054. newBaseTI("tTypeInfoEnum",4 /* tkEnumeration */,rtl.tTypeInfoInteger);
  1055. newBaseTI("tTypeInfoSet",5 /* tkSet */);
  1056. newBaseTI("double",6 /* tkDouble */);
  1057. newBaseTI("boolean",7 /* tkBool */);
  1058. newBaseTI("tTypeInfoProcVar",8 /* tkProcVar */);
  1059. newBaseTI("tTypeInfoMethodVar",9 /* tkMethod */,rtl.tTypeInfoProcVar);
  1060. newBaseTI("tTypeInfoArray",10 /* tkArray */);
  1061. newBaseTI("tTypeInfoDynArray",11 /* tkDynArray */);
  1062. newBaseTI("tTypeInfoPointer",15 /* tkPointer */);
  1063. var t = newBaseTI("pointer",15 /* tkPointer */,rtl.tTypeInfoPointer);
  1064. t.reftype = null;
  1065. newBaseTI("jsvalue",16 /* tkJSValue */);
  1066. newBaseTI("tTypeInfoRefToProcVar",17 /* tkRefToProcVar */,rtl.tTypeInfoProcVar);
  1067. // member kinds
  1068. rtl.tTypeMember = {};
  1069. function newMember(name,kind){
  1070. var m = Object.create(rtl.tTypeMember);
  1071. m.name = name;
  1072. m.kind = kind;
  1073. rtl[name] = m;
  1074. };
  1075. newMember("tTypeMemberField",1); // tmkField
  1076. newMember("tTypeMemberMethod",2); // tmkMethod
  1077. newMember("tTypeMemberProperty",3); // tmkProperty
  1078. // base object for storing members: a simple object
  1079. rtl.tTypeMembers = {};
  1080. // tTypeInfoStruct - base object for tTypeInfoClass, tTypeInfoRecord, tTypeInfoInterface
  1081. var tis = newBaseTI("tTypeInfoStruct",0);
  1082. tis.$addMember = function(name,ancestor,options){
  1083. if (rtl.debug_rtti){
  1084. if (!rtl.hasString(name) || (name.charAt()==='$')) throw 'invalid member "'+name+'", this="'+this.name+'"';
  1085. if (!rtl.is(ancestor,rtl.tTypeMember)) throw 'invalid ancestor "'+ancestor+':'+ancestor.name+'", "'+this.name+'.'+name+'"';
  1086. if ((options!=undefined) && (typeof(options)!='object')) throw 'invalid options "'+options+'", "'+this.name+'.'+name+'"';
  1087. };
  1088. var t = Object.create(ancestor);
  1089. t.name = name;
  1090. this.members[name] = t;
  1091. this.names.push(name);
  1092. if (rtl.isObject(options)){
  1093. for (var key in options) if (options.hasOwnProperty(key)) t[key] = options[key];
  1094. };
  1095. return t;
  1096. };
  1097. tis.addField = function(name,type,options){
  1098. var t = this.$addMember(name,rtl.tTypeMemberField,options);
  1099. if (rtl.debug_rtti){
  1100. if (!rtl.is(type,rtl.tTypeInfo)) throw 'invalid type "'+type+'", "'+this.name+'.'+name+'"';
  1101. };
  1102. t.typeinfo = type;
  1103. this.fields.push(name);
  1104. return t;
  1105. };
  1106. tis.addFields = function(){
  1107. var i=0;
  1108. while(i<arguments.length){
  1109. var name = arguments[i++];
  1110. var type = arguments[i++];
  1111. if ((i<arguments.length) && (typeof(arguments[i])==='object')){
  1112. this.addField(name,type,arguments[i++]);
  1113. } else {
  1114. this.addField(name,type);
  1115. };
  1116. };
  1117. };
  1118. tis.addMethod = function(name,methodkind,params,result,options){
  1119. var t = this.$addMember(name,rtl.tTypeMemberMethod,options);
  1120. t.methodkind = methodkind;
  1121. t.procsig = rtl.newTIProcSig(params);
  1122. t.procsig.resulttype = result?result:null;
  1123. this.methods.push(name);
  1124. return t;
  1125. };
  1126. tis.addProperty = function(name,flags,result,getter,setter,options){
  1127. var t = this.$addMember(name,rtl.tTypeMemberProperty,options);
  1128. t.flags = flags;
  1129. t.typeinfo = result;
  1130. t.getter = getter;
  1131. t.setter = setter;
  1132. // Note: in options: params, stored, defaultvalue
  1133. if (rtl.isArray(t.params)) t.params = rtl.newTIParams(t.params);
  1134. this.properties.push(name);
  1135. if (!rtl.isString(t.stored)) t.stored = "";
  1136. return t;
  1137. };
  1138. tis.getField = function(index){
  1139. return this.members[this.fields[index]];
  1140. };
  1141. tis.getMethod = function(index){
  1142. return this.members[this.methods[index]];
  1143. };
  1144. tis.getProperty = function(index){
  1145. return this.members[this.properties[index]];
  1146. };
  1147. newBaseTI("tTypeInfoRecord",12 /* tkRecord */,rtl.tTypeInfoStruct);
  1148. newBaseTI("tTypeInfoClass",13 /* tkClass */,rtl.tTypeInfoStruct);
  1149. newBaseTI("tTypeInfoClassRef",14 /* tkClassRef */);
  1150. newBaseTI("tTypeInfoInterface",18 /* tkInterface */,rtl.tTypeInfoStruct);
  1151. newBaseTI("tTypeInfoHelper",19 /* tkHelper */,rtl.tTypeInfoStruct);
  1152. },
  1153. tSectionRTTI: {
  1154. $module: null,
  1155. $inherited: function(name,ancestor,o){
  1156. if (rtl.debug_rtti){
  1157. rtl.debug('tSectionRTTI.newTI "'+(this.$module?this.$module.$name:"(no module)")
  1158. +'"."'+name+'" ('+ancestor.name+') '+(o?'init':'forward'));
  1159. };
  1160. var t = this[name];
  1161. if (t){
  1162. if (!t.$forward) throw 'duplicate type "'+name+'"';
  1163. if (!ancestor.isPrototypeOf(t)) throw 'typeinfo ancestor mismatch "'+name+'" ancestor="'+ancestor.name+'" t.name="'+t.name+'"';
  1164. } else {
  1165. t = Object.create(ancestor);
  1166. t.name = name;
  1167. t.$module = this.$module;
  1168. this[name] = t;
  1169. }
  1170. if (o){
  1171. delete t.$forward;
  1172. for (var key in o) if (o.hasOwnProperty(key)) t[key]=o[key];
  1173. } else {
  1174. t.$forward = true;
  1175. }
  1176. return t;
  1177. },
  1178. $Scope: function(name,ancestor,o){
  1179. var t=this.$inherited(name,ancestor,o);
  1180. t.members = {};
  1181. t.names = [];
  1182. t.fields = [];
  1183. t.methods = [];
  1184. t.properties = [];
  1185. return t;
  1186. },
  1187. $TI: function(name,kind,o){ var t=this.$inherited(name,rtl.tTypeInfo,o); t.kind = kind; return t; },
  1188. $Int: function(name,o){ return this.$inherited(name,rtl.tTypeInfoInteger,o); },
  1189. $Enum: function(name,o){ return this.$inherited(name,rtl.tTypeInfoEnum,o); },
  1190. $Set: function(name,o){ return this.$inherited(name,rtl.tTypeInfoSet,o); },
  1191. $StaticArray: function(name,o){ return this.$inherited(name,rtl.tTypeInfoArray,o); },
  1192. $DynArray: function(name,o){ return this.$inherited(name,rtl.tTypeInfoDynArray,o); },
  1193. $ProcVar: function(name,o){ return this.$inherited(name,rtl.tTypeInfoProcVar,o); },
  1194. $RefToProcVar: function(name,o){ return this.$inherited(name,rtl.tTypeInfoRefToProcVar,o); },
  1195. $MethodVar: function(name,o){ return this.$inherited(name,rtl.tTypeInfoMethodVar,o); },
  1196. $Record: function(name,o){ return this.$Scope(name,rtl.tTypeInfoRecord,o); },
  1197. $Class: function(name,o){ return this.$Scope(name,rtl.tTypeInfoClass,o); },
  1198. $ClassRef: function(name,o){ return this.$inherited(name,rtl.tTypeInfoClassRef,o); },
  1199. $Pointer: function(name,o){ return this.$inherited(name,rtl.tTypeInfoPointer,o); },
  1200. $Interface: function(name,o){ return this.$Scope(name,rtl.tTypeInfoInterface,o); },
  1201. $Helper: function(name,o){ return this.$Scope(name,rtl.tTypeInfoHelper,o); }
  1202. },
  1203. newTIParam: function(param){
  1204. // param is an array, 0=name, 1=type, 2=optional flags
  1205. var t = {
  1206. name: param[0],
  1207. typeinfo: param[1],
  1208. flags: (rtl.isNumber(param[2]) ? param[2] : 0)
  1209. };
  1210. return t;
  1211. },
  1212. newTIParams: function(list){
  1213. // list: optional array of [paramname,typeinfo,optional flags]
  1214. var params = [];
  1215. if (rtl.isArray(list)){
  1216. for (var i=0; i<list.length; i++) params.push(rtl.newTIParam(list[i]));
  1217. };
  1218. return params;
  1219. },
  1220. newTIProcSig: function(params,result,flags){
  1221. var s = {
  1222. params: rtl.newTIParams(params),
  1223. resulttype: result,
  1224. flags: flags
  1225. };
  1226. return s;
  1227. }
  1228. }