Bug 1832850 - Part 5: Move the allocateObject definition into gc/Nursery.h r=jandem
[gecko.git] / js / src / jsapi-tests / testAddPropertyPropcache.cpp
blob7fa1e86df9d197f916aff4c37dd6cea87c72a001
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/. */
8 #include "js/Array.h" // JS::NewArrayObject
9 #include "js/PropertyAndElement.h" // JS_DefineElement, JS_DefineProperty
10 #include "jsapi-tests/tests.h"
12 static int callCount = 0;
14 static bool AddProperty(JSContext* cx, JS::HandleObject obj, JS::HandleId id,
15 JS::HandleValue v) {
16 callCount++;
17 return true;
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
33 static const JSClass AddPropertyClass = {"AddPropertyTester", 0,
34 &AddPropertyClassOps};
36 BEGIN_TEST(testAddPropertyHook) {
38 * Do the test a bunch of times, because sometimes we seem to randomly
39 * miss the propcache.
41 static const int ExpectedCount = 100;
43 JS::RootedObject obj(cx, JS::NewArrayObject(cx, 0));
44 CHECK(obj);
45 JS::RootedValue arr(cx, JS::ObjectValue(*obj));
47 CHECK(JS_DefineProperty(cx, global, "arr", arr, JSPROP_ENUMERATE));
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));
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");
63 CHECK(callCount == ExpectedCount);
65 return true;
67 END_TEST(testAddPropertyHook)