-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsqueaky.js
More file actions
151 lines (129 loc) · 4.44 KB
/
squeaky.js
File metadata and controls
151 lines (129 loc) · 4.44 KB
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
/*
* This file is part of the squeakyJS project.
*
* Copyright (C) 2010, Free Software Foundation, Inc.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
nonLocalReturn = function(v) {
arguments.callee.caller.nonLocalReturnValue = v;
throw arguments.callee.caller;
}
// hide real method behind a wrapper methods which catches exceptions
Function.prototype._createMethod = function(name, method) {
// this is a wrapper for method invocation
this.prototype[name] = function() {
try {
return method.apply(this, arguments);
}
catch(e) {
if(e == method)
return e.nonLocalReturnValue;
else
throw e;
}
}
}
Class = function(attrs) {
var newClass = function() {};
if('superClass' in attrs) {
// copy superclass methods and attrs to new class
for(attr in attrs['superClass']) {
newClass.prototype[attr] = attrs['superClass'][attr];
}
newClass.prototype._super = function(method, args) {
// super calls methods without invoker for avoiding infinite recursion because
// just the invoker comes from the superclass, the invoked method comes from the current class
return attrs['superClass'][method].apply(this, args);
};
}
if('classVariables' in attrs) {
// extent by new class variables
for(idx in attrs['classVariables']) {
newClass.prototype[attrs['classVariables'][idx]] = null;
}
}
if('classMethods' in attrs) {
// extent by new class methods
for(method in attrs['classMethods']) {
newClass._createMethod(method, attrs['classMethods'][method]);
}
}
// initialize method is called after instanciation
newClass.prototype._objectPrototype = function() { this.initialize(); };
// default constructor, can be overloaded if wanted
newClass.prototype._objectPrototype.prototype.initialize = function() { };
// "new" is a reserved keyword in Safari -> leading underline
newClass.prototype._new = function() {
// create new instance of our class
return new this._objectPrototype();
}
if('superClass' in attrs) {
// inherit methods and attrs from superclass
newClass.prototype._objectPrototype.prototype = attrs['superClass']._new();
// ability to call superclass methods in the context of the current object
newClass.prototype._objectPrototype.prototype._super = function(method, args) {
// super calls methods without invoker for avoiding infinite recursion because
// just the invoker comes from the superclass, the invoked method comes from the current class
return attrs['superClass']._objectPrototype.prototype[method].apply(this, args);
};
}
if('instanceVariables' in attrs) {
// set instance variables to null by default
for(idx in attrs['instanceVariables']) {
newClass.prototype._objectPrototype.prototype[attrs['instanceVariables'][idx]] = null;
}
}
if('instanceMethods' in attrs) {
// extent by new instance methods
for(method in attrs['instanceMethods']) {
newClass.prototype._objectPrototype._createMethod(method, attrs['instanceMethods'][method]);
}
}
// "class" is a reserved keyword in Safari -> leading underline
newClass.prototype._objectPrototype.prototype._class = new newClass();
return newClass.prototype._objectPrototype.prototype._class;
};
BlockClosure = Class({
instanceMethods: {
value: function() {
return this.$func.apply(this, arguments);
}
}
});
block = function(func) {
b = BlockClosure._new();
b.$creationContext = arguments.callee.caller;
b.$func = function() {
try {
ret = func.apply(this, arguments);
if (ret.$creationContext == func) {
ret.$creationContext = this.$creationContext;
}
return ret;
}
catch(e) {
if(e == func) {
this.$creationContext.nonLocalReturnValue = e.nonLocalReturnValue;
throw this.$creationContext;
}
else {
throw e;
}
}
}
b.$func.$creationContext = arguments.callee.caller;
return b;
}