35dd58e271fd78bb3d8b790dd229add034012418
[xynt.git] / xynt-base.js
1
2 function Extends(sub)
3 {
4     var proto;
5     thinF = function(){};
6     inh = new thinF();
7     inh.xynt_sup = [];
8     inh.xynt_sup_cl = [];
9
10     console.log("EXT START");
11
12     var multi = function(sub, super, is_last)
13     {
14         var proto = super.prototype;
15
16         for (var f in proto) {
17             if (f == "xynt_sup" || f == "xynt_sup_cl")
18                 continue;
19
20             console.log("LOOP: "+f);
21             if( f != "constructor" && typeof(proto[f]) == "function" && !is_last){
22                 console.log("M_FU: "+proto['name']+"  F: "+f);
23                 // sub[f] = proto[f];
24                 sub[f] = function(){
25                     console.log("INFU PR: " + proto['name'] + " FUNC: "+f+" THIS: " + this);
26                     return proto[f].apply(this,arguments);
27                 }
28             }
29             else {
30                 console.log("M_PR: "+proto.name+"  A: "+f+" FUN: "+proto[f]);
31                 sub[f] = proto[f];
32             }
33         }
34     }
35
36     for( var i=1; i < arguments.length ; i++){
37         multi(inh, arguments[i], false);
38         inh.xynt_sup.push(arguments[i].prototype);
39         inh.xynt_sup_cl.push(arguments[i]);
40     }
41
42     multi(inh, sub, true);
43
44
45     inh.callSuper = function(fnc){
46         var len = this.xynt_sup_cl.length;
47         for ( var i=0 ; i<len ; i++) {
48             var super = this.xynt_sup_cl[i];
49             if( (fnc in  super.prototype) && (typeof super.prototype[fnc] == "function") ){
50                 return super.prototype[fnc].apply(this,[].splice.call(arguments,1));
51             }
52         }
53         return null;
54     }
55
56     /*
57     inh.superrr = function(cl, method) {
58         console.log("xyx "+cl);
59         // console.log("xx"+arguments[0]);
60     //    return this.xynt_sup_cl[cl].method.apply(this,arguments.slice(2));
61     }
62     */
63     sub.prototype = inh;
64
65     console.log(sub.prototype.xynt_sup);
66     console.log([ [ "xynt_sup_cl", "zogo" ], sub.prototype.xynt_sup_cl]);
67
68     console.log("EXT FINISH");
69 }
70
71 function ExtendsInst(sub)
72 {
73     var proto, cl, args;
74     
75     console.log("INIZIO EXT INST");
76
77     var multi = function(sub, super, is_last)
78     {
79         var proto = super.prototype;
80
81         if (typeof(sub.xynt_sup) == 'undefined')
82             sub.xynt_sup = new Array();
83         if (typeof(sub.xynt_sup_cl) == 'undefined')
84             sub.xynt_sup_cl = new Array();
85
86         for (var f in proto) {
87             /* NOTE: this allow potentially TO BREAK by the extending class
88                if we disable it we don't have a reason to keep the callSuper function,
89                I need to meditate on it */
90             if (f == "xynt_sup" || f == "xynt_sup_cl")
91                 continue;
92             
93             console.log("ExtensInst::multi: "+f+" type: "+typeof(sub[f]));
94             if (typeof(sub[f]) != 'undefined') {
95                 if (typeof(sub[f]) == typeof(proto[f]))
96                     continue;
97                 console.log("MULTI: false1");
98                 return false;
99             }
100
101             console.log("LOOP: "+f);
102             if( f != "constructor" && typeof(proto[f]) == "function" && !is_last){
103                 console.log("M_FU: "+proto['name']+"  F: "+f);
104                 sub[f] = function(){
105                     console.log("INFU PR: " + proto[f] + " FUNC: "+f+" THIS: " + this);
106                     return proto[f].apply(this, arguments);
107                 }
108             }
109             else {
110                 console.log("M_PR: "+proto.name+"  A: "+f+" FUN: "+proto[f]);
111                 sub[f] = proto[f];
112             }
113         }
114         return true;
115     }
116
117     console.log("POST FUNC");
118
119     for( var i=1; i < arguments.length ; i++){
120         console.log("POST FUNC IN LOOP");
121         cl = arguments[i];
122         if (i+1 < arguments.length && typeof(arguments[i+1]) != 'function') {
123             i++;
124             args = arguments[i];
125         }
126         if (multi(sub, cl, false) == false) {
127             console.log("POST FUNC FALSE 1");
128             return (false);
129         }
130
131         console.log("POST FUNC PRE APPLY");
132         cl.apply(sub, args);
133         console.log("POST FUNC POST APPLY");
134
135         sub.xynt_sup.push(cl.prototype);
136         sub.xynt_sup_cl.push(cl);
137     }
138
139     sub.callSuper = function(fnc){
140         var len = this.xynt_sup_cl.length;
141         for ( var i=0 ; i<len ; i++) {
142             var super = this.xynt_sup_cl[i];
143             if( (fnc in  super.prototype) && (typeof super.prototype[fnc] == "function") ){
144                 return super.prototype[fnc].apply(this,[].splice.call(arguments,1));
145             }
146         }
147         return null;
148     }
149
150     console.log("FINE EXT INST");
151     return true;
152 }
153
154 function show_class(cl) {
155     console.log("Attributes Class of "+cl);
156     for (f in cl.prototype) {
157         console.log(f+"-v");
158         console.log(cl.prototype[f]);
159     }
160     console.log("---");
161 }
162
163 function show_inst(inst) {
164     console.log("Attributes Instance of "+inst);
165     for (f in inst) {
166         console.log(f+"-v");
167         // console.log(inst[f]);
168     }
169     console.log("---");
170 }
171