-
Notifications
You must be signed in to change notification settings - Fork 0
/
dodo.js
306 lines (276 loc) · 8.29 KB
/
dodo.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
"use strict";
var dodo = {FAST: true};
if ("console" in window && "log" in console)
{
dodo.log = function log() {
console.log.apply(console, arguments);
};
dodo.dumpLog = function dumpLog() {};
}
else
{
dodo.logMessages = "";
dodo.log = function log() {
var line = "";
var len = arguments.length;
for (var i = 0; i < len; i++)
{
line += arguments[i];
}
dodo.logMessages += line + "\n";
};
dodo.dumpLog = function dumpLog() {
throw new Error(dodo.logMessages);
};
}
dodo.toString = function toString() {
var desc = "{";
for (var attr in this)
{
var val = this[attr];
if (typeof val === "function")
{
if (!this.hasOwnProperty(attr)) continue;
val = "function()";
}
desc += attr + ": " + val + ",";
}
return desc.replace(/,?$/, "}");
};
dodo.log("Loading dodo object model");
if ("create" in Object)
{
dodo.create = Object.create;
}
else
{
dodo.FAST = false;
dodo.create = function create(o) {
function F() {}
F.prototype = o;
return new F();
};
}
if (dodo.create({}, {v:{value:true}}).v)
{
console.log("FAST copy enabled");
}
else
{
var old_create = dodo.create;
dodo.create = function create(o, props) {
var self = old_create(o);
for (name in props)
{
dodo._defineProperty(self, name, props[name]);
}
return self;
};
}
if ("assign" in Object)
{
dodo.assign = Object.assign;
}
else
{
dodo.assign = function assign(o) {
for (var n = 1; n < arguments.length; ++n)
{
var source = arguments[n];
for (var name in source)
{
o[name] = source[name];
}
}
};
}
dodo.definePropertyFailback = function definePropertyFailback(o, name, descriptor) {
if ("value" in descriptor)
{
o[name] = descriptor.value;
}
else if ("get" in descriptor || "set" in descriptor)
{
throw new Error("dodo._defineProperty implementation only accepts data descriptors: property: " + name);
}
// ignore other cases
};
dodo.canDefineProperty = false;
if ("defineProperty" in Object)
{
try {
// The following fails in IE8
Object.defineProperty({}, "dummy", {value: 1, writable: true});
dodo.canDefineProperty = true;
}
catch (someException) {
dodo.log("Object.defineProperty defined, but fails on non-DOM objects!");
}
}
if (dodo.canDefineProperty)
{
dodo._defineProperty = dodo.FAST
? Object.defineProperty
: function _defineProperty(o, name, descriptor) {
try {
Object.defineProperty(o, name, descriptor);
}
catch (someException) {
dodo.log("dodo._defineProperty: ", someException);
dodo.definePropertyFailback(o, name, descriptor);
}
};
}
else
{
dodo._defineProperty = dodo.definePropertyFailback;
dodo.log("Define object property not available!");
}
dodo.objectType = (function(global) {
return function objectType(value) {
if (value === global)
{
return "global";
}
else if (value === undefined)
{
return "undefined";
}
else if ('typename' in value.constructor)
{
return value.constructor.typename;
}
else
{
return Object.prototype.toString.call(value);
}
};
})(this);
dodo.checkType = function checkType(variable, value, reference) {
if (typeof value !== typeof reference ||
(value instanceof Object && !(value instanceof reference.constructor)))
{
throw new Error("checkType: incompatible type: supplied " + variable + " is " +
dodo.objectType(value) + ", expected: " + dodo.objectType(reference));
}
};
function copy(proto) {
if (arguments.length < 2) return dodo.create(proto);
var props = {};
for (var n = 1; n < arguments.length; ++n)
{
var changes = arguments[n];
for (var attribute in changes)
{
var value = changes[attribute];
var dot = attribute.indexOf("$");
var attr = attribute;
if (dot !== -1)
{
attr = attribute.substr(0, dot);
if (!(attr in proto))
{
throw new Error("copy: unknown attribute: " + attribute);
}
var change = {};
change[attribute.substr(dot + 1)] = value;
value = copy(proto[attr], change);
}
else
{
dodo.checkType(attr, value, proto[attr]);
}
var prop = {value: value, writable: false, enumerable: true};
props[attr] = prop;
}
}
return dodo.create(proto, props);
}
dodo.extend = function extend(self, extension) {
var extended = false;
for (var attribute in extension)
{
var value = extension[attribute];
if (attribute.lastIndexOf("$", 0) === 0)
{
attribute = attribute.substr(1);
if (!(attribute in self))
{
throw new Error("extend: cannot override unknown attribute: " + attribute);
}
dodo.checkType(attribute, value, self[attribute]);
}
else if (attribute in self)
{
throw new Error("extend: attribute already exists: " + attribute);
}
else
{
extended = true;
}
dodo._defineProperty(self, attribute, {value: value, writable: false, enumerable: true});
}
return extended;
}
dodo.MakeType = function MakeType(name, proto, instance) {
dodo.checkType('instance', instance, proto);
var MakeInstance = function MakeInstance() {
var data = {constructor: MakeInstance};
instance.init.apply(data, arguments);
delete data.constructor;
return copy(instance, data);
};
dodo._defineProperty(MakeInstance, 'typename', {value: name, writable: false, enumerable: true});
MakeInstance.prototype = proto;
MakeInstance.instance = instance;
dodo._defineProperty(instance, 'constructor', {value: MakeInstance, writable: false, enumerable: false});
if (!('init' in instance))
{
dodo._defineProperty(instance, 'init', {value: function init() {}, writable: false, enumerable: false});
}
return MakeInstance;
};
function extend(proto, extension) {
var self = dodo.create(proto);
if (dodo.extend(self, extension))
{
// Create a new type since self was extended with new attributes
var name = ('typename' in proto.constructor)
? proto.constructor.typename
: "extend " + dodo.objectType(proto);
dodo.MakeType(name, proto, self);
}
return self;
}
function Create(proto, name, body) {
var instance = dodo.create(proto);
dodo.extend(instance, body);
return dodo.MakeType(name, proto, instance);
}
dodo.Type = Create({}, 'dodo.Type');
var struct = dodo.Type.instance;
dodo._defineProperty(struct, 'toString', {value: dodo.toString, writable: false, enumerable: false});
/*
// Examples of use
// Create a struct x with attributes a, z and b
var x = extend(struct, {a:3, z:true, b:{}});
// Display a copy of x where a is replaced with a different value
alert(dodo.objectType(x) + " " + copy(x, {a:-5.6}));
// Extend x to create a variable y with additional attribute c and a new value for a
var y = extend(x, {c:"hi", $a:9});
// Type checking: x extends {} and y extends x, so z can be created as follows
var z = copy(copy(x, {b:x}), {b:y});
// Nesting: since z.b is a struct containing attribute b (z.b == x), display a copy of z with a new value for z.b.b
alert(copy(z, {b$b:extend(struct, {j:"dodo"})}));
// Create a new type A with prototype struct and attribute col
var A = Create(struct, "A", {col:"blue"});
// Create a new type B with prototype struct and a new constructor
var B = Create(struct, "B", {$init:function(n) {dodo.log(n);}});
// Create a new type C based on A with a new attribute p
var C = Create(A(), "C", {p:0});
// Call the constructor of B
B(1000);
// Create a new C and display it
alert(C());
dodo.dumpLog();
*/