rtl.js 43 KB

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