rtl.js 42 KB

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