This repository has been archived by the owner on Mar 13, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
jpp-0.3.1.js
300 lines (236 loc) · 6.51 KB
/
jpp-0.3.1.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
var jpp = {};
(function (exports){
function Detector () {
throw new ReferenceError("jpp.Detector cannot be instantiated.");
}
Detector.is = function (o, type) {
if (typeof type == "function") {
return Detector.instanceOf(o, type);
} else if (typeof type == "string") {
return typeof o == type;
} else if (type === null) {
return o === type;
}
return false;
};
Detector.isAvailable = function (o) {
return (typeof o != "undefined" && o !== null);
};
Detector.instanceOf = function (o, type) {
function loop (dataList) {
if (Detector.is(dataList, Array)) {
for (var k = 0, l = dataList.length; k < l; k++) {
if (l <= 0) {
return false;
}
var item = dataList[k];
if (o instanceof item.classConstructor) {
return true;
} else {
loop(item.derivedClassList);
}
}
}
return false;
}
var list = type._jpp_derivedClassDataList;
if (list) {
if (o instanceof list.classConstructor) {
return true;
} else {
return loop(list.derivedClassList);
}
}
return o instanceof type;
};
exports.Detector = Detector;
})(jpp);
(function (exports) {
var detector = exports.Detector;
function extendClass (derivedClass, baseClass) {
var k = null,
dc = derivedClass.constructor,
proto = dc.prototype,
emptyObject = {};
for (k in proto) {
emptyObject[k] = 1;
}
if (!detector.isAvailable(baseClass._jpp_derivedClassDataList)) {
baseClass._jpp_derivedClassDataList = {
classConstructor : baseClass,
derivedClassList : new Array()
};
}
if (!detector.isAvailable(dc._jpp_derivedClassDataList)) {
dc._jpp_derivedClassDataList = {
classConstructor : dc,
derivedClassList : new Array()
};
}
baseClass._jpp_derivedClassDataList.derivedClassList.push(dc._jpp_derivedClassDataList);
for (k in baseClass.prototype) {
if (!emptyObject[k] && !isInArray(baseClass._jpp_privateList, k)) {
proto[k] = baseClass.prototype[k];
}
}
dc._jpp_privateList = baseClass._jpp_privateList;
dc._jpp_protectedList = baseClass._jpp_protectedList;
dc._jpp_publicList = baseClass._jpp_publicList;
baseClass._jpp_isOnExtendClass = true;
baseClass._jpp_currentDerivedClass = derivedClass;
baseClass.apply(derivedClass, []);
baseClass._jpp_currentDerivedClass = null;
baseClass._jpp_isOnExtendClass = false;
}
function isInArray (array, what) {
if (!detector.isAvailable(array)) {
return false;
}
for (var i = 0, l = array.length; i < l; i++) {
if (array[i] == what) {
return true;
}
}
return false;
}
function forEach (array, func) {
for (var i = 0, l = array.length; i < l; i++) {
func(array[i]);
}
}
function createClass (list) {
function Class () {
var s = this,
k = null,
ext = list["extends"],
pri = list["private"],
pro = list["protected"],
pub = list["public"],
sta = list["static"],
propertiesAndMethodsList = [];
if (ext) {
extendClass(s, ext);
}
if (!detector.isAvailable(Class._jpp_privateList)) {
Class._jpp_privateList = new Array();
}
if (!detector.isAvailable(Class._jpp_protectedList)) {
Class._jpp_protectedList = new Array();
}
if (!detector.isAvailable(Class._jpp_publicList)) {
Class._jpp_publicList = new Array();
}
if (pri) {
for (k in pri) {
Class._jpp_privateList.push(k);
propertiesAndMethodsList.push({name : k, content : pri[k], isPrivate : true});
}
}
if (pro) {
for (k in pro) {
Class._jpp_protectedList.push(k);
propertiesAndMethodsList.push({name : k, content : pro[k], isProtected : true});
}
}
if (pub) {
for (k in pub) {
var n = k;
if (k == "constructor") {
n = "_jpp_constructor";
}
Class._jpp_publicList.push(n);
propertiesAndMethodsList.push({name : n, content : pub[k]});
}
}
if (sta) {
for (k in sta) {
Class[k] = sta[k];
}
}
if (!detector.isAvailable(s._jpp_propertyAndMethodStorage)) {
s._jpp_propertyAndMethodStorage = {};
}
forEach(propertiesAndMethodsList, function (o) {
var name, content;
if (o.isPrivate && Class._jpp_isOnExtendClass) {
return;
}
name = o.name, content = o.content;
if (typeof content != "function") {
s[name] = content;
} else {
var func = function () {
s._jpp_isCalledInClass = true;
var result = content.apply(s, arguments);
s._jpp_isCalledInClass = false;
return result;
}
s.constructor.prototype[name] = func;
}
s._jpp_propertyAndMethodStorage[name] = content;
if (o.isPrivate || o.isProtected) {
var flag = o.isProtected ? "protected" : "private";
Object.defineProperty(s, o.name, {
get : function () {
if (detector.is(s._jpp_isCalledInClass, "undefined") || !s._jpp_isCalledInClass) {
throw new RangeError(name + " is " + flag + ", you cannot get it out of the class.");
}
return s._jpp_propertyAndMethodStorage[name];
},
set : function (v) {
if (detector.is(s._jpp_isCalledInClass, "undefined") || !s._jpp_isCalledInClass) {
throw new RangeError(name + " is " + flag + ", you cannot set it out of the class.");
}
o.name = v;
s._jpp_propertyAndMethodStorage[name] = v;
}
});
}
});
if (detector.is(s._jpp_constructor, "function")) {
if (!Class._jpp_isOnExtendClass) {
s._jpp_constructor.apply(s, arguments);
} else {
Class._jpp_currentDerivedClass.super = s._jpp_constructor;
}
}
}
return Class;
};
exports.class = createClass;
})(jpp);
(function (exports) {
var detector = exports.Detector;
function overload (argTypeList, func) {
function overloadFunc () {
var list = overloadFunc._jpp_argTypeAndFuncList, l = arguments.length;
for (var j = 0, s = list.length; j < s; j++) {
var item = list[j], match = 0;
if (l != item.argTypeList.length) {
continue;
}
for (var i = 0; i < l; i++) {
var o = arguments[i];
if (detector.is(o, item.argTypeList[i])) {
match++;
}
}
if (match == l) {
return item.func.apply(this, arguments);
}
}
throw new TypeError("do not match any functions by arguments to be called.");
}
overloadFunc._jpp_argTypeAndFuncList = [];
overloadFunc.overload = function (argTypeList, func) {
overloadFunc._jpp_argTypeAndFuncList.push({
argTypeList : argTypeList,
func : func
});
return overloadFunc;
};
overloadFunc.overload(argTypeList, func);
return overloadFunc;
}
exports.overload = overload;
})(jpp);