-
Notifications
You must be signed in to change notification settings - Fork 2
/
WebIDL.as
326 lines (293 loc) · 11.1 KB
/
WebIDL.as
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
/*
Expando by Jumis, Inc
Unless otherwise noted:
All source code is hereby released into public domain.
http://creativecommons.org/publicdomain/zero/1.0/
http://creativecommons.org/licenses/publicdomain/
Lead development by Charles Pritchard, influenced by Brendan Eich
Namespace work-arounds influenced by Sean Chatman and Garrett Woodworth
Additional utilities influenced by WebIDL: http://dev.w3.org/2006/webapi/WebIDL/
*/
package com.w3canvas.ascanvas {
import flash.utils.Proxy;
import flash.utils.flash_proxy;
import flash.utils.getQualifiedClassName;
// [PrototypeRoot]
public dynamic class Expando extends Proxy {
private var _trait = {};
public function set trait(t) { _trait = t; }
public function get trait() { return _trait; }
private var _sort = [];
public function get sort() { return _sort; }
public function set sort(s) { _sort = s; }
public function get length() { return sort.length; };
public function Expando(traits = null, sorts = null) {
// _trait = new Object();
// _sort = new Array();
return;
if(trait != null) {
_trait = traits;
if(sorts == null) {
sorts = []; var i;
for(i in trait) sorts.push(i);
};
} else _trait = {};
if(sorts != null) sort = sorts;
else sort = [];
};
override flash_proxy function setProperty(name:*, value:*):void {
if(!(name in trait)) _sort.push(name);
_setProperty(name,value);
}
private function _setProperty(name:*,value:*) {
// who is using this class?
// _trait[name] = value; return;
if(name in _trait) _trait[name] = value;
else if(name in this) this[name] = value;
else _trait[name] = value;
return;
if(name in this) this[name] = value;
else if (_trait == this) this[name] = value; // prevent infinite loop
else _trait[name] = value;
}
override flash_proxy function getProperty(name:*):* {
if(name == 'trait') return _trait;
return _getProperty(name);
}
protected function _getProperty(name) {
return _trait[name];
}
override flash_proxy function hasProperty(name:*):Boolean {
if(name == 'trait') return true;
return _hasProperty(name);
}
protected function _hasProperty(name:*):Boolean {
return _trait[name] ? true : false;
}
override flash_proxy function deleteProperty(name:*):Boolean {
if(!(name in trait)) return false;
delete trait[name];
var i=0; for(i in _sort) if(_sort[i] == name) delete _sort[i]; // FIXME: use sort.splice ?
}
override flash_proxy function nextName(i:int):String {
return sort[i-1];
}
override flash_proxy function nextValue(i:int):* {
return trait[sort[i-1]];
}
override flash_proxy function nextNameIndex(i:int):int {
return i < length ? i : 0;
}
override flash_proxy function getDescendants(a) { return null; }
override flash_proxy function isAttribute(a:*):Boolean { return false; }
override flash_proxy function callProperty(name:*, ... args):* {
if(name == 'hasOwnProperty') return (args[0] in trait);
if(name in trait) if(typeof(trait[name])=='function') return trait[name].apply(trait,args);
}
}
// OverrideBuiltins ( don't query prototype chain, just return from getter )
// [ImplementedOn], [Prototype]
dynamic public class ExpandoMixin extends Expando {
public function get root() { return _root; };
public function set root(parent:*) { _root = parent; };
public function get self() { return _self; };
public function set self(s:*) { _self = self; };
private var _root;
private var _expando;
private var _self;
override flash_proxy function callProperty(name:*, ... args):* {
if(name == 'hasOwnProperty') return (args[0] in super.trait);
return _callProperty(name).apply(this,args);
return _callProperty(name).apply(root,args);
// if(name in root) if(typeof(root[name])=='function') return root[name].apply(root,args);
// if(name in trait) if(typeof(trait[name])=='function') return super.trait[name].apply(root,args);
}
protected function _callProperty(name) {
if(name in _expando) if(typeof(_expando[name]) == 'function') return _expando[name];
if(name in trait) if(typeof(trait[name][name]) == 'function') return trait[name][name];
if(name in _root) if(typeof(_root[name])=='function') return _root[name]; // PrototypeRoot
throw new Error("Function "+name+" not defined");
}
override flash_proxy function getProperty(name:*):* {
if(name == 'trait') return trait;
if(name == 'root') return root;
return _getProperty(name);
}
// When improperly instantiated, this function may overflow (circular reference)
override protected function _getProperty(name) {
if ( typeof(trait[name]) != 'undefined' )
return trait[name][name];
if(_root == this) return _expando[name]; // throw new Error("Stack Overflow: "+name);
if ( typeof(_root[name]) != 'undefined' )
return _root[name];
return _expando[name];
}
override flash_proxy function setProperty(name:*, value:*):void {
_setProperty(name,value);
}
private function _setProperty(name:*,value:*) {
if(name in trait) return trait[name][name] = value;
else if(name in _root && _root != trait) return _root[name] = value;
else {
if(!(name in _expando)) sort.push(name);
return _expando[name] = value;
}
}
public function ImplementedOn(obj,names) {
var i; for(i in names) trait[names[i]] = obj;
}
public function ExpandoMixin(traits = null, sorts = null, mix = null, parent=null, expando=null, thisself=null) {
super(traits,sorts);
root = (parent == null) ? this : parent;
_expando = expando == null ? {} : expando;
self = this; // thisself ? thisself : root;
if(mix == null) return;
var r = ''; var s = {};
for(var i in mix) for(var ii in mix[i][1]) {
r = mix[i][1][ii];
s = mix[i][0];
trait[r] = s;
}
}
}
// Prototype Chain Temporarily Disabled
dynamic public class ExpandoPrototype extends Expando {
public function ExpandoPrototype(traits = null, sorts = null) {
super(traits,sorts);
}
}
// Mixin prototype
// [Prototype]
dynamic public class ExpandoPrototypeX extends Expando {
private var proto = {};
public function ExpandoPrototypeX(traits = null, sorts = null) {
super(traits,sorts);
}
override flash_proxy function getProperty(name) {
if(name in trait) return trait[name];
else if(proto && typeof(proto.getProperty)=='function') return proto.getProperty(name);
else if (proto) return proto[name];
return null;
}
flash_proxy function set __proto__ (p) {
proto = p;
}
flash_proxy function get prototype() {
return proto;
}
flash_proxy function set prototype(p) {
proto = p;
}
override flash_proxy function hasProperty(name:*):Boolean {
return name in trait || (proto ? proto.hasProperty(name) : false);
}
override flash_proxy function nextName(i:int):String {
if(proto && typeof(proto.nextName)=='function') if(i >= length) return proto.nextName(i-length);
return sort[i-1];
}
override flash_proxy function nextValue(i:int):* {
if(proto && typeof(proto.nextValue)=='function') if(i >= length) return proto.nextValue(i-length);
return trait[sort[i-1]];
}
override flash_proxy function nextNameIndex(i:int):int {
return (proto && i >= length && typeof(proto.nextNameIndex) == 'function') ? proto.nextNameIndex(i-length) : i + 1;
}
override flash_proxy function callProperty(name:*, ... args):* {
if(name == 'hasOwnProperty') return (args[0] in trait);
if(name in trait) return trait[name].apply(trait,args);
if(proto) return proto[name].apply(trait,args);
}
}
// Our best object -- just a copy of ExpandoMixin currently
dynamic public class WebIDL extends Expando {
public function get root() { return _root; };
public function set root(parent:*) { _root = parent; };
public function get self() { return _self; };
public function set self(s:*) { _self = self; };
private var _root;
private var _expando;
private var _self;
override flash_proxy function callProperty(name:*, ... args):* {
if(name == 'hasOwnProperty') return (args[0] in super.trait);
return _callProperty(name).apply(this,args);
return _callProperty(name).apply(root,args);
// if(name in root) if(typeof(root[name])=='function') return root[name].apply(root,args);
// if(name in trait) if(typeof(trait[name])=='function') return super.trait[name].apply(root,args);
}
protected function _callProperty(name) {
if(name in _expando) if(typeof(_expando[name]) == 'function') return _expando[name];
if(name in trait) if(typeof(trait[name][name]) == 'function') return trait[name][name];
if(name in _root) if(typeof(_root[name])=='function') return _root[name]; // PrototypeRoot
throw new Error("Function "+name+" not defined");
}
override flash_proxy function getProperty(name:*):* {
if(name == 'trait') return trait;
if(name == 'root') return root;
return _getProperty(name);
}
// When improperly instantiated, this function may overflow (circular reference)
override protected function _getProperty(name) {
if ( typeof(trait[name]) != 'undefined' )
return trait[name][name];
if(_root == this) return _expando[name]; // throw new Error("Stack Overflow: "+name);
if ( typeof(_root[name]) != 'undefined' )
return _root[name];
return _expando[name];
}
override flash_proxy function setProperty(name:*, value:*):void {
_setProperty(name,value);
}
private function _setProperty(name:*,value:*) {
if(name in trait) return trait[name][name] = value;
else if(name in _root && _root != trait) return _root[name] = value;
else {
if(!(name in _expando)) sort.push(name);
return _expando[name] = value;
}
}
public function Implements(obj,names = null) { // [ImplementedOn], [Supplemental]
if(names === null) {
if('public_vars' in obj && obj.public_vars !== null) Implements(obj,obj.public_vars)
if('public_call' in obj && obj.public_call !== null) Implements(obj,obj.public_call)
}
var i; for(i in names) trait[names[i]] = obj;
}
public function WebIDL(traits = null, sorts = null, mix = null, parent=null, expando=null, thisself=null) {
super(traits,sorts);
root = (parent == null) ? this : parent;
_expando = expando == null ? {} : expando;
self = this; // thisself ? thisself : root;
if(mix == null) return;
var r = ''; var s = {};
for(var i in mix) for(var ii in mix[i][1]) {
r = mix[i][1][ii];
s = mix[i][0];
trait[r] = s;
}
}
}
// Experimental: Events and CSS
// Bubble function calls to parent nodes
dynamic public class ExpandoBubble extends Expando {
var cancelBubble = false;
private var proto = {};
override flash_proxy function callProperty(name:*, ... args):* {
if(name == 'hasOwnProperty') return (args[0] in trait);
if(name in trait) {
trait[name].apply(trait,args);
if(!cancelBubble) if(proto) proto[name].apply(this,args);
cancelBubble = false;
}
}
}
// Priority flag in prototype chain
dynamic public class ExpandoPriority extends Expando {
private var proto = {};
override flash_proxy function getProperty(name) {
if(proto && proto.hasImportantProperty(name)) return proto.getImportantProperty(name);
if(name in trait) return trait[name];
else if(proto) return proto.getProperty(name);
return null;
}
}
}