rtl.js 44 KB

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