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:
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
,
20 static const JSClassOps AddPropertyClassOps
= {
21 AddProperty
, // addProperty
22 nullptr, // delProperty
24 nullptr, // newEnumerate
26 nullptr, // mayResolve
29 nullptr, // hasInstance
34 static const JSClass AddPropertyClass
= {"AddPropertyTester", 0,
35 &AddPropertyClassOps
};
37 BEGIN_TEST(testAddPropertyHook
) {
39 * Do the test a bunch of times, because sometimes we seem to randomly
42 static const int ExpectedCount
= 100;
44 JS::RootedObject
obj(cx
, JS_NewPlainObject(cx
));
46 JS::RootedValue
proto(cx
, JS::ObjectValue(*obj
));
47 JS_InitClass(cx
, global
, obj
, &AddPropertyClass
, nullptr, 0, nullptr, nullptr,
50 obj
= JS::NewArrayObject(cx
, 0);
52 JS::RootedValue
arr(cx
, JS::ObjectValue(*obj
));
54 CHECK(JS_DefineProperty(cx
, global
, "arr", arr
, JSPROP_ENUMERATE
));
56 JS::RootedObject
arrObj(cx
, &arr
.toObject());
57 for (int i
= 0; i
< ExpectedCount
; ++i
) {
58 obj
= JS_NewObject(cx
, &AddPropertyClass
);
60 CHECK(JS_DefineElement(cx
, arrObj
, i
, obj
, JSPROP_ENUMERATE
));
63 // Now add a prop to each of the objects, but make sure to do
64 // so at the same bytecode location so we can hit the propcache.
67 "for (var i = 0; i < arr.length; ++i) \n"
68 " arr[i].prop = 42; \n");
70 CHECK(callCount
== ExpectedCount
);
74 END_TEST(testAddPropertyHook
)