-
Notifications
You must be signed in to change notification settings - Fork 0
/
jeep.js
3139 lines (3019 loc) · 117 KB
/
jeep.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* 2018 August
--------------------------------------------------------------------
Designed and Developed by Vinay.M.S
--------------------------------------------------------------------
JEEP is a C++ inspired framework that brings to robustness JavaScript toenable software engineering.
There are far too many features to list here, so highlighting only eight most attractive ones.
Qualitative
* robustness
* maintainable and extensible classes
* intuitive class description
* improved productivity and performance
Technical
* multiple inheritance
* virtual and abstract functions
* public, protected and private members
* development and production mode split
--------------------------------------------------------------------
This class is usable subject to MIT license.
--------------------------------------------------------------------
The MIT License (MIT) http://opensource.org/licenses/MIT
Copyright (c) 2018 Vinay.M.S
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
-------------------------------------------------------------------
*/
/*
TODO: (star means in no particular order)
1. optimize object layout by adding functions to prototype so that instantiation will be fast for class and struct
2. class constructor directives other than managed, esp arg related ones
3. descriptive error messages like Jeep2017
4. *add more devmode checks in implementation (I have a hunch its incomplete)
5. *extend structure
6. *dynamic cast class
7. *more refactoring with better names
*/
JEEP = {
Utils: {},
InitFramework: function(){
this.impl.fx = this;
this.impl.init(this.Utils)
},
// printer = function(c){}
SetStdErr: function(printer){
let old = this.impl.stderr;
this.impl.stderr = printer;
return old;
},
// info = {mode, client, flags}
CreateEnvironment: function(info){
let fx = this;
let impl = this.impl;
let badProps = impl.validateProps(info, "mode, client, flags");
if(badProps)
impl.abort("invalid-props", {api: "CreateEnvironment", props: badProps.join(',')})
if(!info.mode || !info.client)
impl.abort("invalid-env-desc", {})
if(info.mode != "development-mode" && info.mode != "production-mode")
impl.abort("invalid-env-prop", {prop: "mode", propval: info.mode})
if(info.client != "jeep-aware" && info.client != "jeep-agnostic")
impl.abort("invalid-env-prop", {prop: "client", propval: info.client})
let procres = null;
if(info.flags){
procres = impl.envFlagProc.Process({markError: true, flags: info.flags});
if(procres.errors.length > 0)
impl.abort("invalid-env-prop", {prop: "flags", propval: info.flags})
}
let shared = {internal: false};// a hack to reuse code
let env = {
IsDevMode: function(){return "development-mode" == info.mode},
IsClientJeepAware: function(){return "jeep-aware" == info.clientType},
CreateRecordDef: function(name, def){
impl.validateName(this, "record", name, shared.internal);
return impl.createRecStructDef("CreateRecordDef", this, name, def, true);
},
RegisterRecordDef: function(name, def){
impl.validateName(this, "record", name, shared.internal);
impl.validateRegister(this, "record", name, shared.internal);
let rec = impl.createRecStructDef("RegisterRecordDef", this, name, def, true)
impl.defstore[name] = impl.objRecord.InitNew({def: rec, type: impl.objFlags.record});
},
CreateStructDef: function(name, def){
impl.validateName(this, "struct", name, shared.internal);
return impl.createRecStructDef("CreateStructDef", this, name, def, false);
},
RegisterStructDef: function(name, def){
impl.validateName(this, "struct", name, shared.internal);
impl.validateRegister(this, "struct", name, shared.internal);
let rec = impl.createRecStructDef("RegisterStructDef", this, name, def, false);
impl.defstore[name] = impl.objRecord.InitNew({def: rec, type: impl.objFlags.struct});
},
CreateClassDef: function(name, def){
impl.validateName(this, "class", name, shared.internal);
return impl.processClassDef("CreateClassDef", name, def, env);
},
RegisterClassDef: function(name, def){
impl.validateRegister(this, "class", name, shared.internal);
let d = impl.processClassDef("RegisterClassDef", name, def, env);
impl.defstore[name] = impl.objRecord.InitNew({def: d, type: impl.objFlags.class});
},
CreateClassWrapper: function(classname, def){
return impl.createClassWrapper(this, classname, def);
},
CreateField: function(where, name, def){
impl.createField(env, where, name, def);
},
RegisterField: function(name, def){
let where = {};
impl.createField(env, where, name, def);
impl.defstore[name] = impl.objRecord.InitNew({def:where, type: impl.objFlags.field});
},
CreateGroup: function(where, name, def){
impl.createGroup("CreateGroup", where, name, def, env);
},
RegisterGroup: function(name, def){
let where = {};
impl.createGroup("RegisterGroup", where, name, def, env);
// InitNew always clones objects. If the group is in produnction mode, the variable are
// added to 'where', and cloning would be detremental because the group Init works on the
// original object while the other functions work on the cloned one. So, add the 'where'
// separately.
let rec = impl.objRecord.InitNew({type: impl.objFlags.group});
rec.def = where;
impl.defstore[name] = rec;
},
CreateNamespace: function(){
let env = this;
let pref = ".ns"+(impl.namespaceCounter++)+".";
let Namespace = {
GetEnvironment: function(){return env},
Partition: function(names){
impl.partitionNamespace(env, names, this);
},
Flatten: function(){
if(!this.$) return null;
let errors = [], flatobj = {};
impl.flattenNamespace(env, this.$, flatobj, errors);
if(errors.length > 0)
impl.abort("namespace-flatten-duplicate", {names: errors.join(',')})
return flatobj;
},
}
let fxapi = ["ObjectDefExists", "GetObjectDef"];
for(let k = 0; k<fxapi.length; k++){
Namespace[fxapi[k]] = function(name){
return fx[fxapi[k]](pref+name)
}
}
let envapi = ["RegisterRecordDef", "RegisterStructDef", "RegisterClassDef", "RegisterField", "RegisterGroup"]
for(let k = 0; k<envapi.length; k++){
Namespace[envapi[k]] = function(name, def){
shared.internal = true;
let ex = null;
try{env[envapi[k]](pref+name, def)}catch(e){ex=e}
shared.internal = false;
if(ex) throw ex;
}
}
return Namespace;
},
GetLibrary: function(name){
return impl.getLibrary.apply({impl: impl, env: this},arguments);
},
impl: {
getFlags: function(){return procres ? procres.flags : 0},
}
}
return env;
},
ObjectDefExists: function(name){
return null !== this.impl.getDef(name, true)// nothrow
},
GetObjectDef: function(name){
return this.impl.getDef(name)
},
Typedef: function(newtype, existing){
this.impl.typeDef(newtype, existing);
},
RegisterLibrary: function(name, initFunc){
// To allow using this without having to remember to initialte the framework internally.
// Since initialization is idempotent, this causes no problem and makes JEEP user friendly.
this.InitFramework();
this.impl.registerLibrary(name, initFunc);
},
RegisterLibraryBuilder: function(name, libname, initFunc){
// To allow using this without having to remember to initialte the framework internally.
// Since initialization is idempotent, this causes no problem and makes JEEP user friendly.
this.InitFramework();
this.impl.registerLibraryBuilder(name, libname, initFunc);
},
Equal: function(a, b){
return this.impl.equal(a,b, true);
},
ValEqual: function(a, b){
return this.impl.equal(a,b, false);
},
impl: {
//----------------------------------------
// record & structure
//----------------------------------------
createRecStructDef: function(api, env, name, def, isRec){
let devmode = !env || env.IsDevMode();
let instname = name.substring(name.lastIndexOf('.')+1);
if(devmode){
let props = isRec ? "CONSTRUCTOR, DESTRUCTOR, PMC, PUBLIC, PROTECTED, PRIVATE, STATIC" : "DESTRUCTOR, EXTENDS, PUBLIC, PROTECTED, PRIVATE, STATIC";
let badProps = this.validateProps(def, props, true)
if(badProps)
this.abort("invalid-props", {api: api, props: badProps.join(',')})
}
let res = this.splitVarFunc(def);
if(devmode && !res.vars)
this.addSyntaxError("recstruct-novars", {name: instname, type: (isRec?"record":"struct")})
if(devmode && !isRec && !res.funcs)
this.addSyntaxError("struct-nofuncs", {name: instname})
if(isRec){
if(res.vars && res.vars.EXTENDS){
let vars = this.getExtendedRecordVars(env, instname, def.EXTENDS, res.vars);// this also validates if devmode
if(vars)
this.merge(vars, res.vars);
}
this.merge(res.funcs, res.vars);// for record everything is 'variable'
}
// record ends at this point, so try to abort if needed
if(devmode && isRec)
this.abortOnSyntaxError("record", instname);
let impl = this;
let RecStruct = {};
let jdef = this.createJeepDef({type: isRec?"record":"struct", name: name, desc: def, defobj: RecStruct});
RecStruct._jeepdef_ = jdef;
if(isRec){
jdef.vars = res.vars;
}
else {
if(res.vars)
jdef.vars = this.undecorateVars("struct", env, res.vars);
if(res.funcs){
let ctorInfo = this.getCtor("struct", env, res.funcs);
if(ctorInfo){
jdef.ctor = ctorInfo.ctor
delete res.funcs[ctorInfo.name];// let it not be added to the instance
if(devmode && (ctorInfo.undec.decflags != 0))
this.addSyntaxError("struct-ctor-directives", {})
}
jdef.funcs = this.undecorateFuncs("struct", env, res.funcs);
}
let pmc = this.getPMCFlags(env, def.PMC, this.fxFlags.pmc_memaccess);
if(devmode || (pmc & this.fxFlags.pmc_memaccess))
jdef.priv = this.restrictedMembersExist(jdef.funcs) || this.restrictedMembersExist(jdef.vars);
}
// struct definition validation ends here, check before proceeding
this.abortOnSyntaxError("struct", instname);
let commonAccess = this.createRecursiveFlag();
RecStruct.New = function(){
if(arguments.length > 0 && !jdef.ctor)// both record and struct without ctor
impl.abort("new-noctor", {name: instname, type: isRec?"record":"struct"})
let inst = {};
let access = impl.instRecStruct(env, commonAccess, instname, inst, RecStruct, isRec);
if(jdef.ctor){
if(access) access.add();
commonAccess.add();
jdef.ctor.apply(inst, arguments);
if(access) access.remove();
commonAccess.remove();
}
Object.seal(inst);
return inst;
},
RecStruct.InitNew = function(vars){
if((!env || env.IsDevMode())){
if(arguments.length === 0)
impl.abort("initnew-noargs", {name: instname, type: isRec?"record":"struct"})
if(arguments.length !== 1)
impl.abort("initnew-moreargs", {name: instname, type: isRec?"record":"struct"})
}
let inst = {};
let access = impl.instRecStruct(env, commonAccess, instname, inst, RecStruct, isRec, vars);
if(jdef.ctor){
if(access) access.add();
commonAccess.add();
inst.$initnew = true;
jdef.ctor.apply(inst);
delete inst.$initnew;
if(access) access.remove();
commonAccess.remove();
}
Object.seal(inst);
return inst;
},
RecStruct.InstanceOf = function(inst){
return inst.$def == RecStruct;
}
return RecStruct;
},
getExtendedRecordVars: function(env, name, ext, ref){
let devmode = !env || env.IsDevMode();
let vars = {}, dupnames = [], badExt = false;
for(let i = 0; i<ext.length; i++){
let e = ext[i];
let btype = typeof e;
if(btype !== "string"){
if(btype !== "object" || e._jeepdef_ === undefined){
badExt = true;// mark here, add the error at the end
continue;
}
brec = e;
}
else
brec = this.getDef(e, true);// nothrow
if(brec === null){
if(devmode)
this.addSyntaxError("rec-extends-nodef", {name: e})
continue;
}
let keys = Object.keys(brec._jeepdef_.vars);
for(let j = 0; j<keys.length; j++){
let ky = keys[j];
let v = brec._jeepdef_.vars[ky];
if(!devmode)
vars[ky] = v
else{
if(vars[ky] === undefined && ref[ky] === undefined)
vars[ky] = v
else
dupnames.push({bname: brec.$name, vname: ky})
}
}
}
if(devmode) {
if(badExt)
this.addSyntaxError("rec-extends-badarg", {})
for(let k = 0; k<dupnames.length; k++)
this.addSyntaxError("rec-extends-dupnames", dupnames[k])
}
return dupnames.length > 0 ? null : vars;
},
instRecStruct: function(env, commonAccess, name, inst, defobj, isRec, vars){
let def = defobj._jeepdef_;
let varkeys = null;
if(vars){
if(!env || !env.IsDevMode())
varkeys = Object.keys(vars)
else {
let errors = [];
varkeys = this.testInitNew(env, vars, def.vars, errors)
if(errors.length > 0)
this.abort("initnew-fail", {name: name, type: (isRec?"record":"struct"), errnames: errors.join(',')})
}
}
inst.$name = name;
inst.$def = defobj;
inst._jeepdef_ = def;
let access = null;
this.addRecStructInstVariables(true, inst, def.vars, isRec);
if(vars)
this.addRecStructInstVariables(false, inst, vars, isRec, varkeys);
if(!isRec)
access = this.addStructMembers(inst, commonAccess, def);
return access;
},
addRecStructInstVariables: function(def, inst, varref, isRec, varkeys){
let keys = varkeys || Object.keys(varref);
if(isRec){
for(let k = 0; k<keys.length; k++)
inst[keys[k]] = this.cloneVariable(varref[keys[k]])
}
else{
for(let k = 0; k<keys.length; k++){
let v = varref[keys[k]];
inst[keys[k]] = this.cloneVariable(def ? v.item : v)
}
}
},
makeStructMemFunc: function(inst, func, access, commonAccess){
return function(){
access.add();
commonAccess.add();
let r = null, ex = null;
try{r = func.apply(inst, arguments)}
catch(e){ex = e}
access.remove();
commonAccess.remove();
if(ex) throw ex;
return r;
}
},
addStructMembers: function(inst, commonAccess, def){
let access = this.createRecursiveFlag();
let impl = this;
if(!def.priv){
let keys = Object.keys(def.funcs);
for(let k = 0; k<keys.length; k++)
inst[keys[k]] = def.funcs[keys[k]].item;
}
else {
let items = {};
this.merge(def.funcs, items)
this.merge(def.vars, items)
let keys = Object.keys(items);
for(let k = 0; k<keys.length; k++){
let rec = items[keys[k]];
let ky = keys[k];
let type = typeof rec.item;
rec = this.cloneObject(rec);// because item will be modified
let name = inst.$name+"."+ky;
if(type == "function"){
let func = rec.item;
rec.item = this.makeStructMemFunc(inst, func, access, commonAccess);
}
access.obj[ky] = rec.item;
let priv = rec.decflags & this.fxFlags.private;
Object.defineProperty(inst, ky, {
configurable: false,
enumerable: true,
get: function(){
if(!commonAccess.isSet() && priv && !access.isSet())
impl.abort("restricted-access", {access: type == "function" ? "access" : "read", spectype: "private", memtype: type == "function" ? "function" : "variable", name: name})
return access.obj[ky]
},
set: function(v){
if(!commonAccess.isSet() && priv && !access.isSet())
impl.abort("restricted-access", {access: "write", spectype: "private", memtype: type == "function" ? "function" : "variable", name: name})
access.obj[ky] = v;
},
});
}
}
return access;
},
//----------------------------------------
// class
//----------------------------------------
validateClassProps: function(api, env, def, obj){
if(!env.IsDevMode())
return;
let props = ""
switch(obj){
case "class": props = "CONSTRUCTOR, DESTRUCTOR, EXTENDS, PMC, PUBLIC, PROTECTED, PRIVATE, STATIC"; break;
case "group": props = "CONSTRUCTOR, PMC, PUBLIC, PROTECTED, PRIVATE"; break;
}
let badProps = this.validateProps(def, props);
if(badProps){
let ctor = null;
for(let k = 0; k<badProps.length; k++){
ctor = this.getCtor(null, env, def)
if(ctor) break;
}
if(!ctor)
this.abort("invalid-props", {api: api, props: badProps.join(',')})
}
},
processClassDef: function(api, name, def, env){
name = name.substring(name.lastIndexOf('.')+1);
let gcres = this.generateClass(api, name, def, env, "class");
let Class = gcres.Class;
let classDef = Class.prototype._jeepdef_;
let validate = env.IsDevMode();
let flatBaseDefs = null;
if(classDef.bases){
flatBaseDefs = [];
this.flattenBaseDefTree(classDef, flatBaseDefs);
classDef.flatBaseDefs = flatBaseDefs;
}
if(validate){
this.preValidateClassDef(classDef);
}
this.finalizeClassDef(env, gcres, flatBaseDefs);
if(validate)
this.postValidateClassDef(classDef);
this.abortOnSyntaxError("class", name);
return Class;
},
generateClass: function(api, name, def, env, piggy){
let devmode = env.IsDevMode();
this.validateClassProps(api, env, def, piggy||"class")
let vars = {public: null, protected: null, private: null},
funcs = {public: null, protected: null, private: null};
let res = {vars: {}, funcs: {}};
let virtualFuncs = {};
let staticMembers = {};
if(def.PROTECTED){
res = this.splitVarFunc(def.PROTECTED);
vars.protected = this.undecorateVars(piggy, env, res.vars, name, this.fxFlags.protected);
funcs.protected = this.undecorateFuncs(piggy, env, res.funcs, name, this.fxFlags.protected);
this.getVFuncs(virtualFuncs, funcs.protected);
}
if(def.PRIVATE){
res = this.splitVarFunc(def.PRIVATE);
vars.private = this.undecorateVars(piggy, env, res.vars, name, this.fxFlags.private);
funcs.private = this.undecorateFuncs(piggy, env, res.funcs, name, this.fxFlags.private);
this.getVFuncs(virtualFuncs, funcs.private);
}
if(def.PUBLIC){
res = this.splitVarFunc(def.PUBLIC);
vars.public = this.undecorateVars(piggy, env, res.vars, name, this.fxFlags.public);
res.funcs = res.funcs || {};
let reffuncs = {};
if(devmode){
this.merge(funcs.public, reffuncs)
this.merge(funcs.protected, reffuncs);
this.merge(funcs.private, reffuncs)
}
this.addGetSetFuncs(env, vars.protected, reffuncs, res.funcs);
this.addGetSetFuncs(env, vars.private, reffuncs, res.funcs);
this.addGetSetFuncs(env, vars.public, reffuncs, res.funcs);
funcs.public = this.undecorateFuncs(piggy, env, res.funcs, name, this.fxFlags.public);
this.getVFuncs(virtualFuncs, funcs.public);
}
if(def.STATIC){
res = this.splitVarFunc(def.STATIC);
let statVars = this.undecorateVars("static-variable", env, res.vars, name, this.fxFlags.static);
let statFuncs = this.undecorateFuncs("static-function", env, res.funcs, name, this.fxFlags.static);
// let svar = {};
// let keys = Object.keys(statVars);
// for(let k = 0; k<keys.length; k++){
// let ky = keys[k];
// let rec = statVars[ky]
// if(rec.decflags & (this.fxFlags.public|this.fxFlags.protected)){
// let v = {}
// svar[ky] = rec.item;
// Object.defineProperty(v, ky, {
// configurable: false,
// enumerable: true,
// set: function(v){svar[ky]=v},
// get: function(v){return svar[ky]},
// })
// rec.item = v;
// }
// }
this.merge(statVars, staticMembers)
this.merge(statFuncs, staticMembers)
}
// check for managed constructor
// NOTE: Although this involves a bit of validation, this can't be moved to the validating
// helpers because they won't be called in production mode, which messes up the setting of
// managed mechanism.
let ctorInfo = this.getCtor("class", env, def);
if(ctorInfo){
if(piggy == "group"){
if(devmode && ctorInfo.undec.decflags != 0)
this.addSyntaxError("group-ctor-directives", {})
}
else {
def.CONSTRUCTOR = ctorInfo.ctor
let managed = (ctorInfo.undec.decflags & this.fxFlags.managed);
if(managed && !def.DESTRUCTOR)
this.addSyntaxError("managed-dtor-absent", {})
if(!managed && def.DESTRUCTOR)
this.addSyntaxError("managed-dtor-nomanage", {})
}
}
return this.generateClassDef({name: name, desc: def, env: env,
vars: vars, funcs: funcs, flags: ctorInfo ? ctorInfo.undec.decflags : 0,
vfuncs: Object.keys(virtualFuncs).length > 0 ? virtualFuncs : null,
staticMembers: Object.keys(staticMembers).length > 0 ? staticMembers : null,
})
},
getCtor: function(hint, env, def){
let ctor = null;
let temp = {};
let keys = Object.keys(def);
for(let k = 0; k<keys.length; k++){
if(keys[k].indexOf("CONSTRUCTOR") >= 0){
temp[keys[k]] = def[keys[k]];
ctor = {name: keys[k], ctor: def[keys[k]], undec: null}
break;
}
}
if(!ctor)
return null;
let undec = this.undecorate(hint, env, temp, "constructor");
if(!undec)
return null;
undec = undec["CONSTRUCTOR"];// undecorate returns a map
if(!undec)
return null;
ctor.undec = undec;
return ctor;
},
preValidateClassDef: function(classDef){
if(classDef.dtor && !classDef.ctor)
this.addSyntaxError("dtor-no-ctor", {})
if(classDef.flatBaseDefs){
let err = "";
for(let k = 0; k<classDef.flatBaseDefs.length; k++){
let b = classDef.flatBaseDefs[k];
if(b.flags & this.fxFlags.private)
err += ","+b.$name;
}
if(err.length > 0)
this.addSyntaxError("base-ctor-private", {bases: err.substring(1)})
}
},
postValidateClassDef: function(classDef){
if(Object.keys(classDef.vars).length == 0)
this.addSyntaxError("class-group-empty", {type: "class", mem: "variables"})
if((Object.keys(classDef.funcs).length == 0) && (!classDef.peVFuncs || (Object.keys(classDef.peVFuncs).length == 0)))
this.addSyntaxError("class-group-empty", {type: "class", mem: "functions"})
},
markMembers: function(members, def, code){
if(!def) return;
let acc = ["public", "protected", "private"];
for(let k = 0; k<acc.length; k++){
let part = def[acc[k]];
if(!part) continue;
let keys = Object.keys(part);
for(let j = 0; j<keys.length; j++){
let r = members[keys[j]];
if(!r){
r = {acc: 0, type: 0};
members[keys[j]] = r;
}
r.acc |= 1 << k;
r.type |= code;
}
}
},
markMemberOccurance: function(loc, mem){
let keys = Object.keys(loc)
for(let k = 0; k<keys.length; k++){
let ky = keys[k];
let rec = mem[ky];
if(!rec){
rec = []
mem[ky] = rec;
}
let li = loc[ky]
let p = li.$orig||li.$owner;
if(rec.indexOf(p) < 0)
rec.push(p)
}
},
checkHierarchyDupnames: function(flatBases, classDef, part, type){
let mem = {}
for(let b = 0; b<flatBases.length; b++){
let base = flatBases[b];
this.markMemberOccurance(base[part], mem);
}
this.markMemberOccurance(classDef[part], mem);
let keys = Object.keys(mem);
for(let k = 0; k<keys.length; k++){
let ky = keys[k];
let r = mem[ky];
if(r.length > 1){
let names = "";
for(let g = 0; g<r.length; g++)
names += ","+r[g].$name
this.addSyntaxError("hierarchy-dupname", {memtype: type, name: ky, owners: names.substring(1)})
}
}
},
getVFuncs: function(virtualFuncs, funcs){
if(!funcs)
return;
let iter = this.fx.Utils.ObjectIterator.New(funcs);
while(iter.GetNext()){
let pair = iter.GetCurrPair();
if(pair.value.decflags & (this.fxFlags.virtual|this.fxFlags.abstract))
virtualFuncs[pair.key] = pair.value;
}
},
markVFuncSig: function(funcs, fmap){
let keys = Object.keys(funcs);
for(let k = 0; k<keys.length; k++){
let ky = keys[k];
let fi = funcs[ky];
let vi = fmap[ky];
if(vi === undefined)
fmap[ky] = {args: fi.item.length, flags: fi.decflags};
else {
if(vi.args != fi.item.length)
vi.args = -1;
let f = vi.flags;
if(f > 0){
f &= ~(this.fxFlags.virtual|this.fxFlags.abstract);
if(f != (fi.decflags & ~(this.fxFlags.virtual|this.fxFlags.abstract)))
vi.flags = -1;
}
}
}
},
// returns false to indicate all pevf were found
getPEDVFuncs: function(classDef, allvf, out){
if(classDef.vfuncs){
let keys = Object.keys(allvf);
let total = keys.length, found = 0;
for(let k = 0; k<keys.length; k++){
let afi = allvf[keys[k]];
if(afi.found){
found++;
continue;
}
let fi = classDef.vfuncs[keys[k]];
if(fi){
// this logic is important to enable reabstraction
if(!(fi.decflags & this.fxFlags.abstract))
out[keys[k]] = fi;
afi.found = true;
}
}
return found < total;
}
return true;// always indicate more
},
getVFuncOrigin: function(fname, flatBaseDefs){
let orig = [];
for(let k = 0; k<flatBaseDefs.length; k++){
let bd = flatBaseDefs[k];
if(!bd.vfuncs) continue;
let keys = Object.keys(bd.vfuncs);
for(let j = 0; j<keys.length; j++){
if(keys[j] === fname){
let fi = bd.vfuncs[keys[j]]
if(orig.indexOf(fi.$orig) < 0)
orig.push(fi.$orig)
break;
}
}
}
return (orig.length == 1) ? orig[0] : null;
},
mergeAccessGroups: function(env, classDef){
// first check for name clash between public, protected and private members
if(env.IsDevMode()){
let members = {};
this.markMembers(members, classDef.vars, 2);
this.markMembers(members, classDef.funcs, 1);
let keys = Object.keys(members);
for(let j = 0; j<keys.length; j++){
let r = members[keys[j]];
if(r.type == 3)
this.addSyntaxError("member-varfunc", {name: keys[j]})
else if(r.acc != 1 && r.acc != 2 && r.acc != 4)
this.addSyntaxError("member-multiple-acc", {name: keys[j], type: r.type == 1 ? "function" : "variable"})
}
}
// now merge
let mf = {};
this.merge(classDef.funcs.public, mf);
this.merge(classDef.funcs.protected, mf);
this.merge(classDef.funcs.private, mf);
classDef.funcs = mf;
let mv = {};
this.merge(classDef.vars.public, mv);
this.merge(classDef.vars.protected, mv);
this.merge(classDef.vars.private, mv);
classDef.vars = mv;
},
makeAutoName: function(prefix, name){
return prefix + name[0].toUpperCase() + name.substring(1);
},
addGetSetFuncs: function(env, vars, reffuncs, funcs){
if(!vars) return;
let devmode = env.IsDevMode();
let vkeys = Object.keys(vars);
for(let k = 0; k<vkeys.length; k++){
let ky = vkeys[k];
let vi = vars[ky]
if(vi.decflags & this.fxFlags.getter){
let fname = this.makeAutoName("get", ky)
if(devmode && reffuncs[fname] !== undefined)
this.addSyntaxError("get-set-clash", {type: "get", vname: ky, fname: fname})
else
funcs[fname] = function(){return this[ky]}
}
if(vi.decflags & this.fxFlags.setter){
let fname = this.makeAutoName("set", ky)
if(devmode && vi.decflags & this.fxFlags.constant)
this.addSyntaxError("set-var-const", {vname: ky, fname: fname})
else {
if(devmode && reffuncs[fname] !== undefined)
this.addSyntaxError("get-set-clash", {type: "set", vname: ky, fname: fname})
else
funcs[fname] = function(v){this[ky] = v}
}
}
}
},
setMemOO: function(env, def, mem){
let devmode = env.IsDevMode();
let keys = Object.keys(mem);
for(let k = 0; k<keys.length; k++){
let rec = mem[keys[k]];
rec.$owner = def;
if(devmode)
rec.$orig = def;
}
},
setMemberOwnerOrigin: function(env, classDef){
this.setMemOO(env, classDef, classDef.vars);
this.setMemOO(env, classDef, classDef.funcs);
},
finalizeClassDef: function(env, gcres, flatBaseDefs){
let devmode = env.IsDevMode();
let Class = gcres.Class;
let classDef = Class.prototype._jeepdef_;
this.mergeAccessGroups(env, classDef);
this.setMemberOwnerOrigin(env, classDef);
// do some hierarchy related management and manipulation
if(flatBaseDefs){
let bvfmap = {};
let allvf = {};
// get all virtual functions
if(classDef.vfuncs){
let iter = this.fx.Utils.ObjectIterator.New(classDef.vfuncs);
while(iter.GetNext()){
let pair = iter.GetCurrPair();
if(devmode){
let orig = this.getVFuncOrigin(pair.key, flatBaseDefs);
if(orig)
pair.value.$orig = orig;
}
allvf[pair.key] = {found: false, fi: pair.value}
}
}
// validate the hierarchy, constructor managedness and virtual arguments before merging
if(devmode){
let vfuncsig = {};
if(classDef.vfuncs)
this.markVFuncSig(classDef.vfuncs, vfuncsig)
this.checkHierarchyDupnames(flatBaseDefs, classDef, "vars", "variable")
this.checkHierarchyDupnames(flatBaseDefs, classDef, "funcs", "function")
for(let k = 0; k<flatBaseDefs.length; k++){
let bd = flatBaseDefs[k];
if(bd.vfuncs)
this.markVFuncSig(bd.vfuncs, vfuncsig)
if((bd.flags & this.fxFlags.managed) && classDef.ctor && !(classDef.flags & this.fxFlags.managed)){
this.addSyntaxError("ctor-nomanage", {})
break;
}
}
let keys = Object.keys(vfuncsig);
for(let k = 0; k<keys.length; k++){
let ky = keys[k];
let vi = vfuncsig[ky];
if(vi.args < 0)
this.addSyntaxError("vfunc-args", {fname: ky})
if(vi.flags < 0)
this.addSyntaxError("vfunc-directives", {fname: ky})
}
}
let protctor = false;
for(let k = 0; k<flatBaseDefs.length; k++){
let bd = flatBaseDefs[k];
protctor |= (bd.flags & this.fxFlags.protected);
if(bd.vfuncs){
bvfmap[bd.$name] = bd.vfuncs;
let iter = this.fx.Utils.ObjectIterator.New(bd.vfuncs);
while(iter.GetNext()){
let pair = iter.GetCurrPair();
allvf[pair.key] = {found: false, fi: pair.value}
}
}
this.mergeBaseClass(bd, classDef, k+1);
this.merge(bd.baseVFuncs, bvfmap);
classDef.wrapperInits = classDef.wrapperInits.concat(bd.wrapperInits);
}
if(protctor && !classDef.ctor){
classDef.flags |= this.fxFlags.protected;
delete Class.New;
delete Class.InitNew;
}
if(Object.keys(bvfmap).length > 0){
classDef.baseVFuncs = bvfmap;
// create the instBase object, if necessary, to help fast creationn of $base by doing
// here all the checking as to what items are necessary instead of doing it at runtime
let base = {};
let biter = this.fx.Utils.ObjectIterator.New(bvfmap);
while(biter.GetNext()){
let bpair = biter.GetCurrPair();
let fmap = bpair.value;
let fiter = this.fx.Utils.ObjectIterator.New(fmap);
while(fiter.GetNext()){
let fpair = fiter.GetCurrPair();
if((fpair.value.memAccessFlag != this.fxFlags.private) && !(fpair.value.decflags & this.fxFlags.abstract) && classDef.funcs[fpair.key]){
base[bpair.key] = base[bpair.key] || {};
base[bpair.key][fpair.key] = fpair.value;
}
}
}
if(Object.keys(base).length > 0)
classDef.instBase = base;
}
// Get polymorphism enabling virtual functions. These are basically the latest overrides
// of the virtual functions. They can be at any base, so entire hierarchy must be searched.
// Since they are supposed to be latest, start with leaf class and go backwards till root.
// Similarly get the polymorphism disabling virtual functions, which are exact opposite in
// all respects.
let pevf = {};
if(classDef.vfuncs)
this.getPEDVFuncs(classDef, allvf, pevf);
for(let k = flatBaseDefs.length-1; k>= 0; k--){
if(!this.getPEDVFuncs(flatBaseDefs[k], allvf, pevf))// false returned to say all found
break;
}
if(Object.keys(pevf).length > 0)
classDef.peVFuncs = pevf;
let akeys = Object.keys(allvf);
for(let k = 0; k<akeys.length; k++)
allvf[akeys[k]].found = false;
let pdvf = {};
for(let k = 0; k< flatBaseDefs.length; k++){
if(!this.getPEDVFuncs(flatBaseDefs[k], allvf, pdvf))// false returned to say all found
break;
}
if(classDef.vfuncs)
this.getPEDVFuncs(classDef, allvf, pdvf);
if(Object.keys(pdvf).length > 0)
classDef.pdVFuncs = pdvf;
// mark abstract names if any
let absnames = "";
let iter = this.fx.Utils.ObjectIterator.New(allvf);
while(iter.GetNext()){
let pair = iter.GetCurrPair();
let pvfi = pevf[pair.key];
if((pair.value.fi.decflags & this.fxFlags.abstract) && !pvfi)
absnames += "," + pair.value.fi.$owner.$name + "." + pair.key;
}
if(absnames.length > 0)
classDef.abstractNames = absnames.substring(1);
}
else {
let absnames = "";
let iter = this.fx.Utils.ObjectIterator.New(classDef.vfuncs);
while(iter.GetNext()){
let pair = iter.GetCurrPair();
if(pair.value.decflags & this.fxFlags.abstract)
absnames += "," + pair.value.$owner.$name + "." + pair.key;
}
if(absnames.length > 0)
classDef.abstractNames = absnames.substring(1);
}
// don't forget to add own ctor and dtor to the end!
if(classDef.ctor)
classDef.ctorlist.push(classDef.ctor)
if(classDef.dtor)
classDef.dtorlist.push(classDef.dtor)
// add static members
if(classDef.staticMembers){
if(this.restrictedMembersExist(classDef.staticMembers) && (env.IsDevMode() || (classDef.pmc & this.fxFlags.pmc_memaccess)))
classDef.flags |= this.fxFlags.restrictedStatic;
this.setMemOO(env, classDef, classDef.staticMembers);
let ss = (classDef.flags & this.fxFlags.restrictedStatic) ? gcres.staticSentinelRef : null;
classDef.staticSentinelRef = ss;
this.addStatic(Class, ss ? ss.sentinel : null)
}
let cvnames = this.getConstVarNames(classDef.vars);
if(cvnames.length > 0)
classDef.constVarNames = cvnames;
if(this.restrictedMembersExist(classDef.vars) || this.restrictedMembersExist(classDef.funcs))
classDef.flags |= this.fxFlags.restrictedMem;