rtl.js 41 KB

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