rtl.js 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047
  1. var pas = {};
  2. var rtl = {
  3. quiet: false,
  4. debug_load_units: false,
  5. debug_rtti: false,
  6. debug: function(){
  7. if (rtl.quiet || !console || !console.log) return;
  8. console.log(arguments);
  9. },
  10. error: function(s){
  11. rtl.debug('Error: ',s);
  12. throw s;
  13. },
  14. warn: function(s){
  15. rtl.debug('Warn: ',s);
  16. },
  17. hasString: function(s){
  18. return rtl.isString(s) && (s.length>0);
  19. },
  20. isArray: function(a) {
  21. return Array.isArray(a);
  22. },
  23. isFunction: function(f){
  24. return typeof(f)==="function";
  25. },
  26. isModule: function(m){
  27. return rtl.isObject(m) && rtl.hasString(m.$name) && (pas[m.$name]===m);
  28. },
  29. isImplementation: function(m){
  30. return rtl.isObject(m) && rtl.isModule(m.$module) && (m.$module.$impl===m);
  31. },
  32. isNumber: function(n){
  33. return typeof(n)==="number";
  34. },
  35. isObject: function(o){
  36. var s=typeof(o);
  37. return (typeof(o)==="object") && (o!=null);
  38. },
  39. isString: function(s){
  40. return typeof(s)==="string";
  41. },
  42. getNumber: function(n){
  43. return typeof(n)==="number"?n:NaN;
  44. },
  45. getChar: function(c){
  46. return ((typeof(c)==="string") && (c.length===1)) ? c : "";
  47. },
  48. getObject: function(o){
  49. return ((typeof(o)==="object") || (typeof(o)==='function')) ? o : null;
  50. },
  51. isPasClass: function(type){
  52. return (rtl.isObject(type) && type.hasOwnProperty('$classname') && rtl.isObject(type.$module));
  53. },
  54. isPasClassInstance: function(type){
  55. return (rtl.isObject(type) && rtl.isPasClass(type.$class));
  56. },
  57. m_loading: 0,
  58. m_loading_intf: 1,
  59. m_intf_loaded: 2,
  60. m_loading_impl: 3, // loading all used unit
  61. m_initializing: 4, // running initialization
  62. m_initialized: 5,
  63. module: function(module_name, intfuseslist, intfcode, impluseslist, implcode){
  64. if (rtl.debug_load_units) rtl.debug('rtl.module name="'+module_name+'" intfuses='+intfuseslist+' impluses='+impluseslist+' hasimplcode='+rtl.isFunction(implcode));
  65. if (!rtl.hasString(module_name)) rtl.error('invalid module name "'+module_name+'"');
  66. if (!rtl.isArray(intfuseslist)) rtl.error('invalid interface useslist of "'+module_name+'"');
  67. if (!rtl.isFunction(intfcode)) rtl.error('invalid interface code of "'+module_name+'"');
  68. if (!(impluseslist==undefined) && !rtl.isArray(impluseslist)) rtl.error('invalid implementation useslist of "'+module_name+'"');
  69. if (!(implcode==undefined) && !rtl.isFunction(implcode)) rtl.error('invalid implementation code of "'+module_name+'"');
  70. if (pas[module_name])
  71. rtl.error('module "'+module_name+'" is already registered');
  72. var module = pas[module_name] = {
  73. $name: module_name,
  74. $intfuseslist: intfuseslist,
  75. $impluseslist: impluseslist,
  76. $state: rtl.m_loading,
  77. $intfcode: intfcode,
  78. $implcode: implcode,
  79. $impl: null,
  80. $rtti: Object.create(rtl.tSectionRTTI),
  81. };
  82. module.$rtti.$module = module;
  83. if (implcode) module.$impl = {
  84. $module: module,
  85. $rtti: module.$rtti,
  86. };
  87. },
  88. exitcode: 0,
  89. run: function(module_name){
  90. function doRun(){
  91. if (!rtl.hasString(module_name)) module_name='program';
  92. if (rtl.debug_load_units) rtl.debug('rtl.run module="'+module_name+'"');
  93. rtl.initRTTI();
  94. var module = pas[module_name];
  95. if (!module) rtl.error('rtl.run module "'+module_name+'" missing');
  96. rtl.loadintf(module);
  97. rtl.loadimpl(module);
  98. if (module_name=='program'){
  99. if (rtl.debug_load_units) rtl.debug('running $main');
  100. var r = pas.program.$main();
  101. if (rtl.isNumber(r)) rtl.exitcode = r;
  102. }
  103. }
  104. if (rtl.showUncaughtExceptions) {
  105. try{
  106. doRun();
  107. } catch(re) {
  108. var errMsg = re.hasOwnProperty('$class') ? re.$class.$classname : '';
  109. errMsg += ((errMsg) ? ': ' : '') + (re.hasOwnProperty('fMessage') ? re.fMessage : re);
  110. alert('Uncaught Exception : '+errMsg);
  111. rtl.exitCode = 216;
  112. }
  113. } else {
  114. doRun();
  115. }
  116. return rtl.exitcode;
  117. },
  118. loadintf: function(module){
  119. if (module.$state>rtl.m_loading_intf) return; // already finished
  120. if (rtl.debug_load_units) rtl.debug('loadintf: "'+module.$name+'"');
  121. if (module.$state===rtl.m_loading_intf)
  122. rtl.error('unit cycle detected "'+module.$name+'"');
  123. module.$state=rtl.m_loading_intf;
  124. // load interfaces of interface useslist
  125. rtl.loaduseslist(module,module.$intfuseslist,rtl.loadintf);
  126. // run interface
  127. if (rtl.debug_load_units) rtl.debug('loadintf: run intf of "'+module.$name+'"');
  128. module.$intfcode(module.$intfuseslist);
  129. // success
  130. module.$state=rtl.m_intf_loaded;
  131. // Note: units only used in implementations are not yet loaded (not even their interfaces)
  132. },
  133. loaduseslist: function(module,useslist,f){
  134. if (useslist==undefined) return;
  135. for (var i in useslist){
  136. var unitname=useslist[i];
  137. if (rtl.debug_load_units) rtl.debug('loaduseslist of "'+module.$name+'" uses="'+unitname+'"');
  138. if (pas[unitname]==undefined)
  139. rtl.error('module "'+module.$name+'" misses "'+unitname+'"');
  140. f(pas[unitname]);
  141. }
  142. },
  143. loadimpl: function(module){
  144. if (module.$state>=rtl.m_loading_impl) return; // already processing
  145. if (module.$state<rtl.m_intf_loaded) rtl.error('loadimpl: interface not loaded of "'+module.$name+'"');
  146. if (rtl.debug_load_units) rtl.debug('loadimpl: load uses of "'+module.$name+'"');
  147. module.$state=rtl.m_loading_impl;
  148. // load interfaces of implementation useslist
  149. rtl.loaduseslist(module,module.$impluseslist,rtl.loadintf);
  150. // load implementation of interfaces useslist
  151. rtl.loaduseslist(module,module.$intfuseslist,rtl.loadimpl);
  152. // load implementation of implementation useslist
  153. rtl.loaduseslist(module,module.$impluseslist,rtl.loadimpl);
  154. // Note: At this point all interfaces used by this unit are loaded. If
  155. // there are implementation uses cycles some used units might not yet be
  156. // initialized. This is by design.
  157. // run implementation
  158. if (rtl.debug_load_units) rtl.debug('loadimpl: run impl of "'+module.$name+'"');
  159. if (rtl.isFunction(module.$implcode)) module.$implcode(module.$impluseslist);
  160. // run initialization
  161. if (rtl.debug_load_units) rtl.debug('loadimpl: run init of "'+module.$name+'"');
  162. module.$state=rtl.m_initializing;
  163. if (rtl.isFunction(module.$init)) module.$init();
  164. // unit initialized
  165. module.$state=rtl.m_initialized;
  166. },
  167. createCallback: function(scope, fn){
  168. var cb;
  169. if (typeof(fn)==='string'){
  170. cb = function(){
  171. return scope[fn].apply(scope,arguments);
  172. };
  173. } else {
  174. cb = function(){
  175. return fn.apply(scope,arguments);
  176. };
  177. };
  178. cb.scope = scope;
  179. cb.fn = fn;
  180. return cb;
  181. },
  182. cloneCallback: function(cb){
  183. return rtl.createCallback(cb.scope,cb.fn);
  184. },
  185. eqCallback: function(a,b){
  186. // can be a function or a function wrapper
  187. if (a==b){
  188. return true;
  189. } else {
  190. return (a!=null) && (b!=null) && (a.fn) && (a.scope===b.scope) && (a.fn==b.fn);
  191. }
  192. },
  193. initClass: function(c,parent,name,initfn){
  194. parent[name] = c;
  195. c.$classname = name;
  196. if ((parent.$module) && (parent.$module.$impl===parent)) parent=parent.$module;
  197. c.$parent = parent;
  198. c.$fullname = parent.$name+'.'+name;
  199. if (rtl.isModule(parent)){
  200. c.$module = parent;
  201. c.$name = name;
  202. } else {
  203. c.$module = parent.$module;
  204. c.$name = parent.name+'.'+name;
  205. };
  206. // rtti
  207. if (rtl.debug_rtti) rtl.debug('initClass '+c.$fullname);
  208. var t = c.$module.$rtti.$Class(c.$name,{ "class": c, module: parent });
  209. c.$rtti = t;
  210. if (rtl.isObject(c.$ancestor)) t.ancestor = c.$ancestor.$rtti;
  211. if (!t.ancestor) t.ancestor = null;
  212. // init members
  213. initfn.call(c);
  214. },
  215. createClass: function(parent,name,ancestor,initfn){
  216. // create a normal class,
  217. // ancestor must be null or a normal class,
  218. // the root ancestor can be an external class
  219. var c = null;
  220. if (ancestor != null){
  221. c = Object.create(ancestor);
  222. c.$ancestor = ancestor;
  223. // Note:
  224. // if root is an "object" then c.$ancestor === Object.getPrototypeOf(c)
  225. // if root is a "function" then c.$ancestor === c.__proto__, Object.getPrototypeOf(c) returns the root
  226. } else {
  227. c = {};
  228. c.$create = function(fnname,args){
  229. if (args == undefined) args = [];
  230. var o = Object.create(this);
  231. o.$class = this; // Note: o.$class === Object.getPrototypeOf(o)
  232. o.$init();
  233. try{
  234. o[fnname].apply(o,args);
  235. o.AfterConstruction();
  236. } catch($e){
  237. o.$destroy;
  238. throw $e;
  239. }
  240. return o;
  241. };
  242. c.$destroy = function(fnname){
  243. this.BeforeDestruction();
  244. this[fnname]();
  245. this.$final;
  246. };
  247. };
  248. rtl.initClass(c,parent,name,initfn);
  249. },
  250. createClassExt: function(parent,name,ancestor,newinstancefnname,initfn){
  251. // Create a class using an external ancestor.
  252. // If newinstancefnname is given, use that function to create the new object.
  253. // If exist call BeforeDestruction and AfterConstruction.
  254. var c = null;
  255. c = Object.create(ancestor);
  256. c.$create = function(fnname,args){
  257. if (args == undefined) args = [];
  258. var o = null;
  259. if (newinstancefnname.length>0){
  260. o = this[newinstancefnname](fnname,args);
  261. } else {
  262. o = Object.create(this);
  263. }
  264. o.$class = this; // Note: o.$class === Object.getPrototypeOf(o)
  265. o.$init();
  266. try{
  267. o[fnname].apply(o,args);
  268. if (o.AfterConstruction) o.AfterConstruction();
  269. } catch($e){
  270. o.$destroy;
  271. throw $e;
  272. }
  273. return o;
  274. };
  275. c.$destroy = function(fnname){
  276. if (this.BeforeDestruction) this.BeforeDestruction();
  277. this[fnname]();
  278. this.$final;
  279. };
  280. rtl.initClass(c,parent,name,initfn);
  281. },
  282. tObjectDestroy: "Destroy",
  283. free: function(obj,name){
  284. if (obj[name]==null) return;
  285. obj[name].$destroy(rtl.tObjectDestroy);
  286. obj[name]=null;
  287. },
  288. freeLoc: function(obj){
  289. if (obj==null) return;
  290. obj.$destroy(rtl.tObjectDestroy);
  291. return null;
  292. },
  293. is: function(instance,type){
  294. return type.isPrototypeOf(instance) || (instance===type);
  295. },
  296. isExt: function(instance,type,mode){
  297. // mode===1 means instance must be a Pascal class instance
  298. // mode===2 means instance must be a Pascal class
  299. // Notes:
  300. // isPrototypeOf and instanceof return false on equal
  301. // isPrototypeOf does not work for Date.isPrototypeOf(new Date())
  302. // so if isPrototypeOf is false test with instanceof
  303. // instanceof needs a function on right side
  304. if (instance == null) return false; // Note: ==null checks for undefined too
  305. if ((typeof(type) !== 'object') && (typeof(type) !== 'function')) return false;
  306. if (instance === type){
  307. if (mode===1) return false;
  308. if (mode===2) return rtl.isPasClass(instance);
  309. return true;
  310. }
  311. if (type.isPrototypeOf && type.isPrototypeOf(instance)){
  312. if (mode===1) return rtl.isPasClassInstance(instance);
  313. if (mode===2) return rtl.isPasClass(instance);
  314. return true;
  315. }
  316. if ((typeof type == 'function') && (instance instanceof type)) return true;
  317. return false;
  318. },
  319. Exception: null,
  320. EInvalidCast: null,
  321. EAbstractError: null,
  322. ERangeError: null,
  323. raiseE: function(typename){
  324. var t = rtl[typename];
  325. if (t==null){
  326. var mod = pas.SysUtils;
  327. if (!mod) mod = pas.sysutils;
  328. if (mod){
  329. t = mod[typename];
  330. if (!t) t = mod[typename.toLowerCase()];
  331. if (!t) t = mod['Exception'];
  332. if (!t) t = mod['exception'];
  333. }
  334. }
  335. if (t){
  336. if (t.Create){
  337. throw t.$create("Create");
  338. } else if (t.create){
  339. throw t.$create("create");
  340. }
  341. }
  342. if (typename === "EInvalidCast") throw "invalid type cast";
  343. if (typename === "EAbstractError") throw "Abstract method called";
  344. if (typename === "ERangeError") throw "range error";
  345. throw typename;
  346. },
  347. as: function(instance,type){
  348. if((instance === null) || rtl.is(instance,type)) return instance;
  349. rtl.raiseE("EInvalidCast");
  350. },
  351. asExt: function(instance,type,mode){
  352. if((instance === null) || rtl.isExt(instance,type,mode)) return instance;
  353. rtl.raiseE("EInvalidCast");
  354. },
  355. createInterface: function(module, name, guid, fnnames, ancestor, initfn){
  356. //console.log('createInterface name="'+name+'" guid="'+guid+'" names='+fnnames);
  357. var i = ancestor?Object.create(ancestor):{};
  358. module[name] = i;
  359. i.$module = module;
  360. i.$name = name;
  361. i.$fullname = module.$name+'.'+name;
  362. i.$guid = guid;
  363. i.$names = fnnames?fnnames:[];
  364. if (rtl.isFunction(initfn)){
  365. // rtti
  366. if (rtl.debug_rtti) rtl.debug('createInterface '+i.$fullname);
  367. var t = i.$module.$rtti.$Interface(name,{ "interface": i, module: module });
  368. i.$rtti = t;
  369. if (ancestor) t.ancestor = ancestor.$rtti;
  370. if (!t.ancestor) t.ancestor = null;
  371. initfn.call(i);
  372. }
  373. return i;
  374. },
  375. addIntf: function (aclass, intf, map){
  376. 'use strict';
  377. if(!map) map = {};
  378. var t = intf;
  379. var item = Object.create(t);
  380. aclass.$intfmaps[intf.$guid] = item;
  381. do{
  382. var names = t.$names;
  383. if (!names) break;
  384. for (var i=0; i<names.length; i++){
  385. var intfname = names[i];
  386. var fnname = map[intfname];
  387. if (!fnname) fnname = intfname;
  388. let fn = aclass[fnname];
  389. //console.log('addIntf: intftype='+t.$name+' index='+i+' intfname="'+intfname+'" fnname="'+fnname+'" proc='+typeof(fn));
  390. if (typeof(fn)==="function"){
  391. item[intfname] = function(){ return fn.apply(this.$o,arguments); };
  392. } else {
  393. item[intfname] = function(){ rtl.raiseE('EAbstractError'); };
  394. }
  395. }
  396. t = Object.getPrototypeOf(t);
  397. }while(t!=null);
  398. },
  399. getIntfG: function (obj, guid, query){
  400. if (!obj) return null;
  401. //console.log('getIntfG: obj='+obj.$classname+' guid='+guid+' query='+query);
  402. // search
  403. var maps = obj.$intfmaps;
  404. if (!maps) return null;
  405. var item = maps[guid];
  406. if (!item) return null;
  407. // check delegation
  408. //console.log('getIntfG: obj='+obj.$classname+' guid='+guid+' query='+query+' item='+typeof(item));
  409. if (typeof item === 'function') return item.call(obj);
  410. // check cache
  411. var intf = null;
  412. if (obj.$interfaces){
  413. intf = obj.$interfaces[guid];
  414. // intf can be undefined!
  415. //console.log('getIntfG: obj='+obj.$classname+' guid='+guid+' cache='+typeof(intf));
  416. }
  417. if (!intf){
  418. intf = Object.create(item);
  419. intf.$o = obj;
  420. if (!obj.$interfaces) obj.$interfaces = {};
  421. obj.$interfaces[guid] = intf;
  422. }
  423. if (query===1){
  424. var o = null;
  425. if (intf.QueryInterface(guid,
  426. {get:function(){ return o; }, set:function(v){ o=v; }}) === 0){
  427. return o;
  428. } else {
  429. return null;
  430. }
  431. } else if(query===2){
  432. intf._AddRef();
  433. }
  434. return intf;
  435. },
  436. getIntfT: function(obj,intftype){
  437. return rtl.getIntfG(obj,intftype.$guid);
  438. },
  439. queryIntfG: function(obj,guid){
  440. return rtl.getIntfG(obj,guid,1);
  441. },
  442. queryIntfT: function(obj,intftype){
  443. return rtl.queryIntfG(obj,intftype.$guid);
  444. },
  445. queryIntfIsT: function(obj,intftype){
  446. var i = rtl.queryIntfG(obj,intftype.$guid);
  447. if (!i) return false;
  448. if (i._Release) i._Release();
  449. return true;
  450. },
  451. asIntfT: function (obj,intftype){
  452. var i = getIntfG(obj,intftype.$guid);
  453. if (i!==null) return i;
  454. rtl.raiseEInvalidCast();
  455. },
  456. intfIsClass: function(intf,classtype){
  457. return (intf!=null) && (rtl.is(intf.$o,classtype));
  458. },
  459. intfAsClass: function(intf,classtype){
  460. if (intf==null) return null;
  461. return rtl.as(intf.$o,classtype);
  462. },
  463. intfToClass: function(intf,classtype){
  464. if ((intf!==null) && rtl.is(intf.$o,classtype)) return intf.$o;
  465. return null;
  466. },
  467. // interface reference counting
  468. intfRefs: { // base object for temporary interface variables
  469. ref: function(id,intf){
  470. // called for temporary interface references needing delayed release
  471. var old = this[id];
  472. //console.log('rtl.intfRefs.ref: id='+id+' old="'+(old?old.$name:'null')+'" intf="'+(intf?intf.$name:'null'));
  473. if (old){
  474. // called again, e.g. in a loop
  475. delete this[id];
  476. old._Release(); // may fail
  477. }
  478. this[id]=intf;
  479. return intf;
  480. },
  481. free: function(){
  482. //console.log('rtl.intfRefs.free...');
  483. for (id in this){
  484. if (this.hasOwnProperty(id)) this[id]._Release;
  485. }
  486. },
  487. },
  488. createIntfRefs: function(){
  489. //console.log('rtl.createIntfRefs');
  490. return Object.create(rtl.intfRefs);
  491. },
  492. setIntfP: function(path,name,value,skipAddRef){
  493. var old = path[name];
  494. //console.log('rtl.setIntfP path='+path+' name='+name+' old="'+(old?old.$name:'null')+'" value="'+(value?value.$name:'null')+'"');
  495. if (old === value) return;
  496. if (old !== null){
  497. path[name]=null;
  498. old._Release();
  499. }
  500. if (value !== null){
  501. if (!skipAddRef) value._AddRef();
  502. path[name]=value;
  503. }
  504. },
  505. setIntfL: function(old,value,skipAddRef){
  506. //console.log('rtl.setIntfL old="'+(old?old.$name:'null')+'" value="'+(value?value.$name:'null')+'"');
  507. if (old !== value){
  508. if (value!==null){
  509. if (!skipAddRef) value._AddRef();
  510. }
  511. if (old!==null){
  512. old._Release(); // Release after AddRef, to avoid double Release if Release creates an exception
  513. }
  514. }
  515. return value;
  516. },
  517. _AddRef: function(intf){
  518. //if (intf) console.log('rtl._AddRef intf="'+(intf?intf.$name:'null')+'"');
  519. if (intf) intf._AddRef();
  520. return intf;
  521. },
  522. _Release: function(intf){
  523. //if (intf) console.log('rtl._Release intf="'+(intf?intf.$name:'null')+'"');
  524. if (intf) intf._Release();
  525. return intf;
  526. },
  527. checkMethodCall: function(obj,type){
  528. if (rtl.isObject(obj) && rtl.is(obj,type)) return;
  529. rtl.raiseE("EInvalidCast");
  530. },
  531. rc: function(i,minval,maxval){
  532. // range check integer
  533. if ((Math.floor(i)===i) && (i>=minval) && (i<=maxval)) return i;
  534. rtl.raiseE('ERangeError');
  535. },
  536. length: function(arr){
  537. return (arr == null) ? 0 : arr.length;
  538. },
  539. arraySetLength: function(arr,defaultvalue,newlength){
  540. // multi dim: (arr,defaultvalue,dim1,dim2,...)
  541. if (arr == null) arr = [];
  542. var p = arguments;
  543. function setLength(a,argNo){
  544. var oldlen = a.length;
  545. var newlen = p[argNo];
  546. if (oldlen!==newlength){
  547. a.length = newlength;
  548. if (argNo === p.length-1){
  549. if (rtl.isArray(defaultvalue)){
  550. for (var i=oldlen; i<newlen; i++) a[i]=[]; // nested array
  551. } else if (rtl.isFunction(defaultvalue)){
  552. for (var i=oldlen; i<newlen; i++) a[i]=new defaultvalue(); // e.g. record
  553. } else if (rtl.isObject(defaultvalue)) {
  554. for (var i=oldlen; i<newlen; i++) a[i]={}; // e.g. set
  555. } else {
  556. for (var i=oldlen; i<newlen; i++) a[i]=defaultvalue;
  557. }
  558. } else {
  559. for (var i=oldlen; i<newlen; i++) a[i]=[]; // nested array
  560. }
  561. }
  562. if (argNo < p.length-1){
  563. // multi argNo
  564. for (var i=0; i<newlen; i++) a[i]=setLength(a[i],argNo+1);
  565. }
  566. return a;
  567. }
  568. return setLength(arr,2);
  569. },
  570. arrayClone: function(type,src,srcpos,end,dst,dstpos){
  571. // type: 0 for references, "refset" for calling refSet(), a function for new type()
  572. // src must not be null
  573. // This function does not range check.
  574. if (rtl.isFunction(type)){
  575. for (; srcpos<end; srcpos++) dst[dstpos++] = new type(src[srcpos]); // clone record
  576. } else if((typeof(type)==="string") && (type === 'refSet')) {
  577. for (; srcpos<end; srcpos++) dst[dstpos++] = refSet(src[srcpos]); // ref set
  578. } else {
  579. for (; srcpos<end; srcpos++) dst[dstpos++] = src[srcpos]; // reference
  580. };
  581. },
  582. arrayConcat: function(type){
  583. // type: see rtl.arrayClone
  584. var a = [];
  585. var l = 0;
  586. for (var i=1; i<arguments.length; i++) l+=arguments[i].length;
  587. a.length = l;
  588. l=0;
  589. for (var i=1; i<arguments.length; i++){
  590. var src = arguments[i];
  591. if (src == null) continue;
  592. rtl.arrayClone(type,src,0,src.length,a,l);
  593. l+=src.length;
  594. };
  595. return a;
  596. },
  597. arrayCopy: function(type, srcarray, index, count){
  598. // type: see rtl.arrayClone
  599. // if count is missing, use srcarray.length
  600. if (srcarray == null) return [];
  601. if (index < 0) index = 0;
  602. if (count === undefined) count=srcarray.length;
  603. var end = index+count;
  604. if (end>scrarray.length) end = scrarray.length;
  605. if (index>=end) return [];
  606. if (type===0){
  607. return srcarray.slice(index,end);
  608. } else {
  609. var a = [];
  610. a.length = end-index;
  611. rtl.arrayClone(type,srcarray,index,end,a,0);
  612. return a;
  613. }
  614. },
  615. setCharAt: function(s,index,c){
  616. return s.substr(0,index)+c+s.substr(index+1);
  617. },
  618. getResStr: function(mod,name){
  619. var rs = mod.$resourcestrings[name];
  620. return rs.current?rs.current:rs.org;
  621. },
  622. createSet: function(){
  623. var s = {};
  624. for (var i=0; i<arguments.length; i++){
  625. if (arguments[i]!=null){
  626. s[arguments[i]]=true;
  627. } else {
  628. var first=arguments[i+=1];
  629. var last=arguments[i+=1];
  630. for(var j=first; j<=last; j++) s[j]=true;
  631. }
  632. }
  633. return s;
  634. },
  635. cloneSet: function(s){
  636. var r = {};
  637. for (var key in s) r[key]=true;
  638. return r;
  639. },
  640. refSet: function(s){
  641. s.$shared = true;
  642. return s;
  643. },
  644. includeSet: function(s,enumvalue){
  645. if (s.$shared) s = rtl.cloneSet(s);
  646. s[enumvalue] = true;
  647. return s;
  648. },
  649. excludeSet: function(s,enumvalue){
  650. if (s.$shared) s = rtl.cloneSet(s);
  651. delete s[enumvalue];
  652. return s;
  653. },
  654. diffSet: function(s,t){
  655. var r = {};
  656. for (var key in s) if (!t[key]) r[key]=true;
  657. delete r.$shared;
  658. return r;
  659. },
  660. unionSet: function(s,t){
  661. var r = {};
  662. for (var key in s) r[key]=true;
  663. for (var key in t) r[key]=true;
  664. delete r.$shared;
  665. return r;
  666. },
  667. intersectSet: function(s,t){
  668. var r = {};
  669. for (var key in s) if (t[key]) r[key]=true;
  670. delete r.$shared;
  671. return r;
  672. },
  673. symDiffSet: function(s,t){
  674. var r = {};
  675. for (var key in s) if (!t[key]) r[key]=true;
  676. for (var key in t) if (!s[key]) r[key]=true;
  677. delete r.$shared;
  678. return r;
  679. },
  680. eqSet: function(s,t){
  681. for (var key in s) if (!t[key] && (key!='$shared')) return false;
  682. for (var key in t) if (!s[key] && (key!='$shared')) return false;
  683. return true;
  684. },
  685. neSet: function(s,t){
  686. return !rtl.eqSet(s,t);
  687. },
  688. leSet: function(s,t){
  689. for (var key in s) if (!t[key] && (key!='$shared')) return false;
  690. return true;
  691. },
  692. geSet: function(s,t){
  693. for (var key in t) if (!s[key] && (key!='$shared')) return false;
  694. return true;
  695. },
  696. strSetLength: function(s,newlen){
  697. var oldlen = s.length;
  698. if (oldlen > newlen){
  699. return s.substring(0,newlen);
  700. } else if (s.repeat){
  701. // Note: repeat needs ECMAScript6!
  702. return s+' '.repeat(newlen-oldlen);
  703. } else {
  704. while (oldlen<newlen){
  705. s+=' ';
  706. oldlen++;
  707. };
  708. return s;
  709. }
  710. },
  711. spaceLeft: function(s,width){
  712. var l=s.length;
  713. if (l>=width) return s;
  714. if (s.repeat){
  715. // Note: repeat needs ECMAScript6!
  716. return ' '.repeat(width-l) + s;
  717. } else {
  718. while (l<width){
  719. s=' '+s;
  720. l++;
  721. };
  722. };
  723. },
  724. floatToStr : function(d,w,p){
  725. // input 1-3 arguments: double, width, precision
  726. if (arguments.length>2){
  727. return rtl.spaceLeft(d.toFixed(p),w);
  728. } else {
  729. // exponent width
  730. var pad = "";
  731. var ad = Math.abs(d);
  732. if (ad<1.0e+10) {
  733. pad='00';
  734. } else if (ad<1.0e+100) {
  735. pad='0';
  736. }
  737. if (arguments.length<2) {
  738. w=9;
  739. } else if (w<9) {
  740. w=9;
  741. }
  742. var p = w-8;
  743. var s=(d>0 ? " " : "" ) + d.toExponential(p);
  744. s=s.replace(/e(.)/,'E$1'+pad);
  745. return rtl.spaceLeft(s,w);
  746. }
  747. },
  748. initRTTI: function(){
  749. if (rtl.debug_rtti) rtl.debug('initRTTI');
  750. // base types
  751. rtl.tTypeInfo = { name: "tTypeInfo" };
  752. function newBaseTI(name,kind,ancestor){
  753. if (!ancestor) ancestor = rtl.tTypeInfo;
  754. if (rtl.debug_rtti) rtl.debug('initRTTI.newBaseTI "'+name+'" '+kind+' ("'+ancestor.name+'")');
  755. var t = Object.create(ancestor);
  756. t.name = name;
  757. t.kind = kind;
  758. rtl[name] = t;
  759. return t;
  760. };
  761. function newBaseInt(name,minvalue,maxvalue,ordtype){
  762. var t = newBaseTI(name,1 /* tkInteger */,rtl.tTypeInfoInteger);
  763. t.minvalue = minvalue;
  764. t.maxvalue = maxvalue;
  765. t.ordtype = ordtype;
  766. return t;
  767. };
  768. newBaseTI("tTypeInfoInteger",1 /* tkInteger */);
  769. newBaseInt("shortint",-0x80,0x7f,0);
  770. newBaseInt("byte",0,0xff,1);
  771. newBaseInt("smallint",-0x8000,0x7fff,2);
  772. newBaseInt("word",0,0xffff,3);
  773. newBaseInt("longint",-0x80000000,0x7fffffff,4);
  774. newBaseInt("longword",0,0xffffffff,5);
  775. newBaseInt("nativeint",-0x10000000000000,0xfffffffffffff,6);
  776. newBaseInt("nativeuint",0,0xfffffffffffff,7);
  777. newBaseTI("char",2 /* tkChar */);
  778. newBaseTI("string",3 /* tkString */);
  779. newBaseTI("tTypeInfoEnum",4 /* tkEnumeration */,rtl.tTypeInfoInteger);
  780. newBaseTI("tTypeInfoSet",5 /* tkSet */);
  781. newBaseTI("double",6 /* tkDouble */);
  782. newBaseTI("boolean",7 /* tkBool */);
  783. newBaseTI("tTypeInfoProcVar",8 /* tkProcVar */);
  784. newBaseTI("tTypeInfoMethodVar",9 /* tkMethod */,rtl.tTypeInfoProcVar);
  785. newBaseTI("tTypeInfoArray",10 /* tkArray */);
  786. newBaseTI("tTypeInfoDynArray",11 /* tkDynArray */);
  787. newBaseTI("tTypeInfoPointer",15 /* tkPointer */);
  788. var t = newBaseTI("pointer",15 /* tkPointer */,rtl.tTypeInfoPointer);
  789. t.reftype = null;
  790. newBaseTI("jsvalue",16 /* tkJSValue */);
  791. newBaseTI("tTypeInfoRefToProcVar",17 /* tkRefToProcVar */,rtl.tTypeInfoProcVar);
  792. // member kinds
  793. rtl.tTypeMember = {};
  794. function newMember(name,kind){
  795. var m = Object.create(rtl.tTypeMember);
  796. m.name = name;
  797. m.kind = kind;
  798. rtl[name] = m;
  799. };
  800. newMember("tTypeMemberField",1); // tmkField
  801. newMember("tTypeMemberMethod",2); // tmkMethod
  802. newMember("tTypeMemberProperty",3); // tmkProperty
  803. // base object for storing members: a simple object
  804. rtl.tTypeMembers = {};
  805. // tTypeInfoStruct - base object for tTypeInfoClass, tTypeInfoRecord, tTypeInfoInterface
  806. var tis = newBaseTI("tTypeInfoStruct",0);
  807. tis.$addMember = function(name,ancestor,options){
  808. if (rtl.debug_rtti){
  809. if (!rtl.hasString(name) || (name.charAt()==='$')) throw 'invalid member "'+name+'", this="'+this.name+'"';
  810. if (!rtl.is(ancestor,rtl.tTypeMember)) throw 'invalid ancestor "'+ancestor+':'+ancestor.name+'", "'+this.name+'.'+name+'"';
  811. if ((options!=undefined) && (typeof(options)!='object')) throw 'invalid options "'+options+'", "'+this.name+'.'+name+'"';
  812. };
  813. var t = Object.create(ancestor);
  814. t.name = name;
  815. this.members[name] = t;
  816. this.names.push(name);
  817. if (rtl.isObject(options)){
  818. for (var key in options) if (options.hasOwnProperty(key)) t[key] = options[key];
  819. };
  820. return t;
  821. };
  822. tis.addField = function(name,type,options){
  823. var t = this.$addMember(name,rtl.tTypeMemberField,options);
  824. if (rtl.debug_rtti){
  825. if (!rtl.is(type,rtl.tTypeInfo)) throw 'invalid type "'+type+'", "'+this.name+'.'+name+'"';
  826. };
  827. t.typeinfo = type;
  828. this.fields.push(name);
  829. return t;
  830. };
  831. tis.addFields = function(){
  832. var i=0;
  833. while(i<arguments.length){
  834. var name = arguments[i++];
  835. var type = arguments[i++];
  836. if ((i<arguments.length) && (typeof(arguments[i])==='object')){
  837. this.addField(name,type,arguments[i++]);
  838. } else {
  839. this.addField(name,type);
  840. };
  841. };
  842. };
  843. tis.addMethod = function(name,methodkind,params,result,options){
  844. var t = this.$addMember(name,rtl.tTypeMemberMethod,options);
  845. t.methodkind = methodkind;
  846. t.procsig = rtl.newTIProcSig(params);
  847. t.procsig.resulttype = result?result:null;
  848. this.methods.push(name);
  849. return t;
  850. };
  851. tis.addProperty = function(name,flags,result,getter,setter,options){
  852. var t = this.$addMember(name,rtl.tTypeMemberProperty,options);
  853. t.flags = flags;
  854. t.typeinfo = result;
  855. t.getter = getter;
  856. t.setter = setter;
  857. // Note: in options: params, stored, defaultvalue
  858. if (rtl.isArray(t.params)) t.params = rtl.newTIParams(t.params);
  859. this.properties.push(name);
  860. if (!rtl.isString(t.stored)) t.stored = "";
  861. return t;
  862. };
  863. tis.getField = function(index){
  864. return this.members[this.fields[index]];
  865. };
  866. tis.getMethod = function(index){
  867. return this.members[this.methods[index]];
  868. };
  869. tis.getProperty = function(index){
  870. return this.members[this.properties[index]];
  871. };
  872. newBaseTI("tTypeInfoRecord",12 /* tkRecord */,rtl.tTypeInfoStruct);
  873. newBaseTI("tTypeInfoClass",13 /* tkClass */,rtl.tTypeInfoStruct);
  874. newBaseTI("tTypeInfoClassRef",14 /* tkClassRef */);
  875. newBaseTI("tTypeInfoInterface",15 /* tkInterface */,rtl.tTypeInfoStruct);
  876. },
  877. tSectionRTTI: {
  878. $module: null,
  879. $inherited: function(name,ancestor,o){
  880. if (rtl.debug_rtti){
  881. rtl.debug('tSectionRTTI.newTI "'+(this.$module?this.$module.$name:"(no module)")
  882. +'"."'+name+'" ('+ancestor.name+') '+(o?'init':'forward'));
  883. };
  884. var t = this[name];
  885. if (t){
  886. if (!t.$forward) throw 'duplicate type "'+name+'"';
  887. if (!ancestor.isPrototypeOf(t)) throw 'typeinfo ancestor mismatch "'+name+'" ancestor="'+ancestor.name+'" t.name="'+t.name+'"';
  888. } else {
  889. t = Object.create(ancestor);
  890. t.name = name;
  891. t.module = this.module;
  892. this[name] = t;
  893. }
  894. if (o){
  895. delete t.$forward;
  896. for (var key in o) if (o.hasOwnProperty(key)) t[key]=o[key];
  897. } else {
  898. t.$forward = true;
  899. }
  900. return t;
  901. },
  902. $Scope: function(name,ancestor,o){
  903. var t=this.$inherited(name,ancestor,o);
  904. t.members = {};
  905. t.names = [];
  906. t.fields = [];
  907. t.methods = [];
  908. t.properties = [];
  909. return t;
  910. },
  911. $TI: function(name,kind,o){ var t=this.$inherited(name,rtl.tTypeInfo,o); t.kind = kind; return t; },
  912. $Int: function(name,o){ return this.$inherited(name,rtl.tTypeInfoInteger,o); },
  913. $Enum: function(name,o){ return this.$inherited(name,rtl.tTypeInfoEnum,o); },
  914. $Set: function(name,o){ return this.$inherited(name,rtl.tTypeInfoSet,o); },
  915. $StaticArray: function(name,o){ return this.$inherited(name,rtl.tTypeInfoArray,o); },
  916. $DynArray: function(name,o){ return this.$inherited(name,rtl.tTypeInfoDynArray,o); },
  917. $ProcVar: function(name,o){ return this.$inherited(name,rtl.tTypeInfoProcVar,o); },
  918. $RefToProcVar: function(name,o){ return this.$inherited(name,rtl.tTypeInfoRefToProcVar,o); },
  919. $MethodVar: function(name,o){ return this.$inherited(name,rtl.tTypeInfoMethodVar,o); },
  920. $Record: function(name,o){ return this.$Scope(name,rtl.tTypeInfoRecord,o); },
  921. $Class: function(name,o){ return this.$Scope(name,rtl.tTypeInfoClass,o); },
  922. $ClassRef: function(name,o){ return this.$inherited(name,rtl.tTypeInfoClassRef,o); },
  923. $Pointer: function(name,o){ return this.$inherited(name,rtl.tTypeInfoPointer,o); },
  924. $Interface: function(name,o){ return this.$Scope(name,rtl.tTypeInfoInterface,o); },
  925. },
  926. newTIParam: function(param){
  927. // param is an array, 0=name, 1=type, 2=optional flags
  928. var t = {
  929. name: param[0],
  930. typeinfo: param[1],
  931. flags: (rtl.isNumber(param[2]) ? param[2] : 0),
  932. };
  933. return t;
  934. },
  935. newTIParams: function(list){
  936. // list: optional array of [paramname,typeinfo,optional flags]
  937. var params = [];
  938. if (rtl.isArray(list)){
  939. for (var i=0; i<list.length; i++) params.push(rtl.newTIParam(list[i]));
  940. };
  941. return params;
  942. },
  943. newTIProcSig: function(params,result,flags){
  944. var s = {
  945. params: rtl.newTIParams(params),
  946. resulttype: result,
  947. flags: flags
  948. };
  949. return s;
  950. },
  951. }