-
Notifications
You must be signed in to change notification settings - Fork 62
/
object.js
549 lines (445 loc) · 13 KB
/
object.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
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
/* _________________________________________________________________________
*
* Tachyon : A Self-Hosted JavaScript Virtual Machine
*
*
* This file is part of the Tachyon JavaScript project. Tachyon is
* distributed at:
* http://github.com/Tachyon-Team/Tachyon
*
*
* Copyright (c) 2012-2015, Universite de Montreal
* All rights reserved.
*
* This software is licensed under the following license (Modified BSD
* License):
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of the Universite de Montreal nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL UNIVERSITE DE
* MONTREAL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
* _________________________________________________________________________
*/
/**
@class 15.2.2 The Object Constructor
new Object([value])
Object([value])
*/
function Object(value)
{
if (value !== undefined && value !== null)
{
switch (typeof value)
{
case 'object':
case 'function':
return value;
case 'boolean':
return new Boolean(value);
case 'string':
return new String(value);
case 'number':
return new Number(value);
}
error('invalid value passed to Object constructor');
}
return {};
}
// Set the object prototype object
Object.prototype = $ir_get_obj_proto();
Object.length = 1;
/**
15.2.4.1 Object.prototype.constructor
*/
Object.prototype.constructor = Object;
//-----------------------------------------------------------------------------
/**
15.2.3.2 Get the prototype of an object
*/
Object.getPrototypeOf = function (obj)
{
assert (
$rt_valIsObj(obj),
'non-object value in getPrototypeOf'
);
var proto = $ir_obj_get_proto(obj);
return proto;
};
/**
15.2.3.3 Get a descriptor for an object's property
*/
Object.getOwnPropertyDescriptor = function (obj, prop)
{
if ($rt_valIsObj(obj) === false)
throw TypeError('invalid object in getOwnPropertyDescriptor');
prop = $rt_toString(prop);
// Get the current property attributes
var attrs = $ir_obj_get_attrs(obj, prop);
// If the property doesn't exist
if (attrs & $rt_ATTR_DELETED)
return undefined;
var desc = {};
desc.writable = !!(attrs & $rt_ATTR_WRITABLE);
desc.enumerable = !!(attrs & $rt_ATTR_ENUMERABLE);
desc.configurable = !!(attrs & $rt_ATTR_CONFIGURABLE);
// Get the property value
var propVal;
if (propVal = $ir_obj_get_prop(obj, prop))
{
desc.value = propVal;
}
else
{
// This property is a getter-setter
desc.get = propVal.get;
desc.set = propVal.set;
}
return desc;
};
/**
15.2.3.4 Get the named own properties of an object (excludes the prototype chain)
*/
Object.getOwnPropertyNames = function (O)
{
if ($rt_valIsObj(O) === false)
throw TypeError('invalid object in getOwnPropertyNames');
var propNames = [];
for (k in O)
{
if (O.hasOwnProperty(k) === true)
propNames.push(k);
}
return propNames;
};
/**
15.2.3.5 Object.create ( O [, Properties] )
*/
Object.create = function (proto, properties)
{
if ($rt_valIsObj(proto) === false && proto !== null)
throw TypeError('can only create object with object or null prototype');
var newObj = $rt_newObj(proto);
if (properties !== undefined)
Object.defineProperties(newObj, properties);
return newObj;
};
/**
15.2.3.6 Object.defineProperty ( O, P, Attributes )
*/
Object.defineProperty = function (obj, prop, attribs)
{
// FIXME: use 'in' instead of hasOwnProperty
if (!$rt_valIsObj(obj))
throw TypeError('non-object value in defineProperty');
if (!$rt_valIsObj(attribs))
throw TypeError('property descriptor must be an object');
if ('value' in attribs && ('get' in attribs || 'set' in attribs))
throw TypeError('property cannot have both a value and accessors');
// Convert the property name to a string if necessary
prop = $rt_toString(prop);
// Test if accessors were specified
var isGS = 'get' in attribs || 'set' in attribs;
// If accessors are specified
if (isGS)
{
var defFn = function () {};
var get = ('get' in attribs)? attribs.get:defFn;
var set = ('set' in attribs)? attribs.set:defFn;
if (typeof get !== 'function' || typeof set !== 'function')
throw TypeError('accessors must be functions');
// Create a property descriptor pair
obj[prop] = { get:get, set:set };
}
else
{
// If this is a new property
if (!$rt_hasOwnProp(obj, prop))
{
var objAttrs = $ir_obj_get_attrs(obj, null);
if (!(objAttrs & $rt_ATTR_EXTENSIBLE))
throw TypeError("cannot add new property to non-extensible object");
// Create the new property
obj[prop] = undefined;
}
// If a value is specified, try to set it,
// this will do nothing if writable is false
if ('value' in attribs)
{
obj[prop] = attribs.value;
}
}
// Extract the current property attributes
var oldAttrs = $ir_obj_get_attrs(obj, prop);
var oldWR = !!(oldAttrs & $rt_ATTR_WRITABLE);
var oldEN = !!(oldAttrs & $rt_ATTR_ENUMERABLE);
var oldCF = !!(oldAttrs & $rt_ATTR_CONFIGURABLE);
// Extract the new property attributes
var newWR = !!attribs.writable;
var newEN = !!attribs.enumerable;
var newCF = !!attribs.configurable;
// If the property is not currently configurable
if (oldCF === false)
{
if (newEN != oldEN)
throw TypeError('cannot unset enumerable flag when configurable is false');
if (newWR != oldWR)
throw TypeError('cannot unset writable flag when configurable is false');
if (newCF != oldCF)
throw TypeError('cannot unset configurable flag');
return obj;
}
// Construct the attribute flag bits and
// preserve the current extensible status
var newAttrs = (
(oldAttrs & $rt_ATTR_EXTENSIBLE) |
(newWR? $rt_ATTR_WRITABLE:0) |
(newEN? $rt_ATTR_ENUMERABLE:0) |
(newCF? $rt_ATTR_CONFIGURABLE:0) |
(isGS? $rt_ATTR_GETSET:0)
);
// Set the new property attributes
$ir_obj_set_attrs(obj, prop, newAttrs);
// Return the object
return obj;
};
/**
15.2.3.7 Object.defineProperties ( O, Properties )
*/
Object.defineProperties = function (O, Properties)
{
if ($rt_valIsObj(O) === false)
throw TypeError('invalid object in defineProperties');
for (name in Properties)
{
Object.defineProperty(O, name, Properties[name]);
}
return O;
};
/**
15.2.3.8 Object.seal ( O )
Makes all properties non-configurable and
makes the object non-extensible
*/
Object.seal = function (obj)
{
if ($rt_valIsObj(obj) === false)
throw TypeError('invalid object in seal');
var keys = Object.keys(obj);
// For each property of the object
for (var i = 0; i < keys.length; ++i)
{
var key = keys[i];
var desc = Object.getOwnPropertyDescriptor(obj, key);
Object.defineProperty(
obj,
key,
{
configurable: false,
writable: desc.writable,
enumerable: desc.enumerable
}
);
}
Object.preventExtensions(obj);
return obj;
};
/**
15.2.3.9 Object.freeze ( O )
*/
Object.freeze = function (obj)
{
if ($rt_valIsObj(obj) === false)
throw TypeError('invalid object in freeze');
var keys = Object.keys(obj);
// For each property of the object
for (var i = 0; i < keys.length; ++i)
{
var key = keys[i];
var desc = Object.getOwnPropertyDescriptor(obj, key);
Object.defineProperty(
obj,
key,
{
configurable: false,
writable: false,
enumerable: desc.enumerable
}
);
}
Object.preventExtensions(obj);
return obj;
};
/**
15.2.3.10 Object.preventExtensions ( O )
*/
Object.preventExtensions = function (obj)
{
if ($rt_valIsObj(obj) === false)
throw TypeError('invalid object in preventExtensions');
// Remove the extensible attribute
var attrs = $ir_obj_get_attrs(obj, null);
var newAttrs = attrs & ~$rt_ATTR_EXTENSIBLE;
// Set the new property attributes
$ir_obj_set_attrs(obj, null, newAttrs);
return obj;
};
/**
15.2.3.11 Object.isSealed ( O )
*/
Object.isSealed = function (O)
{
if ($rt_valIsObj(O) === false)
throw TypeError('invalid object in isSealed');
if (Object.isExtensble(obj))
return false;
// TODO: test each property
return false;
};
/**
15.2.3.12 Object.isFrozen ( O )
*/
Object.isFrozen = function (O)
{
if ($rt_valIsObj(O) === false)
throw TypeError('invalid object in isFrozen');
if (Object.isExtensble(obj))
return false;
// TODO: test each property
return false;
};
/**
15.2.3.13 Object.isExtensible ( O )
*/
Object.isExtensible = function (obj)
{
if ($rt_valIsObj(obj) === false)
throw TypeError('invalid object in isExtensible');
var attrs = $ir_obj_get_attrs(obj, null);
return (attrs & $rt_ATTR_EXTENSIBLE)? true:false;
};
/**
15.2.3.14 Object.keys ( O )
*/
Object.keys = function (O)
{
if ($rt_valIsObj(O) === false)
throw TypeError('invalid object in keys');
var propNames = [];
for (var k in O)
{
if ($rt_hasOwnProp(O, k) === true)
propNames.push(k);
}
return propNames;
};
/**
http://people.mozilla.org/~jorendorff/es6-draft.html#sec-object.is (19.1.2.10)
*/
Object.is = function (a, b)
{
// TODO symbol
if (typeof(a) !== typeof(b)) return false;
// Check for NaN
if (a !== a && b !== b) return true;
// Check for 0/-0
if (a === 0 && b === 0) {
// -Infinity or Infinity
return 1/a == 1/b;
}
return a === b;
};
/**
http://people.mozilla.org/~jorendorff/es6-draft.html#sec-object.assign (19.1.2.1)
*/
Object.assign = function (target)
{
// TODO symbol
var to = $rt_toObject(target);
for (var i = 1; i < arguments.length; i++)
{
var arg = arguments[i];
if (arg === null || arg === undefined) continue;
var src = $rt_toObject(arg);
// Own enumables properties
var keys = Object.keys(src);
for (var j = 0; j < keys.length; j++)
{
var key = keys[j];
to[key] = src[key];
}
}
return to;
}
/**
15.2.4.2 Default object to string conversion function
*/
Object.prototype.toString = function ()
{
return "object";
};
/**
15.2.4.3 Object to string conversion function with locale handling
*/
Object.prototype.toLocaleString = function ()
{
return this.toString();
};
/**
15.2.4.4 Object.prototype.valueOf ()
*/
Object.prototype.valueOf = function ()
{
return this;
};
/**
15.2.4.5 Test that an object has a given property
*/
Object.prototype.hasOwnProperty = function (prop)
{
return $rt_hasOwnProp(this, prop);
};
/**
15.2.4.6 Test that an object is the prototype of another
*/
Object.prototype.isPrototypeOf = function (O)
{
var proto = Object.getPrototypeOf(O);
return (this === proto);
};
/**
15.2.4.7 Object.prototype.propertyIsEnumerable (V)
*/
Object.prototype.propertyIsEnumerable = function (prop)
{
prop = $rt_toString(prop);
var attrs = $ir_obj_get_attrs(this, prop);
return !!(attrs & $rt_ATTR_ENUMERABLE);
};
// Make the Object.prototype properties non-enumerable
for (p in Object.prototype)
{
Object.defineProperty(
Object.prototype,
p,
{enumerable:false, writable:true, configurable:true }
);
}