-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
dcl.js
728 lines (645 loc) · 20.4 KB
/
dcl.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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
/* 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';
// set up custom names
var mname = '_meta', pname = 'prototype', cname = 'constructor';
// MODULE: restricted Map shim (used by C3 MRO)
var M; // our map implementation if not defined
if (typeof Map == 'undefined') {
// our fake, inefficient, incomplete, yet totally correct Map
M = function () {
this.list = [];
this.size = 0;
};
M.prototype = {
has: function (key) { return this.get(key); },
get: function (key) {
for (var i = 0, n = this.list.length; i < n; i += 2) {
if (key === this.list[i]) {
return this.list[i + 1];
}
}
// returns undefined if not found
},
set: function (key, value) {
for (var i = 0, n = this.list.length; i < n; i += 2) {
if (key === this.list[i]) {
this.list[i + 1] = value;
return this;
}
}
this.list.push(key, value);
++this.size;
return this;
}
};
} else {
M = Map;
}
// MODULE: C3 MRO
function c3mro (bases, props) {
// build a connectivity matrix
var connectivity = new M();
bases.forEach(function (base) {
(base[mname] ? base[mname].bases : [base]).forEach(function (base, index, array) {
if (connectivity.has(base)) {
var value = connectivity.get(base);
index + 1 != array.length && ++value.counter;
if (index) {
value.links.push(array[index - 1]);
}
} else {
connectivity.set(base, {
links: index ? [array[index - 1]] : [],
counter: index + 1 == array.length ? 0 : 1
});
}
});
});
// Kahn's algorithm
var output = [], unreferenced = [];
// find unreferenced bases
bases.forEach(function (base) {
var last = base[mname] ? base[mname].bases[base[mname].bases.length - 1] : base;
if (!connectivity.get(last).counter) {
unreferenced.push(last);
}
});
while (unreferenced.length) {
var base = unreferenced.pop();
output.push(base);
var value = connectivity.get(base);
value.links.forEach(updateCounter);
}
// final checks and return
if (connectivity.size != output.length) {
dcl._error('cycle', bases, props);
}
return output;
function updateCounter (base) {
var value = connectivity.get(base);
if (!--value.counter) {
unreferenced.push(base);
}
}
}
// MODULE: handling properties
function updateProps (props, defaults, augmentDescriptor, augmentWritable) {
var augmenter;
if ('configurable' in defaults) {
augmenter = augmentDescriptor('configurable', defaults.configurable);
Object.keys(props).forEach(function (name) { augmenter(props[name], name); });
}
if ('enumerable' in defaults) {
augmenter = augmentDescriptor('enumerable', defaults.enumerable);
Object.keys(props).forEach(function (name) { augmenter(props[name], name); });
}
if ('writable' in defaults) {
augmenter = augmentWritable(defaults.writable);
Object.keys(props).forEach(function (name) { augmenter(props[name], name); });
}
return props;
}
var descriptorProperties = {configurable: 1, enumerable: 1, value: 1, writable: 1, get: 1, set: 1};
function toProperties(x, defaults) {
var props, descriptors;
if (x instanceof dcl.Prop) {
props = x.x;
} else {
Object.getOwnPropertyNames(x).forEach(function(key) {
var prop = Object.getOwnPropertyDescriptor(x, key);
if (prop.get || prop.set) {
// accessor descriptor
descriptors = descriptors || {};
descriptors[key] = prop;
} else {
// data descriptor
var value = prop.value;
if (value instanceof dcl.Prop) {
props = props || {};
props[key] = value.x;
} else {
if (defaults.detectProps && value && typeof value == 'object') {
if (Object.keys(value).every(function (name) { return descriptorProperties[name] === 1; })) {
props = props || {};
props[key] = value;
return;
}
}
descriptors = descriptors || {};
descriptors[key] = prop;
}
}
});
}
if (props) {
props = updateProps(props, defaults, augmentDescriptor, augmentWritable);
}
if (descriptors) {
descriptors = updateProps(descriptors, defaults, replaceDescriptor, replaceWritable);
}
if (descriptors && props) {
Object.keys(props).forEach(function(key) {
descriptors[key] = props[key];
});
}
return descriptors || props || {};
}
function cloneDescriptor (descriptor) { // shallow copy
var newDescriptor = {};
Object.keys(descriptor).forEach(function (name) {
newDescriptor[name] = descriptor[name];
});
return newDescriptor;
}
function augmentDescriptor(type, value) {
return typeof value == 'function' ? value : function(descriptor) {
if (!descriptor.hasOwnProperty(type)) {
descriptor[type] = value;
}
};
}
function augmentWritable(value) {
return typeof value == 'function' ? value : function(descriptor) {
if (!descriptor.get && !descriptor.set && !descriptor.hasOwnProperty('writable')) {
descriptor.writable = value;
}
};
}
function replaceDescriptor(type, value) {
return typeof value == 'function' ? value : function(descriptor) {
descriptor[type] = value;
};
}
function replaceWritable(value) {
return typeof value == 'function' ? value : function(descriptor) {
if (descriptor.hasOwnProperty('value')) {
descriptor.writable = value;
}
};
}
function getPropertyDescriptor (o, name) {
while (o && o !== Object.prototype) {
if (Object.prototype.hasOwnProperty.call(o, name)) {
return Object.getOwnPropertyDescriptor(o, name);
}
o = Object.getPrototypeOf(o);
}
// otherwise: undefined
}
// MODULE: produce properties
function recordProp(props, o, recorded) {
return function (name) {
if (recorded[name] !== 1) {
recorded[name] = 1;
props[name] = Object.getOwnPropertyDescriptor(o, name);
}
};
}
function collectPropertyDescriptors (props, o) {
var recorded = {};
while (o && o !== Object.prototype) {
Object.getOwnPropertyNames(o).forEach(recordProp(props, o, recorded));
o = Object.getPrototypeOf(o);
}
return props;
}
// populate properties with simple properties
function populateProps (props, mixins, special) {
var newSpecial = {};
mixins.forEach(function (base) {
// copy properties for dcl objects
if (base[mname]) {
var baseProps = base[mname].props;
Object.keys(baseProps).forEach(function (name) {
if (Object.prototype.hasOwnProperty.call(special, name)) {
newSpecial[name] = special[name];
} else {
props[name] = baseProps[name];
}
});
return;
}
// copy properties for regular objects
collectPropertyDescriptors(props, base[pname]);
});
return newSpecial;
}
var empty = {};
function weaveProp (name, bases, weaver, props) {
var state = {prop: null, backlog: []};
weaver.start && weaver.start(state);
bases.forEach(function (base, index) {
var prop;
if (base[mname]) {
var baseProps = base[mname].props;
if (Object.prototype.hasOwnProperty.call(baseProps, name)) {
prop = baseProps[name];
}
} else {
prop = getPropertyDescriptor(base[pname], name);
if (!prop && name == cname) {
prop = {configurable: true, enumerable: false, writable: true, value: base};
}
}
if (!prop) {
return;
}
var newProp = cloneDescriptor(prop), prevProp, superArg;
if (prop.get || prop.set) {
// accessor
var superGet = isSuper(prop.get) && prop.get.spr.around,
superSet = isSuper(prop.set) && prop.set.spr.around;
if (superGet || superSet) {
processBacklog(state, weaver);
prevProp = state.prop;
}
if (superGet) {
if (typeof prop.get.spr.around != 'function') {
dcl._error('wrong super get call', base, name, index, props);
}
superArg = null;
if (prevProp) {
superArg = prevProp.get || prevProp.set ?
prevProp.get : adaptValue(prevProp.value);
}
if (superArg && typeof superArg != 'function') {
dcl._error('wrong super get arg', base, name, index, props);
}
newProp.get = prop.get.spr.around(superArg);
if (typeof newProp.get != 'function') {
dcl._error('wrong super get result', base, name, index, props);
}
state.prop = null;
}
if (superSet) {
if (typeof prop.set.spr.around != 'function') {
dcl._error('wrong super set call', base, name, index, props);
}
superArg = prevProp && prevProp.set;
if (superArg && typeof superArg != 'function') {
dcl._error('wrong super set arg', base, name, index, props);
}
newProp.set = prop.set.spr.around(superArg);
if (typeof newProp.set != 'function') {
dcl._error('wrong super set result', base, name, index, props);
}
state.prop = null;
}
if ((!prop.get || isSuper(prop.get) && !prop.get.spr.around) && (!prop.set || isSuper(prop.set) && !prop.set.spr.around)) {
return; // skip descriptor: no actionable value
}
} else {
// data
if (isSuper(prop.value) && prop.value.spr.around) {
processBacklog(state, weaver);
prevProp = state.prop;
if (typeof prop.value.spr.around != 'function') {
dcl._error('wrong super value call', base, name, index, props);
}
if (prevProp) {
superArg = prevProp.get || prevProp.set ?
adaptGet(prevProp.get) : prevProp.value;
} else {
superArg = name !== cname && empty[name];
}
if (superArg && typeof superArg != 'function') {
dcl._error('wrong super value arg', base, name, index, props);
}
newProp.value = prop.value.spr.around(superArg);
if (typeof newProp.value != 'function') {
dcl._error('wrong super value result', base, name, index, props);
}
state.prop = null;
}
if (!prop.value || isSuper(prop.value) && !prop.value.spr.around) {
return; // skip descriptor: no actionable value
}
}
if (state.prop) {
if (newProp.get || newProp.set) {
if (state.backlog.length) {
state.backlog = [];
}
} else {
state.backlog.push(convertToValue(state.prop));
}
}
state.prop = newProp;
});
processBacklog(state, weaver);
return weaver.stop ? weaver.stop(state) : state.prop;
}
var dclUtils = {adaptValue: adaptValue, adaptGet: adaptGet,
convertToValue: convertToValue, cloneDescriptor: cloneDescriptor,
augmentDescriptor: augmentDescriptor, augmentWritable: augmentWritable,
replaceDescriptor: replaceDescriptor, replaceWritable: replaceWritable
};
function processBacklog (state, weaver) {
if (state.backlog.length) {
state.backlog.push(convertToValue(state.prop));
state.prop = weaver.weave(state.backlog, dclUtils);
state.backlog = [];
}
}
function adaptValue (f) {
return f ? function () { return f; } : null;
}
function adaptGet (f) {
return f ? function () { return f.call(this).apply(this, arguments); } : null;
}
function convertToValue (prop) {
if (prop.get || prop.set) {
var newProp = cloneDescriptor(prop);
delete newProp.get;
delete newProp.set;
newProp.value = adaptGet(prop.get);
return newProp;
}
return prop;
}
// dcl helpers
function getAccessorSideAdvices (name, bases, propName) {
var before = [], after = [];
bases.forEach(function (base) {
if (base[mname]) {
var prop = base[mname].props[name];
if (typeof prop == 'object') {
if (prop.get || prop.set) {
var f = prop[propName];
if (isSuper(f)) {
f.spr.before && before.push(f.spr.before);
f.spr.after && after .push(f.spr.after);
}
}
}
}
});
if (before.length > 1) { before.reverse(); }
return {before: before, after: after};
}
function getDataSideAdvices (name, bases) {
var before = [], after = [];
bases.forEach(function (base) {
if (base[mname]) {
var prop = base[mname].props[name];
if (typeof prop == 'object') {
var f = prop.get || prop.set ? prop.get : prop.value;
if (isSuper(f)) {
f.spr.before && before.push(f.spr.before);
f.spr.after && after .push(f.spr.after);
}
}
}
});
if (before.length > 1) { before.reverse(); }
return {before: before, after: after};
}
function makeStub (aroundStub, beforeChain, afterChain) {
var beforeLength = beforeChain.length, afterLength = afterChain.length,
stub = function () {
var result, thrown, i;
// running the before chain
for (i = 0; i < beforeLength; ++i) {
beforeChain[i].apply(this, arguments);
}
// running the around chain
if (aroundStub) {
try {
result = aroundStub.apply(this, arguments);
} catch (error) {
result = error;
thrown = true;
}
}
// running the after chain
for (i = 0; i < afterLength; ++i) {
afterChain[i].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.advices = {around: aroundStub, before: beforeChain, after: afterChain};
return stub;
}
function makeCtr (baseClass, finalProps, meta) {
var proto = baseClass ?
Object.create(baseClass[pname], finalProps) :
Object.defineProperties({}, finalProps),
ctr = proto[cname];
ctr[mname] = meta;
ctr[pname] = proto;
meta.bases[meta.bases.length - 1] = ctr;
return ctr;
}
// MODULE: weavers
function weaveAround (chain, utils) {
var newProp = utils.cloneDescriptor(chain[chain.length - 1]);
if (newProp.get || newProp.set) {
// convert to value
newProp.value = utils.adaptGet(newProp.get);
delete newProp.get;
delete newProp.set;
}
return newProp;
}
function weaveChain (chain, utils) {
if (this.reverse) {
chain.reverse();
}
var newProp = utils.cloneDescriptor(chain[chain.length - 1]);
// extract functions
chain = chain.map(function (prop) {
return prop.get || prop.set ? utils.adaptGet(prop.get) : prop.value;
});
newProp.value = function () {
for (var i = 0; i < chain.length; ++i) {
chain[i].apply(this, arguments);
}
};
return newProp;
}
// MODULE: dcl (the main function)
function dcl (superClass, props, options) {
// parse arguments
if (superClass instanceof Array) {
// skip
} else if (typeof superClass == 'function') {
superClass = [superClass];
} else if (!superClass) {
superClass = [];
} else {
options = props;
props = superClass;
superClass = [];
}
props = toProperties(props || {}, options || {});
// find the base class, and mixins
var bases = [], baseClass = superClass[0], mixins = [], baseIndex = -1;
if (superClass.length) {
if (superClass.length > 1) {
bases = c3mro(superClass, props).reverse();
if (baseClass[mname]) {
baseIndex = baseClass[mname].bases.length - 1;
} else {
mixins = bases.slice(1);
baseIndex = 0;
}
} else {
if (baseClass[mname]) {
bases = baseClass[mname].bases.slice(0);
baseIndex = bases.length - 1;
} else {
bases = [baseClass];
baseIndex = 0;
}
}
if (bases[baseIndex] === baseClass) {
mixins = bases.slice(baseIndex + 1);
} else {
baseClass = null;
mixins = bases.slice(0);
}
}
// add a stand-in for our future constructor
var faux = {}, special = {};
faux[mname] = {bases: bases, props: props, special: special};
dcl.chainAfter(faux, cname);
bases.push(faux);
mixins.push(faux);
// collect meta
// merge from bases
superClass.forEach(function (base) {
if (base[mname]) {
var baseSpecial = base[mname].special;
Object.keys(baseSpecial).forEach(function (name) {
dcl.chainWith(faux, name, baseSpecial[name]);
});
}
});
// inspect own props
Object.keys(props).forEach(function (name) {
if (!Object.prototype.hasOwnProperty.call(special, name)) {
var prop = props[name];
if (prop.get || prop.set) {
if (isSuper(prop.get) || isSuper(prop.set)) {
dcl.chainWith(faux, name, dcl.weaveSuper);
}
} else {
if (isSuper(prop.value)) {
dcl.chainWith(faux, name, dcl.weaveSuper);
}
}
}
});
// collect simple props, and a list of special props
var finalProps = {}, finalSpecial = populateProps(finalProps, mixins, special);
if (!Object.prototype.hasOwnProperty.call(finalSpecial, cname)) {
finalSpecial[cname] = Object.prototype.hasOwnProperty.call(special, cname) ? special[cname] : dcl.weaveAfter;
}
// process special props
Object.keys(finalSpecial).forEach(function (name) {
var prop = weaveProp(name, bases, finalSpecial[name], props);
if (!prop) {
prop = {configurable: true, enumerable: false, writable: true, value: function nop () {}};
}
var newProp = cloneDescriptor(prop), advices;
if (prop.get || prop.set) {
// accessor descriptor
advices = getAccessorSideAdvices(name, bases, 'get');
newProp.get = dcl._makeStub(prop.get, advices.before, advices.after);
advices = getAccessorSideAdvices(name, bases, 'set');
newProp.set = dcl._makeStub(prop.set, advices.before, advices.after);
} else {
// data descriptor
advices = getDataSideAdvices(name, bases);
var stub = dcl._makeStub(prop.value, advices.before, advices.after);
advices = getAccessorSideAdvices(name, bases, 'set');
stub.advices.set = advices;
newProp.value = stub;
}
finalProps[name] = newProp;
});
return dcl._makeCtr(baseClass, finalProps, faux[mname]);
}
// build export
// guts, do not use them!
dcl._error = function (msg) {
throw new Error(msg);
};
dcl._makeSuper = makeSuper;
dcl._makeCtr = makeCtr;
dcl._makeStub = makeStub;
// utilities
dcl.collectPropertyDescriptors = collectPropertyDescriptors;
dcl.getPropertyDescriptor = getPropertyDescriptor;
// meta
dcl.Prop = function Prop (x) { this.x = x; };
dcl.prop = function prop(x) { return new dcl.Prop(x); };
dcl.isInstanceOf = function isInstanceOf (o, ctr) {
if (o instanceof ctr) {
return true;
}
if (o && o[cname] && o[cname][mname]) {
for (var bases = o[cname][mname].bases, i = bases.length - 2; i >= 0; --i) {
var base = bases[i];
if (base === ctr || !base[mname] && base[pname] instanceof ctr) {
return true;
}
}
}
return false;
};
// chains
function chainWith (ctr, name, weaver) {
if (ctr && ctr[mname]) {
var special = ctr[mname].special;
if (Object.prototype.hasOwnProperty.call(special, name)) {
var own = special[name];
if (own === weaver || own.name === weaver.name || weaver.name === 'super') {
return true;
}
if (own.name !== 'super') {
dcl._error('different weavers: ' + name, ctr, name, weaver, own);
}
}
special[name] = weaver;
return true;
}
return false;
}
dcl.weaveBefore = {name: 'before', weave: weaveChain, reverse: true};
dcl.weaveAfter = {name: 'after', weave: weaveChain};
dcl.weaveSuper = {name: 'super', weave: weaveAround};
dcl.chainWith = chainWith;
dcl.chainBefore = function (ctr, name) { return dcl.chainWith(ctr, name, dcl.weaveBefore); };
dcl.chainAfter = function (ctr, name) { return dcl.chainWith(ctr, name, dcl.weaveAfter); };
// super & AOP
function makeSuper (advice, S) {
var f = function superNop () {};
f.spr = new S(advice);
return f;
}
function isSuper (f) { return f && f.spr instanceof dcl.Super; }
dcl.Super = function Super (f) { this.around = f; };
dcl.isSuper = isSuper;
dcl.Super[pname].declaredClass = 'dcl.Super';
dcl.Advice = dcl(dcl.Super, {
declaredClass: 'dcl.Advice',
constructor: function () {
this.before = this.around.before;
this.after = this.around.after;
this.around = this.around.around;
}
});
dcl.advise = function (advice) { return dcl._makeSuper(advice, dcl.Advice); };
dcl.superCall = dcl.around = function (f) { return dcl._makeSuper(f, dcl.Super); };
dcl.before = function (f) { return dcl.advise({before: f}); };
dcl.after = function (f) { return dcl.advise({after: f}); };
// export
return dcl;
});