-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
advise.js
207 lines (191 loc) · 5.77 KB
/
advise.js
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
/* UMD.define */ (typeof define=="function"&&define||function(d,f,m){m={module:module,require:require};module.exports=f.apply(null,d.map(function(n){return m[n]||require(n)}))})
([], function () {
'use strict';
var pname = 'prototype';
function Node (parent) {
this.parent = parent || this;
}
Node[pname] = {
removeTopic: function (topic) {
var n = 'next_' + topic, p = 'prev_' + topic;
if (this[n] && this[p]) {
this[n][p] = this[p];
this[p][n] = this[n];
}
},
remove: function () {
this.removeTopic('before');
this.removeTopic('around');
// remove & recreate around advices
var parent = this.parent, next = this.next_around;
this.removeTopic('after');
for (; next && next !== parent; next = next.next_around) {
next.around = next.originalAround(next.prev_around.around);
}
},
addTopic: function (node, topic) {
var n = 'next_' + topic, p = 'prev_' + topic,
prev = node[p] = this[p] || this;
node[n] = this;
prev[n] = this[p] = node;
},
addAdvice: function (advice, instance, name, type) {
var node = new Node(this);
if (advice.before) {
node.before = advice.before;
this.addTopic(node, 'before');
}
if (advice.around) {
if (typeof advice.around != 'function') {
advise._error('wrong super call', instance, name, type);
}
node.originalAround = advice.around;
this.addTopic(node, 'around');
if (node.prev_around.around && typeof node.prev_around.around != 'function') {
advise._error('wrong super arg', instance, name, type);
}
node.around = advice.around(node.prev_around.around || null);
if (typeof node.around != 'function') {
advise._error('wrong super result', instance, name, type);
}
}
if (advice.after) {
node.after = advice.after;
this.addTopic(node, 'after');
}
return node;
}
};
Node[pname].destroy = Node[pname].unadvise = Node[pname].remove;
function addNode (root, topic) {
return function (f) {
var node = new Node(root);
node[topic] = f;
root.addTopic(node, topic);
};
}
function makeStub (value) {
var root = new Node();
if (value) {
if (typeof value.advices == 'object') {
var advices = value.advices;
advices.before.forEach(addNode(root, 'before'));
advices.after. forEach(addNode(root, 'after'));
advices.around && addNode(root, 'around')(advices.around);
} else {
addNode(root, 'around')(value);
}
}
function stub () {
var result, thrown, p;
// running the before chain
for (p = root.prev_before; p && p !== root; p = p.prev_before) {
p.before.apply(this, arguments);
}
// running the around chain
if (root.prev_around && root.prev_around !== root) {
try {
result = root.prev_around.around.apply(this, arguments);
} catch (error) {
result = error;
thrown = true;
}
}
// running the after chain
for (p = root.next_after; p && p !== root; p = p.next_after) {
p.after.call(this, arguments, result, makeReturn, makeThrow);
}
if (thrown) {
throw result;
}
return result;
function makeReturn (value) { result = value; thrown = false; }
function makeThrow (value) { result = value; thrown = true; }
};
stub.node = root;
return stub;
}
function convert (value, advice, instance, name, type) {
if (!value || !(value.node instanceof Node)) {
value = makeStub(value);
value.node.instance = instance;
value.node.name = name;
value.node.type = type;
}
var node = value.node.addAdvice(advice, instance, name, type);
return {value: value, handle: node};
}
function combineHandles (handles) {
var handle = {
remove: function () {
handles.forEach(function (handle) { handle.remove(); });
}
}
handle.destroy = handle.unadvise = handle.remove;
return handle;
}
function advise (instance, name, advice) {
var prop = getPropertyDescriptor(instance, name), handles = [];
if (prop) {
if (prop.get || prop.set) {
var result;
if (prop.get && advice.get) {
result = convert(prop.get, advice.get, instance, name, 'get');
prop.get = result.value;
handles.push(result.handle);
}
if (prop.set && advice.set) {
result = convert(prop.set, advice.set, instance, name, 'set');
prop.set = result.value;
handles.push(result.handle);
}
} else {
if (prop.value && advice) {
result = convert(prop.value, advice, instance, name, 'value');
prop.value = result.value;
handles.push(result.handle);
}
}
} else {
prop = {writable: true, configurable: true, enumerable: true};
if (advice.get || advice.set) {
if (advice.get) {
result = convert(null, advice.get, instance, name, 'get');
prop.get = result.value;
handles.push(result.handle);
}
if (advice.set) {
result = convert(null, advice.set, instance, name, 'set');
prop.set = result.value;
handles.push(result.handle);
}
} else {
result = convert(null, advice, instance, name, 'value');
prop.value = result.value;
handles.push(result.handle);
}
}
Object.defineProperty(instance, name, prop);
return combineHandles(handles);
}
// export
// guts, do not use them!
advise._error = function (msg) {
throw new Error(msg);
};
advise.before = function (instance, name, f) { return advise(instance, name, {before: f}); };
advise.after = function (instance, name, f) { return advise(instance, name, {after: f}); };
advise.around = function (instance, name, f) { return advise(instance, name, {around: f}); };
advise.Node = Node;
return advise;
// copied from dcl.js so we can be independent
function getPropertyDescriptor (o, name) {
while (o && o !== Object[pname]) {
if (o.hasOwnProperty(name)) {
return Object.getOwnPropertyDescriptor(o, name);
}
o = Object.getPrototypeOf(o);
}
return; // undefined
}
});