Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 3 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 4 | |
michael@0 | 5 | #include "jsapi-tests/tests.h" |
michael@0 | 6 | |
michael@0 | 7 | static const JSClass ObjectEmulatingUndefinedClass = { |
michael@0 | 8 | "ObjectEmulatingUndefined", |
michael@0 | 9 | JSCLASS_EMULATES_UNDEFINED, |
michael@0 | 10 | JS_PropertyStub, |
michael@0 | 11 | JS_DeletePropertyStub, |
michael@0 | 12 | JS_PropertyStub, |
michael@0 | 13 | JS_StrictPropertyStub, |
michael@0 | 14 | JS_EnumerateStub, |
michael@0 | 15 | JS_ResolveStub, |
michael@0 | 16 | JS_ConvertStub |
michael@0 | 17 | }; |
michael@0 | 18 | |
michael@0 | 19 | static bool |
michael@0 | 20 | ObjectEmulatingUndefinedConstructor(JSContext *cx, unsigned argc, jsval *vp) |
michael@0 | 21 | { |
michael@0 | 22 | JS::CallArgs args = JS::CallArgsFromVp(argc, vp); |
michael@0 | 23 | JSObject *obj = JS_NewObjectForConstructor(cx, &ObjectEmulatingUndefinedClass, args); |
michael@0 | 24 | if (!obj) |
michael@0 | 25 | return false; |
michael@0 | 26 | args.rval().setObject(*obj); |
michael@0 | 27 | return true; |
michael@0 | 28 | } |
michael@0 | 29 | |
michael@0 | 30 | BEGIN_TEST(testObjectEmulatingUndefined_truthy) |
michael@0 | 31 | { |
michael@0 | 32 | CHECK(JS_InitClass(cx, global, js::NullPtr(), &ObjectEmulatingUndefinedClass, |
michael@0 | 33 | ObjectEmulatingUndefinedConstructor, 0, |
michael@0 | 34 | nullptr, nullptr, nullptr, nullptr)); |
michael@0 | 35 | |
michael@0 | 36 | JS::RootedValue result(cx); |
michael@0 | 37 | |
michael@0 | 38 | EVAL("if (new ObjectEmulatingUndefined()) true; else false;", &result); |
michael@0 | 39 | CHECK_SAME(result, JSVAL_FALSE); |
michael@0 | 40 | |
michael@0 | 41 | EVAL("if (!new ObjectEmulatingUndefined()) true; else false;", &result); |
michael@0 | 42 | CHECK_SAME(result, JSVAL_TRUE); |
michael@0 | 43 | |
michael@0 | 44 | EVAL("var obj = new ObjectEmulatingUndefined(); \n" |
michael@0 | 45 | "var res = []; \n" |
michael@0 | 46 | "for (var i = 0; i < 50; i++) \n" |
michael@0 | 47 | " res.push(Boolean(obj)); \n" |
michael@0 | 48 | "res.every(function(v) { return v === false; });", |
michael@0 | 49 | &result); |
michael@0 | 50 | CHECK_SAME(result, JSVAL_TRUE); |
michael@0 | 51 | |
michael@0 | 52 | return true; |
michael@0 | 53 | } |
michael@0 | 54 | END_TEST(testObjectEmulatingUndefined_truthy) |
michael@0 | 55 | |
michael@0 | 56 | BEGIN_TEST(testObjectEmulatingUndefined_equal) |
michael@0 | 57 | { |
michael@0 | 58 | CHECK(JS_InitClass(cx, global, js::NullPtr(), &ObjectEmulatingUndefinedClass, |
michael@0 | 59 | ObjectEmulatingUndefinedConstructor, 0, |
michael@0 | 60 | nullptr, nullptr, nullptr, nullptr)); |
michael@0 | 61 | |
michael@0 | 62 | JS::RootedValue result(cx); |
michael@0 | 63 | |
michael@0 | 64 | EVAL("if (new ObjectEmulatingUndefined() == undefined) true; else false;", &result); |
michael@0 | 65 | CHECK_SAME(result, JSVAL_TRUE); |
michael@0 | 66 | |
michael@0 | 67 | EVAL("if (new ObjectEmulatingUndefined() == null) true; else false;", &result); |
michael@0 | 68 | CHECK_SAME(result, JSVAL_TRUE); |
michael@0 | 69 | |
michael@0 | 70 | EVAL("if (new ObjectEmulatingUndefined() != undefined) true; else false;", &result); |
michael@0 | 71 | CHECK_SAME(result, JSVAL_FALSE); |
michael@0 | 72 | |
michael@0 | 73 | EVAL("if (new ObjectEmulatingUndefined() != null) true; else false;", &result); |
michael@0 | 74 | CHECK_SAME(result, JSVAL_FALSE); |
michael@0 | 75 | |
michael@0 | 76 | EVAL("var obj = new ObjectEmulatingUndefined(); \n" |
michael@0 | 77 | "var res = []; \n" |
michael@0 | 78 | "for (var i = 0; i < 50; i++) \n" |
michael@0 | 79 | " res.push(obj == undefined); \n" |
michael@0 | 80 | "res.every(function(v) { return v === true; });", |
michael@0 | 81 | &result); |
michael@0 | 82 | CHECK_SAME(result, JSVAL_TRUE); |
michael@0 | 83 | |
michael@0 | 84 | EVAL("var obj = new ObjectEmulatingUndefined(); \n" |
michael@0 | 85 | "var res = []; \n" |
michael@0 | 86 | "for (var i = 0; i < 50; i++) \n" |
michael@0 | 87 | " res.push(obj == null); \n" |
michael@0 | 88 | "res.every(function(v) { return v === true; });", |
michael@0 | 89 | &result); |
michael@0 | 90 | CHECK_SAME(result, JSVAL_TRUE); |
michael@0 | 91 | |
michael@0 | 92 | EVAL("var obj = new ObjectEmulatingUndefined(); \n" |
michael@0 | 93 | "var res = []; \n" |
michael@0 | 94 | "for (var i = 0; i < 50; i++) \n" |
michael@0 | 95 | " res.push(obj != undefined); \n" |
michael@0 | 96 | "res.every(function(v) { return v === false; });", |
michael@0 | 97 | &result); |
michael@0 | 98 | CHECK_SAME(result, JSVAL_TRUE); |
michael@0 | 99 | |
michael@0 | 100 | EVAL("var obj = new ObjectEmulatingUndefined(); \n" |
michael@0 | 101 | "var res = []; \n" |
michael@0 | 102 | "for (var i = 0; i < 50; i++) \n" |
michael@0 | 103 | " res.push(obj != null); \n" |
michael@0 | 104 | "res.every(function(v) { return v === false; });", |
michael@0 | 105 | &result); |
michael@0 | 106 | CHECK_SAME(result, JSVAL_TRUE); |
michael@0 | 107 | |
michael@0 | 108 | return true; |
michael@0 | 109 | } |
michael@0 | 110 | END_TEST(testObjectEmulatingUndefined_equal) |