rtl.js 44 KB

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