rtl.js 42 KB

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