rtl.js 45 KB

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