rtl.js 45 KB

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