forked from tower-archive/tower
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcoffee-inheritance.js
397 lines (385 loc) · 13.1 KB
/
coffee-inheritance.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
require('coffee-script');
// coffeescript extensions
var nodes = require('coffee-script/lib/coffee-script/nodes');
var Scope = require('coffee-script/lib/coffee-script/scope').Scope;
var helpers = require('coffee-script/lib/coffee-script/helpers');
var Literal = nodes.Literal;
var Extends = nodes.Extends;
var Code = nodes.Code;
var Class = nodes.Class;
var Param = nodes.Param;
var Block = nodes.Block;
var Call = nodes.Call;
var Assign = nodes.Assign;
var Value = nodes.Value;
var Parens = nodes.Parens;
var Access = nodes.Access;
var If = nodes.If;
var Arr = nodes.Arr;
var compact = helpers.compact;
var flatten = helpers.flatten;
var extend = helpers.extend;
var merge = helpers.merge;
var del = helpers.del;
var starts = helpers.starts;
var ends = helpers.ends;
var last = helpers.last;
var LEVEL_TOP = 1;
var LEVEL_PAREN = 2;
var LEVEL_LIST = 3;
var LEVEL_COND = 4;
var LEVEL_OP = 5;
var LEVEL_ACCESS = 6;
var TAB = ' ';
var IDENTIFIER_STR = "[$A-Za-z_\\x7f-\\uffff][$\\w\\x7f-\\uffff]*";
var IDENTIFIER = RegExp("^" + IDENTIFIER_STR + "$");
var SIMPLENUM = /^[+-]?\d+$/;
var METHOD_DEF = RegExp("^(?:(" + IDENTIFIER_STR + ")\\.prototype(?:\\.(" + IDENTIFIER_STR + ")|\\[(\"(?:[^\\\\\"\\r\\n]|\\\\.)*\"|'(?:[^\\\\'\\r\\n]|\\\\.)*')\\]|\\[(0x[\\da-fA-F]+|\\d*\\.?\\d+(?:[eE][+-]?\\d+)?)\\]))|(" + IDENTIFIER_STR + ")$");
var IS_STRING = /^['"]/;
var __indexOf = [].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; };
Class.prototype.addProperties = function(node, name, o) {
var assign, base, exprs, func, props;
props = node.base.properties.slice(0);
exprs = (function() {
var _results;
_results = [];
while (assign = props.shift()) {
if (assign instanceof Assign) {
base = assign.variable.base;
delete assign.context;
func = assign.value;
if (base.value === 'constructor') {
if (this.ctor) {
throw new Error('cannot define more than one constructor in a class');
}
if (func.bound) {
throw new Error('cannot define a constructor as a bound function');
}
if (func instanceof Code) {
assign = this.ctor = func;
} else {
this.externalCtor = o.scope.freeVariable('class');
assign = new Assign(new Literal(this.externalCtor), func);
}
} else {
if (assign.variable["this"]) {
assign._isClassProp = true;
func["static"] = true;
if (func.bound) {
func.context = name;
}
} else {
assign._isInstanceProp = true;
assign.variable = new Value(new Literal(name), [new Access(new Literal('prototype')), new Access(base)]);
if (func instanceof Code && func.bound) {
this.boundFuncs.push(base);
func.bound = false;
}
}
}
}
_results.push(assign);
}
return _results;
}).call(this);
return compact(exprs);
};
Class.prototype.compileNode = function(o) {
var call, decl, e, i, key, klass, lname, name, params, value, _i, _len, _ref2, _ref3;
decl = this.determineName();
name = decl || '_Class';
if (name.reserved) {
name = "_" + name;
}
lname = new Literal(name);
this.hoistDirectivePrologue();
this.setContext(name);
this.walkBody(name, o);
if (this.parent) {
this.superClass = new Literal(o.scope.freeVariable('super', false));
this.body.expressions.unshift(new Extends(lname, this.superClass));
}
this.ensureConstructor(name);
this.body.spaced = true;
if (!(this.ctor instanceof Code)) {
this.body.expressions.unshift(this.ctor);
}
this.body.expressions.push(lname);
(_ref2 = this.body.expressions).unshift.apply(_ref2, this.directives);
_ref3 = this.body.expressions;
for (i = _i = 0, _len = _ref3.length; _i < _len; i = ++_i) {
e = _ref3[i];
if (e._isInstanceProp) {
key = e.variable.properties[e.variable.properties.length - 1].name.toString();
value = e.value;
this.body.expressions[i] = this.hookBodyExpression('instance', name, key, value);
} else if (e._isClassProp) {
key = e.variable.properties[e.variable.properties.length - 1].name.toString();
value = e.value;
this.body.expressions[i] = this.hookBodyExpression('class', name, key, value);
}
}
this.addBoundFunctions(o);
call = Closure.wrap(this.body);
if (this.parent) {
call.args.push(this.parent);
params = call.variable.params || call.variable.base.params;
params.push(new Param(this.superClass));
}
klass = new Parens(call, true);
if (this.variable) {
klass = new Assign(this.variable, klass);
}
return klass.compile(o);
}
Code.prototype.compileNode = function(o) {
var code, exprs, i, idt, lit, name, p, param, params, ref, splats, uniqs, val, wasEmpty, _i, _j, _k, _l, _len, _len1, _len2, _len3, _len4, _len5, _m, _n, _ref2, _ref3, _ref4, _ref5, _ref6, _ref7, _ref8;
o.scope = new Scope(o.scope, this.body, this);
o.scope.shared = del(o, 'sharedScope');
o.indent += TAB;
delete o.bare;
delete o.isExistentialEquals;
params = [];
exprs = [];
_ref2 = this.paramNames();
for (_i = 0, _len = _ref2.length; _i < _len; _i++) {
name = _ref2[_i];
if (!o.scope.check(name)) {
o.scope.parameter(name);
}
}
_ref3 = this.params;
for (_j = 0, _len1 = _ref3.length; _j < _len1; _j++) {
param = _ref3[_j];
if (!param.splat) {
continue;
}
_ref4 = this.params;
for (_k = 0, _len2 = _ref4.length; _k < _len2; _k++) {
p = _ref4[_k];
if (p.name.value) {
o.scope.add(p.name.value, 'var', true);
}
}
splats = new Assign(new Value(new Arr((function() {
var _l, _len3, _ref5, _results;
_ref5 = this.params;
_results = [];
for (_l = 0, _len3 = _ref5.length; _l < _len3; _l++) {
p = _ref5[_l];
_results.push(p.asReference(o));
}
return _results;
}).call(this))), new Value(new Literal('arguments')));
break;
}
_ref5 = this.params;
for (_l = 0, _len3 = _ref5.length; _l < _len3; _l++) {
param = _ref5[_l];
if (param.isComplex()) {
val = ref = param.asReference(o);
if (param.value) {
val = new Op('?', ref, param.value);
}
exprs.push(new Assign(new Value(param.name), val, '=', {
param: true
}));
} else {
ref = param;
if (param.value) {
lit = new Literal(ref.name.value + ' == null');
val = new Assign(new Value(param.name), param.value, '=');
exprs.push(new If(lit, val));
}
}
if (!splats) {
params.push(ref);
}
}
wasEmpty = this.body.isEmpty();
if (splats) {
exprs.unshift(splats);
}
if (exprs.length) {
(_ref6 = this.body.expressions).unshift.apply(_ref6, exprs);
}
for (i = _m = 0, _len4 = params.length; _m < _len4; i = ++_m) {
p = params[i];
o.scope.parameter(params[i] = p.compile(o));
}
uniqs = [];
_ref7 = this.paramNames();
for (_n = 0, _len5 = _ref7.length; _n < _len5; _n++) {
name = _ref7[_n];
if (__indexOf.call(uniqs, name) >= 0) {
throw SyntaxError("multiple parameters named '" + name + "'");
}
uniqs.push(name);
}
if (!(wasEmpty || this.noReturn)) {
this.body.makeReturn();
}
if (this.bound) {
if ((_ref8 = o.scope.parent.method) != null ? _ref8.bound : void 0) {
this.bound = this.context = o.scope.parent.method.context;
} else if (!this["static"]) {
o.scope.parent.assign('_this', 'this');
}
}
idt = o.indent;
code = 'function';
if (this.ctor) {
code += ' ' + this.name;
}
code += '(' + params.join(', ') + ') {';
if (!this.body.isEmpty()) {
code += "\n" + (this.body.compileWithDeclarations(o)) + "\n" + this.tab;
}
code += '}';
if (this.ctor) {
return this.tab + code;
}
if (this.front || (o.level >= LEVEL_ACCESS)) {
return "(" + code + ")";
} else {
return code;
}
}
var Closure = {
wrap: function(expressions, statement, noReturn) {
var args, call, func, mentionsArgs, meth;
if (expressions.jumps()) {
return expressions;
}
func = new Code([], Block.wrap([expressions]));
args = [];
if ((mentionsArgs = expressions.contains(this.literalArgs)) || expressions.contains(this.literalThis)) {
meth = new Literal(mentionsArgs ? 'apply' : 'call');
args = [new Literal('this')];
if (mentionsArgs) {
args.push(new Literal('arguments'));
}
func = new Value(func, [new Access(meth)]);
}
func.noReturn = noReturn;
call = new Call(func, args);
if (statement) {
return Block.wrap([call]);
} else {
return call;
}
},
literalArgs: function(node) {
return node instanceof Literal && node.value === 'arguments' && !node.asKey;
},
literalThis: function(node) {
return (node instanceof Literal && node.value === 'this' && !node.asKey) || (node instanceof Code && node.bound) || (node instanceof Call && node.isSuper);
}
};
Extends.prototype.compile = function(o) {
return new nodes.Assign(this.child, new nodes.Call(new nodes.Value(new nodes.Literal(utility('extends'))), [this.child, this.parent])).compile(o);
};
Class.prototype.compileNode = function(o) {
var call, decl, e, i, key, klass, lname, name, params, value, _i, _len, _ref2, _ref3;
decl = this.determineName();
name = decl || '_Class';
if (name.reserved) {
name = "_" + name;
}
lname = new Literal(name);
this.hoistDirectivePrologue();
this.setContext(name);
this.walkBody(name, o);
if (this.parent) {
this.superClass = new Literal(o.scope.freeVariable('super', false));
this.body.expressions.unshift(new Extends(lname, this.superClass));
}
this.ensureConstructor(name);
this.body.spaced = true;
if (!(this.ctor instanceof Code)) {
this.body.expressions.unshift(this.ctor);
}
this.body.expressions.push(lname);
(_ref2 = this.body.expressions).unshift.apply(_ref2, this.directives);
_ref3 = this.body.expressions;
for (i = _i = 0, _len = _ref3.length; _i < _len; i = ++_i) {
e = _ref3[i];
if (e._isInstanceProp) {
key = e.variable.properties[e.variable.properties.length - 1].name.toString();
value = e.value;
this.body.expressions[i] = this.hookBodyExpression('instance', name, key, value);
} else if (e._isClassProp) {
key = e.variable.properties[e.variable.properties.length - 1].name.toString();
value = e.value;
this.body.expressions[i] = this.hookBodyExpression('class', name, key, value);
}
}
this.addBoundFunctions(o);
call = Closure.wrap(this.body);
if (this.parent) {
call.args.push(this.parent);
params = call.variable.params || call.variable.base.params;
params.push(new Param(this.superClass));
}
klass = new Parens(call, true);
if (this.variable) {
klass = new Assign(this.variable, klass);
}
return klass.compile(o);
};
Class.prototype.hookBodyExpression = function(type, name, key, value) {
var clazz, keyL, newExpression, util;
if (type === 'instance') {
util = new Literal(utility('defineProperty'));
} else if (type === 'class') {
util = new Literal(utility('defineStaticProperty'));
} else {
throw new Error('hookBodyExpression() requires type of class or instance');
}
clazz = new Literal(name);
keyL = new Literal(key);
if (value instanceof Code) {
value.klass = name;
if (!(value.name != null)) {
value.name = key;
}
if (value.bound) {
value.context = name;
}
}
newExpression = new Call(new Value(util), [clazz, keyL, value]);
newExpression.klass = name;
if (newExpression.bound) {
newExpression.context = name;
}
return newExpression;
};
utility = function(name) {
var ref;
ref = "__" + name;
Scope.root.assign(ref, UTILITIES[name]());
return ref;
};
UTILITIES = {
defineStaticProperty: function() {
return "function(clazz, key, value) {\n if (typeof clazz.__defineStaticProperty == 'function') return clazz.__defineStaticProperty(key, value);\n return clazz[key] = value;\n}";
},
defineProperty: function() {
return "function(clazz, key, value) {\n if (typeof clazz.__defineProperty == 'function') return clazz.__defineProperty(key, value);\n return clazz.prototype[key] = value;\n}";
},
"extends": function() {
return " function(child, parent) {\n if (typeof parent.__extend == 'function') return parent.__extend(child);\n for (var key in parent) { if (" + (utility('hasProp')) + ".call(parent, key)) child[key] = parent[key]; } \n function ctor() { this.constructor = child; } \n ctor.prototype = parent.prototype; \n child.prototype = new ctor; \n child.__super__ = parent.prototype; \n if (typeof parent.extended == 'function') parent.extended(child); \n return child; \n}";
},
bind: function() {
return 'function(fn, me){ return function(){ return fn.apply(me, arguments); }; }';
},
indexOf: function() {
return "[].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; }";
},
hasProp: function() {
return '{}.hasOwnProperty';
},
slice: function() {
return '[].slice';
}
};