testArgumentsObject.cpp (3758B)
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- 2 * vim: set ts=8 sts=2 et sw=2 tw=80: 3 */ 4 /* This Source Code Form is subject to the terms of the Mozilla Public 5 * License, v. 2.0. If a copy of the MPL was not distributed with this 6 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 7 8 #include "jsapi-tests/tests.h" 9 10 #include "vm/ArgumentsObject-inl.h" 11 #include "vm/JSObject-inl.h" 12 13 using namespace js; 14 15 static const char NORMAL_ZERO[] = "function f() { return arguments; }"; 16 static const char NORMAL_ONE[] = "function f(a) { return arguments; }"; 17 static const char NORMAL_TWO[] = "function f(a, b) { return arguments; }"; 18 static const char NORMAL_THREE[] = "function f(a, b, c) { return arguments; }"; 19 20 static const char STRICT_ZERO[] = 21 "function f() { 'use strict'; return arguments; }"; 22 static const char STRICT_ONE[] = 23 "function f() { 'use strict'; return arguments; }"; 24 static const char STRICT_TWO[] = 25 "function f() { 'use strict'; return arguments; }"; 26 static const char STRICT_THREE[] = 27 "function f() { 'use strict'; return arguments; }"; 28 29 static const char* const CALL_CODES[] = {"f()", "f(0)", 30 "f(0, 1)", "f(0, 1, 2)", 31 "f(0, 1, 2, 3)", "f(0, 1, 2, 3, 4)"}; 32 33 BEGIN_TEST(testArgumentsObject) { 34 return ExhaustiveTest<0>(NORMAL_ZERO) && ExhaustiveTest<1>(NORMAL_ZERO) && 35 ExhaustiveTest<2>(NORMAL_ZERO) && ExhaustiveTest<0>(NORMAL_ONE) && 36 ExhaustiveTest<1>(NORMAL_ONE) && ExhaustiveTest<2>(NORMAL_ONE) && 37 ExhaustiveTest<3>(NORMAL_ONE) && ExhaustiveTest<0>(NORMAL_TWO) && 38 ExhaustiveTest<1>(NORMAL_TWO) && ExhaustiveTest<2>(NORMAL_TWO) && 39 ExhaustiveTest<3>(NORMAL_TWO) && ExhaustiveTest<4>(NORMAL_TWO) && 40 ExhaustiveTest<0>(NORMAL_THREE) && ExhaustiveTest<1>(NORMAL_THREE) && 41 ExhaustiveTest<2>(NORMAL_THREE) && ExhaustiveTest<3>(NORMAL_THREE) && 42 ExhaustiveTest<4>(NORMAL_THREE) && ExhaustiveTest<5>(NORMAL_THREE) && 43 ExhaustiveTest<0>(STRICT_ZERO) && ExhaustiveTest<1>(STRICT_ZERO) && 44 ExhaustiveTest<2>(STRICT_ZERO) && ExhaustiveTest<0>(STRICT_ONE) && 45 ExhaustiveTest<1>(STRICT_ONE) && ExhaustiveTest<2>(STRICT_ONE) && 46 ExhaustiveTest<3>(STRICT_ONE) && ExhaustiveTest<0>(STRICT_TWO) && 47 ExhaustiveTest<1>(STRICT_TWO) && ExhaustiveTest<2>(STRICT_TWO) && 48 ExhaustiveTest<3>(STRICT_TWO) && ExhaustiveTest<4>(STRICT_TWO) && 49 ExhaustiveTest<0>(STRICT_THREE) && ExhaustiveTest<1>(STRICT_THREE) && 50 ExhaustiveTest<2>(STRICT_THREE) && ExhaustiveTest<3>(STRICT_THREE) && 51 ExhaustiveTest<4>(STRICT_THREE) && ExhaustiveTest<5>(STRICT_THREE); 52 } 53 54 static const size_t MAX_ELEMS = 6; 55 56 template <size_t ArgCount> 57 bool ExhaustiveTest(const char funcode[]) { 58 RootedValue v(cx); 59 EVAL(funcode, &v); 60 61 EVAL(CALL_CODES[ArgCount], &v); 62 Rooted<ArgumentsObject*> argsobj(cx, 63 &v.toObjectOrNull()->as<ArgumentsObject>()); 64 65 JS::RootedValueArray<MAX_ELEMS> elems(cx); 66 67 for (size_t i = 0; i <= ArgCount; i++) { 68 for (size_t j = 0; j <= ArgCount - i; j++) { 69 ClearElements(elems); 70 CHECK(argsobj->maybeGetElements(i, j, elems.begin())); 71 for (size_t k = 0; k < j; k++) { 72 CHECK(elems[k].isInt32(i + k)); 73 } 74 for (size_t k = j; k < MAX_ELEMS - 1; k++) { 75 CHECK(elems[k].isNull()); 76 } 77 CHECK(elems[MAX_ELEMS - 1].isInt32(42)); 78 } 79 } 80 81 return true; 82 } 83 84 template <size_t N> 85 static void ClearElements(JS::RootedValueArray<N>& elems) { 86 for (size_t i = 0; i < elems.length() - 1; i++) { 87 elems[i].setNull(); 88 } 89 elems[elems.length() - 1].setInt32(42); 90 } 91 END_TEST(testArgumentsObject)