js/src/jsapi-tests/testCustomIterator.cpp

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

michael@0 1 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 2 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 4
michael@0 5 #include "js/Class.h"
michael@0 6 #include "jsapi-tests/tests.h"
michael@0 7
michael@0 8 static int iterCount = 0;
michael@0 9
michael@0 10 static bool
michael@0 11 IterNext(JSContext *cx, unsigned argc, jsval *vp)
michael@0 12 {
michael@0 13 JS::CallArgs args = JS::CallArgsFromVp(argc, vp);
michael@0 14 if (iterCount++ == 100)
michael@0 15 return JS_ThrowStopIteration(cx);
michael@0 16 args.rval().setInt32(iterCount);
michael@0 17 return true;
michael@0 18 }
michael@0 19
michael@0 20 static JSObject *
michael@0 21 IterHook(JSContext *cx, JS::HandleObject obj, bool keysonly)
michael@0 22 {
michael@0 23 JS::RootedObject iterObj(cx, JS_NewObject(cx, nullptr, JS::NullPtr(), JS::NullPtr()));
michael@0 24 if (!iterObj)
michael@0 25 return nullptr;
michael@0 26 if (!JS_DefineFunction(cx, iterObj, "next", IterNext, 0, 0))
michael@0 27 return nullptr;
michael@0 28 return iterObj;
michael@0 29 }
michael@0 30
michael@0 31 const js::Class HasCustomIterClass = {
michael@0 32 "HasCustomIter",
michael@0 33 0,
michael@0 34 JS_PropertyStub,
michael@0 35 JS_DeletePropertyStub,
michael@0 36 JS_PropertyStub,
michael@0 37 JS_StrictPropertyStub,
michael@0 38 JS_EnumerateStub,
michael@0 39 JS_ResolveStub,
michael@0 40 JS_ConvertStub,
michael@0 41 nullptr,
michael@0 42 nullptr, /* call */
michael@0 43 nullptr, /* hasInstance */
michael@0 44 nullptr, /* construct */
michael@0 45 nullptr, /* mark */
michael@0 46 JS_NULL_CLASS_SPEC,
michael@0 47 {
michael@0 48 nullptr, /* outerObject */
michael@0 49 nullptr, /* innerObject */
michael@0 50 IterHook,
michael@0 51 false /* isWrappedNative */
michael@0 52 }
michael@0 53 };
michael@0 54
michael@0 55 static bool
michael@0 56 IterClassConstructor(JSContext *cx, unsigned argc, jsval *vp)
michael@0 57 {
michael@0 58 JS::CallArgs args = JS::CallArgsFromVp(argc, vp);
michael@0 59 JSObject *obj = JS_NewObjectForConstructor(cx, Jsvalify(&HasCustomIterClass), args);
michael@0 60 if (!obj)
michael@0 61 return false;
michael@0 62 args.rval().setObject(*obj);
michael@0 63 return true;
michael@0 64 }
michael@0 65
michael@0 66 BEGIN_TEST(testCustomIterator_bug612523)
michael@0 67 {
michael@0 68 CHECK(JS_InitClass(cx, global, js::NullPtr(), Jsvalify(&HasCustomIterClass),
michael@0 69 IterClassConstructor, 0, nullptr, nullptr, nullptr, nullptr));
michael@0 70
michael@0 71 JS::RootedValue result(cx);
michael@0 72 EVAL("var o = new HasCustomIter(); \n"
michael@0 73 "var j = 0; \n"
michael@0 74 "for (var i in o) { ++j; }; \n"
michael@0 75 "j;", &result);
michael@0 76
michael@0 77 CHECK(JSVAL_IS_INT(result));
michael@0 78 CHECK_EQUAL(JSVAL_TO_INT(result), 100);
michael@0 79 CHECK_EQUAL(iterCount, 101);
michael@0 80
michael@0 81 return true;
michael@0 82 }
michael@0 83 END_TEST(testCustomIterator_bug612523)

mercurial