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