rtl.js 42 KB

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