@live-codes/pascal-wasm 0.1.0

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