1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
function isArray(a) { |
18 |
return a && !!a.push; |
19 |
} |
20 |
|
21 |
function isString(s) { |
22 |
return typeof s == 'string'; |
23 |
} |
24 |
|
25 |
function isNumber(s) { |
26 |
return typeof s == 'number'; |
27 |
} |
28 |
|
29 |
function isBoolean(s) { |
30 |
return typeof s == 'boolean'; |
31 |
} |
32 |
|
33 |
function isIndexed(s) { |
34 |
return typeof s.length == 'number'; |
35 |
} |
36 |
|
37 |
function isObject(o) { |
38 |
return o.instanceTag == o; |
39 |
} |
40 |
|
41 |
var uid = (function() { |
42 |
var id = 0; |
43 |
return function() { |
44 |
return id++; |
45 |
}; |
46 |
})(); |
47 |
|
48 |
function operationNotSupported() { |
49 |
throw 'operation not supported'; |
50 |
} |
51 |
|
52 |
function operator(defaultOperation) { |
53 |
return function() { |
54 |
var args = arguments; |
55 |
var instance = arguments[0]; |
56 |
if (instance.instanceTag && instance.instanceTag == instance) { |
57 |
var method = instance(arguments.callee); |
58 |
if (method) { |
59 |
return method.apply(method, args); |
60 |
} else { |
61 |
operationNotSupported(); |
62 |
} |
63 |
} else { |
64 |
return defaultOperation ? defaultOperation.apply(defaultOperation, args) : operationNotSupported(); |
65 |
} |
66 |
}; |
67 |
} |
68 |
|
69 |
var asString = operator(String); |
70 |
var asNumber = operator(Number); |
71 |
var hash = operator(function(o) { |
72 |
var s; |
73 |
if (isString(o)) { |
74 |
s = o; |
75 |
} else if (isNumber(o)) { |
76 |
return Math.abs(Math.round(o)); |
77 |
} else { |
78 |
s = o.toString(); |
79 |
} |
80 |
|
81 |
var h = 0; |
82 |
for (var i = 0, l = s.length; i < l; i++) { |
83 |
var c = parseInt(s[i], 36); |
84 |
if (!isNaN(c)) { |
85 |
h = c + (h << 6) + (h << 16) - h; |
86 |
} |
87 |
} |
88 |
return Math.abs(h); |
89 |
}); |
90 |
var equal = operator(function(a, b) { |
91 |
return a == b; |
92 |
}); |
93 |
|
94 |
function object(definition) { |
95 |
var operators = []; |
96 |
var methods = []; |
97 |
var unknown = null; |
98 |
var id = uid(); |
99 |
operators.push(hash); |
100 |
methods.push(function(self) { |
101 |
return id; |
102 |
}); |
103 |
operators.push(equal); |
104 |
methods.push(function(self, other) { |
105 |
return self == other; |
106 |
}); |
107 |
operators.push(asString); |
108 |
methods.push(function(self) { |
109 |
return 'Object:' + id.toString(16); |
110 |
}); |
111 |
definition(function(operator, method) { |
112 |
|
113 |
var size = operators.length; |
114 |
for (var i = 0; i < size; i++) { |
115 |
if (operators[i] == operator) { |
116 |
methods[i] = method; |
117 |
return; |
118 |
} |
119 |
} |
120 |
operators.push(operator); |
121 |
methods.push(method); |
122 |
}, function(method) { |
123 |
unknown = method; |
124 |
}); |
125 |
|
126 |
function self(operator) { |
127 |
var size = operators.length; |
128 |
for (var i = 0; i < size; i++) { |
129 |
if (operators[i] == operator) { |
130 |
return methods[i]; |
131 |
} |
132 |
} |
133 |
|
134 |
return unknown; |
135 |
} |
136 |
|
137 |
|
138 |
return self.instanceTag = self; |
139 |
} |
140 |
|
141 |
function objectWithAncestors() { |
142 |
var definition = arguments[0]; |
143 |
var args = arguments; |
144 |
var o = object(definition); |
145 |
|
146 |
function self(operator) { |
147 |
var method = o(operator); |
148 |
if (method) { |
149 |
return method; |
150 |
} else { |
151 |
var size = args.length; |
152 |
for (var i = 1; i < size; i++) { |
153 |
var ancestor = args[i]; |
154 |
var overriddenMethod = ancestor(operator); |
155 |
if (overriddenMethod) { |
156 |
return overriddenMethod; |
157 |
} |
158 |
} |
159 |
|
160 |
return null; |
161 |
} |
162 |
} |
163 |
|
164 |
|
165 |
return self.instanceTag = self; |
166 |
} |