debug.js
8.4 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
(function(factory){
if(typeof define != "undefined"){
define(["./mini", "./advise"], factory);
}else if(typeof module != "undefined"){
module.exports = factory(require("./mini"), require("./advise"));
}else{
dclDebug = factory(dcl, advise);
}
})(function(dcl, advise){
function DclError(message){
if(Error.captureStackTrace){
Error.captureStackTrace(this, DclError);
}
var e = Error.call(this, message), name;
dcl.allKeys(e).forEach(function(name){
if(e.hasOwnProperty(name)){
this[name] = e[name];
}
});
this.message = message;
}
DclError.prototype = dcl.delegate(Error.prototype);
DclError.prototype.constructor = DclError;
var CycleError = dcl(DclError, {declaredClass: "dcl/debug/CycleError"}),
ChainingError = dcl(DclError, {declaredClass: "dcl/debug/ChainingError"}),
SetChainingError = dcl(DclError, {declaredClass: "dcl/debug/SetChainingError"}),
SuperCallError = dcl(DclError, {declaredClass: "dcl/debug/SuperCallError"}),
SuperError = dcl(DclError, {declaredClass: "dcl/debug/SuperError"}),
SuperResultError = dcl(DclError, {declaredClass: "dcl/debug/SuperResultError"});
var chainNames = ["UNCHAINED BUT CONTAINS ADVICE(S)", "CHAINED BEFORE", "CHAINED AFTER",
"ERRONEOUSLY CHAINED BEFORE AND AFTER"];
function chainName(id){
return id >= 0 && id <= 3 ? chainNames[id] : "UNKNOWN (" + id + ")";
}
var noDecls = " (specify 'declaredClass' string in your classes to get better diagnostics)";
advise.around(dcl, "_error", function(/*sup*/){
return function(reason, a1, a2, a3, a4, a5){
var cName, someUnknown, i, base, name, names = [], c = {};
switch(reason){
case "cycle":
cName = a1.hasOwnProperty("declaredClass") && a1.declaredClass;
someUnknown = !cName;
for(i = a2.length - 1; i >= 0; --i){
base = a2[i][0];
name = base.prototype.hasOwnProperty("declaredClass") && base.prototype.declaredClass;
if(!name){
name = "UNNAMED_" + base._uniqueId;
someUnknown = true;
}
if(!c[name]){
names.push(name);
c[name] = 1;
}
}
throw new CycleError("dcl: base class cycle found in: " + (cName || "UNNAMED") +
" - bases: " + names.join(", ") + " are mutually dependent" +
(someUnknown ? noDecls : ""));
case "chain":
cName = a2.prototype.hasOwnProperty("declaredClass") && a2.prototype.declaredClass;
name = a4.prototype.hasOwnProperty("declaredClass") && a4.prototype.declaredClass;
someUnknown = !(cName && name);
throw new ChainingError("dcl: conflicting chain directives from bases found in: " + (cName || ("UNNAMED_" + a2._uniqueId)) +
", method: " + a1 + " - it was " + chainName(a3) + " yet " +
(name || ("UNNAMED_" + a4._uniqueId)) + " sets it to " + chainName(a5) +
(someUnknown ? noDecls : ""));
case "set chaining":
cName = a2.prototype.hasOwnProperty("declaredClass") && a2.prototype.declaredClass;
someUnknown = !cName;
throw new SetChainingError("dcl: attempt to set conflicting chain directives in: " + (cName || ("UNNAMED_" + a2._uniqueId)) +
", method: " + a1 + " - it was " + chainName(a4) + " yet being changed to " + chainName(a3) +
(someUnknown ? noDecls : ""));
case "wrong super call":
cName = a1.prototype.hasOwnProperty("declaredClass") && a1.prototype.declaredClass;
someUnknown = !cName;
throw new SuperCallError("dcl: argument of around advice or supercall decorator should be a function in: " +
(cName || ("UNNAMED_" + a1._uniqueId)) + ", method: " + a2 + (someUnknown ? noDecls : ""));
case "wrong super":
cName = a1.prototype.hasOwnProperty("declaredClass") && a1.prototype.declaredClass;
someUnknown = !cName;
throw new SuperError("dcl: super method should be a function in: " +
(cName || ("UNNAMED_" + a1._uniqueId)) + ", method: " + a2 + (someUnknown ? noDecls : ""));
case "wrong super result":
cName = a1.prototype.hasOwnProperty("declaredClass") && a1.prototype.declaredClass;
someUnknown = !cName;
throw new SuperResultError("dcl: around advice or supercall should return a function in: " +
(cName || ("UNNAMED_" + a1._uniqueId)) + ", method: " + a2 + (someUnknown ? noDecls : ""));
}
throw new DclError("dcl: " + reason);
};
});
advise.after(dcl, "_postprocess", function(args, ctor){
// validate that chaining is consistent
var meta = ctor._meta, weaver = meta.weaver, bases = meta.bases,
name, chain, base, i, rule;
dcl.allKeys(weaver).forEach(function(name){
chain = (+weaver[name] || 0);
for(i = bases.length - 1; i >= 0; --i){
base = bases[i];
meta = base._meta;
if(meta){
rule = (+meta.weaver[name] || 0);
if(chain != rule && (!chain || rule)){
dcl._error("chain", name, ctor, chain, base, rule);
}
}
}
});
});
advise.around(dcl, "_instantiate", function(/*sup*/){
return function(advice, previous, node){
if(!advice || !advice.spr || typeof advice.spr.around != "function"){
dcl._error("wrong super call", advice.ctr, node);
}
if(previous && typeof previous != "function"){
dcl._error("wrong super", advice.ctr, node);
}
var t = advice.spr.around(previous);
if(typeof t != "function"){
dcl._error("wrong super result", advice.ctr, node);
}
t.ctr = advice.ctr;
return t;
};
});
advise(advise, "_instantiate", {
before: function(advice, previous, node){
if(typeof advice != "function"){
dcl._error("wrong super call", node.instance.constructor, node.name);
}
if(previous && typeof previous != "function"){
dcl._error("wrong super", node.instance.constructor, node.name);
}
},
after: function(a, result){
if(typeof result != "function"){
dcl._error("wrong super result", a[2].instance.constructor, a[2].name);
}
}
});
function logCtor(ctor){
var meta = ctor._meta;
if(!meta){
console.log("*** class does not have meta information compatible with dcl");
return;
}
var weaver = meta.weaver, bases = meta.bases, chains = meta.chains, names = [], base, name, someUnknown, i;
for(i = 0; i < bases.length; ++i){
base = bases[i];
name = base.prototype.hasOwnProperty("declaredClass") && base.prototype.declaredClass;
if(!name){
name = "UNNAMED_" + (base.hasOwnProperty("_uniqueId") ? base._uniqueId : "");
someUnknown = true;
}
names.push(name);
}
console.log("*** class " + names[0] + " depends on " + (names.length - 1) + " classes");
if(names.length > 1){
console.log(" dependencies: " + names.slice(1).join(", "));
}
if(someUnknown){
console.log(" " + noDecls);
}
dcl.allKeys(weaver).forEach(function(name){
i = +weaver[name];
if(!isNaN(i)){
var hasStub = typeof ctor.prototype[name].advices == "object";
if(hasStub){
var b = dcl._extractChain(bases, name, "b").length,
f = dcl._extractChain(bases, name, "f").length,
a = dcl._extractChain(bases, name, "a").length;
}
console.log(" class method " + name + " is " + chainName(i) +
(hasStub ?
", and has an AOP stub (before: " + b + ", around: " + f + ", after: " + a + ")" :
" (length: " + chains[name].length + ")" ));
}
});
}
function countAdvices(node, chain){
for(var counter = 0, p = node[chain]; p != node; p = p[chain], ++counter);
return counter;
}
function log(o, suppressCtor){
switch(typeof o){
case "function":
logCtor(o);
return;
case "object":
var base = o.constructor,
name = base.prototype.hasOwnProperty("declaredClass") && base.prototype.declaredClass;
if(!name){
name = "UNNAMED_" + (base.hasOwnProperty("_uniqueId") ? base._uniqueId : "");
}
console.log("*** object of class " + name);
// log the constructor
if(!suppressCtor){
logCtor(base);
}
// log methods
dcl.allKeys(o).forEach(function(name){
var f = o[name], b, r, a;
if(typeof f == "function"){
if(f.adviceNode && f.adviceNode instanceof advise.Node){
b = countAdvices(f.adviceNode, "pb");
r = countAdvices(f.adviceNode, "pf");
a = countAdvices(f.adviceNode, "pa");
console.log(" object method " + name + " has an AOP stub (before: " +
b + ", around: " + r + ", after: " + a + ")");
}
}
});
return;
}
console.log(o);
}
return {
log: log,
DclError: DclError,
CycleError: CycleError,
ChainingError: ChainingError,
SetChainingError: SetChainingError,
SuperCallError: SuperCallError,
SuperError: SuperError,
SuperResultError: SuperResultError
};
});