jscript: Use bytecode for function expression implementation.
[wine.git] / dlls / jscript / tests / lang.js
blob13c90faf2553b0d3296dfc0b0206d43029512d9b
1 /*
2  * Copyright 2008 Jacek Caban for CodeWeavers
3  *
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.
8  *
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.
13  *
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
17  */
19 var tmp;
21 ok(true, "true is not true?");
22 ok(!false, "!false is not true");
23 ok(!undefined, "!undefined is not true");
24 ok(!null, "!null is not true");
25 ok(!0, "!0 is not true");
26 ok(!0.0, "!0.0 is not true");
28 ok(1 === 1, "1 === 1 is false");
29 ok(!(1 === 2), "!(1 === 2) is false");
30 ok(1.0 === 1, "1.0 === 1 is false");
31 ok("abc" === "abc", "\"abc\" === \"abc\" is false");
32 ok(true === true, "true === true is false");
33 ok(null === null, "null === null is false");
34 ok(undefined === undefined, "undefined === undefined is false");
35 ok(!(undefined === null), "!(undefined === null) is false");
36 ok(1E0 === 1, "1E0 === 1 is false");
37 ok(1000000*1000000 === 1000000000000, "1000000*1000000 === 1000000000000 is false");
38 ok(8.64e15 === 8640000000000000, "8.64e15 !== 8640000000000000");
39 ok(1e2147483648 === Infinity, "1e2147483648 !== Infinity");
41 ok(1 !== 2, "1 !== 2 is false");
42 ok(null !== undefined, "null !== undefined is false");
44 ok(1 == 1, "1 == 1 is false");
45 ok(!(1 == 2), "!(1 == 2) is false");
46 ok(1.0 == 1, "1.0 == 1 is false");
47 ok("abc" == "abc", "\"abc\" == \"abc\" is false");
48 ok(true == true, "true == true is false");
49 ok(null == null, "null == null is false");
50 ok(undefined == undefined, "undefined == undefined is false");
51 ok(undefined == null, "undefined == null is false");
52 ok(true == 1, "true == 1 is false");
53 ok(!(true == 2), "true == 2");
54 ok(0 == false, "0 == false is false");
56 ok(1 != 2, "1 != 2 is false");
57 ok(false != 1, "false != 1 is false");
59 var trueVar = true;
60 ok(trueVar, "trueVar is not true");
62 ok(ScriptEngine.length === 0, "ScriptEngine.length is not 0");
64 function testFunc1(x, y) {
65     ok(this !== undefined, "this is undefined");
66     ok(x === true, "x is not true");
67     ok(y === "test", "y is not \"test\"");
68     ok(arguments.length === 2, "arguments.length is not 2");
69     ok(arguments["0"] === true, "arguments[0] is not true");
70     ok(arguments["1"] === "test", "arguments[1] is not \"test\"");
71     ok(arguments.callee === testFunc1, "arguments.calee !== testFunc1");
72     ok(testFunc1.arguments === arguments, "testFunc1.arguments = " + testFunc1.arguments);
74     return true;
77 ok(testFunc1.length === 2, "testFunc1.length is not 2");
78 ok(testFunc1.arguments === null, "testFunc1.arguments = " + testFunc1.arguments);
80 ok(Object.prototype !== undefined, "Object.prototype is undefined");
81 ok(Object.prototype.prototype === undefined, "Object.prototype is not undefined");
82 ok(String.prototype !== undefined, "String.prototype is undefined");
83 ok(Array.prototype !== undefined, "Array.prototype is undefined");
84 ok(Boolean.prototype !== undefined, "Boolean.prototype is undefined");
85 ok(Number.prototype !== undefined, "Number.prototype is undefined");
86 ok(RegExp.prototype !== undefined, "RegExp.prototype is undefined");
87 ok(Math !== undefined, "Math is undefined");
88 ok(Math.prototype === undefined, "Math.prototype is not undefined");
89 ok(Function.prototype !== undefined, "Function.prototype is undefined");
90 ok(Function.prototype.prototype === undefined, "Function.prototype.prototype is not undefined");
91 ok(Date.prototype !== undefined, "Date.prototype is undefined");
92 ok(Date.prototype.prototype === undefined, "Date.prototype is not undefined");
94 Function.prototype.test = true;
95 ok(testFunc1.test === true, "testFunc1.test !== true");
96 ok(Function.test === true, "Function.test !== true");
98 ok(typeof(0) === "number", "typeof(0) is not number");
99 ok(typeof(1.5) === "number", "typeof(1.5) is not number");
100 ok(typeof("abc") === "string", "typeof(\"abc\") is not string");
101 ok(typeof("") === "string", "typeof(\"\") is not string");
102 ok(typeof(true) === "boolean", "typeof(true) is not boolean");
103 ok(typeof(null) === "object", "typeof(null) is not object");
104 ok(typeof(undefined) === "undefined", "typeof(undefined) is not undefined");
105 ok(typeof(Math) === "object", "typeof(Math) is not object");
106 ok(typeof(String.prototype) === "object", "typeof(String.prototype) is not object");
107 ok(typeof(testFunc1) === "function", "typeof(testFunc1) is not function");
108 ok(typeof(String) === "function", "typeof(String) is not function");
109 ok(typeof(ScriptEngine) === "function", "typeof(ScriptEngine) is not function");
110 ok(typeof(this) === "object", "typeof(this) is not object");
111 ok(typeof(doesnotexist) === "undefined", "typeof(doesnotexist) = " + typeof(doesnotexist));
112 tmp = typeof((new Object()).doesnotexist);
113 ok(tmp === "undefined", "typeof((new Object).doesnotexist = " + tmp);
114 tmp = typeof(testObj.onlyDispID);
115 ok(tmp === "unknown", "typeof(testObj.onlyDispID) = " + tmp);
117 ok(testFunc1(true, "test") === true, "testFunc1 not returned true");
119 ok(testFunc1.arguments === null, "testFunc1.arguments = " + testFunc1.arguments);
121 (tmp) = 3;
122 ok(tmp === 3, "tmp = " + tmp);
124 function testRecFunc(x) {
125     ok(testRecFunc.arguments === arguments, "testRecFunc.arguments = " + testRecFunc.arguments);
126     if(x) {
127         testRecFunc(false);
128         ok(testRecFunc.arguments === arguments, "testRecFunc.arguments = " + testRecFunc.arguments);
129         ok(testRecFunc.arguments[0] === true, "testRecFunc.arguments.x = " + testRecFunc.arguments[0]);
130     }
133 testRecFunc.arguments = 5;
134 ok(testRecFunc.arguments === null, "testRecFunc.arguments = " + testRecFunc.arguments);
135 testRecFunc(true);
136 ok(testRecFunc.arguments === null, "testRecFunc.arguments = " + testRecFunc.arguments);
138 tmp = (function() {1;})();
139 ok(tmp === undefined, "tmp = " + tmp);
140 tmp = eval("1;");
141 ok(tmp === 1, "tmp = " + tmp);
143 var obj1 = new Object();
144 ok(typeof(obj1) === "object", "typeof(obj1) is not object");
145 ok(obj1.constructor === Object, "unexpected obj1.constructor");
146 obj1.test = true;
147 obj1.func = function () {
148     ok(this === obj1, "this is not obj1");
149     ok(this.test === true, "this.test is not true");
150     ok(arguments.length === 1, "arguments.length is not 1");
151     ok(arguments["0"] === true, "arguments[0] is not true");
152     ok(typeof(arguments.callee) === "function", "typeof(arguments.calee) = " + typeof(arguments.calee));
154     return "test";
157 ok(obj1.func(true) === "test", "obj1.func(true) is not \"test\"");
159 function testConstr1() {
160     this.var1 = 1;
162     ok(this !== undefined, "this is undefined");
163     ok(arguments.length === 1, "arguments.length is not 1");
164     ok(arguments["0"] === true, "arguments[0] is not 1");
165     ok(arguments.callee === testConstr1, "arguments.calee !== testConstr1");
167     return false;
170 testConstr1.prototype.pvar = 1;
172 var obj2 = new testConstr1(true);
173 ok(typeof(obj2) === "object", "typeof(obj2) is not object");
174 ok(obj2.constructor === testConstr1, "unexpected obj2.constructor");
175 ok(obj2.pvar === 1, "obj2.pvar is not 1");
177 testConstr1.prototype.pvar = 2;
178 ok(obj2.pvar === 2, "obj2.pvar is not 2");
180 obj2.pvar = 3;
181 testConstr1.prototype.pvar = 1;
182 ok(obj2.pvar === 3, "obj2.pvar is not 3");
184 obj1 = new Object();
185 function testConstr3() {
186     return obj1;
189 obj2 = new testConstr3();
190 ok(obj1 === obj2, "obj1 != obj2");
192 function testConstr4() {
193     return 2;
196 obj2 = new testConstr3();
197 ok(typeof(obj2) === "object", "typeof(obj2) = " + typeof(obj2));
199 var obj3 = new Object;
200 ok(typeof(obj3) === "object", "typeof(obj3) is not object");
202 for(var iter in "test")
203     ok(false, "unexpected forin call, test = " + iter);
205 for(var iter in null)
206     ok(false, "unexpected forin call, test = " + iter);
208 for(var iter in false)
209     ok(false, "unexpected forin call, test = " + iter);
211 for(var iter in pureDisp)
212     ok(false, "unexpected forin call in pureDisp object");
214 tmp = new Object();
215 ok(!tmp.nonexistent, "!tmp.nonexistent = " + !tmp.nonexistent);
216 ok(!("nonexistent" in tmp), "nonexistent is in tmp after '!' expression")
218 tmp = new Object();
219 ok((~tmp.nonexistent) === -1, "!tmp.nonexistent = " + ~tmp.nonexistent);
220 ok(!("nonexistent" in tmp), "nonexistent is in tmp after '~' expression")
222 tmp = new Object();
223 ok(isNaN(+tmp.nonexistent), "!tmp.nonexistent = " + (+tmp.nonexistent));
224 ok(!("nonexistent" in tmp), "nonexistent is in tmp after '+' expression")
226 tmp = new Object();
227 tmp[tmp.nonexistent];
228 ok(!("nonexistent" in tmp), "nonexistent is in tmp after array expression")
230 tmp = 0;
231 if(true)
232     tmp = 1;
233 else
234     ok(false, "else evaluated");
235 ok(tmp === 1, "tmp !== 1, if not evaluated?");
237 tmp = 0;
238 if(1 === 0)
239     ok(false, "if evaluated");
240 else
241     tmp = 1;
242 ok(tmp === 1, "tmp !== 1, if not evaluated?");
244 if(false)
245     ok(false, "if(false) evaluated");
247 tmp = 0;
248 if(true)
249     tmp = 1;
250 ok(tmp === 1, "tmp !== 1, if(true) not evaluated?");
252 if(false) {
253 }else {
256 var obj3 = { prop1: 1,  prop2: typeof(false) };
257 ok(obj3.prop1 === 1, "obj3.prop1 is not 1");
258 ok(obj3.prop2 === "boolean", "obj3.prop2 is not \"boolean\"");
259 ok(obj3.constructor === Object, "unexpected obj3.constructor");
262     var blockVar = 1;
263     blockVar = 2;
265 ok(blockVar === 2, "blockVar !== 2");
267 ok((true ? 1 : 2) === 1, "conditional expression true is not 1");
268 ok((0 === 2 ? 1 : 2) === 2, "conditional expression true is not 2");
270 ok(getVT(undefined) === "VT_EMPTY", "getVT(undefined) is not VT_EMPTY");
271 ok(getVT(null) === "VT_NULL", "getVT(null) is not VT_NULL");
272 ok(getVT(0) === "VT_I4", "getVT(0) is not VT_I4");
273 ok(getVT(0.5) === "VT_R8", "getVT(0.5) is not VT_R8");
274 ok(getVT("test") === "VT_BSTR", "getVT(\"test\") is not VT_BSTR");
275 ok(getVT(Math) === "VT_DISPATCH", "getVT(Math) is not VT_DISPATCH");
276 ok(getVT(false) === "VT_BOOL", "getVT(false) is not VT_BOOL");
278 tmp = 2+2;
279 ok(tmp === 4, "2+2 !== 4");
280 ok(getVT(tmp) === "VT_I4", "getVT(2+2) !== VT_I4");
282 tmp = 2+2.5;
283 ok(tmp === 4.5, "2+2.5 !== 4.5");
284 ok(getVT(tmp) === "VT_R8", "getVT(2+2.5) !== VT_R8");
286 tmp = 1.5+2.5;
287 ok(tmp === 4, "1.4+2.5 !== 4");
288 ok(getVT(tmp) === "VT_I4", "getVT(1.5+2.5) !== VT_I4");
290 tmp = 4-2;
291 ok(tmp === 2, "4-2 !== 2");
292 ok(getVT(tmp) === "VT_I4", "getVT(4-2) !== VT_I4");
294 tmp = 4.5-2;
295 ok(tmp === 2.5, "4.5-2 !== 2.5");
296 ok(getVT(tmp) === "VT_R8", "getVT(4.5-2) !== VT_R8");
298 tmp = -2;
299 ok(tmp === 0-2, "-2 !== 0-2");
300 ok(getVT(tmp) === "VT_I4", "getVT(-2) !== VT_I4");
302 tmp = 2*3;
303 ok(tmp === 6, "2*3 !== 6");
304 ok(getVT(tmp) === "VT_I4", "getVT(2*3) !== VT_I4");
306 tmp = 2*3.5;
307 ok(tmp === 7, "2*3.5 !== 7");
308 ok(getVT(tmp) === "VT_I4", "getVT(2*3.5) !== VT_I4");
310 tmp = 2.5*3.5;
311 /* FIXME: the parser loses precision */
312 /* ok(tmp === 8.75, "2.5*3.5 !== 8.75"); */
313 ok(tmp > 8.749999 && tmp < 8.750001, "2.5*3.5 !== 8.75");
314 ok(getVT(tmp) === "VT_R8", "getVT(2.5*3.5) !== VT_R8");
316 tmp = 4/2;
317 ok(tmp === 2, "4/2 !== 2");
318 ok(getVT(tmp) === "VT_I4", "getVT(4/2) !== VT_I4");
320 tmp = 4.5/1.5;
321 ok(tmp === 3, "4.5/1.5 !== 3");
322 ok(getVT(tmp) === "VT_I4", "getVT(4.5/1.5) !== VT_I4");
324 tmp = 3/2;
325 ok(tmp === 1.5, "3/2 !== 1.5");
326 ok(getVT(tmp) === "VT_R8", "getVT(3/2) !== VT_R8");
328 tmp = 3%2;
329 ok(tmp === 1, "3%2 = " + tmp);
331 tmp = 4%2;
332 ok(tmp ===0, "4%2 = " + tmp);
334 tmp = 3.5%1.5;
335 ok(tmp === 0.5, "3.5%1.5 = " + tmp);
337 tmp = 3%true;
338 ok(tmp === 0, "3%true = " + tmp);
340 tmp = "ab" + "cd";
341 ok(tmp === "abcd", "\"ab\" + \"cd\" !== \"abcd\"");
343 tmp = 1;
344 ok((tmp += 1) === 2, "tmp += 1 !== 2");
345 ok(tmp === 2, "tmp !== 2");
347 tmp = 2;
348 ok((tmp -= 1) === 1, "tmp -= 1 !== 1");
349 ok(tmp === 1, "tmp !=== 1");
351 tmp = 2;
352 ok((tmp *= 1.5) === 3, "tmp *= 1.5 !== 3");
353 ok(tmp === 3, "tmp !=== 3");
355 tmp = 5;
356 ok((tmp /= 2) === 2.5, "tmp /= 2 !== 2.5");
357 ok(tmp === 2.5, "tmp !=== 2.5");
359 tmp = 3;
360 ok((tmp %= 2) === 1, "tmp %= 2 !== 1");
361 ok(tmp === 1, "tmp !== 1");
363 tmp = 8;
364 ok((tmp <<= 1) === 16, "tmp <<= 1 !== 16");
366 tmp = 8;
367 ok((tmp >>= 1) === 4, "tmp >>= 1 !== 4");
369 tmp = 8;
370 ok((tmp >>>= 1) === 4, "tmp >>>= 1 !== 4");
372 tmp = 3 || ok(false, "second or expression called");
373 ok(tmp === 3, "3 || (...) is not 3");
375 tmp = false || 2;
376 ok(tmp === 2, "false || 2 is not 2");
378 tmp = 0 && ok(false, "second and expression called");
379 ok(tmp === 0, "0 && (...) is not 0");
381 tmp = true && "test";
382 ok(tmp === "test", "true && \"test\" is not \"test\"");
384 tmp = true && 0;
385 ok(tmp === 0, "true && 0 is not 0");
387 tmp = 3 | 4;
388 ok(tmp === 7, "3 | 4 !== 7");
389 ok(getVT(tmp) === "VT_I4", "getVT(3|4) = " + getVT(tmp));
391 tmp = 3.5 | 0;
392 ok(tmp === 3, "3.5 | 0 !== 3");
393 ok(getVT(tmp) === "VT_I4", "getVT(3.5|0) = " + getVT(tmp));
395 tmp = -3.5 | 0;
396 ok(tmp === -3, "-3.5 | 0 !== -3");
397 ok(getVT(tmp) === "VT_I4", "getVT(3.5|0) = " + getVT(tmp));
399 tmp = 0 | NaN;
400 ok(tmp === 0, "0 | NaN = " + tmp);
402 tmp = 0 | Infinity;
403 ok(tmp === 0, "0 | NaN = " + tmp);
405 tmp = 0 | (-Infinity);
406 ok(tmp === 0, "0 | NaN = " + tmp);
408 tmp = 10;
409 ok((tmp |= 0x10) === 26, "tmp(10) |= 0x10 !== 26");
410 ok(getVT(tmp) === "VT_I4", "getVT(tmp |= 10) = " + getVT(tmp));
412 tmp = 3 & 5;
413 ok(tmp === 1, "3 & 5 !== 1");
414 ok(getVT(tmp) === "VT_I4", "getVT(3|5) = " + getVT(tmp));
416 tmp = 3.5 & 0xffff;
417 ok(tmp === 3, "3.5 & 0xffff !== 3 ");
418 ok(getVT(tmp) === "VT_I4", "getVT(3.5&0xffff) = " + getVT(tmp));
420 tmp = (-3.5) & 0xffffffff;
421 ok(tmp === -3, "-3.5 & 0xffff !== -3");
422 ok(getVT(tmp) === "VT_I4", "getVT(3.5&0xffff) = " + getVT(tmp));
424 tmp = 2 << 3;
425 ok(tmp === 16, "2 << 3 = " + tmp);
427 tmp = 2 << 35;
428 ok(tmp === 16, "2 << 35 = " + tmp);
430 tmp = 8 >> 2;
431 ok(tmp === 2, "8 >> 2 = " + tmp);
433 tmp = -64 >> 4;
434 ok(tmp === -4, "-64 >> 4 = " + tmp);
436 tmp = 8 >>> 2;
437 ok(tmp === 2, "8 >> 2 = " + tmp);
439 tmp = -64 >>> 4;
440 ok(tmp === 0x0ffffffc, "-64 >>> 4 = " + tmp);
442 tmp = 4 >>> NaN;
443 ok(tmp === 4, "4 >>> NaN = " + tmp);
445 tmp = 10;
446 ok((tmp &= 8) === 8, "tmp(10) &= 8 !== 8");
447 ok(getVT(tmp) === "VT_I4", "getVT(tmp &= 8) = " + getVT(tmp));
449 tmp = 0xf0f0^0xff00;
450 ok(tmp === 0x0ff0, "0xf0f0^0xff00 !== 0x0ff0");
451 ok(getVT(tmp) === "VT_I4", "getVT(0xf0f0^0xff00) = " + getVT(tmp));
453 tmp = 5;
454 ok((tmp ^= 3) === 6, "tmp(5) ^= 3 !== 6");
455 ok(getVT(tmp) === "VT_I4", "getVT(tmp ^= 3) = " + getVT(tmp));
457 tmp = ~1;
458 ok(tmp === -2, "~1 !== -2");
459 ok(getVT(tmp) === "VT_I4", "getVT(~1) = " + getVT(tmp));
461 ok((3,4) === 4, "(3,4) !== 4");
463 ok(+3 === 3, "+3 !== 3");
464 ok(+true === 1, "+true !== 1");
465 ok(+false === 0, "+false !== 0");
466 ok(+null === 0, "+null !== 0");
467 ok(+"0" === 0, "+'0' !== 0");
468 ok(+"3" === 3, "+'3' !== 3");
469 ok(+"-3" === -3, "+'-3' !== -3");
470 ok(+"0xff" === 255, "+'0xff' !== 255");
471 ok(+"3e3" === 3000, "+'3e3' !== 3000");
473 tmp = new Number(1);
474 ok(+tmp === 1, "+(new Number(1)) = " + (+tmp));
475 ok(tmp.constructor === Number, "unexpected tmp.constructor");
476 tmp = new String("1");
477 ok(+tmp === 1, "+(new String('1')) = " + (+tmp));
478 ok(tmp.constructor === String, "unexpected tmp.constructor");
480 ok("" + 0 === "0", "\"\" + 0 !== \"0\"");
481 ok("" + 123 === "123", "\"\" + 123 !== \"123\"");
482 ok("" + (-5) === "-5", "\"\" + (-5) !== \"-5\"");
483 ok("" + null === "null", "\"\" + null !== \"null\"");
484 ok("" + undefined === "undefined", "\"\" + undefined !== \"undefined\"");
485 ok("" + true === "true", "\"\" + true !== \"true\"");
486 ok("" + false === "false", "\"\" + false !== \"false\"");
487 ok("" + 0.5 === "0.5", "'' + 0.5 = " + 0.5);
488 ok("" + (-0.5432) === "-0.5432", "'' + (-0.5432) = " + (-0.5432));
490 ok(1 < 3.4, "1 < 3.4 failed");
491 ok(!(3.4 < 1), "3.4 < 1");
492 ok("abc" < "abcd", "abc < abcd failed");
493 ok("abcd" < "abce", "abce < abce failed");
494 ok("" < "x", "\"\" < \"x\" failed");
495 ok(!(0 < 0), "0 < 0");
497 ok(1 <= 3.4, "1 <= 3.4 failed");
498 ok(!(3.4 <= 1), "3.4 <= 1");
499 ok("abc" <= "abcd", "abc <= abcd failed");
500 ok("abcd" <= "abce", "abce <= abce failed");
501 ok("" <= "x", "\"\" <= \"x\" failed");
502 ok(0 <= 0, "0 <= 0 failed");
504 ok(3.4 > 1, "3.4 > 1 failed");
505 ok(!(1 > 3.4), "1 > 3.4");
506 ok("abcd" > "abc", "abc > abcd failed");
507 ok("abce" > "abcd", "abce > abce failed");
508 ok("x" > "", "\"x\" > \"\" failed");
509 ok(!(0 > 0), "0 > 0");
511 ok(3.4 >= 1, "3.4 >= 1 failed");
512 ok(!(1 >= 3.4), "1 >= 3.4");
513 ok("abcd" >= "abc", "abc >= abcd failed");
514 ok("abce" >= "abcd", "abce >= abce failed");
515 ok("x" >= "", "\"x\" >= \"\" failed");
516 ok(0 >= 0, "0 >= 0");
518 tmp = 1;
519 ok(++tmp === 2, "++tmp (1) is not 2");
520 ok(tmp === 2, "incremented tmp is not 2");
521 ok(--tmp === 1, "--tmp (2) is not 1");
522 ok(tmp === 1, "decremented tmp is not 1");
523 ok(tmp++ === 1, "tmp++ (1) is not 1");
524 ok(tmp === 2, "incremented tmp(1) is not 2");
525 ok(tmp-- === 2, "tmp-- (2) is not 2");
526 ok(tmp === 1, "decremented tmp is not 1");
528 tmp = new Object();
529 tmp.iii++;
530 ok(isNaN(tmp.iii), "tmp.iii = " + tmp.iii);
532 String.prototype.test = true;
533 ok("".test === true, "\"\".test is not true");
535 Boolean.prototype.test = true;
536 ok(true.test === true, "true.test is not true");
538 Number.prototype.test = true;
539 ok((0).test === true, "(0).test is not true");
540 ok((0.5).test === true, "(0.5).test is not true");
542 var state = "";
543 try {
544     ok(state === "", "try: state = " + state);
545     state = "try";
546 }catch(ex) {
547     ok(false, "unexpected catch");
549 ok(state === "try", "state = " + state + " expected try");
551 state = "";
552 try {
553     ok(state === "", "try: state = " + state);
554     state = "try";
555 }finally {
556     ok(state === "try", "finally: state = " + state);
557     state = "finally";
559 ok(state === "finally", "state = " + state + " expected finally");
561 state = "";
562 try {
563     ok(state === "", "try: state = " + state);
564     state = "try";
565 }catch(ex) {
566     ok(false, "unexpected catch");
567 }finally {
568     ok(state === "try", "finally: state = " + state);
569     state = "finally";
571 ok(state === "finally", "state = " + state + " expected finally");
573 var state = "";
574 try {
575     ok(state === "", "try: state = " + state);
576     state = "try";
577     throw "except";
578 }catch(ex) {
579     ok(state === "try", "catch: state = " + state);
580     ok(ex === "except", "ex is not \"except\"");
581     state = "catch";
583 ok(state === "catch", "state = " + state + " expected catch");
585 var state = "";
586 try {
587     ok(state === "", "try: state = " + state);
588     state = "try";
589     throw true;
590 }catch(ex) {
591     ok(state === "try", "catch: state = " + state);
592     ok(ex === true, "ex is not true");
593     state = "catch";
594 }finally {
595     ok(state === "catch", "finally: state = " + state);
596     state = "finally";
598 ok(state === "finally", "state = " + state + " expected finally");
600 var state = "";
601 try {
602     ok(state === "", "try: state = " + state);
603     state = "try";
604     try { throw true; } finally {}
605 }catch(ex) {
606     ok(state === "try", "catch: state = " + state);
607     ok(ex === true, "ex is not true");
608     state = "catch";
609 }finally {
610     ok(state === "catch", "finally: state = " + state);
611     state = "finally";
613 ok(state === "finally", "state = " + state + " expected finally");
615 var state = "";
616 try {
617     ok(state === "", "try: state = " + state);
618     state = "try";
619     try { throw "except"; } catch(ex) { throw true; }
620 }catch(ex) {
621     ok(state === "try", "catch: state = " + state);
622     ok(ex === true, "ex is not true");
623     state = "catch";
624 }finally {
625     ok(state === "catch", "finally: state = " + state);
626     state = "finally";
628 ok(state === "finally", "state = " + state + " expected finally");
630 function throwFunc(x) {
631     throw x;
634 var state = "";
635 try {
636     ok(state === "", "try: state = " + state);
637     state = "try";
638     throwFunc(true);
639 }catch(ex) {
640     ok(state === "try", "catch: state = " + state);
641     ok(ex === true, "ex is not true");
642     state = "catch";
643 }finally {
644     ok(state === "catch", "finally: state = " + state);
645     state = "finally";
647 ok(state === "finally", "state = " + state + " expected finally");
649 state = "";
650 switch(1) {
651 case "1":
652     ok(false, "unexpected case \"1\"");
653 case 1:
654     ok(state === "", "case 1: state = " + state);
655     state = "1";
656 default:
657     ok(state === "1", "default: state = " + state);
658     state = "default";
659 case false:
660     ok(state === "default", "case false: state = " + state);
661     state = "false";
663 ok(state === "false", "state = " + state);
665 state = "";
666 switch("") {
667 case "1":
668 case 1:
669     ok(false, "unexpected case 1");
670 default:
671     ok(state === "", "default: state = " + state);
672     state = "default";
673 case false:
674     ok(state === "default", "case false: state = " + state);
675     state = "false";
677 ok(state === "false", "state = " + state);
679 state = "";
680 switch(1) {
681 case "1":
682     ok(false, "unexpected case \"1\"");
683 case 1:
684     ok(state === "", "case 1: state = " + state);
685     state = "1";
686 default:
687     ok(state === "1", "default: state = " + state);
688     state = "default";
689     break;
690 case false:
691     ok(false, "unexpected case false");
693 ok(state === "default", "state = " + state);
695 tmp = eval("1");
696 ok(tmp === 1, "eval(\"1\") !== 1");
697 eval("{ ok(tmp === 1, 'eval: tmp !== 1'); } tmp = 2;");
698 ok(tmp === 2, "tmp !== 2");
700 ok(eval(false) === false, "eval(false) !== false");
701 ok(eval() === undefined, "eval() !== undefined");
703 tmp = eval("1", "2");
704 ok(tmp === 1, "eval(\"1\", \"2\") !== 1");
706 var state = "";
707 try {
708     ok(state === "", "try: state = " + state);
709     state = "try";
710     eval("throwFunc(true);");
711 }catch(ex) {
712     ok(state === "try", "catch: state = " + state);
713     ok(ex === true, "ex is not true");
714     state = "catch";
715 }finally {
716     ok(state === "catch", "finally: state = " + state);
717     state = "finally";
719 ok(state === "finally", "state = " + state + " expected finally");
721 tmp = [,,1,2,,,true];
722 ok(tmp.length === 7, "tmp.length !== 7");
723 ok(tmp["0"] === undefined, "tmp[0] is not undefined");
724 ok(tmp["3"] === 2, "tmp[3] !== 2");
725 ok(tmp["6"] === true, "tmp[6] !== true");
726 ok(tmp[2] === 1, "tmp[2] !== 1");
728 ok([1,].length === 2, "[1,].length !== 2");
729 ok([,,].length === 3, "[,,].length !== 3");
730 ok([,].length === 2, "[].length != 2");
731 ok([].length === 0, "[].length != 0");
733 tmp = 0;
734 while(tmp < 4) {
735     ok(tmp < 4, "tmp >= 4");
736     tmp++;
738 ok(tmp === 4, "tmp !== 4");
740 tmp = 0;
741 while(true) {
742     ok(tmp < 4, "tmp >= 4");
743     tmp++;
744     if(tmp === 4) {
745         break;
746         ok(false, "break did not break");
747     }
749 ok(tmp === 4, "tmp !== 4");
751 tmp = 0;
752 do {
753     ok(tmp < 4, "tmp >= 4");
754     tmp++;
755 } while(tmp < 4);
756 ok(tmp === 4, "tmp !== 4");
758 tmp = 0;
759 do {
760     ok(tmp === 0, "tmp !=== 0");
761     tmp++;
762 } while(false);
763 ok(tmp === 1, "tmp !== 1");
765 tmp = 0;
766 do {
767     ok(tmp < 4, "tmp >= 4");
768     tmp++;
769 } while(tmp < 4)
770 ok(tmp === 4, "tmp !== 4")
772 tmp = 0;
773 while(tmp < 4) {
774     tmp++;
775     if(tmp === 2) {
776         continue;
777         ok(false, "break did not break");
778     }
779     ok(tmp <= 4 && tmp != 2, "tmp = " + tmp);
781 ok(tmp === 4, "tmp !== 4");
783 for(tmp=0; tmp < 4; tmp++)
784     ok(tmp < 4, "tmp = " + tmp);
785 ok(tmp === 4, "tmp !== 4");
787 for(tmp=0; tmp < 4; tmp++) {
788     if(tmp === 2)
789         break;
790     ok(tmp < 2, "tmp = " + tmp);
792 ok(tmp === 2, "tmp !== 2");
794 for(tmp=0; tmp < 4; tmp++) {
795     if(tmp === 2)
796         continue;
797     ok(tmp < 4 && tmp != 2, "tmp = " + tmp);
799 ok(tmp === 4, "tmp !== 4");
801 for(var fi=0; fi < 4; fi++)
802     ok(fi < 4, "fi = " + fi);
803 ok(fi === 4, "fi !== 4");
805 tmp = true;
806 obj1 = new Object();
807 for(obj1.nonexistent; tmp; tmp = false)
808     ok(!("nonexistent" in obj1), "nonexistent added to obj1");
810 obj1 = new Object();
811 for(tmp in obj1.nonexistent)
812     ok(false, "for(tmp in obj1.nonexistent) called with tmp = " + tmp);
813 ok(!("nonexistent" in obj1), "nonexistent added to obj1 by for..in loop");
815 ok((void 1) === undefined, "(void 1) !== undefined");
817 var inobj = new Object();
819 for(var iter in inobj)
820     ok(false, "unexpected iter = " + iter);
822 inobj.test = true;
823 tmp = 0;
824 for(iter in inobj) {
825     ok(iter == "test", "unexpected iter = " + iter);
826     tmp++;
828 ok(tmp === 1, "for..in tmp = " + tmp);
830 function forinTestObj() {}
832 forinTestObj.prototype.test3 = true;
834 var arr = new Array();
835 inobj = new forinTestObj();
836 inobj.test1 = true;
837 inobj.test2 = true;
839 tmp = 0;
840 for(iter in inobj) {
841     arr[iter] = true;
842     tmp++;
845 ok(tmp === 3, "for..in tmp = " + tmp);
846 ok(arr["test1"] === true, "arr[test1] !== true");
847 ok(arr["test2"] === true, "arr[test2] !== true");
848 ok(arr["test3"] === true, "arr[test3] !== true");
850 tmp = new Object();
851 tmp.test = false;
852 ok((delete tmp.test) === true, "delete returned false");
853 ok(typeof(tmp.test) === "undefined", "tmp.test type = " + typeof(tmp.test));
854 ok(!("test" in tmp), "test is still in tmp after delete?");
855 for(iter in tmp)
856     ok(false, "tmp has prop " + iter);
858 tmp = new Object();
859 tmp.test = false;
860 ok((delete tmp["test"]) === true, "delete returned false");
861 ok(typeof(tmp.test) === "undefined", "tmp.test type = " + typeof(tmp.test));
862 ok(!("test" in tmp), "test is still in tmp after delete?");
864 tmp.testWith = true;
865 with(tmp)
866     ok(testWith === true, "testWith !== true");
868 if(false) {
869     var varTest1 = true;
872 ok(varTest1 === undefined, "varTest1 = " + varTest1);
873 ok(varTest2 === undefined, "varTest2 = " + varTest1);
875 var varTest2;
877 function varTestFunc(varTest3) {
878     var varTest3;
880     ok(varTest3 === 3, "varTest3 = " + varTest3);
881     ok(varTest4 === undefined, "varTest4 = " + varTest4);
883     var varTest4;
886 varTestFunc(3);
888 deleteTest = 1;
889 delete deleteTest;
890 try {
891     tmp = deleteTest;
892     ok(false, "deleteTest not throwed exception?");
893 }catch(ex) {}
895 if (false)
896     if (true)
897         ok(false, "if evaluated");
898     else
899         ok(false, "else should be associated with nearest if statement");
901 if (true)
902     if (false)
903         ok(false, "if evaluated");
904     else
905         ok(true, "else should be associated with nearest if statement");
907 function instanceOfTest() {}
908 tmp = new instanceOfTest();
910 ok((tmp instanceof instanceOfTest) === true, "tmp is not instance of instanceOfTest");
911 ok((tmp instanceof Object) === true, "tmp is not instance of Object");
912 ok((tmp instanceof String) === false, "tmp is instance of String");
914 instanceOfTest.prototype = new Object();
915 ok((tmp instanceof instanceOfTest) === false, "tmp is instance of instanceOfTest");
916 ok((tmp instanceof Object) === true, "tmp is not instance of Object");
918 ok((1 instanceof Object) === false, "1 is instance of Object");
919 ok((false instanceof Boolean) === false, "false is instance of Boolean");
920 ok(("" instanceof Object) === false, "'' is instance of Object");
922 (function () {
923     ok((arguments instanceof Object) === true, "argument is not instance of Object");
924     ok((arguments instanceof Array) === false, "argument is not instance of Array");
925     ok(arguments.toString() === "[object Object]", "arguments.toString() = " + arguments.toString());
926 })(1,2);
928 obj = new String();
929 ok(("length" in obj) === true, "length is not in obj");
930 ok(("isPrototypeOf" in obj) === true, "isPrototypeOf is not in obj");
931 ok(("abc" in obj) === false, "test is in obj");
932 obj.abc = 1;
933 ok(("abc" in obj) === true, "test is not in obj");
934 ok(("1" in obj) === false, "1 is in obj");
936 obj = [1,2,3];
937 ok((1 in obj) === true, "1 is not in obj");
939 obj = new Object();
940 try {
941     obj.prop["test"];
942     ok(false, "expected exception");
943 }catch(e) {}
944 ok(!("prop" in obj), "prop in obj");
946 ok(isNaN(NaN) === true, "isNaN(NaN) !== true");
947 ok(isNaN(0.5) === false, "isNaN(0.5) !== false");
948 ok(isNaN(Infinity) === false, "isNaN(Infinity) !== false");
949 ok(isNaN() === true, "isNaN() !== true");
950 ok(isNaN(NaN, 0) === true, "isNaN(NaN, 0) !== true");
951 ok(isNaN(0.5, NaN) === false, "isNaN(0.5, NaN) !== false");
952 ok(isNaN(+undefined) === true, "isNaN(+undefined) !== true");
954 ok(isFinite(0.5) === true, "isFinite(0.5) !== true");
955 ok(isFinite(Infinity) === false, "isFinite(Infinity) !== false");
956 ok(isFinite(-Infinity) === false, "isFinite(Infinity) !== false");
957 ok(isFinite(NaN) === false, "isFinite(NaN) !== false");
958 ok(isFinite(0.5, NaN) === true, "isFinite(0.5, NaN) !== true");
959 ok(isFinite(NaN, 0.5) === false, "isFinite(NaN, 0.5) !== false");
960 ok(isFinite() === false, "isFinite() !== false");
962 ok((1 < NaN) === false, "(1 < NaN) !== false");
963 ok((1 > NaN) === false, "(1 > NaN) !== false");
964 ok((1 <= NaN) === false, "(1 <= NaN) !== false");
965 ok((1 >= NaN) === false, "(1 >= NaN) !== false");
966 ok((NaN < 1) === false, "(NaN < 1) !== false");
967 ok((NaN > 1) === false, "(NaN > 1) !== false");
968 ok((NaN <= 1) === false, "(NaN <= 1) !== false");
969 ok((NaN >= 1) === false, "(NaN >= 1) !== false");
970 ok((Infinity < 2) === false, "(Infinity < 2) !== false");
971 ok((Infinity > 2) === true, "(Infinity > 2) !== true");
972 ok((-Infinity < 2) === true, "(-Infinity < 2) !== true");
974 ok(isNaN(+"test") === true, "isNaN(+'test') !== true");
975 ok(isNaN(+"123t") === true, "isNaN(+'123t') !== true");
976 ok(isNaN(+"Infinity x") === true, "isNaN(+'Infinity x') !== true");
977 ok(+"Infinity" === Infinity, "+'Infinity' !== Infinity");
978 ok(+" Infinity " === Infinity, "+' Infinity ' !== Infinity");
979 ok(+"-Infinity" === -Infinity, "+'-Infinity' !== -Infinity");
981 ok((NaN !== NaN) === true, "(NaN !== NaN) !== true");
982 ok((NaN === NaN) === false, "(NaN === NaN) !== false");
983 ok((Infinity !== NaN) === true, "(Infinity !== NaN) !== true");
984 ok((Infinity !== NaN) === true, "(Infinity !== NaN) !== true");
985 ok((0 === NaN) === false, "(0 === NaN) !== false");
987 ok((NaN != NaN) === true, "(NaN !== NaN) != true");
988 ok((NaN == NaN) === false, "(NaN === NaN) != false");
989 ok((Infinity != NaN) === true, "(Infinity != NaN) !== true");
990 ok((Infinity != NaN) === true, "(Infinity != NaN) !== true");
991 ok((0 == NaN) === false, "(0 === NaN) != false");
993 // escape tests
994 var escapeTests = [
995     ["\'", "\\'", 39],
996     ["\"", "\\\"", 34],
997     ["\\", "\\\\", 92],
998     ["\b", "\\b", 8],
999     ["\t", "\\t", 9],
1000     ["\n", "\\n", 10],
1001     ["\v", "\\v", 118],
1002     ["\f", "\\f", 12],
1003     ["\r", "\\r", 13],
1004     ["\xf3", "\\xf3", 0xf3],
1005     ["\u1234", "\\u1234", 0x1234],
1006     ["\a", "\\a", 97],
1007     ["\?", "\\?", 63]
1010 for(i=0; i<escapeTests.length; i++) {
1011     tmp = escapeTests[i][0].charCodeAt(0);
1012     ok(tmp === escapeTests[i][2], "escaped '" + escapeTests[i][1] + "' = " + tmp + " expected " + escapeTests[i][2]);
1015 tmp = !+"\v1";
1016 ok(tmp === true, '!+"\v1" = ' + tmp);
1018 ok(typeof(testFunc2) === "function", "typeof(testFunc2) = " + typeof(testFunc2));
1019 tmp = testFunc2(1);
1020 ok(tmp === 2, "testFunc2(1) = " + tmp);
1021 function testFunc2(x) { return x+1; }
1023 ok(typeof(testFunc3) === "function", "typeof(testFunc3) = " + typeof(testFunc3));
1024 tmp = testFunc3(1);
1025 ok(tmp === 3, "testFunc3(1) = " + tmp);
1026 tmp = function testFunc3(x) { return x+2; };
1028 tmp = testFunc4(1);
1029 ok(tmp === 5, "testFunc4(1) = " + tmp);
1030 tmp = function testFunc4(x) { return x+3; };
1031 tmp = testFunc4(1);
1032 testFunc4 = 1;
1033 ok(testFunc4 === 1, "testFunc4 = " + testFunc4);
1034 ok(tmp === 5, "testFunc4(1) = " + tmp);
1035 tmp = function testFunc4(x) { return x+4; };
1036 ok(testFunc4 === 1, "testFunc4 = " + testFunc4);
1038 function testEmbededFunctions() {
1039     ok(typeof(testFunc5) === "function", "typeof(testFunc5) = " + typeof(testFunc5));
1040     tmp = testFunc5(1);
1041     ok(tmp === 3, "testFunc5(1) = " + tmp);
1042     tmp = function testFunc5(x) { return x+2; };
1044     tmp = testFunc6(1);
1045     ok(tmp === 5, "testFunc6(1) = " + tmp);
1046     tmp = function testFunc6(x) { return x+3; };
1047     tmp = testFunc6(1);
1048     ok(tmp === 5, "testFunc6(1) = " + tmp);
1049     tmp = function testFunc6(x) { return x+4; };
1050     testFunc6 = 1;
1051     ok(testFunc6 === 1, "testFunc4 = " + testFunc6);
1054 testEmbededFunctions();
1056 date = new Date();
1057 date.toString = function() { return "toString"; }
1058 ok(""+date === "toString", "''+date = " + date);
1059 date.toString = function() { return this; }
1060 ok(""+date === ""+date.valueOf(), "''+date = " + date);
1062 str = new String("test");
1063 str.valueOf = function() { return "valueOf"; }
1064 ok(""+str === "valueOf", "''+str = " + str);
1065 str.valueOf = function() { return new Date(); }
1066 ok(""+str === "test", "''+str = " + str);
1068 ok((function (){return 1;})() === 1, "(function (){return 1;})() = " + (function (){return 1;})());
1070 var re = /=(\?|%3F)/g;
1071 ok(re.source === "=(\\?|%3F)", "re.source = " + re.source);
1073 tmp = new Array();
1074 for(var i=0; i<2; i++)
1075     tmp[i] = /b/;
1076 ok(tmp[0] != tmp[1], "tmp[0] == tmp [1]");
1078 ok(createNullBSTR() === '', "createNullBSTR() !== ''");
1080 ok(getVT(nullDisp) === "VT_DISPATCH", "getVT(nullDisp) = " + getVT(nullDisp));
1081 ok(typeof(nullDisp) === "object", "typeof(nullDisp) = " + typeof(nullDisp));
1082 ok(nullDisp === nullDisp, "nullDisp !== nullDisp");
1083 ok(nullDisp !== re, "nullDisp === re");
1084 ok(nullDisp === null, "nullDisp === null");
1085 ok(nullDisp == null, "nullDisp == null");
1086 ok(getVT(true && nullDisp) === "VT_DISPATCH",
1087    "getVT(0 && nullDisp) = " + getVT(true && nullDisp));
1088 ok(!nullDisp === true, "!nullDisp = " + !nullDisp);
1089 ok(String(nullDisp) === "null", "String(nullDisp) = " + String(nullDisp));
1090 ok(nullDisp != new Object(), "nullDisp == new Object()");
1091 ok(new Object() != nullDisp, "new Object() == nullDisp");
1092 ok((typeof Object(nullDisp)) === "object", "typeof Object(nullDisp) !== 'object'");
1093 tmp = getVT(Object(nullDisp));
1094 ok(tmp === "VT_DISPATCH", "getVT(Object(nullDisp) = " + tmp);
1095 tmp = Object(nullDisp).toString();
1096 ok(tmp === "[object Object]", "Object(nullDisp).toString() = " + tmp);
1098 function do_test() {}
1099 function nosemicolon() {} nosemicolon();
1100 function () {} nosemicolon();
1102 if(false) {
1103     function in_if_false() { return true; } ok(false, "!?");
1106 ok(in_if_false(), "in_if_false failed");
1108 (function() { newValue = 1; })();
1109 ok(newValue === 1, "newValue = " + newValue);
1111 obj = {undefined: 3};
1113 ok(typeof(name_override_func) === "function", "typeof(name_override_func) = " + typeof(name_override_func));
1114 name_override_func = 3;
1115 ok(name_override_func === 3, "name_override_func = " + name_override_func);
1116 function name_override_func() {};
1117 ok(name_override_func === 3, "name_override_func = " + name_override_func);
1119 /* Keep this test in the end of file */
1120 undefined = 6;
1121 ok(undefined === 6, "undefined = " + undefined);
1123 NaN = 6;
1124 ok(NaN === 6, "NaN !== 6");
1126 Infinity = 6;
1127 ok(Infinity === 6, "Infinity !== 6");
1129 Math = 6;
1130 ok(Math === 6, "NaN !== 6");
1132 reportSuccess();