wined3d: Pass a wined3d_device_context to wined3d_cs_emit_blt_sub_resource().
[wine/zf.git] / dlls / mshtml / tests / es5.js
blob474c09d3bd7160b2bfe81017586ca8c6c84b28c6
1 /*
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;
24 var tests = [];
26 sync_test("date_now", function() {
27 var now = Date.now();
28 var time = (new Date()).getTime();
30 ok(time >= now && time-now < 50, "unexpected Date.now() result " + now + " expected " + time);
32 Date.now(1, 2, 3);
33 });
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) {
42 try {
43 func();
44 }catch(e) {
45 return;
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(); });
61 });
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);
87 });
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]);
97 expect.shift();
98 });
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"]]);
106 test([], []);
108 [1,2].forEach(function() {
109 ok(this === window, "this != window");
111 [1,2].forEach(function() {
112 ok(this === window, "this != window");
113 }, undefined);
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");
119 }, 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);
133 function C() {}
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 */
142 calls = "";
143 arr = [1,2,3];
144 ctx = {};
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");
149 calls += x + ",";
150 return x * 2;
151 }, ctx);
152 ok(calls === "1,2,3,", "calls = " + calls);
153 ok(m.join() === "2,4,6", "m = " + m);
155 /* non-array object as this argument */
156 calls = "";
157 arr = { 1: "one", 2: "two", 3: "three", length: 3 };
158 m = Array.prototype.map.call(arr, function(x, i) {
159 calls += i + ":" + x + ",";
160 return 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 */
167 calls = "";
168 arr = [1,2,3];
169 m = Array.prototype.map.call(arr, function(x, i) {
170 calls += i + ":" + x + ",";
171 for(var j = i; j < arr.length; j++)
172 arr[j]++;
173 arr.push(i * i);
174 return x - 1;
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");
184 }, undefined);
187 sync_test("array_sort", function() {
188 var r;
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);
196 try {
197 r = [3,1,2].sort(null);
198 ok(false, "expected sort(null) exception");
199 }catch(e) {
200 ok(e.name === "TypeError", "got exception " + e.name);
204 sync_test("identifier_keywords", function() {
205 var o = {
206 if: 1,
207 default: 2,
208 function: 3,
209 break: true,
210 case: true,
211 catch: true,
212 continue: true,
213 delete: true,
214 do: true,
215 else: true,
216 finally: true,
217 for: true,
218 in: true,
219 instanceof: true,
220 new: true,
221 return: true,
222 switch: true,
223 throw: true,
224 try: true,
225 typeof: true,
226 var: true,
227 void: true,
228 while: true,
229 with: true,
230 true: true,
231 false: true,
232 null: true,
233 this: true
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() {
256 var obj;
258 obj = { test: 1 };
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);
270 obj = new Array();
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);
277 obj = /test/;
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);
284 (function() {
285 test_own_data_prop_desc(arguments, "length", true, false, true);
286 test_own_data_prop_desc(arguments, "callee", true, false, true);
287 })();
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) {
310 try {
311 func();
312 }catch(e) {
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);
318 return;
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;
339 var desc = {
340 get: function() {
341 return getsetprop_value;
343 set: function(v) {
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);
351 obj.getsetprop = 2;
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);
379 obj.notConf2 = 2;
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);
407 desc = {
408 get: function() {
409 return getsetprop_value;
411 set: function(v) {
412 getsetprop_value = v;
414 configurable: false
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);
444 desc = {
445 get: function() {
446 return getsetprop_value;
448 set: function(v) {
449 getsetprop_value = v;
451 configurable: true
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);
475 function child() {}
476 desc = {
477 get: function() {
478 ok(this === obj, "this != obj");
479 return getsetprop_value;
481 set: function(v) {
482 ok(this === obj, "this != obj");
483 getsetprop_value = v;
485 configurable: true
487 Object.defineProperty(child.prototype, "parent_accessor", desc);
489 obj = new child();
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);
504 /* no setter */
505 desc = {
506 get: function() {
507 ok(this === obj, "this != obj");
508 return true;
510 configurable: true
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 */
518 desc = {
519 get: function() {
520 return function(x) {
521 ok(x === 100, "x = " + x);
522 return 10;
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;
534 descs = {
535 acc_prop: { get: function() { return 1; } },
536 prop: { value: 2 },
537 e: { enumerable: true }
540 o = new Object();
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);
551 o = new Object();
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);
579 if(have_getter) {
580 ok(typeof(desc.get) === "function", "desc.get = " + desc.get);
581 ok(typeof(desc.get.prototype) === "object", "desc.get.prototype = " + desc.get.prototype);
582 }else {
583 ok(!("get" in obj), "desc.get = " + desc.get);
586 if(have_setter) {
587 ok(typeof(desc.set) === "function", "desc.set = " + desc.set);
588 ok(typeof(desc.set.prototype) === "object", "desc.set.prototype = " + desc.set.prototype);
589 }else {
590 ok(!("set" in obj), "desc.get = " + desc.get);
594 obj = {
595 get prop() { return val + 1; },
596 set prop(v) { val = v; }
598 test_accessor_prop_desc(obj, "prop", true, true);
599 val = 0;
600 ok(obj.prop === 1, "obj.prop = " + obj.prop);
601 obj.prop = 3;
602 ok(val === 3, "val = " + val);
603 ok(obj.prop === 4, "obj.prop = " + obj.prop);
605 arr = [];
606 for(i in obj)
607 arr.push(i);
608 ok(arr.join() === "prop", "prop of obj = " + arr.join());
610 obj = {
611 set prop(v) { val = v; }
613 test_accessor_prop_desc(obj, "prop", false, true);
614 val = 1;
615 ok(obj.prop === undefined, "obj.prop = " + obj.prop);
616 obj.prop = 2;
617 ok(val === 2, "val = " + val);
618 ok(obj.prop === undefined, "obj.prop = " + obj.prop);
620 obj = {
621 get prop() { return val + 1; },
622 get 0() { return val + 2; }
624 test_accessor_prop_desc(obj, "prop", true, false);
625 val = 5;
626 ok(obj.prop === 6, "obj.prop = " + obj.prop);
627 obj.prop = 10;
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");
644 test_trim("", "");
645 test_trim(" \t\n", "");
648 sync_test("global_properties", function() {
649 var o;
651 /* Make sure that global properties are not writable. */
652 o = NaN;
653 NaN = 1;
654 ok(isNaN(NaN), "NaN = " + NaN);
655 o = undefined;
656 undefined = 1;
657 ok(undefined === o, "NaN = " + NaN);
658 o = Infinity;
659 Infinity = 1;
660 ok(Infinity === o, "Infinity = " + NaN);
663 sync_test("string_split", function() {
664 var r;
666 /* IE9 got this wrong*/
667 if("1undefined2".split(undefined).length != 1) {
668 win_skip("detected broken String.prototype.split implementation");
669 return;
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");
694 test("a", "a", ",");
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]);
734 r = "".split();
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");
747 function Constr() {}
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");
758 obj = new Constr();
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() {
778 var tmp, obj;
780 (function () { tmp = Object.prototype.toString.call(arguments); })();
781 todo_wine.
782 ok(tmp === "[object Arguments]", "toString.call(arguments) = " + tmp);
783 tmp = Object.prototype.toString.call(this);
784 todo_wine.
785 ok(tmp === "[object Window]", "toString.call(null) = " + tmp);
786 tmp = Object.prototype.toString.call(null);
787 todo_wine.
788 ok(tmp === "[object Null]", "toString.call(null) = " + tmp);
789 tmp = Object.prototype.toString.call(undefined);
790 todo_wine.
791 ok(tmp === "[object Undefined]", "toString.call(undefined) = " + tmp);
792 tmp = Object.prototype.toString.call();
793 todo_wine.
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() {
805 var f, r;
806 var o = new Object(), o2 = new Object();
808 f = (function() {
809 ok(this === o, "this != o");
810 ok(arguments.length === 0, "arguments.length = " + arguments.length);
811 return 1;
812 }).bind(o);
813 ok(f.length === 0, "f.length = " + f.length);
814 r = f.call(o2);
815 ok(r === 1, "r = " + r);
817 f = (function() {
818 ok(this === o, "this != o");
819 ok(arguments.length === 1, "arguments.length = " + arguments.length);
820 ok(arguments[0] === 1, "arguments[0] = " + arguments[0]);
821 return 1;
822 }).bind(o, 1);
823 ok(f.length === 0, "f.length = " + f.length);
824 r = f.call(o2);
825 ok(r === 1, "r = " + r);
827 f = (function() {
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]);
832 return 1;
833 }).bind(o, 1);
834 r = f.call(o2, 2);
835 ok(r === 1, "r = " + r);
837 o2.f = f;
838 r = o2.f(2);
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);
847 return 1;
848 }).bind(o, 1);
849 ok(f.length === 2, "f.length = " + f.length);
850 r = f.call(o2, 2);
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");
855 var a = [];
856 f = Array.prototype.push.bind(a, 1);
857 f();
858 ok(a.length === 1, "a.length = " + a.length);
859 f(2);
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);
864 f.call([], 3);
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());
873 var t;
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);
891 o.test = 1;
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() {
899 var r, array;
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);
910 array = [1,2,3];
911 r = array.reduce(function(a, value, index, src) {
912 ok(src === array, "src != array");
913 return a + "(" + index + "," + value + ")";
914 }, "str");
915 ok(r === "str(0,1)(1,2)(2,3)", "reduce() returned " + r);
917 r = [1,2,3].reduce(function(a, value, index, src) {
918 src[0] = false;
919 delete src[1];
920 src[2] = "test";
921 return a + value;
922 }, "");
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);
937 try {
938 [].reduce(function(a) { return 0; });
939 ok(false, "expected exception");
940 }catch(e) {}
942 ok(Array.prototype.reduce.length === 1, "Array.prototype.reduce.length = " + Array.prototype.reduce.length);
945 sync_test("preventExtensions", function() {
946 var o = {};
947 var r = Object.preventExtensions(o);
948 ok(r === o, "r != o");
949 o.x = 1;
950 ok(!("x" in o), "x property added to o");
951 try {
952 Object.defineProperty(o, "y", { value: true });
953 ok(false, "expected exception");
954 }catch(e) {
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);
963 function Constr() {}
964 o = Object.preventExtensions(new Constr());
965 Constr.prototype.prop = 1;
966 ok(o.prop === 1, "o.prop = " + o.prop);
967 o.prop = 2;
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);
975 try {
976 Object.isExtensible(1);
977 ok(false, "exception expected");
978 }catch(e) {
979 ok(e.name === "TypeError", "got " + e.name + " exception");
982 o = [];
983 Object.preventExtensions(o);
984 try {
985 o.push(1);
986 ok(false, "exception expected on o.push");
987 }catch(e) {
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);
993 o = [1];
994 Object.preventExtensions(o);
995 o.pop();
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);
1005 try {
1006 Object.freeze(1);
1007 ok(false, "exception expected");
1008 }catch(e) {
1009 ok(e.name === "TypeError", "got " + e.name + " exception");
1012 function f() {}
1014 var o = {}, r;
1015 o.prop = 1;
1016 o.func = f;
1017 Object.defineProperty(o, "accprop", {
1018 get: function() {
1019 return r;
1021 set: function(v) {
1022 r = v;
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");
1032 o.prop = false;
1033 o.func = false;
1034 ok(o.prop === 1, "o.prop = " + o.prop);
1035 ok(o.func === f, "o.func = " + o.func);
1037 r = 1;
1038 o.accprop = 2;
1039 ok(r === 2, "r = " + r);
1040 r = 3;
1041 ok(o.accprop === 3, "o.accprop = " + o.accprop);
1043 o = [1];
1044 Object.freeze(o);
1045 try {
1046 o.pop();
1047 ok(false, "exception expected on o.pop");
1048 }catch(e) {
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);
1057 try {
1058 Object.seal(1);
1059 ok(false, "exception expected");
1060 }catch(e) {
1061 ok(e.name === "TypeError", "got " + e.name + " exception");
1064 function f() {}
1066 var o = {}, r;
1067 o.prop = 1;
1068 o.func = f;
1069 Object.defineProperty(o, "accprop", {
1070 get: function() {
1071 return r;
1073 set: function(v) {
1074 r = v;
1078 test_own_data_prop_desc(o, "prop", true, true, true);
1079 r = Object.seal(o);
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");
1084 o.prop = false;
1085 o.func = false;
1086 ok(o.prop === false, "o.prop = " + o.prop);
1087 ok(o.func === false, "o.func = " + o.func);
1089 r = 1;
1090 o.accprop = 2;
1091 ok(r === 2, "r = " + r);
1092 r = 3;
1093 ok(o.accprop === 3, "o.accprop = " + o.accprop);
1095 o = [1];
1096 Object.seal(o);
1097 try {
1098 o.pop();
1099 ok(false, "exception expected on o.pop");
1100 }catch(e) {
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");
1139 o = {};
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");
1146 o = {};
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");
1153 o = {};
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");
1160 o = {};
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");
1167 o = {};
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() {
1176 document.head = "";
1177 ok(typeof(document.head) === "object", "typeof(document.head) = " + typeof(document.head));