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