2
0

rtl.js 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668
  1. var pas = { $libimports: {}};
  2. var rtl = {
  3. version: 30301,
  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. if (t) rtl[typename]=t;
  496. }
  497. if (t) {
  498. if (t.Create){
  499. var e = t.$create("Create");
  500. } else if (t.create) {
  501. var e = t.$create("create");
  502. }
  503. if (e) {
  504. e.FJSError = new Error;
  505. throw e ;
  506. }
  507. }
  508. if (typename === "EInvalidCast") throw new Error("invalid type cast");
  509. if (typename === "EAbstractError") throw new Error("Abstract method called");
  510. if (typename === "ERangeError") throw new Error("range error");
  511. throw typename;
  512. },
  513. as: function(instance,type){
  514. if((instance === null) || rtl.is(instance,type)) return instance;
  515. rtl.raiseE("EInvalidCast");
  516. },
  517. asExt: function(instance,type,mode){
  518. if((instance === null) || rtl.isExt(instance,type,mode)) return instance;
  519. rtl.raiseE("EInvalidCast");
  520. },
  521. createInterface: function(module, name, guid, fnnames, ancestor, initfn, rttiname){
  522. //console.log('createInterface name="'+name+'" guid="'+guid+'" names='+fnnames);
  523. var i = ancestor?Object.create(ancestor):{};
  524. module[name] = i;
  525. i.$module = module;
  526. i.$name = rttiname?rttiname:name;
  527. i.$fullname = module.$name+'.'+i.$name;
  528. i.$guid = guid;
  529. i.$guidr = null;
  530. i.$names = fnnames?fnnames:[];
  531. if (rtl.isFunction(initfn)){
  532. // rtti
  533. if (rtl.debug_rtti) rtl.debug('createInterface '+i.$fullname);
  534. var t = i.$module.$rtti.$Interface(i.$name,{ "interface": i, module: module });
  535. i.$rtti = t;
  536. if (ancestor) t.ancestor = ancestor.$rtti;
  537. if (!t.ancestor) t.ancestor = null;
  538. initfn.call(i);
  539. }
  540. return i;
  541. },
  542. strToGUIDR: function(s,g){
  543. var p = 0;
  544. function n(l){
  545. var h = s.substr(p,l);
  546. p+=l;
  547. return parseInt(h,16);
  548. }
  549. p+=1; // skip {
  550. g.D1 = n(8);
  551. p+=1; // skip -
  552. g.D2 = n(4);
  553. p+=1; // skip -
  554. g.D3 = n(4);
  555. p+=1; // skip -
  556. if (!g.D4) g.D4=[];
  557. g.D4[0] = n(2);
  558. g.D4[1] = n(2);
  559. p+=1; // skip -
  560. for(var i=2; i<8; i++) g.D4[i] = n(2);
  561. return g;
  562. },
  563. guidrToStr: function(g){
  564. if (g.$intf) return g.$intf.$guid;
  565. var h = rtl.hexStr;
  566. var s='{'+h(g.D1,8)+'-'+h(g.D2,4)+'-'+h(g.D3,4)+'-'+h(g.D4[0],2)+h(g.D4[1],2)+'-';
  567. for (var i=2; i<8; i++) s+=h(g.D4[i],2);
  568. s+='}';
  569. return s;
  570. },
  571. createTGUID: function(guid){
  572. var TGuid = (pas.System)?pas.System.TGuid:pas.system.tguid;
  573. var g = rtl.strToGUIDR(guid,TGuid.$new());
  574. return g;
  575. },
  576. getIntfGUIDR: function(intfTypeOrVar){
  577. if (!intfTypeOrVar) return null;
  578. if (!intfTypeOrVar.$guidr){
  579. var g = rtl.createTGUID(intfTypeOrVar.$guid);
  580. if (!intfTypeOrVar.hasOwnProperty('$guid')) intfTypeOrVar = Object.getPrototypeOf(intfTypeOrVar);
  581. g.$intf = intfTypeOrVar;
  582. intfTypeOrVar.$guidr = g;
  583. }
  584. return intfTypeOrVar.$guidr;
  585. },
  586. addIntf: function (aclass, intf, map){
  587. function jmp(fn){
  588. if (typeof(fn)==="function"){
  589. return function(){ return fn.apply(this.$o,arguments); };
  590. } else {
  591. return function(){ rtl.raiseE('EAbstractError'); };
  592. }
  593. }
  594. if(!map) map = {};
  595. var t = intf;
  596. var item = Object.create(t);
  597. if (!aclass.hasOwnProperty('$intfmaps')) aclass.$intfmaps = {};
  598. aclass.$intfmaps[intf.$guid] = item;
  599. do{
  600. var names = t.$names;
  601. if (!names) break;
  602. for (var i=0; i<names.length; i++){
  603. var intfname = names[i];
  604. var fnname = map[intfname];
  605. if (!fnname) fnname = intfname;
  606. //console.log('addIntf: intftype='+t.$name+' index='+i+' intfname="'+intfname+'" fnname="'+fnname+'" old='+typeof(item[intfname]));
  607. item[intfname] = jmp(aclass[fnname]);
  608. }
  609. t = Object.getPrototypeOf(t);
  610. }while(t!=null);
  611. },
  612. getIntfG: function (obj, guid, query){
  613. if (!obj) return null;
  614. //console.log('getIntfG: obj='+obj.$classname+' guid='+guid+' query='+query);
  615. // search
  616. var maps = obj.$intfmaps;
  617. if (!maps) return null;
  618. var item = maps[guid];
  619. if (!item) return null;
  620. // check delegation
  621. //console.log('getIntfG: obj='+obj.$classname+' guid='+guid+' query='+query+' item='+typeof(item));
  622. if (typeof item === 'function') return item.call(obj); // delegate. Note: COM contains _AddRef
  623. // check cache
  624. var intf = null;
  625. if (obj.$interfaces){
  626. intf = obj.$interfaces[guid];
  627. //console.log('getIntfG: obj='+obj.$classname+' guid='+guid+' cache='+typeof(intf));
  628. }
  629. if (!intf){ // intf can be undefined!
  630. intf = Object.create(item);
  631. intf.$o = obj;
  632. if (!obj.$interfaces) obj.$interfaces = {};
  633. obj.$interfaces[guid] = intf;
  634. }
  635. if (typeof(query)==='object'){
  636. // called by queryIntfT
  637. var o = null;
  638. if (intf.QueryInterface(rtl.getIntfGUIDR(query),
  639. {get:function(){ return o; }, set:function(v){ o=v; }}) === 0){
  640. return o;
  641. } else {
  642. return null;
  643. }
  644. } else if(query===2){
  645. // called by TObject.GetInterfaceByStr
  646. if (intf.$kind === 'com') intf._AddRef();
  647. }
  648. return intf;
  649. },
  650. getIntfT: function(obj,intftype){
  651. return rtl.getIntfG(obj,intftype.$guid);
  652. },
  653. queryIntfT: function(obj,intftype){
  654. return rtl.getIntfG(obj,intftype.$guid,intftype);
  655. },
  656. queryIntfIsT: function(obj,intftype){
  657. var i = rtl.getIntfG(obj,intftype.$guid);
  658. if (!i) return false;
  659. if (i.$kind === 'com') i._Release();
  660. return true;
  661. },
  662. asIntfT: function (obj,intftype){
  663. var i = rtl.getIntfG(obj,intftype.$guid);
  664. if (i!==null) return i;
  665. rtl.raiseEInvalidCast();
  666. },
  667. intfIsIntfT: function(intf,intftype){
  668. return (intf!==null) && rtl.queryIntfIsT(intf.$o,intftype);
  669. },
  670. intfAsIntfT: function (intf,intftype){
  671. if (!intf) return null;
  672. var i = rtl.getIntfG(intf.$o,intftype.$guid);
  673. if (i) return i;
  674. rtl.raiseEInvalidCast();
  675. },
  676. intfIsClass: function(intf,classtype){
  677. return (intf!=null) && (rtl.is(intf.$o,classtype));
  678. },
  679. intfAsClass: function(intf,classtype){
  680. if (intf==null) return null;
  681. return rtl.as(intf.$o,classtype);
  682. },
  683. intfToClass: function(intf,classtype){
  684. if ((intf!==null) && rtl.is(intf.$o,classtype)) return intf.$o;
  685. return null;
  686. },
  687. // interface reference counting
  688. intfRefs: { // base object for temporary interface variables
  689. ref: function(id,intf){
  690. // called for temporary interface references needing delayed release
  691. var old = this[id];
  692. //console.log('rtl.intfRefs.ref: id='+id+' old="'+(old?old.$name:'null')+'" intf="'+(intf?intf.$name:'null')+' $o='+(intf?intf.$o:'null'));
  693. if (old){
  694. // called again, e.g. in a loop
  695. delete this[id];
  696. old._Release(); // may fail
  697. }
  698. if(intf) {
  699. this[id]=intf;
  700. }
  701. return intf;
  702. },
  703. free: function(){
  704. //console.log('rtl.intfRefs.free...');
  705. for (var id in this){
  706. if (this.hasOwnProperty(id)){
  707. var intf = this[id];
  708. if (intf){
  709. //console.log('rtl.intfRefs.free: id='+id+' '+intf.$name+' $o='+intf.$o.$classname);
  710. intf._Release();
  711. }
  712. }
  713. }
  714. }
  715. },
  716. createIntfRefs: function(){
  717. //console.log('rtl.createIntfRefs');
  718. return Object.create(rtl.intfRefs);
  719. },
  720. setIntfP: function(path,name,value,skipAddRef){
  721. var old = path[name];
  722. //console.log('rtl.setIntfP path='+path+' name='+name+' old="'+(old?old.$name:'null')+'" value="'+(value?value.$name:'null')+'"');
  723. if (old === value) return;
  724. if (old !== null){
  725. path[name]=null;
  726. old._Release();
  727. }
  728. if (value !== null){
  729. if (!skipAddRef) value._AddRef();
  730. path[name]=value;
  731. }
  732. },
  733. setIntfL: function(old,value,skipAddRef){
  734. //console.log('rtl.setIntfL old="'+(old?old.$name:'null')+'" value="'+(value?value.$name:'null')+'"');
  735. if (old !== value){
  736. if (value!==null){
  737. if (!skipAddRef) value._AddRef();
  738. }
  739. if (old!==null){
  740. old._Release(); // Release after AddRef, to avoid double Release if Release creates an exception
  741. }
  742. } else if (skipAddRef){
  743. if (old!==null){
  744. old._Release(); // value has an AddRef
  745. }
  746. }
  747. return value;
  748. },
  749. _AddRef: function(intf){
  750. //if (intf) console.log('rtl._AddRef intf="'+(intf?intf.$name:'null')+'"');
  751. if (intf) intf._AddRef();
  752. return intf;
  753. },
  754. _Release: function(intf){
  755. //if (intf) console.log('rtl._Release intf="'+(intf?intf.$name:'null')+'"');
  756. if (intf) intf._Release();
  757. return intf;
  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',2);
  825. return a;
  826. },
  827. arrayManaged: function(refCnt,mode,a){
  828. // mode: 0: don't touch elements, 1: null elements, 2: _AddRef elements
  829. if(!a) a = [];
  830. a.$pas2jsrefcnt = refCnt?refCnt:0;
  831. a._AddRef = function(){
  832. this.$pas2jsrefcnt++;
  833. };
  834. a._Release = function(){
  835. this.$pas2jsrefcnt--;
  836. if (this.$pas2jsrefcnt==0){
  837. for (var i=0; i<this.length; i++){
  838. rtl.setIntfP(this,i,null);
  839. }
  840. }
  841. };
  842. if (mode>0){
  843. for (var i=0; i<a.length; i++){
  844. if (mode === 2){
  845. rtl._AddRef(a[i]);
  846. } else {
  847. a[i]=null;
  848. }
  849. }
  850. }
  851. return a;
  852. },
  853. arraySetLength: function(arr,defaultvalue,newlength){
  854. var stack = [];
  855. var s = 9999;
  856. for (var i=2; i<arguments.length; i++){
  857. var j = arguments[i];
  858. if (j==='s'){ s = i-2; }
  859. else {
  860. stack.push({ dim:j+0, a:null, i:0, src:null });
  861. }
  862. }
  863. var dimmax = stack.length-1;
  864. var depth = 0;
  865. var newlen = 0;
  866. var item = null;
  867. var a = null;
  868. var src = arr;
  869. var srclen = 0, oldlen = 0;
  870. var type = 0;
  871. var managed = false;
  872. if (rtl.isArray(defaultvalue)){
  873. // array of dyn array
  874. type = 1;
  875. } else if (rtl.isObject(defaultvalue)) {
  876. if (rtl.isTRecord(defaultvalue)){
  877. // array of record
  878. type = 2;
  879. } else {
  880. // array of set
  881. type = 3;
  882. }
  883. } else if (defaultvalue == 'R'){
  884. // array of COM interface
  885. type = 4;
  886. managed = true;
  887. }
  888. do{
  889. if (depth>0){
  890. item = stack[depth-1];
  891. src = (item.src && item.src.length>item.i) ? item.src[item.i] : null;
  892. }
  893. if (!src){
  894. // init array
  895. managed ? a=rtl.arrayManaged(1) : a=[];
  896. srclen = 0;
  897. oldlen = 0;
  898. } else if (src.$pas2jsrefcnt>1 || depth>=s){
  899. // clone
  900. if (managed){
  901. a = rtl.arrayManaged(1);
  902. src.$pas2jsrefcnt--;
  903. } else {
  904. a = [];
  905. }
  906. srclen = src.length;
  907. oldlen = srclen;
  908. } else {
  909. // keep old
  910. a = src;
  911. srclen = 0;
  912. oldlen = a.length;
  913. }
  914. newlen = stack[depth].dim;
  915. if (managed){
  916. if (a.length>=newlen){
  917. // shrink -> release elements
  918. for (var i=a.length-1; i>=newlen; i--){
  919. rtl.setIntfP(a,i,null);
  920. }
  921. a.length = newlen;
  922. } else {
  923. // enlarge -> null elements
  924. var l = a.length;
  925. a.length = newlen;
  926. for (var i=l; i<newlen; i++){
  927. a[i]=null;
  928. }
  929. oldlen = newlen;
  930. }
  931. } else {
  932. a.length = newlen;
  933. }
  934. if (depth>0){
  935. item.a[item.i]=a;
  936. item.i++;
  937. if ((newlen===0) && (item.i<item.a.length)) continue;
  938. }
  939. if (newlen>0){
  940. if (depth<dimmax){
  941. item = stack[depth];
  942. item.a = a;
  943. item.i = 0;
  944. item.src = src;
  945. depth++;
  946. continue;
  947. } else {
  948. if (srclen>newlen) srclen=newlen;
  949. if (type == 0){
  950. // array of simple value
  951. for (var i=0; i<srclen; i++) a[i]=src[i];
  952. for (var i=oldlen; i<newlen; i++) a[i]=defaultvalue;
  953. } else if (type == 1){
  954. // array of dyn array
  955. for (var i=0; i<srclen; i++) a[i]=src[i];
  956. for (var i=oldlen; i<newlen; i++) a[i]=[];
  957. } else if (type == 2) {
  958. // array of record
  959. for (var i=0; i<srclen; i++) a[i]=defaultvalue.$clone(src[i]);
  960. for (var i=oldlen; i<newlen; i++) a[i]=defaultvalue.$new();
  961. } else if (type == 3) {
  962. // array of set
  963. for (var i=0; i<srclen; i++) a[i]=rtl.refSet(src[i]);
  964. for (var i=oldlen; i<newlen; i++) a[i]={};
  965. } else if (type == 4){
  966. // array of interface
  967. for (var i=0; i<srclen; i++) rtl.setIntfP(a,i,src[i]);
  968. for (var i=oldlen; i<newlen; i++) a[i]=null;
  969. }
  970. }
  971. }
  972. // backtrack
  973. while ((depth>0) && (stack[depth-1].i>=stack[depth-1].dim)){
  974. depth--;
  975. };
  976. if (depth===0){
  977. return dimmax===0 ? a : stack[0].a;
  978. }
  979. }while (true);
  980. },
  981. arrayEq: function(a,b){
  982. if (a===null) return b===null;
  983. if (b===null) return false;
  984. if (a.length!==b.length) return false;
  985. for (var i=0; i<a.length; i++) if (a[i]!==b[i]) return false;
  986. return true;
  987. },
  988. arrayClone: function(type,src,srcpos,endpos,dst,dstpos){
  989. // type: 0 for references or simple values
  990. // src must not be null
  991. // dst at dstpos must not contain managed old values
  992. // This function does not range check.
  993. if(type === 'refSet') {
  994. for (; srcpos<endpos; srcpos++) dst[dstpos++] = rtl.refSet(src[srcpos]); // ref set
  995. } else if (type === 'slice'){
  996. for (; srcpos<endpos; srcpos++) dst[dstpos++] = src[srcpos].slice(0); // clone static array of simple types
  997. } else if (typeof(type)==='function'){
  998. for (; srcpos<endpos; srcpos++) dst[dstpos++] = type(src[srcpos]); // clone function
  999. } else if (rtl.isTRecord(type)){
  1000. for (; srcpos<endpos; srcpos++) dst[dstpos++] = type.$clone(src[srcpos]); // clone record
  1001. } else if (type === 'R'){
  1002. // clone managed instance
  1003. for (; srcpos<endpos; srcpos++){
  1004. dst[dstpos++]=rtl._AddRef(src[srcpos]);
  1005. }
  1006. } else {
  1007. for (; srcpos<endpos; srcpos++) dst[dstpos++] = src[srcpos]; // reference
  1008. };
  1009. },
  1010. arrayConcat: function(type){
  1011. // type: see rtl.arrayClone
  1012. // returns refCnt=1
  1013. var a = [];
  1014. var l = 0;
  1015. for (var i=1; i<arguments.length; i++){
  1016. var src = arguments[i];
  1017. if (src !== null) l+=src.length;
  1018. };
  1019. a.length = l;
  1020. if (type === 'R'){
  1021. rtl.arrayManaged(1,1,a);
  1022. }
  1023. l=0;
  1024. for (var i=1; i<arguments.length; i++){
  1025. var src = arguments[i];
  1026. if (src === null) continue;
  1027. rtl.arrayClone(type,src,0,src.length,a,l);
  1028. l+=src.length;
  1029. };
  1030. return a;
  1031. },
  1032. arrayConcatN: function(){
  1033. var a = null;
  1034. for (var i=0; i<arguments.length; i++){
  1035. var src = arguments[i];
  1036. if (src === null) continue;
  1037. if (a===null){
  1038. a=rtl.arrayRef(src); // Note: concat(arr) does not clone
  1039. } else if (a.$pas2jsrefcnt>1){
  1040. a=a.concat(src); // clone a and append src
  1041. } else {
  1042. for (var i=0; i<src.length; i++){
  1043. a.push(src[i]);
  1044. }
  1045. }
  1046. };
  1047. return a;
  1048. },
  1049. arrayPush: function(type,a){
  1050. if(a===null){
  1051. a=(type==='R') ? rtl.arrayManaged(1) : [];
  1052. } else if (a.$pas2jsrefcnt>1){
  1053. a=rtl.arrayCopy(type,a,0,a.length);
  1054. }
  1055. rtl.arrayClone(type,arguments,2,arguments.length,a,a.length);
  1056. return a;
  1057. },
  1058. arrayPushN: function(a){
  1059. if(a===null){
  1060. a=[];
  1061. } else if (a.$pas2jsrefcnt>1){
  1062. a=a.concat();
  1063. }
  1064. for (var i=1; i<arguments.length; i++){
  1065. a.push(arguments[i]);
  1066. }
  1067. return a;
  1068. },
  1069. arrayCopy: function(type, srcarray, index, count){
  1070. // type: see rtl.arrayClone
  1071. // if count is missing, use srcarray.length
  1072. if (srcarray === null) return (type === 'R') ? null : [];
  1073. if (count === undefined) count=srcarray.length;
  1074. if (index < 0){
  1075. count+=index;
  1076. index = 0;
  1077. }
  1078. var end = index+count;
  1079. if (end>srcarray.length) end = srcarray.length;
  1080. if (index>=end) return (type === 'R') ? null : [];
  1081. if (type===0){
  1082. return srcarray.slice(index,end);
  1083. } else {
  1084. var a = [];
  1085. a.length = end-index;
  1086. if (type === 'R'){
  1087. rtl.arrayManaged(1,1,a);
  1088. }
  1089. rtl.arrayClone(type,srcarray,index,end,a,0);
  1090. return a;
  1091. }
  1092. },
  1093. arrayInsert: function(item, a, index, type){
  1094. var m = (type === 'R');
  1095. if (m) rtl._AddRef(item);
  1096. if (a){
  1097. if (a.$pas2jsrefcnt>1){
  1098. if (m){
  1099. // clone
  1100. a.$pas2jsrefcnt--;
  1101. a=rtl.arrayManaged(1,2,a.concat());
  1102. } else {
  1103. a=a.concat();
  1104. }
  1105. }
  1106. a.splice(index,0,item);
  1107. return a;
  1108. } else {
  1109. a = [item];
  1110. if (m) a=rtl.arrayManaged(1,0,a);
  1111. return a;
  1112. }
  1113. },
  1114. arrayDeleteR: function(a, index, count){
  1115. if (a===null || index<0 || index>=a.length || count<=0) return a;
  1116. if (index+count>a.length) count=a.length-index;
  1117. if (a.$pas2jsrefcnt>1){
  1118. // clone
  1119. a.$pas2jsrefcnt--;
  1120. a=rtl.arrayManaged(1,2,a.concat());
  1121. }
  1122. for (var i=0; i<count; i++) rtl.setIntfP(a,index+i,null);
  1123. a.splice(index,count);
  1124. return a;
  1125. },
  1126. setCharAt: function(s,index,c){
  1127. return s.substr(0,index)+c+s.substr(index+1);
  1128. },
  1129. getResStr: function(mod,name){
  1130. var rs = mod.$resourcestrings[name];
  1131. return rs.current?rs.current:rs.org;
  1132. },
  1133. createSet: function(){
  1134. var s = {};
  1135. for (var i=0; i<arguments.length; i++){
  1136. if (arguments[i]!=null){
  1137. s[arguments[i]]=true;
  1138. } else {
  1139. var first=arguments[i+=1];
  1140. var last=arguments[i+=1];
  1141. for(var j=first; j<=last; j++) s[j]=true;
  1142. }
  1143. }
  1144. return s;
  1145. },
  1146. cloneSet: function(s){
  1147. var r = {};
  1148. for (var key in s) r[key]=true;
  1149. return r;
  1150. },
  1151. refSet: function(s){
  1152. rtl.hideProp(s,'$shared',true);
  1153. return s;
  1154. },
  1155. includeSet: function(s,enumvalue){
  1156. if (s.$shared) s = rtl.cloneSet(s);
  1157. s[enumvalue] = true;
  1158. return s;
  1159. },
  1160. excludeSet: function(s,enumvalue){
  1161. if (s.$shared) s = rtl.cloneSet(s);
  1162. delete s[enumvalue];
  1163. return s;
  1164. },
  1165. diffSet: function(s,t){
  1166. var r = {};
  1167. for (var key in s) if (!t[key]) r[key]=true;
  1168. return r;
  1169. },
  1170. unionSet: function(s,t){
  1171. var r = {};
  1172. for (var key in s) r[key]=true;
  1173. for (var key in t) r[key]=true;
  1174. return r;
  1175. },
  1176. intersectSet: function(s,t){
  1177. var r = {};
  1178. for (var key in s) if (t[key]) r[key]=true;
  1179. return r;
  1180. },
  1181. symDiffSet: function(s,t){
  1182. var r = {};
  1183. for (var key in s) if (!t[key]) r[key]=true;
  1184. for (var key in t) if (!s[key]) r[key]=true;
  1185. return r;
  1186. },
  1187. eqSet: function(s,t){
  1188. for (var key in s) if (!t[key]) return false;
  1189. for (var key in t) if (!s[key]) return false;
  1190. return true;
  1191. },
  1192. neSet: function(s,t){
  1193. return !rtl.eqSet(s,t);
  1194. },
  1195. leSet: function(s,t){
  1196. for (var key in s) if (!t[key]) return false;
  1197. return true;
  1198. },
  1199. geSet: function(s,t){
  1200. for (var key in t) if (!s[key]) return false;
  1201. return true;
  1202. },
  1203. strSetLength: function(s,newlen){
  1204. var oldlen = s.length;
  1205. if (oldlen > newlen){
  1206. return s.substring(0,newlen);
  1207. } else if (s.repeat){
  1208. // Note: repeat needs ECMAScript6!
  1209. return s+' '.repeat(newlen-oldlen);
  1210. } else {
  1211. while (oldlen<newlen){
  1212. s+=' ';
  1213. oldlen++;
  1214. };
  1215. return s;
  1216. }
  1217. },
  1218. spaceLeft: function(s,width){
  1219. var l=s.length;
  1220. if (l>=width) return s;
  1221. if (s.repeat){
  1222. // Note: repeat needs ECMAScript6!
  1223. return ' '.repeat(width-l) + s;
  1224. } else {
  1225. while (l<width){
  1226. s=' '+s;
  1227. l++;
  1228. };
  1229. return s;
  1230. };
  1231. },
  1232. floatToStr: function(d,w,p){
  1233. // input 1-3 arguments: double, width, precision
  1234. if (arguments.length>2){
  1235. return rtl.spaceLeft(d.toFixed(p),w);
  1236. } else {
  1237. // exponent width
  1238. var pad = "";
  1239. var ad = Math.abs(d);
  1240. if (((ad>1) && (ad<1.0e+10)) || ((ad>1.e-10) && (ad<1))) {
  1241. pad='00';
  1242. } else if ((ad>1) && (ad<1.0e+100) || (ad<1.e-10)) {
  1243. pad='0';
  1244. }
  1245. if (arguments.length<2) {
  1246. w=24;
  1247. } else if (w<9) {
  1248. w=9;
  1249. }
  1250. var p = w-8;
  1251. var s=(d>0 ? " " : "" ) + d.toExponential(p);
  1252. s=s.replace(/e(.)/,'E$1'+pad);
  1253. return rtl.spaceLeft(s,w);
  1254. }
  1255. },
  1256. valEnum: function(s, enumType, setCodeFn){
  1257. s = s.toLowerCase();
  1258. for (var key in enumType){
  1259. if((typeof(key)==='string') && (key.toLowerCase()===s)){
  1260. setCodeFn(0);
  1261. return enumType[key];
  1262. }
  1263. }
  1264. setCodeFn(1);
  1265. return 0;
  1266. },
  1267. lw: function(l){
  1268. // fix longword bitwise operation
  1269. return l<0?l+0x100000000:l;
  1270. },
  1271. and: function(a,b){
  1272. var hi = 0x80000000;
  1273. var low = 0x7fffffff;
  1274. var h = (a / hi) & (b / hi);
  1275. var l = (a & low) & (b & low);
  1276. return h*hi + l;
  1277. },
  1278. or: function(a,b){
  1279. var hi = 0x80000000;
  1280. var low = 0x7fffffff;
  1281. var h = (a / hi) | (b / hi);
  1282. var l = (a & low) | (b & low);
  1283. return h*hi + l;
  1284. },
  1285. xor: function(a,b){
  1286. var hi = 0x80000000;
  1287. var low = 0x7fffffff;
  1288. var h = (a / hi) ^ (b / hi);
  1289. var l = (a & low) ^ (b & low);
  1290. return h*hi + l;
  1291. },
  1292. shr: function(a,b){
  1293. if (a<0) a += rtl.hiInt;
  1294. if (a<0x80000000) return a >> b;
  1295. if (b<=0) return a;
  1296. if (b>54) return 0;
  1297. return Math.floor(a / Math.pow(2,b));
  1298. },
  1299. shl: function(a,b){
  1300. if (a<0) a += rtl.hiInt;
  1301. if (b<=0) return a;
  1302. if (b>54) return 0;
  1303. var r = a * Math.pow(2,b);
  1304. if (r <= rtl.hiInt) return r;
  1305. return r % rtl.hiInt;
  1306. },
  1307. initRTTI: function(){
  1308. if (rtl.debug_rtti) rtl.debug('initRTTI');
  1309. // base types
  1310. rtl.tTypeInfo = { name: "tTypeInfo", kind: 0, $module: null, attr: null };
  1311. function newBaseTI(name,kind,ancestor){
  1312. if (!ancestor) ancestor = rtl.tTypeInfo;
  1313. if (rtl.debug_rtti) rtl.debug('initRTTI.newBaseTI "'+name+'" '+kind+' ("'+ancestor.name+'")');
  1314. var t = Object.create(ancestor);
  1315. t.name = name;
  1316. t.kind = kind;
  1317. rtl[name] = t;
  1318. return t;
  1319. };
  1320. function newBaseInt(name,minvalue,maxvalue,ordtype){
  1321. var t = newBaseTI(name,1 /* tkInteger */,rtl.tTypeInfoInteger);
  1322. t.minvalue = minvalue;
  1323. t.maxvalue = maxvalue;
  1324. t.ordtype = ordtype;
  1325. return t;
  1326. };
  1327. newBaseTI("tTypeInfoInteger",1 /* tkInteger */);
  1328. newBaseInt("shortint",-0x80,0x7f,0);
  1329. newBaseInt("byte",0,0xff,1);
  1330. newBaseInt("smallint",-0x8000,0x7fff,2);
  1331. newBaseInt("word",0,0xffff,3);
  1332. newBaseInt("longint",-0x80000000,0x7fffffff,4);
  1333. newBaseInt("longword",0,0xffffffff,5);
  1334. newBaseInt("nativeint",-0x10000000000000,0xfffffffffffff,6);
  1335. newBaseInt("nativeuint",0,0xfffffffffffff,7);
  1336. newBaseInt("char",0,65535,3 /* word */).kind=2 /* tkChar */;
  1337. newBaseTI("string",3 /* tkString */);
  1338. newBaseTI("tTypeInfoEnum",4 /* tkEnumeration */,rtl.tTypeInfoInteger);
  1339. newBaseTI("tTypeInfoSet",5 /* tkSet */);
  1340. newBaseTI("double",6 /* tkDouble */);
  1341. newBaseTI("boolean",7 /* tkBool */);
  1342. newBaseTI("tTypeInfoProcVar",8 /* tkProcVar */);
  1343. newBaseTI("tTypeInfoMethodVar",9 /* tkMethod */,rtl.tTypeInfoProcVar);
  1344. newBaseTI("tTypeInfoArray",10 /* tkArray */);
  1345. newBaseTI("tTypeInfoDynArray",11 /* tkDynArray */);
  1346. newBaseTI("tTypeInfoPointer",15 /* tkPointer */);
  1347. var t = newBaseTI("pointer",15 /* tkPointer */,rtl.tTypeInfoPointer);
  1348. t.reftype = null;
  1349. newBaseTI("jsvalue",16 /* tkJSValue */);
  1350. newBaseTI("tTypeInfoRefToProcVar",17 /* tkRefToProcVar */,rtl.tTypeInfoProcVar);
  1351. // member kinds
  1352. rtl.tTypeMember = { attr: null };
  1353. function newMember(name,kind){
  1354. var m = Object.create(rtl.tTypeMember);
  1355. m.name = name;
  1356. m.kind = kind;
  1357. rtl[name] = m;
  1358. };
  1359. newMember("tTypeMemberField",1); // tmkField
  1360. newMember("tTypeMemberMethod",2); // tmkMethod
  1361. newMember("tTypeMemberProperty",3); // tmkProperty
  1362. // base object for storing members: a simple object
  1363. rtl.tTypeMembers = {};
  1364. // tTypeInfoStruct - base object for tTypeInfoClass, tTypeInfoRecord, tTypeInfoInterface
  1365. var tis = newBaseTI("tTypeInfoStruct",0);
  1366. tis.$addMember = function(name,ancestor,vis,options){
  1367. if (rtl.debug_rtti){
  1368. if (!rtl.hasString(name) || (name.charAt()==='$')) throw 'invalid member "'+name+'", this="'+this.name+'"';
  1369. if (!rtl.is(ancestor,rtl.tTypeMember)) throw 'invalid ancestor "'+ancestor+':'+ancestor.name+'", "'+this.name+'.'+name+'"';
  1370. if ((options!=undefined) && (typeof(options)!='object')) throw 'invalid options "'+options+'", "'+this.name+'.'+name+'"';
  1371. };
  1372. var t = Object.create(ancestor);
  1373. t.name = name;
  1374. this.members[name] = t;
  1375. this.names.push(name);
  1376. t.visibility = vis;
  1377. if (rtl.isObject(options)){
  1378. for (var key in options) if (options.hasOwnProperty(key)) t[key] = options[key];
  1379. };
  1380. return t;
  1381. };
  1382. tis.addField = function(name,type,vis,options){
  1383. var t = this.$addMember(name,rtl.tTypeMemberField,vis?vis:2,options);
  1384. if (rtl.debug_rtti){
  1385. if (!rtl.is(type,rtl.tTypeInfo)) throw 'invalid type "'+type+'", "'+this.name+'.'+name+'"';
  1386. };
  1387. t.typeinfo = type;
  1388. this.fields.push(name);
  1389. return t;
  1390. };
  1391. tis.addFields = function(){
  1392. var i=0;
  1393. while(i<arguments.length){
  1394. var name = arguments[i++];
  1395. var type = arguments[i++];
  1396. if ((i<arguments.length) && (typeof(arguments[i])==='object')){
  1397. this.addField(name,type,arguments[i++]);
  1398. } else {
  1399. this.addField(name,type);
  1400. };
  1401. };
  1402. };
  1403. tis.addMethod = function(name,methodkind,params,vis,result,flags,options){
  1404. // optional: vis, result, flags, options
  1405. var t = this.$addMember(name,rtl.tTypeMemberMethod,vis?vis:2,options);
  1406. t.methodkind = methodkind;
  1407. t.procsig = rtl.newTIProcSig(params,result,flags);
  1408. this.methods.push(name);
  1409. return t;
  1410. };
  1411. tis.addProperty = function(name,flags,result,getter,setter,vis,options){
  1412. var t = this.$addMember(name,rtl.tTypeMemberProperty,vis?vis:4,options);
  1413. t.flags = flags;
  1414. t.typeinfo = result;
  1415. t.getter = getter;
  1416. t.setter = setter;
  1417. // Note: in options: params, stored, defaultvalue
  1418. t.params = rtl.isArray(t.params) ? rtl.newTIParams(t.params) : null;
  1419. this.properties.push(name);
  1420. if (!rtl.isString(t.stored)) t.stored = "";
  1421. return t;
  1422. };
  1423. tis.getField = function(index){
  1424. return this.members[this.fields[index]];
  1425. };
  1426. tis.getMethod = function(index){
  1427. return this.members[this.methods[index]];
  1428. };
  1429. tis.getProperty = function(index){
  1430. return this.members[this.properties[index]];
  1431. };
  1432. newBaseTI("tTypeInfoRecord",12 /* tkRecord */,rtl.tTypeInfoStruct);
  1433. newBaseTI("tTypeInfoClass",13 /* tkClass */,rtl.tTypeInfoStruct);
  1434. newBaseTI("tTypeInfoClassRef",14 /* tkClassRef */);
  1435. newBaseTI("tTypeInfoInterface",18 /* tkInterface */,rtl.tTypeInfoStruct);
  1436. newBaseTI("tTypeInfoHelper",19 /* tkHelper */,rtl.tTypeInfoStruct);
  1437. newBaseTI("tTypeInfoExtClass",20 /* tkExtClass */,rtl.tTypeInfoClass);
  1438. },
  1439. tSectionRTTI: {
  1440. $module: null,
  1441. $inherited: function(name,ancestor,o){
  1442. if (rtl.debug_rtti){
  1443. rtl.debug('tSectionRTTI.newTI "'+(this.$module?this.$module.$name:"(no module)")
  1444. +'"."'+name+'" ('+ancestor.name+') '+(o?'init':'forward'));
  1445. };
  1446. var t = this[name];
  1447. if (t){
  1448. if (!t.$forward) throw 'duplicate type "'+name+'"';
  1449. if (!ancestor.isPrototypeOf(t)) throw 'typeinfo ancestor mismatch "'+name+'" ancestor="'+ancestor.name+'" t.name="'+t.name+'"';
  1450. } else {
  1451. t = Object.create(ancestor);
  1452. t.name = name;
  1453. t.$module = this.$module;
  1454. this[name] = t;
  1455. }
  1456. if (o){
  1457. delete t.$forward;
  1458. for (var key in o) if (o.hasOwnProperty(key)) t[key]=o[key];
  1459. } else {
  1460. t.$forward = true;
  1461. }
  1462. return t;
  1463. },
  1464. $Scope: function(name,ancestor,o){
  1465. var t=this.$inherited(name,ancestor,o);
  1466. t.members = {};
  1467. t.names = [];
  1468. t.fields = [];
  1469. t.methods = [];
  1470. t.properties = [];
  1471. return t;
  1472. },
  1473. $TI: function(name,kind,o){ var t=this.$inherited(name,rtl.tTypeInfo,o); t.kind = kind; return t; },
  1474. $Int: function(name,o){ return this.$inherited(name,rtl.tTypeInfoInteger,o); },
  1475. $Enum: function(name,o){ return this.$inherited(name,rtl.tTypeInfoEnum,o); },
  1476. $Set: function(name,o){ return this.$inherited(name,rtl.tTypeInfoSet,o); },
  1477. $StaticArray: function(name,o){ return this.$inherited(name,rtl.tTypeInfoArray,o); },
  1478. $DynArray: function(name,o){ return this.$inherited(name,rtl.tTypeInfoDynArray,o); },
  1479. $ProcVar: function(name,o){ return this.$inherited(name,rtl.tTypeInfoProcVar,o); },
  1480. $RefToProcVar: function(name,o){ return this.$inherited(name,rtl.tTypeInfoRefToProcVar,o); },
  1481. $MethodVar: function(name,o){ return this.$inherited(name,rtl.tTypeInfoMethodVar,o); },
  1482. $Record: function(name,o,typ){ if(typ) o.$record = typ; return this.$Scope(name,rtl.tTypeInfoRecord,o); },
  1483. $Class: function(name,o){ return this.$Scope(name,rtl.tTypeInfoClass,o); },
  1484. $ClassRef: function(name,o){ return this.$inherited(name,rtl.tTypeInfoClassRef,o); },
  1485. $Pointer: function(name,o){ return this.$inherited(name,rtl.tTypeInfoPointer,o); },
  1486. $Interface: function(name,o){ return this.$Scope(name,rtl.tTypeInfoInterface,o); },
  1487. $Helper: function(name,o){ return this.$Scope(name,rtl.tTypeInfoHelper,o); },
  1488. $ExtClass: function(name,o){ return this.$Scope(name,rtl.tTypeInfoExtClass,o); }
  1489. },
  1490. newTIParam: function(param){
  1491. // param is an array, 0=name, 1=type, 2=optional flags
  1492. var t = {
  1493. name: param[0],
  1494. typeinfo: param[1],
  1495. flags: (rtl.isNumber(param[2]) ? param[2] : 0)
  1496. };
  1497. return t;
  1498. },
  1499. newTIParams: function(list){
  1500. // list: optional array of [paramname,typeinfo,optional flags]
  1501. var params = [];
  1502. if (rtl.isArray(list)){
  1503. for (var i=0; i<list.length; i++) params.push(rtl.newTIParam(list[i]));
  1504. };
  1505. return params;
  1506. },
  1507. newTIProcSig: function(params,result,flags){
  1508. var s = {
  1509. params: rtl.newTIParams(params),
  1510. resulttype: result?result:null,
  1511. flags: flags?flags:0
  1512. };
  1513. return s;
  1514. },
  1515. addResource: function(aRes){
  1516. rtl.$res[aRes.name]=aRes;
  1517. },
  1518. getResource: function(aName){
  1519. var res = rtl.$res[aName];
  1520. if (res !== undefined) {
  1521. return res;
  1522. } else {
  1523. return null;
  1524. }
  1525. },
  1526. getResourceList: function(){
  1527. return Object.keys(rtl.$res);
  1528. }
  1529. }