DXR is a code search and navigation tool aimed at making sense of large projects. It supports full-text and regex searches as well as structural queries.

Mercurial (31ec81b5d7bb)

VCS Links

Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
 * vim: set ts=8 sts=4 et sw=4 tw=99:
 */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "jsapi-tests/tests.h"

#include "jsobjinlines.h"

#include "vm/ArgumentsObject-inl.h"

using namespace js;

static const char NORMAL_ZERO[] =
    "function f() { return arguments; }";
static const char NORMAL_ONE[] =
    "function f(a) { return arguments; }";
static const char NORMAL_TWO[] =
    "function f(a, b) { return arguments; }";
static const char NORMAL_THREE[] =
    "function f(a, b, c) { return arguments; }";

static const char STRICT_ZERO[] =
    "function f() { 'use strict'; return arguments; }";
static const char STRICT_ONE[] =
    "function f() { 'use strict'; return arguments; }";
static const char STRICT_TWO[] =
    "function f() { 'use strict'; return arguments; }";
static const char STRICT_THREE[] =
    "function f() { 'use strict'; return arguments; }";

static const char * const CALL_CODES[] =
    { "f()", "f(0)", "f(0, 1)", "f(0, 1, 2)", "f(0, 1, 2, 3)", "f(0, 1, 2, 3, 4)" };

BEGIN_TEST(testArgumentsObject)
{
    return ExhaustiveTest<0>(NORMAL_ZERO) &&
           ExhaustiveTest<1>(NORMAL_ZERO) &&
           ExhaustiveTest<2>(NORMAL_ZERO) &&
           ExhaustiveTest<0>(NORMAL_ONE) &&
           ExhaustiveTest<1>(NORMAL_ONE) &&
           ExhaustiveTest<2>(NORMAL_ONE) &&
           ExhaustiveTest<3>(NORMAL_ONE) &&
           ExhaustiveTest<0>(NORMAL_TWO) &&
           ExhaustiveTest<1>(NORMAL_TWO) &&
           ExhaustiveTest<2>(NORMAL_TWO) &&
           ExhaustiveTest<3>(NORMAL_TWO) &&
           ExhaustiveTest<4>(NORMAL_TWO) &&
           ExhaustiveTest<0>(NORMAL_THREE) &&
           ExhaustiveTest<1>(NORMAL_THREE) &&
           ExhaustiveTest<2>(NORMAL_THREE) &&
           ExhaustiveTest<3>(NORMAL_THREE) &&
           ExhaustiveTest<4>(NORMAL_THREE) &&
           ExhaustiveTest<5>(NORMAL_THREE) &&
           ExhaustiveTest<0>(STRICT_ZERO) &&
           ExhaustiveTest<1>(STRICT_ZERO) &&
           ExhaustiveTest<2>(STRICT_ZERO) &&
           ExhaustiveTest<0>(STRICT_ONE) &&
           ExhaustiveTest<1>(STRICT_ONE) &&
           ExhaustiveTest<2>(STRICT_ONE) &&
           ExhaustiveTest<3>(STRICT_ONE) &&
           ExhaustiveTest<0>(STRICT_TWO) &&
           ExhaustiveTest<1>(STRICT_TWO) &&
           ExhaustiveTest<2>(STRICT_TWO) &&
           ExhaustiveTest<3>(STRICT_TWO) &&
           ExhaustiveTest<4>(STRICT_TWO) &&
           ExhaustiveTest<0>(STRICT_THREE) &&
           ExhaustiveTest<1>(STRICT_THREE) &&
           ExhaustiveTest<2>(STRICT_THREE) &&
           ExhaustiveTest<3>(STRICT_THREE) &&
           ExhaustiveTest<4>(STRICT_THREE) &&
           ExhaustiveTest<5>(STRICT_THREE);
}

static const size_t MAX_ELEMS = 6;

template<size_t ArgCount> bool
ExhaustiveTest(const char funcode[])
{
    RootedValue v(cx);
    EVAL(funcode, v.address());

    EVAL(CALL_CODES[ArgCount], v.address());
    Rooted<ArgumentsObject*> argsobj(cx, &JSVAL_TO_OBJECT(v)->as<ArgumentsObject>());

    Value elems[MAX_ELEMS];

    for (size_t i = 0; i <= ArgCount; i++) {
        for (size_t j = 0; j <= ArgCount - i; j++) {
            ClearElements(elems);
            CHECK(argsobj->maybeGetElements(i, j, elems));
            for (size_t k = 0; k < j; k++)
                CHECK_SAME(elems[k], INT_TO_JSVAL(i + k));
            for (size_t k = j; k < MAX_ELEMS - 1; k++)
                CHECK_SAME(elems[k], JSVAL_NULL);
            CHECK_SAME(elems[MAX_ELEMS - 1], INT_TO_JSVAL(42));
        }
    }

    return true;
}

static void
ClearElements(Value elems[MAX_ELEMS])
{
    for (size_t i = 0; i < MAX_ELEMS - 1; i++)
        elems[i] = NullValue();
    elems[MAX_ELEMS - 1] = Int32Value(42);
}
END_TEST(testArgumentsObject)