2 * Copyright 2018 Jacek Caban for CodeWeavers
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 var JS_E_PROP_DESC_MISMATCH
= 0x800a01bd;
20 var JS_E_INVALID_WRITABLE_PROP_DESC
= 0x800a13ac;
21 var JS_E_NONCONFIGURABLE_REDEFINED
= 0x800a13d6;
22 var JS_E_NONWRITABLE_MODIFIED
= 0x800a13d7;
26 sync_test("date_now", function() {
28 var time
= (new Date()).getTime();
30 ok(time
>= now
&& time
-now
< 50, "unexpected Date.now() result " + now
+ " expected " + time
);
35 sync_test("toISOString", function() {
36 function expect(date
, expected
) {
37 var s
= date
.toISOString();
38 ok(s
=== expected
, "toISOString returned " + s
+ " expected " + expected
);
41 function expect_exception(func
) {
47 ok(false, "expected exception");
50 expect(new Date(0), "1970-01-01T00:00:00.000Z");
51 expect(new Date(0xdeadbeef), "1970-02-13T05:45:28.559Z");
52 expect(new Date(10928309128301), "2316-04-22T01:25:28.301Z");
53 expect(new Date(-1), "1969-12-31T23:59:59.999Z");
54 expect(new Date(-62167219200000), "0000-01-01T00:00:00.000Z");
55 expect(new Date(-62167219200001), "-000001-12-31T23:59:59.999Z");
56 expect(new Date(-6216721920000100), "-195031-12-03T23:59:59.900Z");
57 expect(new Date(1092830912830100), "+036600-06-07T22:27:10.100Z");
59 expect_exception(function() { new Date(NaN
).toISOString(); });
60 expect_exception(function() { new Date(31494784780800001).toISOString(); });
63 sync_test("indexOf", function() {
64 function expect(array
, args
, exr
) {
65 var r
= Array
.prototype.indexOf
.apply(array
, args
);
66 ok(r
== exr
, "indexOf returned " + r
+ " expected " + exr
);
69 ok(Array
.prototype.indexOf
.length
== 1, "indexOf.length = " + Array
.prototype.indexOf
.length
);
71 expect([1,2,3], [2], 1);
72 expect([1,undefined,3], [undefined], 1);
73 expect([1,undefined,3], [], 1);
74 expect([1,,3], [undefined], -1);
75 expect([1,2,3,4,5,6], [2, 2], -1);
76 expect([1,2,3,4,5,6], [5, -1], -1);
77 expect([1,2,3,4,5,6], [5, -2], 4);
78 expect([1,2,3,4,5,6], [5, -20], 4);
79 expect([1,2,3,4,5,6], [5, 20], -1);
80 expect("abc", ["b"], 1);
81 expect(true, [true], -1);
82 expect({"4": 4, length
: 5}, [4], 4);
83 expect({"4": 4, length
: 5}, [undefined], -1);
84 expect({"4": 4, length
: 3}, [4], -1);
85 expect({"test": true}, [true], -1);
86 expect([1,2,3], [2, 1.9], 1);
89 sync_test("forEach", function() {
90 ok(Array
.prototype.forEach
.length
=== 1, "forEach.length = " + Array
.prototype.forEach
.length
);
92 function test(array
, expect
) {
93 var r
= Array
.prototype.forEach
.call(array
, function(value
, index
, arr
) {
94 ok(arr
=== array
, "unexpected array " + arr
);
95 ok(index
=== expect
[0][0], "index = " + index
+ " expected " + expect
[0][0]);
96 ok(value
=== expect
[0][1], "value = " + value
+ " expected " + expect
[0][1]);
99 ok(r
=== undefined, "forEach returned " + r
);
100 ok(expect
.length
=== 0, "too few forEach() calls, expected " + expect
.length
+ " more");
103 test(["a",2,"c"], [[0,"a"],[1,2],[2,"c"]]);
104 test({length
: 1000, 500: false, c
: 30, 3: "x", 999: 1}, [[3,"x"],[500,false],[999,1]]);
105 test(new String("abc"), [[0,"a"],[1,"b"],[2,"c"]]);
108 [1,2].forEach(function() {
109 ok(this === window
, "this != window");
111 [1,2].forEach(function() {
112 ok(this === window
, "this != window");
115 var o
= new Object(), a
= [1,2];
116 a
.forEach(function(value
, index
, array
) {
117 ok(array
=== a
, "array != a");
118 ok(this === o
, "this != o");
122 sync_test("isArray", function() {
123 function expect_array(a
, exr
) {
124 var r
= Array
.isArray(a
);
125 ok(r
=== exr
, "isArray returned " + r
+ " expected " + exr
);
128 expect_array([1], true);
129 expect_array(Array
, false);
130 expect_array(new Array(), true);
131 expect_array({"1": 1, "2": 2, length
: 2}, false);
134 C
.prototype = Array
.prototype;
135 expect_array(new C(), false);
138 sync_test("array_map", function() {
139 var calls
, m
, arr
, ctx
;
141 /* basic map call with context */
145 m
= arr
.map(function(x
, i
, a
) {
146 ok(this === ctx
, "this != ctx");
147 ok(i
=== x
- 1, "i = " + i
);
148 ok(a
=== arr
, "a != arr");
152 ok(calls
=== "1,2,3,", "calls = " + calls
);
153 ok(m
.join() === "2,4,6", "m = " + m
);
155 /* non-array object as this argument */
157 arr
= { 1: "one", 2: "two", 3: "three", length
: 3 };
158 m
= Array
.prototype.map
.call(arr
, function(x
, i
) {
159 calls
+= i
+ ":" + x
+ ",";
162 ok(calls
=== "1:one,2:two,", "calls = " + calls
);
163 ok(m
.join() === ",one!,two!", "m = " + m
);
164 ok(!("0" in m
), "0 is in m");
166 /* mutate array in callback */
169 m
= Array
.prototype.map
.call(arr
, function(x
, i
) {
170 calls
+= i
+ ":" + x
+ ",";
171 for(var j
= i
; j
< arr
.length
; j
++)
176 ok(calls
=== "0:1,1:3,2:5,", "calls = " + calls
);
177 ok(m
.join() === "0,2,4", "m = " + m
);
179 [1,2].map(function() {
180 ok(this === window
, "this != window");
182 [1,2].map(function() {
183 ok(this === window
, "this != window");
187 sync_test("array_sort", function() {
190 r
= [3,1,2].sort(function(x
,y
) { return y
-x
; }, 1, 2, 3, true, undefined ).join();
191 ok(r
=== "3,2,1", "reverse sorted [3,1,2] = " + r
);
193 r
= [3,1,2].sort(undefined).join();
194 ok(r
=== "1,2,3", "null sorted [3,1,2] = " + r
);
197 r
= [3,1,2].sort(null);
198 ok(false, "expected sort(null) exception");
200 ok(e
.name
=== "TypeError", "got exception " + e
.name
);
204 sync_test("identifier_keywords", function() {
235 function ro() { return o
; };
237 ok(o
.if === 1, "o.if = " + o
.if);
238 ok(ro().default === 2, "ro().default = " + ro().default);
239 ok(o
.false === true, "o.false = " + o
.false);
242 function test_own_data_prop_desc(obj
, prop
, expected_writable
, expected_enumerable
,
243 expected_configurable
) {
244 var desc
= Object
.getOwnPropertyDescriptor(obj
, prop
);
245 ok("value" in desc
, "value is not in desc");
246 ok(desc
.value
=== obj
[prop
], "desc.value = " + desc
.value
+ " expected " + obj
[prop
]);
247 ok(desc
.writable
=== expected_writable
, "desc(" + prop
+ ").writable = " + desc
.writable
248 + " expected " + expected_writable
);
249 ok(desc
.enumerable
=== expected_enumerable
, "desc.enumerable = " + desc
.enumerable
250 + " expected " + expected_enumerable
);
251 ok(desc
.configurable
=== expected_configurable
, "desc.configurable = " + desc
.configurable
252 + " expected " + expected_configurable
);
255 sync_test("getOwnPropertyDescriptor", function() {
259 test_own_data_prop_desc(obj
, "test", true, true, true);
261 test_own_data_prop_desc(Object
, "getOwnPropertyDescriptor", true, false, true);
262 test_own_data_prop_desc(Math
, "PI", false, false, false);
264 var obj
= new String();
265 test_own_data_prop_desc(obj
, "length", false, false, false);
266 ok(Object
.getOwnPropertyDescriptor(obj
, "slice") === undefined,
267 "getOwnPropertyDescriptor(slice) did not return undefined");
268 test_own_data_prop_desc(String
.prototype, "slice", true, false, true);
271 test_own_data_prop_desc(obj
, "length", true, false, false);
273 obj
= new Function();
274 test_own_data_prop_desc(obj
, "length", false, false, false);
275 test_own_data_prop_desc(obj
, "arguments", false, false, false);
278 test_own_data_prop_desc(obj
, "global", false, false, false);
279 test_own_data_prop_desc(obj
, "ignoreCase", false, false, false);
280 test_own_data_prop_desc(obj
, "lastIndex", true, false, false);
281 test_own_data_prop_desc(obj
, "multiline", false, false, false);
282 test_own_data_prop_desc(obj
, "source", false, false, false);
285 test_own_data_prop_desc(arguments
, "length", true, false, true);
286 test_own_data_prop_desc(arguments
, "callee", true, false, true);
289 test_own_data_prop_desc(String
, "prototype", false, false, false);
290 test_own_data_prop_desc(function(){}, "prototype", true, false, false);
291 test_own_data_prop_desc(Function
, "prototype", false, false, false);
292 test_own_data_prop_desc(String
.prototype, "constructor", true, false, true);
295 sync_test("defineProperty", function() {
296 function test_accessor_prop_desc(obj
, prop
, orig_desc
) {
297 var expected_enumerable
= "enumerable" in orig_desc
&& !!orig_desc
.enumerable
;
298 var expected_configurable
= "configurable" in orig_desc
&& !!orig_desc
.configurable
;
300 var desc
= Object
.getOwnPropertyDescriptor(obj
, prop
);
301 ok(desc
.enumerable
=== expected_enumerable
, "desc.enumerable = " + desc
.enumerable
302 + " expected " + expected_enumerable
);
303 ok(desc
.configurable
=== expected_configurable
, "desc.configurable = " + desc
.configurable
304 + " expected " + expected_configurable
);
305 ok(desc
.get === orig_desc
.get, "desc.get = " + desc
.get);
306 ok(desc
.set === orig_desc
.set, "desc.set = " + desc
.set);
309 function expect_exception(func
, expected_number
) {
313 var n
= e
.number
+ 0x100000000; /* make it unsigned like HRESULT */
314 todo_wine_if(expected_number
== JS_E_PROP_DESC_MISMATCH
).
315 ok(n
=== expected_number
, "got exception " + n
.toString(16)
316 + " expected " + expected_number
.toString(16));
317 ok(e
.name
=== "TypeError", "e.name = " + e
.name
);
320 ok(false, "expected exception");
323 var obj
= new Object(), defined
;
324 defined
= Object
.defineProperty(obj
, "test", {});
325 ok(defined
=== obj
, "defined != obj");
326 ok("test" in obj
, "test is not in obj");
327 test_own_data_prop_desc(obj
, "test", false, false, false);
328 ok(obj
.test
=== undefined, "obj.test = " + obj
.test
);
330 Object
.defineProperty(obj
, "all", {writable
: true, enumerable
: true, configurable
: true, value
: 1});
331 test_own_data_prop_desc(obj
, "all", true, true, true);
332 ok(obj
.all
=== 1, "obj.test = " + obj
.test
);
334 Object
.defineProperty(obj
, "all", {writable
: false});
335 test_own_data_prop_desc(obj
, "all", false, true, true);
336 ok(obj
.all
=== 1, "obj.test = " + obj
.test
);
338 var getsetprop_value
= 1;
341 return getsetprop_value
;
344 getsetprop_value
= v
;
347 Object
.defineProperty(obj
, "getsetprop", desc
);
348 test_accessor_prop_desc(obj
, "getsetprop", desc
);
350 ok(obj
.getsetprop
=== 1, "getsetprop = " + obj
.getsetprop
);
352 ok(getsetprop_value
=== 2, "getsetprop_value = " + getsetprop_value
);
353 test_accessor_prop_desc(obj
, "getsetprop", desc
);
354 ok(obj
.getsetprop
=== 2, "getsetprop = " + obj
.getsetprop
);
356 Object
.defineProperty(obj
, "notConf", {writable
: true, enumerable
: true, configurable
: false, value
: 1});
357 test_own_data_prop_desc(obj
, "notConf", true, true, false);
359 expect_exception(function() {
360 Object
.defineProperty(obj
, "notConf",
361 {writable
: true, enumerable
: true, configurable
: true, value
: 1});
362 }, JS_E_NONCONFIGURABLE_REDEFINED
);
364 expect_exception(function() {
365 Object
.defineProperty(obj
, "notConf",
366 {writable
: true, enumerable
: false, configurable
: false, value
: 1});
367 }, JS_E_NONCONFIGURABLE_REDEFINED
);
369 Object
.defineProperty(obj
, "notConf",
370 {writable
: true, enumerable
: true, configurable
: false, value
: 2});
371 test_own_data_prop_desc(obj
, "notConf", true, true, false);
373 Object
.defineProperty(obj
, "notConf", {writable
: true, value
: 2});
374 test_own_data_prop_desc(obj
, "notConf", true, true, false);
376 Object
.defineProperty(obj
, "notConf2",
377 {writable
: false, enumerable
: false, configurable
: false, value
: 1});
378 test_own_data_prop_desc(obj
, "notConf2", false, false, false);
380 ok(obj
.notConf2
=== 1, "obj.notConf2 = " + obj
.notConf2
)
382 expect_exception(function() {
383 Object
.defineProperty(obj
, "notConf2",
384 {writable
: false, enumerable
: false, configurable
: true, value
: 1});
385 }, JS_E_NONCONFIGURABLE_REDEFINED
);
387 expect_exception(function() {
388 Object
.defineProperty(obj
, "notConf2",
389 {writable
: false, enumerable
: true, configurable
: false, value
: 1});
390 }, JS_E_NONCONFIGURABLE_REDEFINED
);
392 expect_exception(function() {
393 Object
.defineProperty(obj
, "notConf2", {writable
: true, value
: 1});
394 }, JS_E_NONWRITABLE_MODIFIED
);
396 expect_exception(function() {
397 Object
.defineProperty(obj
, "notConf2", {value
: 2});
398 }, JS_E_NONWRITABLE_MODIFIED
);
400 Object
.defineProperty(obj
, "notConf2",
401 {writable
: false, enumerable
: false, configurable
: false, value
: 1});
402 test_own_data_prop_desc(obj
, "notConf2", false, false, false);
404 Object
.defineProperty(obj
, "notConf2", {writable
: false, value
: 1});
405 test_own_data_prop_desc(obj
, "notConf2", false, false, false);
409 return getsetprop_value
;
412 getsetprop_value
= v
;
416 Object
.defineProperty(obj
, "notConfAcc", desc
);
417 test_accessor_prop_desc(obj
, "notConfAcc", desc
);
419 expect_exception(function() {
420 Object
.defineProperty(obj
, "notConfAcc", {value
: 1});
421 }, JS_E_NONCONFIGURABLE_REDEFINED
);
423 expect_exception(function() {
424 Object
.defineProperty(obj
, "notConfAcc", {get: desc
.get, set: function () {}});
425 }, JS_E_NONCONFIGURABLE_REDEFINED
);
427 expect_exception(function() {
428 Object
.defineProperty(obj
, "notConfAcc", {get: undefined, set: desc
.set});
429 }, JS_E_NONCONFIGURABLE_REDEFINED
);
431 expect_exception(function() {
432 Object
.defineProperty(obj
, "notConfAcc", {writable
: true});
433 }, JS_E_NONCONFIGURABLE_REDEFINED
);
435 Object
.defineProperty(obj
, "notConfAcc", {get: desc
.get});
436 test_accessor_prop_desc(obj
, "notConfAcc", desc
);
438 Object
.defineProperty(obj
, "notConfAcc", {set: desc
.set});
439 test_accessor_prop_desc(obj
, "notConfAcc", desc
);
441 Object
.defineProperty(obj
, "notConfAcc", {configurable
: false});
442 test_accessor_prop_desc(obj
, "notConfAcc", desc
);
446 return getsetprop_value
;
449 getsetprop_value
= v
;
453 Object
.defineProperty(obj
, "confAcc", desc
);
454 test_accessor_prop_desc(obj
, "confAcc", desc
);
456 Object
.defineProperty(obj
, "confAcc", {writable
: 1});
457 test_own_data_prop_desc(obj
, "confAcc", true, false, true);
459 Object
.defineProperty(obj
, "confAcc", desc
);
460 test_accessor_prop_desc(obj
, "confAcc", desc
);
462 desc
.get = function() {};
463 desc
.set = undefined;
464 Object
.defineProperty(obj
, "confAcc", desc
);
465 test_accessor_prop_desc(obj
, "confAcc", desc
);
467 expect_exception(function() {
468 Object
.defineProperty(obj
, "invaliddesc", {get: undefined, value
: 1});
469 }, JS_E_PROP_DESC_MISMATCH
);
471 expect_exception(function() {
472 Object
.defineProperty(obj
, "invaliddesc", {set: undefined, writable
: true});
473 }, JS_E_INVALID_WRITABLE_PROP_DESC
);
478 ok(this === obj
, "this != obj");
479 return getsetprop_value
;
482 ok(this === obj
, "this != obj");
483 getsetprop_value
= v
;
487 Object
.defineProperty(child
.prototype, "parent_accessor", desc
);
490 getsetprop_value
= 6;
491 ok(obj
.parent_accessor
=== 6, "parent_accessor = " + obj
.parent_accessor
);
492 obj
.parent_accessor
= 1;
493 ok(getsetprop_value
=== 1, "getsetprop_value = " + getsetprop_value
);
494 ok(obj
.parent_accessor
=== 1, "parent_accessor = " + obj
.parent_accessor
);
496 ok(Object
.getOwnPropertyDescriptor(obj
, "parent_accessor") === undefined,
497 "getOwnPropertyDescriptor(parent_accessor) did not return undefined");
498 test_accessor_prop_desc(child
.prototype, "parent_accessor", desc
);
500 desc
.get = undefined;
501 Object
.defineProperty(child
.prototype, "parent_accessor", desc
);
502 ok(obj
.parent_accessor
=== undefined, "parent_accessor = " + obj
.parent_accessor
);
507 ok(this === obj
, "this != obj");
512 Object
.defineProperty(obj
, "no_setter", desc
);
513 test_accessor_prop_desc(obj
, "no_setter", desc
);
514 obj
.no_setter
= false;
515 ok(obj
.no_setter
=== true, "no_setter = " + obj
.no_setter
);
517 /* call prop with getter */
521 ok(x
=== 100, "x = " + x
);
526 Object
.defineProperty(obj
, "funcprop", desc
);
527 test_accessor_prop_desc(obj
, "funcprop", desc
);
528 ok(obj
.funcprop(100) === 10, "obj.funcprop() = " + obj
.funcprop(100));
531 sync_test("defineProperties", function() {
532 var o
, defined
, descs
;
535 acc_prop
: { get: function() { return 1; } },
537 e
: { enumerable
: true }
541 defined
= Object
.defineProperties(o
, descs
);
542 ok(defined
=== o
, "defined != o");
543 ok(o
.acc_prop
=== 1, "o.acc_prop = " + o
.acc_prop
);
544 ok(o
.prop
=== 2, "o.prop = " + o
.prop
);
545 ok(o
.e
=== undefined, "o.e = " + o
.e
);
546 ok("e" in o
, "e is not in o");
547 test_own_data_prop_desc(o
, "prop", false, false, false);
548 test_own_data_prop_desc(o
, "e", false, true, false);
549 for(var p
in o
) ok(p
=== "e", "p = " + p
);
552 Object
.defineProperties(o
, 1);
553 for(var p
in o
) ok(false, "o has property " + p
);
555 o
= Object
.create(null, descs
);
556 ok(o
.acc_prop
=== 1, "o.acc_prop = " + o
.acc_prop
);
557 ok(o
.prop
=== 2, "o.prop = " + o
.prop
);
558 ok(o
.e
=== undefined, "o.e = " + o
.e
);
559 ok("e" in o
, "e is not in o");
560 test_own_data_prop_desc(o
, "prop", false, false, false);
561 test_own_data_prop_desc(o
, "e", false, true, false);
562 for(var p
in o
) ok(p
=== "e", "p = " + p
);
564 var desc_proto
= new Object();
565 desc_proto
.proto_prop
= { value
: true, enumerable
: true };
566 descs
= Object
.create(desc_proto
);
567 o
= Object
.create(null, descs
);
568 ok(!("proto_prop" in o
), "proto_prop is in o");
571 sync_test("property_definitions", function() {
572 var obj
, val
, i
, arr
;
574 function test_accessor_prop_desc(obj
, prop
, have_getter
, have_setter
) {
575 var desc
= Object
.getOwnPropertyDescriptor(obj
, prop
);
576 ok(desc
.enumerable
=== true, "desc.enumerable = " + desc
.enumerable
);
577 ok(desc
.configurable
=== true, "desc.configurable = " + desc
.configurable
);
580 ok(typeof(desc
.get) === "function", "desc.get = " + desc
.get);
581 ok(typeof(desc
.get.prototype) === "object", "desc.get.prototype = " + desc
.get.prototype);
583 ok(!("get" in obj
), "desc.get = " + desc
.get);
587 ok(typeof(desc
.set) === "function", "desc.set = " + desc
.set);
588 ok(typeof(desc
.set.prototype) === "object", "desc.set.prototype = " + desc
.set.prototype);
590 ok(!("set" in obj
), "desc.get = " + desc
.get);
595 get prop() { return val
+ 1; },
596 set prop(v
) { val
= v
; }
598 test_accessor_prop_desc(obj
, "prop", true, true);
600 ok(obj
.prop
=== 1, "obj.prop = " + obj
.prop
);
602 ok(val
=== 3, "val = " + val
);
603 ok(obj
.prop
=== 4, "obj.prop = " + obj
.prop
);
608 ok(arr
.join() === "prop", "prop of obj = " + arr
.join());
611 set prop(v
) { val
= v
; }
613 test_accessor_prop_desc(obj
, "prop", false, true);
615 ok(obj
.prop
=== undefined, "obj.prop = " + obj
.prop
);
617 ok(val
=== 2, "val = " + val
);
618 ok(obj
.prop
=== undefined, "obj.prop = " + obj
.prop
);
621 get prop() { return val
+ 1; },
622 get 0() { return val
+ 2; }
624 test_accessor_prop_desc(obj
, "prop", true, false);
626 ok(obj
.prop
=== 6, "obj.prop = " + obj
.prop
);
628 ok(val
=== 5, "val = " + val
);
629 ok(obj
.prop
=== 6, "obj.prop = " + obj
.prop
);
630 test_accessor_prop_desc(obj
, "0", true, false);
631 ok(obj
[0] === 7, "obj.prop = " + obj
[0]);
634 sync_test("string_trim", function() {
635 function test_trim(value
, expected
) {
636 var r
= String
.prototype.trim
.call(value
);
637 ok(r
=== expected
, "trim(" + value
+ ") = " + r
);
640 test_trim("test", "test");
641 test_trim(false, "false");
642 test_trim("\n \t\rte st\t\t\n", "te st");
643 test_trim({ toString: function() { return " test "; } }, "test");
645 test_trim(" \t\n", "");
648 sync_test("global_properties", function() {
651 /* Make sure that global properties are not writable. */
654 ok(isNaN(NaN
), "NaN = " + NaN
);
657 ok(undefined === o
, "NaN = " + NaN
);
660 ok(Infinity
=== o
, "Infinity = " + NaN
);
663 sync_test("string_split", function() {
666 /* IE9 got this wrong*/
667 if("1undefined2".split(undefined).length
!= 1) {
668 win_skip("detected broken String.prototype.split implementation");
672 function test(string
, separator
, result
) {
673 var r
= string
.split(separator
);
674 ok(r
== result
, "\"" + string
+ "\".split(" + separator
+ ") returned " + r
+ " expected " + result
);
677 test("test", /^|\s+/, "test");
678 test("test", /$|\s+/, "test");
679 test("test", /^|./, "t,,,");
680 test("test", /.*/, ",");
681 test("test", /x*/, "t,e,s,t");
682 test("test", /$|x*/, "t,e,s,t");
683 test("test", /^|x*/, "t,e,s,t");
684 test("test", /t*/, ",e,s,");
685 test("xaabaax", /a*|b*/, "x,b,x");
686 test("xaabaax", /a+|b+/, "x,,,x");
687 test("xaabaax", /a+|b*/, "x,,,x");
688 test("xaaxbaax", /b+|a+/, "x,x,,x");
689 test("test", /^|t/, "tes,");
690 test("test", /^|t/, "tes,");
691 test("a,,b", /,/, "a,,b");
692 test("ab", /a*/, ",b");
693 test("aab", "a", ",,b");
696 function test_length(string
, separator
, len
) {
697 var r
= string
.split(separator
);
698 ok(r
.length
=== len
, "\"" + string
+ "\".split(" + separator
+ ").length = "
699 + r
.length
+ " expected " + len
);
702 test_length("", /a*/, 0);
703 test_length("", /a+/, 1);
704 test_length("", "", 0);
705 test_length("", "x", 1);
707 r
= "1,2,3".split(undefined);
708 ok(typeof(r
) === "object", "typeof(r) = " + typeof(r
));
709 ok(r
.length
=== 1, "r.length = " + r
.length
);
710 ok(r
[0] === "1,2,3", "r[0] = " + r
[0]);
712 r
= "1,undefined2undefined,3".split(undefined);
713 ok(typeof(r
) === "object", "typeof(r) = " + typeof(r
));
714 ok(r
.length
=== 1, "r.length = " + r
.length
);
715 ok(r
[0] === "1,undefined2undefined,3", "r[0] = " + r
[0]);
717 r
= "1,undefined2undefined,3".split();
718 ok(typeof(r
) === "object", "typeof(r) = " + typeof(r
));
719 ok(r
.length
=== 1, "r.length = " + r
.length
);
720 ok(r
[0] === "1,undefined2undefined,3", "r[0] = " + r
[0]);
722 /* note: spec violation, limit is ignored */
723 r
= "1,undefined2undefined,3".split(undefined, 0);
724 ok(typeof(r
) === "object", "typeof(r) = " + typeof(r
));
725 ok(r
.length
=== 1, "r.length = " + r
.length
);
726 ok(r
[0] === "1,undefined2undefined,3", "r[0] = " + r
[0]);
728 r
= "1,undefined2null,3".split(null);
729 ok(typeof(r
) === "object", "typeof(r) = " + typeof(r
));
730 ok(r
.length
=== 2, "r.length = " + r
.length
);
731 ok(r
[0] === "1,undefined2", "r[0] = " + r
[0]);
732 ok(r
[1] === ",3", "r[1] = " + r
[1]);
735 ok(typeof(r
) === "object", "typeof(r) = " + typeof(r
));
736 ok(r
.length
=== 1, "r.length = " + r
.length
);
737 ok(r
[0] === "", "r[0] = " + r
[0]);
740 sync_test("getPrototypeOf", function() {
741 ok(Object
.create
.length
=== 2, "Object.create.length = " + Object
.create
.length
);
742 ok(Object
.getPrototypeOf
.length
=== 1, "Object.getPrototypeOf.length = " + Object
.getPrototypeOf
.length
);
744 ok(Object
.getPrototypeOf(new Object()) === Object
.prototype,
745 "Object.getPrototypeOf(new Object()) !== Object.prototype");
748 var obj
= new Constr();
749 ok(Object
.getPrototypeOf(Constr
.prototype) === Object
.prototype,
750 "Object.getPrototypeOf(Constr.prototype) !== Object.prototype");
751 ok(Object
.getPrototypeOf(obj
) === Constr
.prototype,
752 "Object.getPrototypeOf(obj) !== Constr.prototype");
754 var proto
= new Object();
755 Constr
.prototype = proto
;
756 ok(Object
.getPrototypeOf(obj
) != proto
,
757 "Object.getPrototypeOf(obj) == proto");
759 ok(Object
.getPrototypeOf(obj
) === proto
,
760 "Object.getPrototypeOf(obj) !== proto");
761 ok(Object
.getPrototypeOf(obj
, 2, 3, 4) === proto
,
762 "Object.getPrototypeOf(obj) !== proto");
764 ok(Object
.getPrototypeOf(Object
.prototype) === null,
765 "Object.getPrototypeOf(Object.prototype) !== null");
767 obj
= Object
.create(proto
= { test
: 1 });
768 ok(Object
.getPrototypeOf(obj
) === proto
,
769 "Object.getPrototypeOf(obj) !== proto");
770 ok(obj
.test
=== 1, "obj.test = " + obj
.test
);
772 obj
= Object
.create(null);
773 ok(!("toString" in obj
), "toString is in obj");
774 ok(Object
.getPrototypeOf(obj
) === null, "Object.getPrototypeOf(obj) = " + Object
.getPrototypeOf(obj
));
777 sync_test("toString", function() {
780 (function () { tmp
= Object
.prototype.toString
.call(arguments
); })();
782 ok(tmp
=== "[object Arguments]", "toString.call(arguments) = " + tmp
);
783 tmp
= Object
.prototype.toString
.call(this);
785 ok(tmp
=== "[object Window]", "toString.call(null) = " + tmp
);
786 tmp
= Object
.prototype.toString
.call(null);
788 ok(tmp
=== "[object Null]", "toString.call(null) = " + tmp
);
789 tmp
= Object
.prototype.toString
.call(undefined);
791 ok(tmp
=== "[object Undefined]", "toString.call(undefined) = " + tmp
);
792 tmp
= Object
.prototype.toString
.call();
794 ok(tmp
=== "[object Undefined]", "toString.call() = " + tmp
);
796 obj
= Object
.create(null);
797 tmp
= Object
.prototype.toString
.call(obj
);
798 ok(tmp
=== "[object Object]", "toString.call(Object.create(null)) = " + tmp
);
799 obj
= Object
.create(Number
.prototype);
800 tmp
= Object
.prototype.toString
.call(obj
);
801 ok(tmp
=== "[object Object]", "toString.call(Object.create(Number.prototype)) = " + tmp
);
804 sync_test("bind", function() {
806 var o
= new Object(), o2
= new Object();
809 ok(this === o
, "this != o");
810 ok(arguments
.length
=== 0, "arguments.length = " + arguments
.length
);
813 ok(f
.length
=== 0, "f.length = " + f
.length
);
815 ok(r
=== 1, "r = " + r
);
818 ok(this === o
, "this != o");
819 ok(arguments
.length
=== 1, "arguments.length = " + arguments
.length
);
820 ok(arguments
[0] === 1, "arguments[0] = " + arguments
[0]);
823 ok(f
.length
=== 0, "f.length = " + f
.length
);
825 ok(r
=== 1, "r = " + r
);
828 ok(this === o
, "this != o");
829 ok(arguments
.length
=== 2, "arguments.length = " + arguments
.length
);
830 ok(arguments
[0] === 1, "arguments[0] = " + arguments
[0]);
831 ok(arguments
[1] === 2, "arguments[1] = " + arguments
[0]);
835 ok(r
=== 1, "r = " + r
);
839 ok(r
=== 1, "r = " + r
);
841 f
= (function test(x
, y
, z
) {
842 ok(this === o
, "this != o");
843 ok(arguments
.length
=== 2, "arguments.length = " + arguments
.length
);
844 ok(x
=== 1, "x = " + x
);
845 ok(y
=== 2, "y = " + y
);
846 ok(z
=== undefined, "z = " + z
);
849 ok(f
.length
=== 2, "f.length = " + f
.length
);
851 ok(r
=== 1, "r = " + r
);
852 ok(f
.toString() === "\nfunction() {\n [native code]\n}\n", "f.toString() = " + f
.toString());
853 ok(!("prototype" in f
), "bound function has prototype");
856 f
= Array
.prototype.push
.bind(a
, 1);
858 ok(a
.length
=== 1, "a.length = " + a
.length
);
860 ok(a
.length
=== 3, "a.length = " + a
.length
);
861 ok(f
.length
=== 0, "f.length = " + f
.length
);
862 ok(f
.toString() === "\nfunction() {\n [native code]\n}\n", "f.toString() = " + f
.toString());
863 ok(a
.toString() === "1,1,2", "a = " + a
);
865 ok(a
.toString() === "1,1,2,1,3", "a = " + a
);
867 f
= (function() { return this; }).bind(a
);
868 ok(f() === a
, "f() != a");
870 f
= (function() { return this; }).bind(null);
871 ok(f() === window
, "f() = " + f());
874 f
= (function() { return t
= this; }).bind(a
);
875 ok(new f() === t
, "new f() != a");
876 ok(typeof(t
) === "object", "typeof(t) = " + typeof(t
));
877 ok(t
!= a
, "t == a");
879 ok(Function
.prototype.bind
.length
=== 1, "Function.prototype.bind.length = " + Function
.prototype.bind
.length
);
882 sync_test("keys", function() {
883 var o
= { a
: 1, b
: 2, c
: 3 };
884 var keys
= Object
.keys(o
).sort().join();
885 ok(keys
=== "a,b,c", "keys = " + keys
);
887 o
= Object
.create(o
);
888 keys
= Object
.keys(o
).sort().join();
889 ok(keys
=== "", "keys = " + keys
);
892 keys
= Object
.keys(o
).sort().join();
893 ok(keys
=== "test", "keys = " + keys
);
895 ok(Object
.keys
.length
=== 1, "Object.keys.length = " + Object
.keys
.length
);
898 sync_test("reduce", function() {
901 r
= [1,2,3].reduce(function(a
, value
) { return a
+ value
+ 10; });
902 ok(r
=== 26, "reduce() returned " + r
);
904 r
= [1,2,3].reduce(function(a
, value
) { return a
+ value
+ 10; }, 1);
905 ok(r
=== 37, "reduce() returned " + r
);
907 r
= [1,2,3].reduce(function(a
, value
) { return a
+ value
; }, "str");
908 ok(r
=== "str123", "reduce() returned " + r
);
911 r
= array
.reduce(function(a
, value
, index
, src
) {
912 ok(src
=== array
, "src != array");
913 return a
+ "(" + index
+ "," + value
+ ")";
915 ok(r
=== "str(0,1)(1,2)(2,3)", "reduce() returned " + r
);
917 r
= [1,2,3].reduce(function(a
, value
, index
, src
) {
923 ok(r
=== "1test", "reduce() returned " + r
);
925 r
= [1].reduce(function(a
) { return 0; });
926 ok(r
=== 1, "[1].reduce() returned " + r
);
928 r
= [1].reduce(function(a
) { return 0; }, 2);
929 ok(r
=== 0, "[1].reduce(2) returned " + r
);
931 r
= [].reduce(function(a
) { return 0; }, 2);
932 ok(r
=== 2, "[].reduce(2) returned " + r
);
934 r
= [].reduce(function(a
) { return 0; }, undefined);
935 ok(r
=== undefined, "[].reduce(undefined) returned " + r
);
938 [].reduce(function(a
) { return 0; });
939 ok(false, "expected exception");
942 ok(Array
.prototype.reduce
.length
=== 1, "Array.prototype.reduce.length = " + Array
.prototype.reduce
.length
);
945 sync_test("preventExtensions", function() {
947 var r
= Object
.preventExtensions(o
);
948 ok(r
=== o
, "r != o");
950 ok(!("x" in o
), "x property added to o");
952 Object
.defineProperty(o
, "y", { value
: true });
953 ok(false, "expected exception");
955 ok(e
.name
=== "TypeError", "got " + e
.name
+ " exception");
958 r
= Object
.preventExtensions(o
);
959 ok(r
=== o
, "r != o");
960 r
= Object
.isExtensible(o
);
961 ok(r
=== false, "isExtensible(o) returned " + r
);
964 o
= Object
.preventExtensions(new Constr());
965 Constr
.prototype.prop
= 1;
966 ok(o
.prop
=== 1, "o.prop = " + o
.prop
);
968 ok(o
.prop
=== 1, "o.prop = " + o
.prop
);
969 r
= Object
.isExtensible(o
);
970 ok(r
=== false, "isExtensible(o) returned " + r
);
972 r
= Object
.isExtensible({});
973 ok(r
=== true, "isExtensible(o) returned " + r
);
976 Object
.isExtensible(1);
977 ok(false, "exception expected");
979 ok(e
.name
=== "TypeError", "got " + e
.name
+ " exception");
983 Object
.preventExtensions(o
);
986 ok(false, "exception expected on o.push");
988 ok(e
.name
=== "TypeError", "got " + e
.name
+ " exception");
990 ok(!("0" in o
), "0 is in o");
991 ok(o
.length
=== 0, "o.length = " + o
.length
);
994 Object
.preventExtensions(o
);
996 ok(!("0" in o
), "0 is in o");
997 ok(o
.length
=== 0, "o.length = " + o
.length
);
999 ok(Object
.preventExtensions
.length
=== 1, "Object.preventExtensions.length = " + Object
.preventExtensions
.length
);
1000 ok(Object
.isExtensible
.length
=== 1, "Object.isExtensible.length = " + Object
.isExtensible
.length
);
1003 sync_test("freeze", function() {
1004 ok(Object
.freeze
.length
=== 1, "Object.freeze.length = " + Object
.freeze
.length
);
1007 ok(false, "exception expected");
1009 ok(e
.name
=== "TypeError", "got " + e
.name
+ " exception");
1017 Object
.defineProperty(o
, "accprop", {
1026 test_own_data_prop_desc(o
, "prop", true, true, true);
1027 r
= Object
.freeze(o
);
1028 ok(r
=== o
, "r != o");
1029 test_own_data_prop_desc(o
, "prop", false, true, false);
1030 test_own_data_prop_desc(o
, "func", false, true, false);
1031 ok(!Object
.isExtensible(o
), "o is still extensible");
1034 ok(o
.prop
=== 1, "o.prop = " + o
.prop
);
1035 ok(o
.func
=== f
, "o.func = " + o
.func
);
1039 ok(r
=== 2, "r = " + r
);
1041 ok(o
.accprop
=== 3, "o.accprop = " + o
.accprop
);
1047 ok(false, "exception expected on o.pop");
1049 ok(e
.name
=== "TypeError", "got " + e
.name
+ " exception");
1051 ok(o
[0] === 1, "o[0] = " + o
[0]);
1052 ok(o
.length
=== 1, "o.length = " + o
.length
);
1055 sync_test("seal", function() {
1056 ok(Object
.seal
.length
=== 1, "Object.seal.length = " + Object
.seal
.length
);
1059 ok(false, "exception expected");
1061 ok(e
.name
=== "TypeError", "got " + e
.name
+ " exception");
1069 Object
.defineProperty(o
, "accprop", {
1078 test_own_data_prop_desc(o
, "prop", true, true, true);
1080 ok(r
=== o
, "r != o");
1081 test_own_data_prop_desc(o
, "prop", true, true, false);
1082 test_own_data_prop_desc(o
, "func", true, true, false);
1083 ok(!Object
.isExtensible(o
), "o is still extensible");
1086 ok(o
.prop
=== false, "o.prop = " + o
.prop
);
1087 ok(o
.func
=== false, "o.func = " + o
.func
);
1091 ok(r
=== 2, "r = " + r
);
1093 ok(o
.accprop
=== 3, "o.accprop = " + o
.accprop
);
1099 ok(false, "exception expected on o.pop");
1101 ok(e
.name
=== "TypeError", "got " + e
.name
+ " exception");
1103 ok(o
[0] === 1, "o[0] = " + o
[0]);
1104 ok(o
.length
=== 1, "o.length = " + o
.length
);
1107 sync_test("isFrozen", function() {
1108 ok(Object
.isFrozen
.length
=== 1, "Object.isFrozen.length = " + Object
.isFrozen
.length
);
1109 ok(Object
.isSealed
.length
=== 1, "Object.isSealed.length = " + Object
.isSealed
.length
);
1111 var o
= Object
.freeze({});
1112 ok(Object
.isFrozen(o
) === true, "o is not frozen");
1113 ok(Object
.isSealed(o
) === true, "o is not sealed");
1114 ok(Object
.isExtensible(o
) === false, "o is extensible");
1116 ok(Object
.isFrozen({}) === false, "{} is frozen");
1117 ok(Object
.isSealed({}) === false, "{} is sealed");
1119 o
= Object
.preventExtensions({});
1120 ok(Object
.isFrozen(o
) === true, "o is not frozen");
1121 ok(Object
.isSealed(o
) === true, "o is not sealed");
1122 ok(Object
.isExtensible(o
) === false, "o is extensible");
1124 o
= Object
.preventExtensions({ prop
: 1 });
1125 ok(Object
.isFrozen(o
) === false, "o is frozen");
1126 ok(Object
.isSealed(o
) === false, "o is sealed");
1127 ok(Object
.isExtensible(o
) === false, "o is extensible");
1129 o
= Object
.freeze({ prop
: 1 });
1130 ok(Object
.isFrozen(o
) === true, "o is not frozen");
1131 ok(Object
.isSealed(o
) === true, "o is not sealed");
1132 ok(Object
.isExtensible(o
) === false, "o is extensible");
1134 o
= Object
.seal({ prop
: 1 });
1135 ok(Object
.isFrozen(o
) === false, "o is frozen");
1136 ok(Object
.isSealed(o
) === true, "o is not sealed");
1137 ok(Object
.isExtensible(o
) === false, "o is extensible");
1140 Object
.defineProperty(o
, "prop", { value
: 1 });
1141 Object
.preventExtensions(o
);
1142 ok(Object
.isFrozen(o
) === true, "o is not frozen");
1143 ok(Object
.isSealed(o
) === true, "o is not sealed");
1144 ok(Object
.isExtensible(o
) === false, "o is extensible");
1147 Object
.defineProperty(o
, "prop", { value
: 1, writable
: true });
1148 Object
.preventExtensions(o
);
1149 ok(Object
.isFrozen(o
) === false, "o is frozen");
1150 ok(Object
.isSealed(o
) === true, "o is not sealed");
1151 ok(Object
.isExtensible(o
) === false, "o is extensible");
1154 Object
.defineProperty(o
, "prop", { value
: 1, writable
: true, configurable
: true });
1155 Object
.preventExtensions(o
);
1156 ok(Object
.isFrozen(o
) === false, "o is frozen");
1157 ok(Object
.isSealed(o
) === false, "o is sealed");
1158 ok(Object
.isExtensible(o
) === false, "o is extensible");
1161 Object
.defineProperty(o
, "prop", { value
: 1, configurable
: true });
1162 Object
.preventExtensions(o
);
1163 ok(Object
.isFrozen(o
) === false, "o is frozen");
1164 ok(Object
.isSealed(o
) === false, "o is sealed");
1165 ok(Object
.isExtensible(o
) === false, "o is extensible");
1168 Object
.defineProperty(o
, "prop", { get: function() {}, set: function() {} });
1169 Object
.preventExtensions(o
);
1170 ok(Object
.isFrozen(o
) === true, "o is not frozen");
1171 ok(Object
.isSealed(o
) === true, "o is not sealed");
1172 ok(Object
.isExtensible(o
) === false, "o is extensible");
1175 sync_test("head_setter", function() {
1177 ok(typeof(document
.head
) === "object", "typeof(document.head) = " + typeof(document
.head
));