-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdefineClass.js
379 lines (339 loc) · 11.1 KB
/
defineClass.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
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
(function (exports) {
"use strict";
var methodTest = /resig/.test(function(){this.resig();}) ? /\b_super\b/ : /.*/;
function isFunc(fn) {
return typeof fn === "function";
}
function isArray(array) {
return array instanceof Array;
}
// creates a new object with baseObj as prototype.
function derive(baseObj) {
var result, clazz;
if (Object.create) {
try {
result = Object.create(baseObj);
} catch (err) {
result = null;
}
}
if (!result) {
clazz = function () {};
clazz.prototype = baseObj;
result = new clazz();
}
return result;
}
// trivial jQuery.extend
function extend(target, members) {
var key;
if (!members) return;
for (key in members) {
target[key] = members[key];
}
return target;
}
function applyAll(funcs, arg, arg2) {
var i;
if (funcs == null) {
return arg;
} else if (isFunc(funcs)) {
funcs = [funcs];
} else if (typeof funcs !== "object") {
throw new Error("Unexpected decorator " + funcs);
}
for (i = 0; i < funcs.length; i++) {
if (isFunc(funcs[i])) {
arg = funcs[i](arg, arg2) || arg;
}
}
return arg;
}
function decorateSelf() {
var supers = Array.prototype.slice.call(arguments, 0);
supers.splice(0, 0, this);
return this.__factory__({ _super: supers });
}
// derives a prototype from another one, inherits/overrides methods and nested classes
function inherit(superPrototype, subPrototype, skipCtor) {
function overrideMethod(superMethod, method) {
return function () {
var origSuper = this._super,
result;
this._super = superMethod;
result = method.apply(this, arguments);
this._super = origSuper;
return result;
};
}
var overriddenConstructor = false,
newPrototype = derive(superPrototype),
p, member, baseMember;
for (p in subPrototype) {
if (p === "constructor") {
if (skipCtor) continue;
overriddenConstructor = true;
}
member = subPrototype[p];
baseMember = superPrototype[p];
if (isFunc(member)) {
if (isFunc(baseMember) && !member.isClass && methodTest.test(member)) {
member = overrideMethod(baseMember, member);
}
} else if (member && typeof member === "object" && baseMember.isClass) {
member = derive(member);
member._super = baseMember;
member = defineClass(member);
}
newPrototype[p] = member;
}
// IE omits "constructor" property in a for loop if it belongs to the object and not to its prototype.
if (!skipCtor && !overriddenConstructor && Object.prototype.hasOwnProperty.call(subPrototype, "constructor") && isFunc(superPrototype.constructor)) {
newPrototype.constructor = overrideMethod(superPrototype.constructor, subPrototype.constructor);
}
return newPrototype;
}
function compileProto(prototype) {
function excludeMemberDecorators(prototype) {
var decorators = null,
name;
for (name in prototype) {
if (name.length > 1 && name[name.length - 1] == "$") {
decorators = decorators || {};
decorators[name.substr(0, name.length - 1)] = prototype[name];
delete prototype[name];
}
}
return decorators;
}
function applyMemberDecorators(prototype, decorators) {
var name, decorator;
if (!decorators) return;
for (name in prototype) {
decorator = decorators[name];
if (decorator) {
prototype[name] = applyAll(decorator, prototype[name], { parent: prototype, name: name });
}
}
}
function combineSupers(supers) {
var result = null,
sup, i;
if (!supers) return null;
supers = isArray(supers) ? supers : [supers];
for (i = 0; i < supers.length; i++) {
sup = supers[i];
if (!isFunc(sup)) throw new Error("Unexpected _super value: " + sup + ". There may be only functions");
if (sup.isClass) {
if (result) throw new Error("Base class must be the first in _super field and there must be only one base class");
result = sup.prototype;
} else {
result = sup(result || {});
}
}
return result;
}
var superProto = combineSupers(prototype._super),
classDecorators = prototype.$,
memberDecorators = excludeMemberDecorators(prototype);
delete prototype._super;
delete prototype.$;
if (superProto) {
prototype = inherit(superProto, prototype);
}
prototype = applyAll(classDecorators, prototype);
applyMemberDecorators(prototype, memberDecorators);
return prototype;
}
// Makes a class based on a prototype and its _super field value
// Simple example:
// var Person = defineClass({
// lovesMusic: true,
// constructor: function(name) {
// this.name = name;
// },
// greet: function () {
// console.log("Hi, I'm " + this.name);
// }
// });
// var person = new Person("Anna");
// assert(person.name === "Anna" && person.lovesMusic);
function defineClass(prototype) {
function getBaseCtor(supers) {
var i;
if (supers == null) return null;
if (!isArray(supers)) {
supers = [supers];
}
for (i = supers.length - 1; i >= 0; i--) {
if (isFunc(supers[i]) && supers[i].isClass) return supers[i];
if (!Object.prototype.hasOwnProperty.call(supers[i], "constructor")) continue;
if (supers[i].constructor) return supers[i].constructor;
}
return null;
}
var baseCtor;
// fix prototype.constructor
if (!Object.prototype.hasOwnProperty.call(prototype, "constructor")) {
baseCtor = getBaseCtor(prototype._super);
if (baseCtor) {
prototype.constructor = function () {
return baseCtor.apply(this, arguments);
};
} else {
prototype.constructor = function () {};
}
}
prototype = compileProto(prototype);
prototype.constructor.prototype = prototype;
prototype.constructor.isClass = true;
prototype.constructor.decorate = decorateSelf;
prototype.constructor.__factory__ = defineClass;
return prototype.constructor;
}
defineClass.trait = function (traitDef) {
function extractSupers() {
var supers = traitDef._super,
i;
if (!supers) return supers;
delete traitDef._super;
supers = isArray(supers) ? supers : [supers];
for (i = 0; i < supers.length; i++) {
if (!isFunc(supers[i])) {
throw "A trait can have only functions in its _super field";
}
if (supers[i].isClass) {
throw "A trait canont have a class in its _super field";
}
}
return supers;
}
function applyTrait(prototype) {
var i;
if (superTraits) {
for (i = 0; i < superTraits.length; i++) {
prototype = superTraits[i](prototype);
}
}
return inherit(prototype, traitDef, true);
}
function trait(clazz) {
var proto;
if (traitDef.isPrototypeOf(this)) {
throw new Error("Trait cannot be instantiated");
}
if (!isFunc(clazz)) {
return applyTrait(clazz);
} else if (isFunc(clazz.decorate)) {
return clazz.decorate(trait);
} else {
throw Error("Unexpected parameter value");
}
}
var superTraits = extractSupers();
if (traitDef.hasOwnProperty("constructor")) {
throw new Error("Traits cannot have constructors");
}
trait.__factory__ = defineClass.trait;
trait.decorate = decorateSelf;
trait.prototype = traitDef;
trait.prototype.constructor = trait;
return trait;
};
defineClass.decorator = function (fnDecorator, settings) {
function decoratePrototype(target, prototype, parent, info) {
var name, decorated;
parent = parent || prototype;
for (name in prototype) {
if (name == "constructor") continue;
if (settings.predicate && !settings.predicate(name, prototype[name], name)) continue;
decorated = fnDecorator.call(prototype[name], prototype[name], {
name: name,
parent: parent,
parentInfo: info
}, settings.options);
if (decorated && decorated != prototype[name]) {
target[name] = decorated;
}
}
return target;
}
function decorator(clazz, info) {
var proto;
if (!isFunc(clazz)) {
return decoratePrototype(derive(clazz), clazz, clazz, info);
} else if (isFunc(clazz.__factory__)) {
proto = { _super: clazz };
decoratePrototype(proto, clazz.prototype, clazz, info);
return clazz.__factory__(proto);
} else {
// just a func
return fnDecorator(clazz, info, settings.options);
}
}
function deriveDecorator(setting, value) {
var settings2 = derive(settings);
settings2[setting] = value;
return defineClass.decorator(fnDecorator, settings2);
}
decorator.where = function (predicate) {
return deriveDecorator("predicate", function () {
return predicate.apply(this, arguments) && (!settings.predicate || settings.predicate.apply(this, arguments));
});
};
decorator.opt = function (newOptions) {
var opt = derive(settings.options);
extend(opt, newOptions);
return deriveDecorator("options", opt);
};
settings = settings || {};
settings.options = settings.options && derive(settings.options) || {};
return decorator;
};
// make a proxy class that delegates methods calls to the base object.
// Example:
// var PersonProxy = defineProxy(Person);
// var person = new Person("Anna");
// var proxy = new PersonProxy(person);
// proxy.greet(); // Hi, I'm Anna
function makeProxy(classOrMethodNames, fieldName, makeTrait) {
var prototype = {},
clazz, name, i;
if (classOrMethodNames.isClass) {
clazz = arguments[0];
classOrMethodNames = [];
for (name in clazz.prototype) {
if (name[0] != "_" && isFunc(clazz.prototype[name])) {
classOrMethodNames.push(name);
}
}
}
fieldName = fieldName || "_real";
for (i = 0; i < classOrMethodNames.length; i++) {
if (classOrMethodNames[i] == "constructor") continue;
(function(name) {
prototype[name] = function () {
var real = this[fieldName];
return real[name].apply(real, arguments);
};
})(classOrMethodNames[i]);
}
if (makeTrait) {
return defineClass.trait(prototype);
}
prototype.constructor = function (real) {
this[fieldName] = real;
};
return defineClass(prototype);
}
defineClass.proxy = function (classOrMethodNames, fieldName) {
return makeProxy(classOrMethodNames, fieldName, false);
};
defineClass.proxyTrait = function (classOrMethodNames, fieldName) {
return makeProxy(classOrMethodNames, fieldName, true);
};
defineClass.abstractMethod = function () {
throw new Error("Abstract method cannot be called");
};
exports.defineClass = defineClass;
})(this);