Imported Upstream version 1.0.0
[platform/upstream/js.git] / js / src / jsapi-tests / testCustomIterator.cpp
1 #include "tests.h"
2
3 #include "jsvalue.h"
4
5 int count = 0;
6
7 static JSBool
8 IterNext(JSContext *cx, uintN argc, jsval *vp)
9 {
10     if (count++ == 100)
11         return JS_ThrowStopIteration(cx);
12     JS_SET_RVAL(cx, vp, INT_TO_JSVAL(count));
13     return true;
14 }
15
16 static JSObject *
17 IterHook(JSContext *cx, JSObject *obj, JSBool keysonly)
18 {
19     JSObject *iterObj = JS_NewObject(cx, NULL, NULL, NULL);
20     if (!iterObj)
21         return NULL;
22     if (!JS_DefineFunction(cx, iterObj, "next", IterNext, 0, 0))
23         return NULL;
24     return iterObj;
25 }
26
27 js::Class HasCustomIterClass = {
28     "HasCustomIter",
29     0,
30     js::PropertyStub,
31     js::PropertyStub,
32     js::PropertyStub,
33     js::StrictPropertyStub,
34     js::EnumerateStub,
35     js::ResolveStub,
36     js::ConvertStub,
37     NULL,
38     NULL, /* reserved0 */
39     NULL, /* checkAccess */
40     NULL, /* call */
41     NULL, /* construct */
42     NULL, /* xdrObject */
43     NULL, /* hasInstance */
44     NULL, /* mark */
45     {
46         NULL,
47         NULL,
48         NULL,
49         IterHook,
50         NULL
51     }
52 };
53
54 JSBool
55 IterClassConstructor(JSContext *cx, uintN argc, jsval *vp)
56 {
57     JSObject *obj = JS_NewObjectForConstructor(cx, vp);
58     if (!obj)
59         return false;
60     JS_SET_RVAL(cx, vp, OBJECT_TO_JSVAL(obj));
61     return true;
62 }
63
64 BEGIN_TEST(testCustomIterator_bug612523)
65 {
66     CHECK(JS_InitClass(cx, JS_GetGlobalObject(cx), NULL, Jsvalify(&HasCustomIterClass),
67                        IterClassConstructor, 0, NULL, NULL, NULL, NULL));
68
69     jsval result;
70     EVAL("var o = new HasCustomIter(); \n"
71          "var j = 0; \n"
72          "for (var i in o) { ++j; }; \n"
73          "j;", &result);
74
75     CHECK(JSVAL_IS_INT(result));
76     CHECK(JSVAL_TO_INT(result) == 100);
77     CHECK(count == 101);
78
79     return true;
80 }
81 END_TEST(testCustomIterator_bug612523)