testAddPropertyPropcache.cpp (2173B)
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- 2 * vim: set ts=8 sts=2 et sw=2 tw=80: 3 */ 4 /* This Source Code Form is subject to the terms of the Mozilla Public 5 * License, v. 2.0. If a copy of the MPL was not distributed with this 6 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 7 8 #include "js/Array.h" // JS::NewArrayObject 9 #include "js/PropertyAndElement.h" // JS_DefineElement, JS_DefineProperty 10 #include "jsapi-tests/tests.h" 11 12 static int callCount = 0; 13 14 static bool AddProperty(JSContext* cx, JS::HandleObject obj, JS::HandleId id, 15 JS::HandleValue v) { 16 callCount++; 17 return true; 18 } 19 20 static const JSClassOps AddPropertyClassOps = { 21 AddProperty, // addProperty 22 nullptr, // delProperty 23 nullptr, // enumerate 24 nullptr, // newEnumerate 25 nullptr, // resolve 26 nullptr, // mayResolve 27 nullptr, // finalize 28 nullptr, // call 29 nullptr, // construct 30 nullptr, // trace 31 }; 32 33 static const JSClass AddPropertyClass = {"AddPropertyTester", 0, 34 &AddPropertyClassOps}; 35 36 BEGIN_TEST(testAddPropertyHook) { 37 /* 38 * Do the test a bunch of times, because sometimes we seem to randomly 39 * miss the propcache. 40 */ 41 static const int ExpectedCount = 100; 42 43 JS::RootedObject obj(cx, JS::NewArrayObject(cx, 0)); 44 CHECK(obj); 45 JS::RootedValue arr(cx, JS::ObjectValue(*obj)); 46 47 CHECK(JS_DefineProperty(cx, global, "arr", arr, JSPROP_ENUMERATE)); 48 49 JS::RootedObject arrObj(cx, &arr.toObject()); 50 for (int i = 0; i < ExpectedCount; ++i) { 51 obj = JS_NewObject(cx, &AddPropertyClass); 52 CHECK(obj); 53 CHECK(JS_DefineElement(cx, arrObj, i, obj, JSPROP_ENUMERATE)); 54 } 55 56 // Now add a prop to each of the objects, but make sure to do 57 // so at the same bytecode location so we can hit the propcache. 58 EXEC( 59 "'use strict'; \n" 60 "for (var i = 0; i < arr.length; ++i) \n" 61 " arr[i].prop = 42; \n"); 62 63 CHECK(callCount == ExpectedCount); 64 65 return true; 66 } 67 END_TEST(testAddPropertyHook)