-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxmo.js
279 lines (221 loc) · 8.51 KB
/
xmo.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
// xmo.js <https://github.com/jsstuff/xmo>
(function($export, $as) {
"use strict";
const VERSION = "1.0.0";
const hasOwn = Object.prototype.hasOwnProperty;
const freeze = Object.freeze;
const isArray = Array.isArray;
const isFrozen = Object.isFrozen;
const NoArray = freeze([]);
const NoObject = freeze(Object.create(null));
function isFunction(x) { return typeof x === "function"; }
function isFunctionArray(x) { return isArray(x) && x.every(isFunction); }
function asArray(x) { return isArray(x) ? x : [x]; }
function throwTypeError(msg) { throw TypeError(msg); }
function isEmpty(obj) {
for (var k in obj)
if (hasOwn.call(obj, k))
return false;
return true;
}
function dict(src) {
const out = Object.create(null);
if (src) {
if (isArray(src)) {
const arr = src;
for (var i = 0; i < arr.length; i++)
out[arr[i]] = true;
}
else {
Object.assign(out, src);
}
}
return out;
}
function clone(obj) {
if (!obj || typeof obj !== "object")
return obj;
return isArray(obj) ? obj.slice(0) : dict(obj);
}
function newConstructor(Super) {
return function(...params) {
Super.call(this, ...params);
};
}
function MetaInfo$getMutable(name) {
var member = this[name];
if (!member)
throwTypeError(`No such member '${dstName} in Class.$metaInfo`);
if (isFrozen(member))
member = this[name] = clone(member);
return member;
}
function MetaInfo$freeze(metaInfo) {
for (var k in metaInfo) {
const obj = metaInfo[k];
if (obj != null && typeof obj === "object")
freeze(obj);
}
return freeze(metaInfo);
}
function MetaInfo$addInitHandlers(metaInfo, handlers, handlerType) {
if (!isFunction(handlers) && !isFunctionArray(handlers))
throwTypeError(`$${handlerType} must be Function or Function[], not '${typeof handlers}'`);
const arr = asArray(handlers);
for (var i = 0; i < arr.length; i++) {
const handler = arr[i];
if (metaInfo[handlerType].indexOf(handler) !== -1)
continue;
metaInfo.getMutable(handlerType).push(handler);
}
}
function MetaInfo$addExtensions(metaInfo, extensions) {
if (typeof extensions !== "object")
throwTypeError(`$extensions must be Object, not '${typeof extensions}'`);
const ignoredMap = metaInfo.getMutable("ignored");
const extensionsMap = metaInfo.getMutable("extensions");
for (var k in extensions) {
const ext = extensions[k];
ignoredMap[k] = true;
if (typeof ext === "function")
extensionsMap[k] = ext;
}
}
function Class$mergeMembers(Class, proto, src) {
const metaInfo = Class.$metaInfo;
const ignored = metaInfo.ignored;
const extensions = metaInfo.extensions;
for (var k in src) {
const v = src[k];
const ext = extensions[k];
if (ext)
ext.call(Class, k, v);
else if (!ignored[k])
proto[k] = src[k];
}
}
function Class$mergeMixinStatics(Class, Mixin) {
const mixinMetaInfo = Mixin.$metaInfo;
const mixinStatics = mixinMetaInfo.statics;
if (isEmpty(mixinStatics))
return;
const classMetaInfo = Class.$metaInfo;
const classStatics = classMetaInfo.getMutable("statics");
for (var k in mixinStatics) {
Class[k] = Mixin[k];
classStatics[k] = true;
}
}
function Class$mergeStatics(Class, src) {
if (!src || isEmpty(src))
return;
const metaInfo = Class.$metaInfo;
const staticsMap = metaInfo.getMutable("statics");
for (var k in src) {
Class[k] = src[k];
staticsMap[k] = true;
}
}
const IgnoredDefinitions = freeze(dict({
$extend : true, // Built-in keyword '$extend'.
$mixins : true, // Built-in keyword '$mixins'.
$preInit : true, // Built-in keyword '$preInit'.
$postInit : true, // Built-in keyword '$postInit'.
$extensions : true, // Built-in keyword '$extensions'.
$statics : true, // Built-in keyword '$statics'.
$metaInfo : true, // Ingore 'Mixin.$metaInfo' when including mixins.
constructor : true, // Built-in keyword 'constructor'.
prototype : true // Ignore 'Mixin.prototype' when including mixins.
}));
const ClassMetaInfo = freeze(dict({
isMixin : false, // Boolean indicating whether this is a mixin.
ignored : IgnoredDefinitions, // Which properties must be ignored (cannot be merged).
preInit : NoArray, // Pre-initialization handlers (array of functions).
postInit : NoArray, // Post-initialization handlers (array of functions).
statics : NoObject, // Which properties of `Class` were defined as statics.
extensions : NoObject, // Map of extensions (string -> handler).
getMutable : MetaInfo$getMutable // Get a mutable member (cloned if frozen).
}));
const MixinMetaInfo = freeze(dict({
isMixin : true, // Boolean indicating whether this is a mixin.
ignored : IgnoredDefinitions, // Which properties must be ignored (cannot be merged).
preInit : NoArray, // Pre-initialization handlers (array of functions).
postInit : NoArray, // Post-initialization handlers (array of functions).
statics : NoObject, // Which properties of `Class` were defined as statics.
extensions : NoObject, // Map of extensions (string -> handler).
getMutable : MetaInfo$getMutable // Get a mutable member (cloned if frozen).
}));
function xmo_(def, isMixin) {
var k, i;
// Setup class and its superclass.
var Super = hasOwn.call(def, "$extend") ? def.$extend : Object;
var Class = hasOwn.call(def, "constructor") ? def.constructor : newConstructor(Super);
if (!isMixin) {
// HACK: If the class `constructor` was defind as member function we
// have to wrap it as it's non-constructible by default. I don't know
// about any other way how to make it constructible.
if (!Class.prototype)
Class = newConstructor(Class);
if (Super !== Object)
Class.prototype = Object.create(Super.prototype);
}
else {
Class.prototype = Object.create(null);
}
const proto = Class.prototype;
const metaInfo = dict(Super !== Object ? Super.$metaInfo : (isMixin ? MixinMetaInfo : ClassMetaInfo));
if (metaInfo.isMixin !== isMixin)
throwTypeError(`Class/Mixin mismatch: Class.$metaInfo.isMixin(${metaInfo.isMixin}) != ${isMixin}`);
metaInfo.super = (Super !== Object) ? Super : null;
Class.$metaInfo = metaInfo;
const mixins = def.$mixins ? asArray(def.$mixins) : null;
const mixinsLen = mixins ? mixins.length : 0;
// Setup pre-init handlers, post-init handlers, and extensions.
for (i = 0; i < mixinsLen; i++) {
const mixin = mixins[i];
const meta = mixin.$metaInfo;
if (meta.preInit) MetaInfo$addInitHandlers(metaInfo, meta.preInit, "preInit");
if (meta.postInit) MetaInfo$addInitHandlers(metaInfo, meta.postInit, "postInit");
if (meta.extensions) MetaInfo$addExtensions(metaInfo, meta.extensions);
}
if (def.$preInit) MetaInfo$addInitHandlers(metaInfo, def.$preInit, "preInit");
if (def.$postInit) MetaInfo$addInitHandlers(metaInfo, def.$postInit, "postInit");
if (def.$extensions) MetaInfo$addExtensions(metaInfo, def.$extensions);
// Call preInit handlers (contains also new preInit handlers defined by mixins and class itself).
const preInit = metaInfo.preInit;
for (i = 0; i < preInit.length; i++) {
preInit[i].call(Class, def, mixins);
}
// Merge members and statics included by `$mixins` and `def` object.
for (i = 0; i < mixinsLen; i++) {
const mixin = mixins[i];
Class$mergeMembers(Class, proto, mixin.prototype);
Class$mergeMixinStatics(Class, mixin);
}
Class$mergeMembers(Class, proto, def);
Class$mergeStatics(Class, def.$statics);
// Call postInit handlers (contains also new postInit handlers defined by mixins and class itself).
const postInit = metaInfo.postInit;
for (i = 0; i < postInit.length; i++) {
postInit[i].call(Class, def, mixins);
}
// Freeze MetaInfo so it cannot be altered later by accident.
MetaInfo$freeze(metaInfo);
// Everything should be done at this moment.
return Class;
}
// `xmo(def)`
//
// Create a new `Class` based on the given definition `def`.
function xmo(def) { return xmo_(def, false); }
// `xmo.VERSION`
//
// Version information in a "major.minor.patch" form.
xmo.VERSION = VERSION;
// `xmo.mixin(def)`
//
// Create a new `Mixin` based on the given definition `def`.
function mixin(def) { return xmo_(def, true); }
xmo.mixin = mixin;
$export[$as] = xmo;
}).apply(this, typeof module === "object" ? [module, "exports"] : [this, "xmo"]);