-
Notifications
You must be signed in to change notification settings - Fork 0
/
pia.js
223 lines (175 loc) · 7 KB
/
pia.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
//***********************************************
// pia.js v0.0.2
//***********************************************
(function(){
var pfunc = {};
pfunc.new = function(){
var PiaBaseObject = function(){};
this.appendToProterty(PiaBaseObject);
var klassInstance = (!!this.__super__ && !!this.__super__.initialize) ?
this.callInitialize(new PiaBaseObject(), arguments, this.__super__.initialize) :
this.callInitialize(new PiaBaseObject(), arguments);
klassInstance.__super__ = pfunc.createSuperObject(this.__super__);
return this.createInstanceMethods(klassInstance);
};
pfunc.createInstanceMethods = function(klassInstance){
return this.convert(klassInstance);
};
pfunc.callInitialize = function(klassInstance, args, superInitialize){
if(!this.initialize) return klassInstance;
if(!!superInitialize){
var initialize = '(function(){'+
'function $super(){return superInitialize.apply(klassInstance, arguments)};'+
'('+this.initialize.toString()+').apply(klassInstance, args[0])'+
'})()';
eval(initialize);
}else{
this.initialize.apply(klassInstance, args[0]);
};
return klassInstance;
};
pfunc.convert = function(klassInstance){
var __publicProperties__ = this.public;
var __privateProperties__ = this.private;
var PiaObject = function(){};
function classEval(__proto__){
var __self__ = this;
var __super__ = this.__super__;
for(var __publicProperty__ in __publicProperties__){
if(typeof this[__publicProperty__] != 'function') continue;
var funcString = '__proto__.'+__publicProperty__+' = function(){var func='+
this[__publicProperty__].toString()+';return func.apply(__self__, arguments)}';
if(!!__super__){
funcString = '(function(){ '+'function $super(){'+
'return __super__["'+__publicProperty__+'"].apply(__self__, arguments)};'+
funcString+' })();';
};
eval(funcString);
};
};
classEval.call(klassInstance, PiaObject.prototype);
return new PiaObject();
};
pfunc.appendToProterty = function(PiaBaseObject){
function to(klass, properties){
for(var property in properties)
if(properties.hasOwnProperty(property))
klass.prototype[property] = properties[property];
return klass
};
PiaBaseObject = to(PiaBaseObject, this.public);
PiaBaseObject = to(PiaBaseObject, this.private);
return PiaBaseObject;
};
pfunc.createSuperObject = function(__super__){
var PiaSuperObject = function(){};
PiaSuperObject = pfunc.appendToProterty.call(__super__, PiaSuperObject);
return new PiaSuperObject();
};
pfunc.extendProperties = function(superClass, childClass){
var klass = {};
klass.public = pfunc.merge(superClass.public, childClass.public);
klass.private = pfunc.merge(superClass.private, childClass.private);
var superSelf = superClass.self;
var childSelf = childClass.self;
if(!!superSelf || !!childSelf){
klass.self = {};
klass.self.public = pfunc.merge(
!!superSelf ? superSelf.public : {},
!!childSelf ? childSelf.public : {}
);
klass.self.private = pfunc.merge(
!!superSelf ? superSelf.private : {},
!!childSelf ? childSelf.private : {}
);
};
klass.initialize = childClass.initialize || superClass.initialize;
return klass;
};
pfunc.merge = function merge(mainObj, subObj){
var obj = {};
for(var mainKey in mainObj)
if(mainObj.hasOwnProperty(mainKey)) obj[mainKey] = mainObj[mainKey];
for(var subKey in subObj)
if(subObj.hasOwnProperty(subKey)) obj[subKey] = subObj[subKey];
return obj;
};
var pia = {};
//***********************************************
// InstanceConstruction constructor
//***********************************************
pia.InstanceConstruction = function(_class){
this.klass = _class;
this.initialize = _class.initialize || function(){};
this.public = _class.public || {};
this.private = _class.private || {};
};
pia.InstanceConstruction.prototype.new = pfunc.new;
pia.InstanceConstruction.prototype.createInstanceMethods = pfunc.createInstanceMethods;
pia.InstanceConstruction.prototype.callInitialize = pfunc.callInitialize;
pia.InstanceConstruction.prototype.convert = pfunc.convert;
pia.InstanceConstruction.prototype.appendToProterty = pfunc.appendToProterty;
//***********************************************
// ClassConstruction constructor
//***********************************************
pia.ClassConstruction = function(_class, __super__){
if(!!_class.self) this.private = _class.self.private || {};
this.klass = _class;
this.public = this.makePublicProperty(_class);
this.initialize = function(klass){
this.instanceConst = new pia.InstanceConstruction(klass);
this.instanceConst.__super__ = __super__;
this.__klass__ = klass;
};
return this;
};
pia.ClassConstruction.prototype.new = function(){
var PiaBaseObject = function(){};
this.appendToProterty(PiaBaseObject);
var klassInstance = this.callInitialize(new PiaBaseObject(), this.klass);
klassInstance = this.createInstanceMethods(klassInstance);
klassInstance.__klass__ = this.klass;
return klassInstance;
};
pia.ClassConstruction.prototype.createInstanceMethods = pfunc.createInstanceMethods;
pia.ClassConstruction.prototype.callInitialize = function(klassInstance, args){
if(!this.initialize) return klassInstance;
this.initialize.call(klassInstance, args);
return klassInstance;
};
pia.ClassConstruction.prototype.convert = pfunc.convert;
pia.ClassConstruction.prototype.appendToProterty = pfunc.appendToProterty;
pia.ClassConstruction.prototype.makePublicProperty = function(_class, __super__){
var public = {};
if(!!_class.self)
for(var key in _class.self.public) public[key] = _class.self.public[key];
public.new = this.createNewMethod;
public.extend = this.createExtendMethod;
return public;
};
pia.ClassConstruction.prototype.createNewMethod = function(){
for(var i=0, args=[]; i<arguments.length; i++) args[i]=arguments[i];
return this.instanceConst.new(args);
};
pia.ClassConstruction.prototype.createExtendMethod = function(SuperClass){
var klass = pfunc.extendProperties(SuperClass.__klass__, this.__klass__);
return new pia.ClassConstruction(klass, SuperClass.__klass__).new();
};
//***********************************************
// $class
//***********************************************
var BaseObject = function(_class){
this.classConst = new pia.ClassConstruction(_class);
return this.classConst.new();
};
var g = null;
try{
//g = global;
g = window;
}catch(e){
g = window;
};
g.$class = function(_class){
return new BaseObject(_class);
};
})();