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