1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/js/src/jsapi-tests/testArgumentsObject.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,113 @@ 1.4 +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- 1.5 + * vim: set ts=8 sts=4 et sw=4 tw=99: 1.6 + */ 1.7 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.8 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.9 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.10 + 1.11 +#include "jsapi-tests/tests.h" 1.12 + 1.13 +#include "jsobjinlines.h" 1.14 + 1.15 +#include "vm/ArgumentsObject-inl.h" 1.16 + 1.17 +using namespace js; 1.18 + 1.19 +static const char NORMAL_ZERO[] = 1.20 + "function f() { return arguments; }"; 1.21 +static const char NORMAL_ONE[] = 1.22 + "function f(a) { return arguments; }"; 1.23 +static const char NORMAL_TWO[] = 1.24 + "function f(a, b) { return arguments; }"; 1.25 +static const char NORMAL_THREE[] = 1.26 + "function f(a, b, c) { return arguments; }"; 1.27 + 1.28 +static const char STRICT_ZERO[] = 1.29 + "function f() { 'use strict'; return arguments; }"; 1.30 +static const char STRICT_ONE[] = 1.31 + "function f() { 'use strict'; return arguments; }"; 1.32 +static const char STRICT_TWO[] = 1.33 + "function f() { 'use strict'; return arguments; }"; 1.34 +static const char STRICT_THREE[] = 1.35 + "function f() { 'use strict'; return arguments; }"; 1.36 + 1.37 +static const char * const CALL_CODES[] = 1.38 + { "f()", "f(0)", "f(0, 1)", "f(0, 1, 2)", "f(0, 1, 2, 3)", "f(0, 1, 2, 3, 4)" }; 1.39 + 1.40 +BEGIN_TEST(testArgumentsObject) 1.41 +{ 1.42 + return ExhaustiveTest<0>(NORMAL_ZERO) && 1.43 + ExhaustiveTest<1>(NORMAL_ZERO) && 1.44 + ExhaustiveTest<2>(NORMAL_ZERO) && 1.45 + ExhaustiveTest<0>(NORMAL_ONE) && 1.46 + ExhaustiveTest<1>(NORMAL_ONE) && 1.47 + ExhaustiveTest<2>(NORMAL_ONE) && 1.48 + ExhaustiveTest<3>(NORMAL_ONE) && 1.49 + ExhaustiveTest<0>(NORMAL_TWO) && 1.50 + ExhaustiveTest<1>(NORMAL_TWO) && 1.51 + ExhaustiveTest<2>(NORMAL_TWO) && 1.52 + ExhaustiveTest<3>(NORMAL_TWO) && 1.53 + ExhaustiveTest<4>(NORMAL_TWO) && 1.54 + ExhaustiveTest<0>(NORMAL_THREE) && 1.55 + ExhaustiveTest<1>(NORMAL_THREE) && 1.56 + ExhaustiveTest<2>(NORMAL_THREE) && 1.57 + ExhaustiveTest<3>(NORMAL_THREE) && 1.58 + ExhaustiveTest<4>(NORMAL_THREE) && 1.59 + ExhaustiveTest<5>(NORMAL_THREE) && 1.60 + ExhaustiveTest<0>(STRICT_ZERO) && 1.61 + ExhaustiveTest<1>(STRICT_ZERO) && 1.62 + ExhaustiveTest<2>(STRICT_ZERO) && 1.63 + ExhaustiveTest<0>(STRICT_ONE) && 1.64 + ExhaustiveTest<1>(STRICT_ONE) && 1.65 + ExhaustiveTest<2>(STRICT_ONE) && 1.66 + ExhaustiveTest<3>(STRICT_ONE) && 1.67 + ExhaustiveTest<0>(STRICT_TWO) && 1.68 + ExhaustiveTest<1>(STRICT_TWO) && 1.69 + ExhaustiveTest<2>(STRICT_TWO) && 1.70 + ExhaustiveTest<3>(STRICT_TWO) && 1.71 + ExhaustiveTest<4>(STRICT_TWO) && 1.72 + ExhaustiveTest<0>(STRICT_THREE) && 1.73 + ExhaustiveTest<1>(STRICT_THREE) && 1.74 + ExhaustiveTest<2>(STRICT_THREE) && 1.75 + ExhaustiveTest<3>(STRICT_THREE) && 1.76 + ExhaustiveTest<4>(STRICT_THREE) && 1.77 + ExhaustiveTest<5>(STRICT_THREE); 1.78 +} 1.79 + 1.80 +static const size_t MAX_ELEMS = 6; 1.81 + 1.82 +template<size_t ArgCount> bool 1.83 +ExhaustiveTest(const char funcode[]) 1.84 +{ 1.85 + RootedValue v(cx); 1.86 + EVAL(funcode, &v); 1.87 + 1.88 + EVAL(CALL_CODES[ArgCount], &v); 1.89 + Rooted<ArgumentsObject*> argsobj(cx, &JSVAL_TO_OBJECT(v)->as<ArgumentsObject>()); 1.90 + 1.91 + JS::AutoValueArray<MAX_ELEMS> elems(cx); 1.92 + 1.93 + for (size_t i = 0; i <= ArgCount; i++) { 1.94 + for (size_t j = 0; j <= ArgCount - i; j++) { 1.95 + ClearElements(elems); 1.96 + CHECK(argsobj->maybeGetElements(i, j, elems.begin())); 1.97 + for (size_t k = 0; k < j; k++) 1.98 + CHECK_SAME(elems[k], INT_TO_JSVAL(i + k)); 1.99 + for (size_t k = j; k < MAX_ELEMS - 1; k++) 1.100 + CHECK_SAME(elems[k], JSVAL_NULL); 1.101 + CHECK_SAME(elems[MAX_ELEMS - 1], INT_TO_JSVAL(42)); 1.102 + } 1.103 + } 1.104 + 1.105 + return true; 1.106 +} 1.107 + 1.108 +template <size_t N> 1.109 +static void 1.110 +ClearElements(JS::AutoValueArray<N> &elems) 1.111 +{ 1.112 + for (size_t i = 0; i < elems.length() - 1; i++) 1.113 + elems[i].setNull(); 1.114 + elems[elems.length() - 1].setInt32(42); 1.115 +} 1.116 +END_TEST(testArgumentsObject)