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
/* 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 "js/Class.h"
#include "jsapi-tests/tests.h"

static int iterCount = 0;

static bool
IterNext(JSContext *cx, unsigned argc, jsval *vp)
{
    if (iterCount++ == 100)
        return JS_ThrowStopIteration(cx);
    JS_SET_RVAL(cx, vp, INT_TO_JSVAL(iterCount));
    return true;
}

static JSObject *
IterHook(JSContext *cx, JS::HandleObject obj, bool keysonly)
{
    JS::RootedObject iterObj(cx, JS_NewObject(cx, nullptr, nullptr, nullptr));
    if (!iterObj)
        return nullptr;
    if (!JS_DefineFunction(cx, iterObj, "next", IterNext, 0, 0))
        return nullptr;
    return iterObj;
}

const js::Class HasCustomIterClass = {
    "HasCustomIter",
    0,
    JS_PropertyStub,
    JS_DeletePropertyStub,
    JS_PropertyStub,
    JS_StrictPropertyStub,
    JS_EnumerateStub,
    JS_ResolveStub,
    JS_ConvertStub,
    nullptr,
    nullptr, /* checkAccess */
    nullptr, /* call */
    nullptr, /* hasInstance */
    nullptr, /* construct */
    nullptr, /* mark */
    {
        nullptr,     /* outerObject */
        nullptr,     /* innerObject */
        IterHook,
        false        /* isWrappedNative */
    }
};

static bool
IterClassConstructor(JSContext *cx, unsigned argc, jsval *vp)
{
    JSObject *obj = JS_NewObjectForConstructor(cx, Jsvalify(&HasCustomIterClass), vp);
    if (!obj)
        return false;
    JS_SET_RVAL(cx, vp, OBJECT_TO_JSVAL(obj));
    return true;
}

BEGIN_TEST(testCustomIterator_bug612523)
{
    CHECK(JS_InitClass(cx, global, nullptr, Jsvalify(&HasCustomIterClass),
                       IterClassConstructor, 0, nullptr, nullptr, nullptr, nullptr));

    JS::RootedValue result(cx);
    EVAL("var o = new HasCustomIter(); \n"
         "var j = 0; \n"
         "for (var i in o) { ++j; }; \n"
         "j;", result.address());

    CHECK(JSVAL_IS_INT(result));
    CHECK_EQUAL(JSVAL_TO_INT(result), 100);
    CHECK_EQUAL(iterCount, 101);

    return true;
}
END_TEST(testCustomIterator_bug612523)