legacy.js
7.37 KB
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
(function(factory){
if(typeof define != "undefined"){
define([], factory);
}else if(typeof module != "undefined"){
module.exports = factory();
}else{
dcl = factory();
}
})(function(){
"use strict";
var shadowedNames = ["hasOwnProperty", "valueOf", "isPrototypeOf", "propertyIsEnumerable",
"toLocaleString", "toString", "constructor"], op = Object.prototype;
for(var post in {toString: 1}){
shadowedNames = [];
}
function allKeys(o){
var keys = [];
for(var name in o){
var t = o[name];
if(t !== op[name] || !(name in op)){
keys.push(name);
}
}
for(var i = 0; name = shadowedNames[i]; ++i){ // intentional assignment
var t = o[name];
if(t !== op[name] || !(name in op)){
keys.push(name);
}
}
return keys;
}
var counter = 0, cname = "constructor", pname = "prototype", empty = {}, mix;
function dcl(superClass, props){
var bases = [0], proto, base, ctor, meta, connectionMap,
output, vector, superClasses, i, j = 0, n;
if(superClass){
if(superClass instanceof Array){
// mixins: C3 MRO
connectionMap = {};
superClasses = superClass.slice(0).reverse();
for(i = superClasses.length - 1; i >= 0; --i){
base = superClasses[i];
// pre-process a base
// 1) add a unique id
base._uniqueId = base._uniqueId || counter++;
// 2) build a connection map and the base list
if((proto = base._meta)){ // intentional assignment
for(vector = proto.bases, j = vector.length - 1; j > 0; --j){
n = vector[j]._uniqueId;
connectionMap[n] = (connectionMap[n] || 0) + 1;
}
superClasses[i] = vector.slice(0);
}else{
superClasses[i] = [base];
}
}
// build output
output = {};
c: while(superClasses.length){
for(i = 0; i < superClasses.length; ++i){
vector = superClasses[i];
base = vector[0];
n = base._uniqueId;
if(!connectionMap[n]){
if(!output[n]){
bases.push(base);
output[n] = 1;
}
vector.shift();
if(vector.length){
--connectionMap[vector[0]._uniqueId];
}else{
superClasses.splice(i, 1);
}
continue c;
}
}
// error
dcl._error("cycle", props, superClasses);
}
// calculate a base class
superClass = superClass[0];
j = bases.length - ((meta = superClass._meta) && superClass === bases[bases.length - (j = meta.bases.length)] ? j : 1) - 1; // intentional assignments
}else{
// 1) add a unique id
superClass._uniqueId = superClass._uniqueId || counter++;
// 2) single inheritance
bases = bases.concat((meta = superClass._meta) ? meta.bases : superClass); // intentional assignment
}
}
// create a base class
proto = superClass ? dcl.delegate(superClass[pname]) : {};
// the next line assumes that constructor is actually named "constructor", should be changed if desired
vector = superClass && (meta = superClass._meta) ? dcl.delegate(meta.weaver) : {constructor: 2}; // intentional assignment
// create prototype: mix in mixins and props
for(; j > 0; --j){
base = bases[j];
meta = base._meta;
dcl.mix(proto, meta && meta.ownProps || base[pname]);
if(meta){
allKeys(superClasses = meta.weaver).forEach(function(n){ // intentional assignment
vector[n] = (+vector[n] || 0) | superClasses[n];
});
}
}
allKeys(props).forEach(function(n){
if(isSuper(meta = props[n])){ // intentional assignment
vector[n] = +vector[n] || 0;
}else{
proto[n] = meta;
}
});
// create stubs with fake constructor
//
meta = {bases: bases, ownProps: props, weaver: vector, chains: {}};
// meta information is coded like that:
// bases: an array of super classes (bases) and mixins
// ownProps: a bag of immediate prototype properties for the constructor
// weaver: a bag of chain instructions (before is 1, after is 2)
// chains: a bag of chains (ordered arrays)
bases[0] = {_meta: meta, prototype: proto};
buildStubs(meta, proto);
ctor = proto[cname];
// put in place all decorations and return a constructor
ctor._meta = meta;
ctor[pname] = proto;
//proto.constructor = ctor; // uncomment if constructor is not named "constructor"
bases[0] = ctor;
// each constructor may have two properties on it:
// _meta: a meta information object as above
// _uniqueId: a unique number, which is used to id the constructor
return dcl._postprocess(ctor); // fully prepared constructor
}
// decorators
function Super(f){ this.around = f; }
function isSuper(f){ return f && f.spr instanceof Super; }
// utilities
(mix = function(a, b){
allKeys(b).forEach(function(n){
a[n] = b[n];
});
})(dcl, {
// piblic API
mix: mix,
delegate: function(o){
return Object.create(o);
},
allKeys: allKeys,
Super: Super,
superCall: function superCall(f){ return dcl._makeSuper(f); },
// protected API starts with _ (don't use it!)
// make a Super marker
_makeSuper: function makeSuper(advice, S){ var f = function(){}; f.spr = new (S || Super)(advice); return f; },
// post-processor for a constructor, can be used to add more functionality
// or augment its behavior
_postprocess: function(ctor){ return ctor; }, // identity, used to hang on advices
// error function, augmented by debug.js
_error: function(msg){ throw Error("dcl: " + msg); },
// supercall instantiation, augmented by debug.js
_instantiate: function(advice, previous, node){ var t = advice.spr.around(previous); t.ctr = advice.ctr; return t; },
// the "buildStubs()" helpers, can be overwritten
_extractChain: function(bases, name, advice){
var i = bases.length - 1, chain = [], base, f, around = advice == "around";
for(; base = bases[i]; --i){
// next line contains 5 intentional assignments
if((f = base._meta) ? (f = f.ownProps).hasOwnProperty(name) && (isSuper(f = f[name]) ? (around ? f.spr.around : (f = f.spr[advice])) : around) : around && (f = name == cname ? base : base[pname][name]) && f !== empty[name]){
f.ctr = base;
chain.push(f);
}
}
return chain;
},
_stubChain: function(chain){ // this is "after" chain
var l = chain.length, f;
return !l ? 0 : l == 1 ?
(f = chain[0], function(){
f.apply(this, arguments);
}) :
function(){
for(var i = 0; i < l; ++i){
chain[i].apply(this, arguments);
}
};
},
_stubSuper: function(chain, name){
var i = 0, f, p = empty[name];
for(; f = chain[i]; ++i){
p = isSuper(f) ? (chain[i] = dcl._instantiate(f, p, name)) : f;
}
return name != cname ? p : function(){ p.apply(this, arguments); };
},
_stubChainSuper: function(chain, stub, name){
var i = 0, f, diff, pi = 0;
for(; f = chain[i]; ++i){
if(isSuper(f)){
diff = i - pi;
chain[i] = dcl._instantiate(f, !diff ? 0 : diff == 1 ? chain[pi] : stub(chain.slice(pi, i)), name);
pi = i;
}
}
diff = i - pi;
return !diff ? 0 : diff == 1 && name != cname ? chain[pi] : stub(pi ? chain.slice(pi) : chain);
},
_stub: /*generic stub*/ function(id, bases, name, chains){
var f = chains[name] = dcl._extractChain(bases, name, "around");
return (id ? dcl._stubChainSuper(f, dcl._stubChain, name) : dcl._stubSuper(f, name)) || function(){};
}
});
function buildStubs(meta, proto){
var weaver = meta.weaver, bases = meta.bases, chains = meta.chains;
allKeys(weaver).forEach(function(name){
proto[name] = dcl._stub(weaver[name], bases, name, chains);
});
}
return dcl;
});