|
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- |
|
2 * vim: set ts=8 sts=4 et sw=4 tw=99: |
|
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 "jsobjinlines.h" |
|
11 |
|
12 #include "vm/ArgumentsObject-inl.h" |
|
13 |
|
14 using namespace js; |
|
15 |
|
16 static const char NORMAL_ZERO[] = |
|
17 "function f() { return arguments; }"; |
|
18 static const char NORMAL_ONE[] = |
|
19 "function f(a) { return arguments; }"; |
|
20 static const char NORMAL_TWO[] = |
|
21 "function f(a, b) { return arguments; }"; |
|
22 static const char NORMAL_THREE[] = |
|
23 "function f(a, b, c) { return arguments; }"; |
|
24 |
|
25 static const char STRICT_ZERO[] = |
|
26 "function f() { 'use strict'; return arguments; }"; |
|
27 static const char STRICT_ONE[] = |
|
28 "function f() { 'use strict'; return arguments; }"; |
|
29 static const char STRICT_TWO[] = |
|
30 "function f() { 'use strict'; return arguments; }"; |
|
31 static const char STRICT_THREE[] = |
|
32 "function f() { 'use strict'; return arguments; }"; |
|
33 |
|
34 static const char * const CALL_CODES[] = |
|
35 { "f()", "f(0)", "f(0, 1)", "f(0, 1, 2)", "f(0, 1, 2, 3)", "f(0, 1, 2, 3, 4)" }; |
|
36 |
|
37 BEGIN_TEST(testArgumentsObject) |
|
38 { |
|
39 return ExhaustiveTest<0>(NORMAL_ZERO) && |
|
40 ExhaustiveTest<1>(NORMAL_ZERO) && |
|
41 ExhaustiveTest<2>(NORMAL_ZERO) && |
|
42 ExhaustiveTest<0>(NORMAL_ONE) && |
|
43 ExhaustiveTest<1>(NORMAL_ONE) && |
|
44 ExhaustiveTest<2>(NORMAL_ONE) && |
|
45 ExhaustiveTest<3>(NORMAL_ONE) && |
|
46 ExhaustiveTest<0>(NORMAL_TWO) && |
|
47 ExhaustiveTest<1>(NORMAL_TWO) && |
|
48 ExhaustiveTest<2>(NORMAL_TWO) && |
|
49 ExhaustiveTest<3>(NORMAL_TWO) && |
|
50 ExhaustiveTest<4>(NORMAL_TWO) && |
|
51 ExhaustiveTest<0>(NORMAL_THREE) && |
|
52 ExhaustiveTest<1>(NORMAL_THREE) && |
|
53 ExhaustiveTest<2>(NORMAL_THREE) && |
|
54 ExhaustiveTest<3>(NORMAL_THREE) && |
|
55 ExhaustiveTest<4>(NORMAL_THREE) && |
|
56 ExhaustiveTest<5>(NORMAL_THREE) && |
|
57 ExhaustiveTest<0>(STRICT_ZERO) && |
|
58 ExhaustiveTest<1>(STRICT_ZERO) && |
|
59 ExhaustiveTest<2>(STRICT_ZERO) && |
|
60 ExhaustiveTest<0>(STRICT_ONE) && |
|
61 ExhaustiveTest<1>(STRICT_ONE) && |
|
62 ExhaustiveTest<2>(STRICT_ONE) && |
|
63 ExhaustiveTest<3>(STRICT_ONE) && |
|
64 ExhaustiveTest<0>(STRICT_TWO) && |
|
65 ExhaustiveTest<1>(STRICT_TWO) && |
|
66 ExhaustiveTest<2>(STRICT_TWO) && |
|
67 ExhaustiveTest<3>(STRICT_TWO) && |
|
68 ExhaustiveTest<4>(STRICT_TWO) && |
|
69 ExhaustiveTest<0>(STRICT_THREE) && |
|
70 ExhaustiveTest<1>(STRICT_THREE) && |
|
71 ExhaustiveTest<2>(STRICT_THREE) && |
|
72 ExhaustiveTest<3>(STRICT_THREE) && |
|
73 ExhaustiveTest<4>(STRICT_THREE) && |
|
74 ExhaustiveTest<5>(STRICT_THREE); |
|
75 } |
|
76 |
|
77 static const size_t MAX_ELEMS = 6; |
|
78 |
|
79 template<size_t ArgCount> bool |
|
80 ExhaustiveTest(const char funcode[]) |
|
81 { |
|
82 RootedValue v(cx); |
|
83 EVAL(funcode, &v); |
|
84 |
|
85 EVAL(CALL_CODES[ArgCount], &v); |
|
86 Rooted<ArgumentsObject*> argsobj(cx, &JSVAL_TO_OBJECT(v)->as<ArgumentsObject>()); |
|
87 |
|
88 JS::AutoValueArray<MAX_ELEMS> elems(cx); |
|
89 |
|
90 for (size_t i = 0; i <= ArgCount; i++) { |
|
91 for (size_t j = 0; j <= ArgCount - i; j++) { |
|
92 ClearElements(elems); |
|
93 CHECK(argsobj->maybeGetElements(i, j, elems.begin())); |
|
94 for (size_t k = 0; k < j; k++) |
|
95 CHECK_SAME(elems[k], INT_TO_JSVAL(i + k)); |
|
96 for (size_t k = j; k < MAX_ELEMS - 1; k++) |
|
97 CHECK_SAME(elems[k], JSVAL_NULL); |
|
98 CHECK_SAME(elems[MAX_ELEMS - 1], INT_TO_JSVAL(42)); |
|
99 } |
|
100 } |
|
101 |
|
102 return true; |
|
103 } |
|
104 |
|
105 template <size_t N> |
|
106 static void |
|
107 ClearElements(JS::AutoValueArray<N> &elems) |
|
108 { |
|
109 for (size_t i = 0; i < elems.length() - 1; i++) |
|
110 elems[i].setNull(); |
|
111 elems[elems.length() - 1].setInt32(42); |
|
112 } |
|
113 END_TEST(testArgumentsObject) |