rtl.js 41 KB

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