rtl.js 43 KB

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