rtl.js 41 KB

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