Pas2JS_WebGL.js 59 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763
  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("SysUtils",["System","JS"],function () {
  1195. "use strict";
  1196. var $mod = this;
  1197. this.IntToStr = function (Value) {
  1198. var Result = "";
  1199. Result = "" + Value;
  1200. return Result;
  1201. };
  1202. rtl.createClass($mod,"TFormatSettings",pas.System.TObject,function () {
  1203. });
  1204. this.FormatSettings = null;
  1205. $mod.$init = function () {
  1206. $mod.FormatSettings = $mod.TFormatSettings.$create("Create");
  1207. };
  1208. });
  1209. rtl.module("math",["System","SysUtils"],function () {
  1210. "use strict";
  1211. var $mod = this;
  1212. this.DegToRad = function (deg) {
  1213. var Result = 0.0;
  1214. Result = deg * (Math.PI / 180.0);
  1215. return Result;
  1216. };
  1217. });
  1218. rtl.module("Mat4",["System","browserconsole","JS","math"],function () {
  1219. "use strict";
  1220. var $mod = this;
  1221. var $impl = $mod.$impl;
  1222. rtl.createClass($mod,"TMat4",pas.System.TObject,function () {
  1223. this.$init = function () {
  1224. pas.System.TObject.$init.call(this);
  1225. this.RawComponents = rtl.arraySetLength(null,0.0,4,4);
  1226. };
  1227. this.$final = function () {
  1228. this.RawComponents = undefined;
  1229. pas.System.TObject.$final.call(this);
  1230. };
  1231. this.Identity = function () {
  1232. this.RawComponents[0][0] = 1.0;
  1233. this.RawComponents[0][1] = 0.0;
  1234. this.RawComponents[0][2] = 0.0;
  1235. this.RawComponents[0][3] = 0.0;
  1236. this.RawComponents[1][0] = 0.0;
  1237. this.RawComponents[1][1] = 1.0;
  1238. this.RawComponents[1][2] = 0.0;
  1239. this.RawComponents[1][3] = 0.0;
  1240. this.RawComponents[2][0] = 0.0;
  1241. this.RawComponents[2][1] = 0.0;
  1242. this.RawComponents[2][2] = 1.0;
  1243. this.RawComponents[2][3] = 0.0;
  1244. this.RawComponents[3][0] = 0.0;
  1245. this.RawComponents[3][1] = 0.0;
  1246. this.RawComponents[3][2] = 0.0;
  1247. this.RawComponents[3][3] = 1.0;
  1248. };
  1249. this.Translate = function (tx, ty, tz) {
  1250. this.RawComponents[0][0] = 1.0;
  1251. this.RawComponents[0][1] = 0.0;
  1252. this.RawComponents[0][2] = 0.0;
  1253. this.RawComponents[0][3] = 0.0;
  1254. this.RawComponents[1][0] = 0.0;
  1255. this.RawComponents[1][1] = 1.0;
  1256. this.RawComponents[1][2] = 0.0;
  1257. this.RawComponents[1][3] = 0.0;
  1258. this.RawComponents[2][0] = 0.0;
  1259. this.RawComponents[2][1] = 0.0;
  1260. this.RawComponents[2][2] = 1.0;
  1261. this.RawComponents[2][3] = 0.0;
  1262. this.RawComponents[3][0] = tx;
  1263. this.RawComponents[3][1] = ty;
  1264. this.RawComponents[3][2] = tz;
  1265. this.RawComponents[3][3] = 1.0;
  1266. };
  1267. this.RotateZ = function (Angle) {
  1268. $mod.SinCos(Angle,{a: 1, p: this.RawComponents[0], get: function () {
  1269. return this.p[this.a];
  1270. }, set: function (v) {
  1271. this.p[this.a] = v;
  1272. }},{a: 0, p: this.RawComponents[0], get: function () {
  1273. return this.p[this.a];
  1274. }, set: function (v) {
  1275. this.p[this.a] = v;
  1276. }});
  1277. this.RawComponents[0][2] = 0.0;
  1278. this.RawComponents[0][3] = 0.0;
  1279. this.RawComponents[1][0] = -this.RawComponents[0][1];
  1280. this.RawComponents[1][1] = this.RawComponents[0][0];
  1281. this.RawComponents[1][2] = 0.0;
  1282. this.RawComponents[1][3] = 0.0;
  1283. this.RawComponents[2][0] = 0.0;
  1284. this.RawComponents[2][1] = 0.0;
  1285. this.RawComponents[2][2] = 1.0;
  1286. this.RawComponents[2][3] = 0.0;
  1287. this.RawComponents[3][0] = 0.0;
  1288. this.RawComponents[3][1] = 0.0;
  1289. this.RawComponents[3][2] = 0.0;
  1290. this.RawComponents[3][3] = 1.0;
  1291. };
  1292. this.Ortho = function (Left, Right, Bottom, Top, zNear, zFar) {
  1293. var rml = 0.0;
  1294. var tmb = 0.0;
  1295. var fmn = 0.0;
  1296. rml = Right - Left;
  1297. tmb = Top - Bottom;
  1298. fmn = zFar - zNear;
  1299. this.RawComponents[0][0] = 2.0 / rml;
  1300. this.RawComponents[0][1] = 0.0;
  1301. this.RawComponents[0][2] = 0.0;
  1302. this.RawComponents[0][3] = 0.0;
  1303. this.RawComponents[1][0] = 0.0;
  1304. this.RawComponents[1][1] = 2.0 / tmb;
  1305. this.RawComponents[1][2] = 0.0;
  1306. this.RawComponents[1][3] = 0.0;
  1307. this.RawComponents[2][0] = 0.0;
  1308. this.RawComponents[2][1] = 0.0;
  1309. this.RawComponents[2][2] = -2.0 / fmn;
  1310. this.RawComponents[2][3] = 0.0;
  1311. this.RawComponents[3][0] = -(Right + Left) / rml;
  1312. this.RawComponents[3][1] = -(Top + Bottom) / tmb;
  1313. this.RawComponents[3][2] = -(zFar + zNear) / fmn;
  1314. this.RawComponents[3][3] = 1.0;
  1315. };
  1316. this.Multiply = function (m) {
  1317. var Result = null;
  1318. Result = $mod.TMat4.$create("Identity");
  1319. Result.RawComponents[0][0] = (((m.RawComponents[0][0] * this.RawComponents[0][0]) + (m.RawComponents[0][1] * this.RawComponents[1][0])) + (m.RawComponents[0][2] * this.RawComponents[2][0])) + (m.RawComponents[0][3] * this.RawComponents[3][0]);
  1320. Result.RawComponents[0][1] = (((m.RawComponents[0][0] * this.RawComponents[0][1]) + (m.RawComponents[0][1] * this.RawComponents[1][1])) + (m.RawComponents[0][2] * this.RawComponents[2][1])) + (m.RawComponents[0][3] * this.RawComponents[3][1]);
  1321. Result.RawComponents[0][2] = (((m.RawComponents[0][0] * this.RawComponents[0][2]) + (m.RawComponents[0][1] * this.RawComponents[1][2])) + (m.RawComponents[0][2] * this.RawComponents[2][2])) + (m.RawComponents[0][3] * this.RawComponents[3][2]);
  1322. Result.RawComponents[0][3] = (((m.RawComponents[0][0] * this.RawComponents[0][3]) + (m.RawComponents[0][1] * this.RawComponents[1][3])) + (m.RawComponents[0][2] * this.RawComponents[2][3])) + (m.RawComponents[0][3] * this.RawComponents[3][3]);
  1323. Result.RawComponents[1][0] = (((m.RawComponents[1][0] * this.RawComponents[0][0]) + (m.RawComponents[1][1] * this.RawComponents[1][0])) + (m.RawComponents[1][2] * this.RawComponents[2][0])) + (m.RawComponents[1][3] * this.RawComponents[3][0]);
  1324. Result.RawComponents[1][1] = (((m.RawComponents[1][0] * this.RawComponents[0][1]) + (m.RawComponents[1][1] * this.RawComponents[1][1])) + (m.RawComponents[1][2] * this.RawComponents[2][1])) + (m.RawComponents[1][3] * this.RawComponents[3][1]);
  1325. Result.RawComponents[1][2] = (((m.RawComponents[1][0] * this.RawComponents[0][2]) + (m.RawComponents[1][1] * this.RawComponents[1][2])) + (m.RawComponents[1][2] * this.RawComponents[2][2])) + (m.RawComponents[1][3] * this.RawComponents[3][2]);
  1326. Result.RawComponents[1][3] = (((m.RawComponents[1][0] * this.RawComponents[0][3]) + (m.RawComponents[1][1] * this.RawComponents[1][3])) + (m.RawComponents[1][2] * this.RawComponents[2][3])) + (m.RawComponents[1][3] * this.RawComponents[3][3]);
  1327. Result.RawComponents[2][0] = (((m.RawComponents[2][0] * this.RawComponents[0][0]) + (m.RawComponents[2][1] * this.RawComponents[1][0])) + (m.RawComponents[2][2] * this.RawComponents[2][0])) + (m.RawComponents[2][3] * this.RawComponents[3][0]);
  1328. Result.RawComponents[2][1] = (((m.RawComponents[2][0] * this.RawComponents[0][1]) + (m.RawComponents[2][1] * this.RawComponents[1][1])) + (m.RawComponents[2][2] * this.RawComponents[2][1])) + (m.RawComponents[2][3] * this.RawComponents[3][1]);
  1329. Result.RawComponents[2][2] = (((m.RawComponents[2][0] * this.RawComponents[0][2]) + (m.RawComponents[2][1] * this.RawComponents[1][2])) + (m.RawComponents[2][2] * this.RawComponents[2][2])) + (m.RawComponents[2][3] * this.RawComponents[3][2]);
  1330. Result.RawComponents[2][3] = (((m.RawComponents[2][0] * this.RawComponents[0][3]) + (m.RawComponents[2][1] * this.RawComponents[1][3])) + (m.RawComponents[2][2] * this.RawComponents[2][3])) + (m.RawComponents[2][3] * this.RawComponents[3][3]);
  1331. Result.RawComponents[3][0] = (((m.RawComponents[3][0] * this.RawComponents[0][0]) + (m.RawComponents[3][1] * this.RawComponents[1][0])) + (m.RawComponents[3][2] * this.RawComponents[2][0])) + (m.RawComponents[3][3] * this.RawComponents[3][0]);
  1332. Result.RawComponents[3][1] = (((m.RawComponents[3][0] * this.RawComponents[0][1]) + (m.RawComponents[3][1] * this.RawComponents[1][1])) + (m.RawComponents[3][2] * this.RawComponents[2][1])) + (m.RawComponents[3][3] * this.RawComponents[3][1]);
  1333. Result.RawComponents[3][2] = (((m.RawComponents[3][0] * this.RawComponents[0][2]) + (m.RawComponents[3][1] * this.RawComponents[1][2])) + (m.RawComponents[3][2] * this.RawComponents[2][2])) + (m.RawComponents[3][3] * this.RawComponents[3][2]);
  1334. Result.RawComponents[3][3] = (((m.RawComponents[3][0] * this.RawComponents[0][3]) + (m.RawComponents[3][1] * this.RawComponents[1][3])) + (m.RawComponents[3][2] * this.RawComponents[2][3])) + (m.RawComponents[3][3] * this.RawComponents[3][3]);
  1335. return Result;
  1336. };
  1337. this.CopyList = function () {
  1338. var Result = [];
  1339. var x = 0;
  1340. var y = 0;
  1341. var list = null;
  1342. list = new Array();
  1343. for (x = 0; x <= 3; x++) for (y = 0; y <= 3; y++) list.push(this.RawComponents[x][y]);
  1344. Result = list;
  1345. return Result;
  1346. };
  1347. });
  1348. this.SinCos = function (angle, sinus, cosinus) {
  1349. sinus.set(Math.sin(angle));
  1350. cosinus.set(Math.cos(angle));
  1351. };
  1352. $mod.$init = function () {
  1353. $impl.Matrix4x4Identity = $mod.TMat4.$create("Identity");
  1354. };
  1355. },null,function () {
  1356. "use strict";
  1357. var $mod = this;
  1358. var $impl = $mod.$impl;
  1359. $impl.Matrix4x4Identity = null;
  1360. });
  1361. rtl.module("MemoryBuffer",["System","JS"],function () {
  1362. "use strict";
  1363. var $mod = this;
  1364. rtl.createClass($mod,"TMemoryBuffer",pas.System.TObject,function () {
  1365. this.$init = function () {
  1366. pas.System.TObject.$init.call(this);
  1367. this.byteBuffer = null;
  1368. this.byteOffset = 0;
  1369. this.floatBuffer = null;
  1370. };
  1371. this.$final = function () {
  1372. this.byteBuffer = undefined;
  1373. this.floatBuffer = undefined;
  1374. pas.System.TObject.$final.call(this);
  1375. };
  1376. this.Create$1 = function (size) {
  1377. this.byteBuffer = new Uint8Array(size);
  1378. };
  1379. this.AddBytes = function (count, data) {
  1380. this.byteBuffer.set(data,this.byteOffset);
  1381. this.byteOffset = this.byteOffset + (count * 1);
  1382. };
  1383. this.AddFloats = function (count, data) {
  1384. var floatOffset = 0;
  1385. floatOffset = Math.floor(this.byteOffset / 4);
  1386. if (this.floatBuffer === null) this.floatBuffer = new Float32Array(this.byteBuffer.buffer,0,Math.floor(this.byteBuffer.byteLength / 4));
  1387. this.floatBuffer.set(data,floatOffset);
  1388. this.byteOffset = this.byteOffset + (count * 4);
  1389. };
  1390. });
  1391. });
  1392. rtl.module("webgl",["System","JS","Web"],function () {
  1393. "use strict";
  1394. var $mod = this;
  1395. });
  1396. rtl.module("GLTypes",["System","webgl","JS"],function () {
  1397. "use strict";
  1398. var $mod = this;
  1399. this.TVec2 = function (s) {
  1400. if (s) {
  1401. this.x = s.x;
  1402. this.y = s.y;
  1403. } else {
  1404. this.x = 0.0;
  1405. this.y = 0.0;
  1406. };
  1407. this.$equal = function (b) {
  1408. return (this.x === b.x) && (this.y === b.y);
  1409. };
  1410. };
  1411. this.V2 = function (x, y) {
  1412. var Result = new $mod.TVec2();
  1413. Result.x = x;
  1414. Result.y = y;
  1415. return Result;
  1416. };
  1417. this.ToFloats$1 = function (v) {
  1418. var Result = [];
  1419. Result = rtl.arraySetLength(Result,0.0,2);
  1420. Result[0] = v.x;
  1421. Result[1] = v.y;
  1422. return Result;
  1423. };
  1424. this.RGBAb = function (r, g, b, a) {
  1425. var Result = [];
  1426. Result[0] = r;
  1427. Result[1] = g;
  1428. Result[2] = b;
  1429. Result[3] = a;
  1430. return Result;
  1431. };
  1432. });
  1433. rtl.module("GLUtils",["System","Mat4","GLTypes","browserconsole","Web","webgl","JS","math","SysUtils"],function () {
  1434. "use strict";
  1435. var $mod = this;
  1436. var $impl = $mod.$impl;
  1437. rtl.createClass($mod,"TShader",pas.System.TObject,function () {
  1438. this.$init = function () {
  1439. pas.System.TObject.$init.call(this);
  1440. this.gl = null;
  1441. this.vertexShader = null;
  1442. this.fragmentShader = null;
  1443. this.programID = null;
  1444. };
  1445. this.$final = function () {
  1446. this.gl = undefined;
  1447. this.vertexShader = undefined;
  1448. this.fragmentShader = undefined;
  1449. this.programID = undefined;
  1450. pas.System.TObject.$final.call(this);
  1451. };
  1452. this.Create$1 = function (context, vertexShaderSource, fragmentShaderSource) {
  1453. this.gl = context;
  1454. this.vertexShader = this.CreateShader(this.gl.VERTEX_SHADER,vertexShaderSource);
  1455. this.fragmentShader = this.CreateShader(this.gl.FRAGMENT_SHADER,fragmentShaderSource);
  1456. };
  1457. this.Compile = function () {
  1458. this.programID = this.gl.createProgram();
  1459. this.gl.attachShader(this.programID,this.vertexShader);
  1460. this.gl.attachShader(this.programID,this.fragmentShader);
  1461. };
  1462. this.Link = function () {
  1463. this.gl.linkProgram(this.programID);
  1464. if (!this.gl.getProgramParameter(this.programID,this.gl.LINK_STATUS)) {
  1465. $impl.Fatal(this.gl.getProgramInfoLog(this.programID));
  1466. };
  1467. };
  1468. this.Use = function () {
  1469. this.gl.useProgram(this.programID);
  1470. };
  1471. this.BindAttribLocation = function (index, name) {
  1472. this.gl.bindAttribLocation(this.programID,index,name);
  1473. };
  1474. this.SetUniformMat4 = function (name, value) {
  1475. var list = [];
  1476. list = value.CopyList();
  1477. this.gl.uniformMatrix4fv(this.GetUniformLocation(name),false,list);
  1478. $impl.GLFatal(this.gl,"gl.uniformMatrix4fv");
  1479. };
  1480. this.GetUniformLocation = function (name) {
  1481. var Result = null;
  1482. Result = this.gl.getUniformLocation(this.programID,name);
  1483. $impl.GLFatal(this.gl,"gl.getUniformLocation");
  1484. return Result;
  1485. };
  1486. this.CreateShader = function (theType, source) {
  1487. var Result = null;
  1488. var shader = null;
  1489. shader = this.gl.createShader(theType);
  1490. if (shader === null) $impl.Fatal("create shader failed");
  1491. this.gl.shaderSource(shader,source);
  1492. this.gl.compileShader(shader);
  1493. if (this.gl.getShaderParameter(shader,this.gl.COMPILE_STATUS)) {
  1494. return shader;
  1495. } else {
  1496. $impl.Fatal$1(this.gl.getShaderInfoLog(shader));
  1497. };
  1498. return Result;
  1499. };
  1500. });
  1501. this.GLSizeof = function (glType) {
  1502. var Result = 0;
  1503. var $tmp1 = glType;
  1504. if (($tmp1 === WebGLRenderingContext.UNSIGNED_BYTE) || ($tmp1 === WebGLRenderingContext.BYTE)) {
  1505. Result = 1}
  1506. else if (($tmp1 === WebGLRenderingContext.SHORT) || ($tmp1 === WebGLRenderingContext.UNSIGNED_SHORT)) {
  1507. Result = 2}
  1508. else if (($tmp1 === WebGLRenderingContext.INT) || ($tmp1 === WebGLRenderingContext.UNSIGNED_INT)) {
  1509. Result = 4}
  1510. else if ($tmp1 === WebGLRenderingContext.FLOAT) {
  1511. Result = 4}
  1512. else {
  1513. $impl.Fatal("GLSizeof type is invalid.");
  1514. };
  1515. return Result;
  1516. };
  1517. },null,function () {
  1518. "use strict";
  1519. var $mod = this;
  1520. var $impl = $mod.$impl;
  1521. $impl.Fatal = function (messageString) {
  1522. pas.System.Writeln("*** FATAL: ",messageString);
  1523. return;
  1524. };
  1525. $impl.Fatal$1 = function (messageString) {
  1526. pas.System.Writeln("*** FATAL: ",messageString);
  1527. return;
  1528. };
  1529. $impl.GLFatal = function (gl, messageString) {
  1530. var error = 0;
  1531. error = gl.getError();
  1532. if (error !== WebGLRenderingContext.NO_ERROR) {
  1533. var $tmp1 = error;
  1534. if ($tmp1 === WebGLRenderingContext.INVALID_VALUE) {
  1535. messageString = messageString + " (GL_INVALID_VALUE)"}
  1536. else if ($tmp1 === WebGLRenderingContext.INVALID_OPERATION) {
  1537. messageString = messageString + " (GL_INVALID_OPERATION)"}
  1538. else if ($tmp1 === WebGLRenderingContext.INVALID_ENUM) {
  1539. messageString = messageString + " (GL_INVALID_ENUM)"}
  1540. else {
  1541. messageString = (messageString + " ") + pas.SysUtils.IntToStr(error);
  1542. };
  1543. $impl.Fatal(messageString);
  1544. };
  1545. };
  1546. });
  1547. rtl.module("program",["System","Mat4","MemoryBuffer","GLUtils","GLTypes","SysUtils","browserconsole","Web","webgl","JS","math"],function () {
  1548. "use strict";
  1549. var $mod = this;
  1550. this.GLVertex2 = function (s) {
  1551. if (s) {
  1552. this.pos = new pas.GLTypes.TVec2(s.pos);
  1553. this.color = s.color;
  1554. } else {
  1555. this.pos = new pas.GLTypes.TVec2();
  1556. this.color = [];
  1557. };
  1558. this.$equal = function (b) {
  1559. return this.pos.$equal(b.pos) && (this.color === b.color);
  1560. };
  1561. };
  1562. this.kSIZEOF_VERTEX = 12;
  1563. this.GetVertexData = function () {
  1564. var Result = null;
  1565. var buffer = null;
  1566. var verts = null;
  1567. var v = new $mod.GLVertex2();
  1568. var i = 0;
  1569. verts = new Array();
  1570. v.pos = new pas.GLTypes.TVec2(pas.GLTypes.V2(0,0));
  1571. v.color = pas.GLTypes.RGBAb(255,0,0,255);
  1572. verts.push(new $mod.GLVertex2(v));
  1573. v.pos = new pas.GLTypes.TVec2(pas.GLTypes.V2(0,100));
  1574. v.color = pas.GLTypes.RGBAb(0,255,0,255);
  1575. verts.push(new $mod.GLVertex2(v));
  1576. v.pos = new pas.GLTypes.TVec2(pas.GLTypes.V2(100,100));
  1577. v.color = pas.GLTypes.RGBAb(0,0,255,255);
  1578. verts.push(new $mod.GLVertex2(v));
  1579. buffer = pas.MemoryBuffer.TMemoryBuffer.$create("Create$1",[12 * verts.length]);
  1580. for (var $l1 = 0, $end2 = verts.length - 1; $l1 <= $end2; $l1++) {
  1581. i = $l1;
  1582. v = new $mod.GLVertex2(rtl.getObject(verts[i]));
  1583. buffer.AddFloats(2,pas.GLTypes.ToFloats$1(new pas.GLTypes.TVec2(v.pos)));
  1584. buffer.AddBytes(4,v.color);
  1585. };
  1586. Result = buffer.byteBuffer;
  1587. return Result;
  1588. };
  1589. this.nextTime = 0;
  1590. this.deltaTime = 0;
  1591. this.gl = null;
  1592. this.shader = null;
  1593. this.projTransform = null;
  1594. this.viewTransform = null;
  1595. this.modelTransform = null;
  1596. this.rotateAngle = 0;
  1597. this.UpdateCanvas = function (time) {
  1598. var now = 0.0;
  1599. now = time * 0.001;
  1600. $mod.deltaTime = now - $mod.nextTime;
  1601. $mod.nextTime = now;
  1602. $mod.modelTransform = pas.Mat4.TMat4.$create("Identity");
  1603. $mod.modelTransform = $mod.modelTransform.Multiply(pas.Mat4.TMat4.$create("Translate",[100,100,0]));
  1604. $mod.modelTransform = $mod.modelTransform.Multiply(pas.Mat4.TMat4.$create("RotateZ",[pas.math.DegToRad($mod.rotateAngle)]));
  1605. $mod.rotateAngle = $mod.rotateAngle + (20 * $mod.deltaTime);
  1606. $mod.shader.SetUniformMat4("modelTransform",$mod.modelTransform);
  1607. $mod.gl.clear($mod.gl.COLOR_BUFFER_BIT);
  1608. $mod.gl.drawArrays($mod.gl.TRIANGLES,0,3);
  1609. window.requestAnimationFrame($mod.UpdateCanvas);
  1610. };
  1611. this.canvas = null;
  1612. this.stride = 0;
  1613. this.offset = 0;
  1614. this.vertexShaderSource = "";
  1615. this.fragmentShaderSource = "";
  1616. this.buffer = null;
  1617. $mod.$main = function () {
  1618. $mod.canvas = document.createElement("canvas");
  1619. $mod.canvas.width = 300;
  1620. $mod.canvas.height = 300;
  1621. document.body.appendChild($mod.canvas);
  1622. $mod.gl = $mod.canvas.getContext("webgl");
  1623. if ($mod.gl === null) {
  1624. pas.System.Writeln("failed to load webgl!");
  1625. return;
  1626. };
  1627. $mod.vertexShaderSource = document.getElementById("vertex.glsl").textContent;
  1628. $mod.fragmentShaderSource = document.getElementById("fragment.glsl").textContent;
  1629. $mod.shader = pas.GLUtils.TShader.$create("Create$1",[$mod.gl,$mod.vertexShaderSource,$mod.fragmentShaderSource]);
  1630. $mod.shader.Compile();
  1631. $mod.shader.BindAttribLocation(0,"in_position");
  1632. $mod.shader.BindAttribLocation(1,"in_color");
  1633. $mod.shader.Link();
  1634. $mod.shader.Use();
  1635. $mod.gl.clearColor(0.9,0.9,0.9,1);
  1636. $mod.gl.viewport(0,0,$mod.canvas.width,$mod.canvas.height);
  1637. $mod.gl.clear($mod.gl.COLOR_BUFFER_BIT);
  1638. $mod.projTransform = pas.Mat4.TMat4.$create("Ortho",[0,$mod.gl.canvas.width,$mod.gl.canvas.height,0,-1,1]);
  1639. $mod.viewTransform = pas.Mat4.TMat4.$create("Identity");
  1640. $mod.modelTransform = pas.Mat4.TMat4.$create("Identity");
  1641. $mod.shader.SetUniformMat4("projTransform",$mod.projTransform);
  1642. $mod.shader.SetUniformMat4("viewTransform",$mod.viewTransform);
  1643. $mod.shader.SetUniformMat4("modelTransform",$mod.modelTransform);
  1644. $mod.buffer = $mod.gl.createBuffer();
  1645. $mod.gl.bindBuffer($mod.gl.ARRAY_BUFFER,$mod.buffer);
  1646. $mod.gl.bufferData($mod.gl.ARRAY_BUFFER,$mod.GetVertexData(),$mod.gl.STATIC_DRAW);
  1647. $mod.offset = 0;
  1648. $mod.stride = 12;
  1649. $mod.gl.enableVertexAttribArray(0);
  1650. $mod.gl.vertexAttribPointer(0,2,$mod.gl.FLOAT,false,$mod.stride,$mod.offset);
  1651. $mod.offset += pas.GLUtils.GLSizeof($mod.gl.FLOAT) * 2;
  1652. $mod.gl.enableVertexAttribArray(1);
  1653. $mod.gl.vertexAttribPointer(1,4,$mod.gl.UNSIGNED_BYTE,true,$mod.stride,$mod.offset);
  1654. $mod.offset += pas.GLUtils.GLSizeof($mod.gl.UNSIGNED_BYTE) * 4;
  1655. window.requestAnimationFrame($mod.UpdateCanvas);
  1656. };
  1657. });