-
Notifications
You must be signed in to change notification settings - Fork 0
/
object.js
3983 lines (3568 loc) · 113 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
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* @fileOverview <title>Basic Object Library</title>
* @file $Id$
*
* @author (C) 2004-2016 Thomas Lahn <js@PointedEars.de>
*
* @partof PointedEars' JavaScript Extensions (JSX)
*
* JSX 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 3 of the License, or
* (at your option) any later version.
*
* JSX 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 JSX. If not, see <http://www.gnu.org/licenses/>.
*/
if (typeof jsx == "undefined")
{
/**
* @namespace
*/
var jsx = {};
}
/* allows for de.pointedears.jsx.object */
if (typeof de == "undefined")
{
/**
* @namespace
*/
var de = {};
}
if (typeof de.pointedears == "undefined")
{
/**
* @namespace
*/
de.pointedears = {};
}
/**
* @namespace
*/
de.pointedears.jsx = jsx;
(function (global) {
"use strict";
/**
* Wrapper for a safer <code>try</code>...<code>catch</code>.
*
* Attempts to evaluate a value as a <i>StatementList</i>, and attempts
* to evaluate another value as a <i>StatementList</i> if an exception
* is thrown in the process. The following words may be used within:
*
* <table>
* <thead>
* <tr>
* <th align="left">Word</th>
* <th align="left">Refers to</th>
* </tr>
* </thead>
* <tbody>
* <tr valign="top">
* <td><code>statements</code><br>
* <code>errorHandler</code></td>
* <td>the passed values</td>
* </tr>
* <tr valign="top">
* <td>code</td>
* <td>the entire constructed <code>try</code>...<code>catch</code>
* string that is evaluated as a <i>Program</i></td>
* </tr>
* <tr valign="top">
* <td>e</td>
* <td>Only within <var>errorHandler</var>:
* the value thrown in case of an exception</td>
* </tr>
* <tr valign="top">
* <td>result</td>
* <td>Only within <var>finalizer</var>:
* the previous evaluation value</td>
* </tr>
* </tbody>
* </table>
*
* NOTE: This method has previously been provided by {@link exception.js};
* optimizations in code reuse moved it here.
*
* @function
* @param {Function|string|any} statements
* Value to be evaluated as a <i>StatementList</i>.
* Called if a <code>Function</code>, used as-is otherwise.
* @param {Function|string|any} errorHandler
* Value to be evaluated as a <i>StatementList</i> in case of an
* exception. Called if a <code>Function</code>,
* used as-is otherwise.
* @param {Function|string|any} finalizer
* Value to be evaluated as a <i>StatementList</i> in any case,
* after the statements and error handler. Called if a
* <code>Function</code>, used as-is otherwise.
* @return {any}
* The result of <code>statements</code>, or the result
* of <code>errorHandlers</code> if an error occurred,
* unless <var>finalizer</var> is provided; if it is,
* the evaluation result of <var>finalizer</var>.
*/
jsx.tryThis =
// (function () {
// /**
// * @param s Value to be stringified
// * @param {String} sCall
// * CallStatement that should be used instead of the value
// * @return {string} Stringified version of <code>s</code>
// */
// function stringify(s, sCall)
// {
// if (typeof s == "function")
// {
// s = sCall;
// }
// else if (typeof s == "undefined")
// {
// s = "";
// }
//
// return s;
// }
/*return*/ function (statements, errorHandler, finalizer) {
/*
* Replaced because eval() performs magnitudes worse;
* TODO: Backwards compatibility (branching for NN4 & friends?)
*/
// var sStatements = stringify(statements, "statements();");
// var sErrorHandlers = stringify(errorHandlers, "errorHandlers(e);");
//
// var code = 'try {\n ' + sStatements + '\n}\n'
// + 'catch (e) {\n ' + sErrorHandlers + '\n}';
//
// return eval(code);
var t = typeof statements;
var result;
/*jshint -W061*/
try
{
result = (t == "function" ? statements() : eval(statements));
}
catch (e)
{
t = typeof errorHandler;
result = (t == "function" ? errorHandler(e) : eval(errorHandler));
}
finally
{
if (finalizer != null)
{
t = typeof finalizer;
result = (t == "function" ? finalizer() : eval(finalizer));
}
}
/*jshint +W061*/
return result;
};
//}());
/**
* @namespace
*/
jsx.object = (/** @constructor */ function () {
var _MAX_ARRAY_LENGTH = Math.pow(2, 32) - 1;
var
rxUnknown = /^unknown$/,
rxMethod = /^(function|object)$/;
/**
* Determines whether an object is, or several objects are,
* likely to be callable.
*
* @author (C) 2003-2010 <a href="mailto:js@PointedEars.de">Thomas Lahn</a>
* @param {Object} obj
* Object which should be tested for a method, or checked
* for being a method if no further arguments are provided.
* <p>
* <em>NOTE: If you pass a primitive value for this argument,
* the properties of the object created from that value are considered.
* In particular, if you pass a string value containing
* a <i>MemberExpression</i>, the properties of the corresponding
* <code>String</code> instance are considered, not of the object that
* the <i>MemberExpression</i> might refer to. If you need to use such
* a string to refer to an object (e.g., if you do not know whether it
* is safe to refer to the object), use the return value of
* {@link jsx#tryThis jsx.tryThis("<var>MemberExpression</var>")}
* as argument to this method instead.</em>
* </p>
* @param {string|Array} prop (optional)
* Path of the property to be determined a method, i.e. a reference to
* a callable object assigned as property of another object.
* Use a string argument for each component of the path, e.g.
* the argument list <code>(o, "foo", "bar")</code> for testing whether
* <code>o.foo.bar</code> is a method.
* If the last argument is an {@link Array}, all elements of
* this array are used for property names; e.g.
* <code>(o, "foo", ["bar", "baz"])</code>. This allows for testing
* several properties of the same object with one call.
* @return {boolean}
* <code>true</code> if all arguments refer to methods,
* <code>false</code> otherwise.
*/
function _isMethod (obj, prop)
{
var len = arguments.length;
if (len < 1)
{
jsx.throwThis("jsx.InvalidArgumentError",
["Not enough arguments", "saw 0", "(obj : Object[, prop : string])"]);
return false;
}
/*
* Determine if we were apply'd by jsx.object.isNativeMethod;
*/
var checkNative = (this == _isNativeMethod);
var t = typeof obj;
/* When no property names are provided, test if the first argument is a method */
if (len < 2)
{
if (checkNative)
{
return t == "function" && obj && true || false;
}
return rxUnknown.test(t) || rxMethod.test(t) && obj && true || false;
}
/* otherwise the first argument must refer to a suitable object */
/* FIXME: Does not recognize the zero-length string as convertible */
if (rxUnknown.test(t) || !obj)
{
return false;
}
for (var i = 1; i < len; i++)
{
prop = arguments[i];
/* NOTE: Handle null _and_ undefined */
if (prop == null)
{
return false;
}
var isLastSeg = (i == len - 1);
if (isLastSeg)
{
if (typeof prop.valueOf() == "string")
{
prop = [prop];
}
var aProp = prop;
}
for (var j = (isLastSeg && aProp.length || 1); j--;)
{
if (isLastSeg)
{
prop = aProp[j];
}
t = typeof obj[prop];
/*
* NOTE: Test for "unknown" required in any case;
* this order speeds up evaluation
*/
if (rxUnknown.test(t) || (rxMethod.test(t) && obj[prop]))
{
if (i < len - 1)
{
obj = obj[prop];
if (!(rxUnknown.test(typeof obj) || obj))
{
return false;
}
}
else if (checkNative && t != "function")
{
return false;
}
}
else
{
return false;
}
}
}
return true;
}
/**
* Determines whether an object is, or several objects are,
* likely to be a native method.
*
* @author (C) 2011 <a href="mailto:js@PointedEars.de">Thomas Lahn</a>
* @param {Object} obj
* Object which should be tested for a method, or checked
* for being a method if no further arguments are provided.
* <p>
* <em>NOTE: If you pass a primitive value for this argument,
* the properties of the object created from that value are considered.
* In particular, if you pass a string value containing
* a <i>MemberExpression</i>, the properties of the corresponding
* <code>String</code> instance are considered, not of the object that
* the <i>MemberExpression</i> might refer to. If you need to use such
* a string to refer to an object (e.g., if you do not know whether it
* is safe to refer to the object), use the return value of
* {@link jsx#tryThis jsx.tryThis("<var>MemberExpression</var>")}
* as argument to this method instead.</em>
* </p>
* @param {string|Array} prop (optional)
* Path of the property to be determined a method, i.e. a reference to
* a callable object assigned as property of another object.
* Use a string argument for each component of the path, e.g.
* the argument list <code>(o, "foo", "bar")</code> for testing whether
* <code>o.foo.bar</code> is a method.
* If the last argument is an {@link Array}, all elements of
* this array are used for property names; e.g.
* <code>(o, "foo", ["bar", "baz"])</code>. This allows for testing
* several properties of the same object with one call.
* @return {boolean}
* <code>true</code> if all arguments refer to methods,
* <code>false</code> otherwise.
*/
/*jshint -W098*/
function _isNativeMethod (obj, prop)
{
/* NOTE: Thread-safe, argument-safe code reuse -- `this' is our ID */
return _isMethod.apply(_isNativeMethod, arguments);
}
/*jshint +W098*/
/**
* Determines if an object has a (non-inherited) property.
*
* @param {Object} obj (optional)
* Object which property should be checked for existence.
* @param {string} sProperty
* Name of the property to check.
* @return {boolean}
* <code>true</code> if there is such a property;
* <code>false</code> otherwise.
*/
function _hasOwnProperty (obj, sProperty)
{
if (arguments.length < 2 && obj)
{
sProperty = obj;
obj = jsx_object;
}
var proto;
return (_isMethod(obj, "hasOwnProperty")
? obj.hasOwnProperty(sProperty)
: (typeof obj[sProperty] != "undefined"
&& (null == obj.constructor
|| ((proto = obj.constructor.prototype)
&& typeof proto[sProperty] == "undefined"))));
}
/**
* Determines if a value refers to an object.
*
* <p>Returns <code>true</code> if the value is a reference
* to an object; <code>false</code> otherwise.</p>
*
* <p>A value "is an object" if it is a function,
* <code>typeof "object"</code> or a host object
* (not a primitive value), but not <code>null</code>.
*
* @return {boolean}
*/
function _isObject (a)
{
var t = typeof a;
return t == "function"
|| (t == "object"
|| (t != "undefined" && t != "boolean"
&& t != "number" && t != "string")
&& a !== null);
}
/**
* Returns the own enumerable properties of an object
*
* @param {Object} obj
* Object from which to get the keys
* @return {Array}
* Own enumerable properties of <var>obj</var>
* @see Object#keys
*/
function _getKeys (obj)
{
if (typeof Object.keys == "function" && !Object.keys._emulated)
{
return Object.keys(obj);
}
if (!_isObject(obj))
{
return jsx.throwThis("TypeError",
"jsx.object.getKeys() called on non-object");
}
var names = [];
for (var p in obj)
{
if (_hasOwnProperty(obj, p))
{
names.push(p);
}
}
return names;
}
function Dummy () {}
/**
* Lets one object inherit from another
*
* @param {Object} obj = Object.prototype
* Object from which to inherit. The default is
* <code>Object.prototype</code>.
* @return {Object}
* Inheriting (child) object
*/
function _inheritFrom (obj)
{
var prototype = (typeof obj == "undefined"
? Object.prototype
: (obj || null));
if (typeof Object.create == "function" && !Object.create._emulated)
{
return Object.create(prototype);
}
if (typeof obj == "object" && obj == null)
{
var result = {};
/*jshint -W103*/
result.__proto__ = null;
/*jshint +W103*/
return result;
}
Dummy.prototype = prototype;
return new Dummy();
}
/**
* Returns the value of an object's internal <code>[[Class]]</code>
* property.
*
* Calls the <code>Object.prototype.toString()</code> method on
* the object and returns the result of matching against
* the specified return value, which includes the value of
* the object's internal <code>[[Class]]</code> property. Although
* implementations use prototype-based inheritance, the property
* value is useful for determining the type of an object regardless
* of the current value of its <code>constructor</code> property.
* For example, that makes it possible to recognize <code>Array</code>
* instances independent of the global context in which they were
* constructed, even if {@link Array.isArray} is not provided by
* the ECMAScript implementation.
*
* @see ECMAScript Language Specification, Edition 5.1, section 15.4.3.2
* @see jsx.object.isArray()
* @function
*/
var _getClass = (function () {
var _toString = ({}).toString;
/**
* @param obj
* @return {string|Undefined}
* The value of an object's internal [[Class]] property, or
* <code>undefined</code> if the property value cannot be determined.
*/
function getClass (obj)
{
return (_toString.call(obj)
.match(/^\s*\[object\s+(\S+)\s*\]\s*$/) || [, ""])[1];
}
return getClass;
}());
/**
* Determines if a value refers to an {@link Array}.
* <p>
* Returns <code>true</code> if the value is a reference to an object
* whose <code>[[Class]]</code> internal property is <code>"Array"</code>;
* <code>false</code> otherwise.
* </p>
*
* @param a
* Potential <code>Array</code>
* @return {boolean}
*/
function _isArray (a)
{
return (typeof Array.isArray == "function" && !Array.isArray._emulated
? Array.isArray(a)
: (a && a.constructor == Array) || _getClass(a) == "Array");
}
var _rxPrimitive = /^(boolean|function|number|object|string)$/;
/**
* Determines if a value is a primitive value convertible to
* an object or a reference to a native object.
*
* @param value
* @return {boolean}
*/
function _isNativeObject (value)
{
var t = (_isObject(value)
&& typeof value.valueOf == "function"
&& typeof value.valueOf());
return (t
&& (_rxPrimitive.test(t)
|| _isArray(value)
|| /^(Date|Error|RegExp)$/.test(_getClass(value))
|| (typeof Math != "undefined" && value == Math)
|| (typeof JSON != "undefined" && value == JSON))
);
}
/**
* Used by {@link #extend()} and {@link #setProperties()}
* to overwrite existing properties.
*/
var _ADD_OVERWRITE = 1;
/**
* Used by {@link #extend()} and {@link #clone()}
* to make a shallow copy of all enumerable properties (default).
*/
var _COPY_ENUM = 0;
/**
* Used by {@link #extend()} and {@link #clone()}
* to make a deep copy of all enumerable properties.
*/
var _COPY_ENUM_DEEP = 2;
/**
* Used by {@link #extend()} and {@link #clone()}
* to copy a property by inheritance.
*/
var _COPY_INHERIT = 4;
function _getProto (o)
{
if (typeof Object.getPrototypeOf == "function"
&& !Object.getPrototypeOf._emulated)
{
return Object.getPrototypeOf(o);
}
/*jshint -W103*/
return o.__proto__ || (o.constructor && o.constructor.prototype);
/*jshint +W103*/
}
function _createTypedObject (oOriginal)
{
var prototype = _getProto(oOriginal);
return (prototype ? _inheritFrom(prototype) : _inheritFrom());
}
/**
* Creates a duplicate (clone) of an object
*
* @param {Object} oSource (optional)
* Object to be cloned. If omitted or <code>null</code>,
* the calling object is cloned.
* @param {Number} iLevel (optional)
* Use the {@link #COPY_ENUM jsx.object.COPY_*}
* properties to specify the level of cloning.
* The default is {@link #COPY_ENUM}.
* @return {Object}
* A reference to the clone.
*/
function _clone (oSource, iLevel)
{
if (typeof oSource == "number")
{
var tmp = oSource;
oSource = iLevel;
iLevel = tmp;
}
if (!oSource)
{
oSource = jsx_object;
}
if (typeof iLevel == "undefined")
{
iLevel = _COPY_ENUM;
}
if (iLevel & _COPY_INHERIT)
{
return _inheritFrom(oSource);
}
var me = _clone;
/*
* NOTE: For objects, valueOf() only copies the object reference,
* so we are creating an instance that inherits from the
* original's prototype, if possible.
*/
var o2 = (typeof oSource == "object" && oSource
? _createTypedObject(oSource)
: oSource.valueOf());
for (var p in oSource)
{
if (_hasOwnProperty(oSource, p))
{
if (iLevel && _isObject(oSource[p]))
{
jsx.tryThis(function () {
o2[p] = me(oSource[p], iLevel);
});
}
else
{
jsx.tryThis(function () {
o2[p] = oSource[p];
});
}
}
}
/*
* "var p in ..." might not have copied (all) the array elements
* (NN < 4.8 or user-defined non-enumerable elements only)
*/
if (_isArray(o2))
{
for (var i = oSource.length; i--;)
{
if (_hasOwnProperty(oSource, i) && !_hasOwnProperty(o2, i))
{
if (iLevel && _isObject(oSource[i]))
{
jsx.tryThis(function () {
o2[i] = me(oSource[i], iLevel);
});
}
else
{
jsx.tryThis(function () {
o2[i] = oSource[i];
});
}
}
}
}
return o2;
}
/**
* Defines a property of an object.
*
* Emulation of the Object.defineProperty() method from ES 5.1,
* section 15.2.3.6.
*
* Uses {@link Object.prototype#__defineGetter__} and
* {@link Object.prototype#__defineSetter__} (JavaScript only) as fallback.
*
* @function
* @return {Object} Reference to the object
*/
var _defineProperty = (function () {
function _toPropertyDescriptor (obj)
{
if (!_isObject(obj))
{
jsx.throwThis("TypeError", "Object expected");
}
var desc = {};
if (_hasOwnProperty(obj, "enumerable"))
{
desc.enumerable = !!obj.enumerable;
}
if (_hasOwnProperty(obj, "configurable"))
{
desc.configurable = !!obj.configurable;
}
var hasValue = obj.hasOwnProperty("value");
if (hasValue)
{
desc.value = obj.value;
}
var hasWritable = _hasOwnProperty(obj, "writable");
if (hasWritable)
{
desc.writable = !!obj.writable;
}
var hasGetter = _hasOwnProperty(obj, "get");
if (hasGetter)
{
if (typeof obj.get != "function")
{
return jsx.throwThis("TypeError", "Function expected for getter");
}
desc.get = obj.get;
}
var hasSetter = _hasOwnProperty(obj, "set");
if (hasSetter)
{
if (typeof obj.set != "function")
{
return jsx.throwThis("TypeError", "Function expected for setter");
}
desc.set = obj.set;
}
if ((hasGetter || hasSetter) && (hasValue || hasWritable))
{
return jsx.throwThis("TypeError", "Cannot define getter/setter and value/writable");
}
return desc;
}
function _defineOwnProperty (obj, propertyName, descriptor, _throw, context)
{
function _isAccessorDescriptor (desc)
{
if (typeof desc == "undefined")
{
return false;
}
return _hasOwnProperty(desc, "get") || _hasOwnProperty(desc, "set");
}
function _isDataDescriptor (desc)
{
if (typeof desc == "undefined")
{
return false;
}
return desc.hasOwnProperty("value") || _hasOwnProperty(desc, "writable");
}
function _isGenericDescriptor (desc)
{
if (typeof desc == "undefined")
{
return false;
}
return !_isAccessorDescriptor(desc) && !_isDataDescriptor(desc);
}
// var current = obj.hasOwnProperty(propertyName);
// var extensible = obj[propertyName].[[Extensible]]
if (_isGenericDescriptor(descriptor) || _isDataDescriptor(descriptor))
{
var value = descriptor.value;
obj[propertyName] = value;
if (!descriptor.writable)
{
jsx.tryThis(
function () {
/* NOTE: Need getter because __defineSetter__() undefines value */
obj.__defineGetter__(propertyName, function () {
return value;
});
obj.__defineSetter__(propertyName, function () {});
},
function () {
obj[propertyName] = value;
jsx.warn((context ? context + ": " : "")
+ "Could not define property `" + propertyName
+ "' as read-only");
});
}
}
else
{
/* accessor property descriptor */
jsx.tryThis(
function () {
if (descriptor["get"])
{
obj.__defineGetter__(propertyName, descriptor["get"]);
}
if (descriptor["set"])
{
obj.__defineSetter__(propertyName, descriptor["set"]);
}
},
function () {
jsx.warn((context ? context + ": " : "")
+ "Could not define special property `" + propertyName + "'."
+ " Please use explicit getters and setters instead.");
});
}
return false;
}
/**
* @function
*/
var _defineProperty = _extend(
/**
* @param {Object} o
* @param {Object} descriptor (optional)
* Property descriptor, a reference to an object that defines
* the attributes of the property. Must be of the form
* <code><pre>{
* propertyName: {
* configurable: …,
* enumerable: …,
* value: …,
* writable: …,
* get: function () {…},
* set: function (newValue) {…}
* },
* …
* }
* </pre></code> as specified in the ECMAScript Language Specification,
* Edition 5 Final, section 15.2.3.7. Note that the
* <code>[[Configurable]]</code> and <code>[[Enumerable]]</code>
* attributes cannot be emulated. The [[Writable]] attribute,
* and getter and setter can only be emulated if the
* <code>__defineGetter__()</code> and <code>__defineSetter__()</code>
* methods are available, respectively.
* @param {string} sContext (optional)
* The context in which the property definition was attempted.
* Included in the info message in case getters and setters
* could not be defined.
*/
function (o, propertyName, descriptor, sContext) {
var done = false;
if (typeof Object.defineProperty == "function"
&& !Object.defineProperty._emulated)
{
jsx.tryThis(function () {
Object.defineProperty(o, propertyName, descriptor);
done = true;
});
}
if (!done)
{
if (!/^(object|function)$/.test(typeof o) || !o)
{
return jsx.throwThis("TypeError", "Object expected");
}
var name = String(propertyName);
var desc = _toPropertyDescriptor(descriptor);
_defineOwnProperty(o, name, desc, true, sContext);
}
return o;
},
{
_emulated: true
});
return _defineProperty;
}());
/**
* Adds/replaces properties of an object.
*
* <p>
* <em>Not to be confused with {@link Function.prototype.extend}.</em>
* </p>
*
* @param {Object} oTarget
* Target object whose properties should be set.
* @param {Object} oSource
* Object specifying the properties to be set.
* The name of each property serves as the name for the
* property of the target object, its value as the value
* of that property.
* @param {Number} iFlags = 0
* Flags for the modification, see {@link #ADD_OVERWRITE}
* and {@link #COPY_ENUM jsx.object.COPY_*}.
* @return {Object}
* The extended object
*/
function _extend (oTarget, oSource, iFlags)
{
if (typeof iFlags == "undefined")
{
iFlags = 0;
}
var cloneLevel = (iFlags & (_COPY_ENUM_DEEP | _COPY_INHERIT));
for (var i = 0, keys = _getKeys(oSource), len = keys.length;
i < len; ++i)
{
var p = keys[i];
if (typeof oTarget[p] == "undefined" || (iFlags & _ADD_OVERWRITE))
{
jsx.tryThis(function () {
/* TODO: Support cloning of property attributes */
oTarget[p] = (cloneLevel
? _clone(oSource[p], cloneLevel)
: oSource[p]);
oTarget[p]._userDefined = true;
});
}
}
return oTarget;
}
/**
* Returns a feature of an object
*
* @param {Object} obj
* Object which provides the feature
* @param {string|Array} path
* Property names on the feature path, including the property
* for the feature itself. For example, use
* <code>jsx.object.getFeature(foo, "bar", "baz")</code> for
* safe access to <code>foo.bar.baz</code>.
* If this argument is an <code>Array</code>, it is used
* instead of the remaining argument list; this is the
* recommended way to call this method to ensure upwards
* compatibility.
* @return {any}
* <code>undefined</code> if <var>obj</var> does not have such
* a feature. Note that features whose value can be
* <code>undefined</code> cannot be detected with this method.
*/
function _getFeature (obj, path)
{
var realPath = path;
var start = 0;
if (!_isArray(realPath))
{
realPath = arguments;
start = 1;
}
for (var i = start, len = realPath.length; i < len; i++)