1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/js/src/jsapi-tests/testCustomIterator.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,83 @@ 1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.7 + 1.8 +#include "js/Class.h" 1.9 +#include "jsapi-tests/tests.h" 1.10 + 1.11 +static int iterCount = 0; 1.12 + 1.13 +static bool 1.14 +IterNext(JSContext *cx, unsigned argc, jsval *vp) 1.15 +{ 1.16 + JS::CallArgs args = JS::CallArgsFromVp(argc, vp); 1.17 + if (iterCount++ == 100) 1.18 + return JS_ThrowStopIteration(cx); 1.19 + args.rval().setInt32(iterCount); 1.20 + return true; 1.21 +} 1.22 + 1.23 +static JSObject * 1.24 +IterHook(JSContext *cx, JS::HandleObject obj, bool keysonly) 1.25 +{ 1.26 + JS::RootedObject iterObj(cx, JS_NewObject(cx, nullptr, JS::NullPtr(), JS::NullPtr())); 1.27 + if (!iterObj) 1.28 + return nullptr; 1.29 + if (!JS_DefineFunction(cx, iterObj, "next", IterNext, 0, 0)) 1.30 + return nullptr; 1.31 + return iterObj; 1.32 +} 1.33 + 1.34 +const js::Class HasCustomIterClass = { 1.35 + "HasCustomIter", 1.36 + 0, 1.37 + JS_PropertyStub, 1.38 + JS_DeletePropertyStub, 1.39 + JS_PropertyStub, 1.40 + JS_StrictPropertyStub, 1.41 + JS_EnumerateStub, 1.42 + JS_ResolveStub, 1.43 + JS_ConvertStub, 1.44 + nullptr, 1.45 + nullptr, /* call */ 1.46 + nullptr, /* hasInstance */ 1.47 + nullptr, /* construct */ 1.48 + nullptr, /* mark */ 1.49 + JS_NULL_CLASS_SPEC, 1.50 + { 1.51 + nullptr, /* outerObject */ 1.52 + nullptr, /* innerObject */ 1.53 + IterHook, 1.54 + false /* isWrappedNative */ 1.55 + } 1.56 +}; 1.57 + 1.58 +static bool 1.59 +IterClassConstructor(JSContext *cx, unsigned argc, jsval *vp) 1.60 +{ 1.61 + JS::CallArgs args = JS::CallArgsFromVp(argc, vp); 1.62 + JSObject *obj = JS_NewObjectForConstructor(cx, Jsvalify(&HasCustomIterClass), args); 1.63 + if (!obj) 1.64 + return false; 1.65 + args.rval().setObject(*obj); 1.66 + return true; 1.67 +} 1.68 + 1.69 +BEGIN_TEST(testCustomIterator_bug612523) 1.70 +{ 1.71 + CHECK(JS_InitClass(cx, global, js::NullPtr(), Jsvalify(&HasCustomIterClass), 1.72 + IterClassConstructor, 0, nullptr, nullptr, nullptr, nullptr)); 1.73 + 1.74 + JS::RootedValue result(cx); 1.75 + EVAL("var o = new HasCustomIter(); \n" 1.76 + "var j = 0; \n" 1.77 + "for (var i in o) { ++j; }; \n" 1.78 + "j;", &result); 1.79 + 1.80 + CHECK(JSVAL_IS_INT(result)); 1.81 + CHECK_EQUAL(JSVAL_TO_INT(result), 100); 1.82 + CHECK_EQUAL(iterCount, 101); 1.83 + 1.84 + return true; 1.85 +} 1.86 +END_TEST(testCustomIterator_bug612523)