Pas2JS_WebGL_Minimal.js 45 KB

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