Bug 1716030 [wpt PR 29346] - Update wpt metadata, a=testonly
[gecko.git] / js / src / jsapi-tests / testAddPropertyPropcache.cpp
blob0be8a9d99c419a54f096c250ba1616e968d93f56
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, // hasInstance
30 nullptr, // construct
31 nullptr, // trace
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
40 * miss the propcache.
42 static const int ExpectedCount = 100;
44 JS::RootedObject obj(cx, JS_NewPlainObject(cx));
45 CHECK(obj);
46 JS::RootedValue proto(cx, JS::ObjectValue(*obj));
47 JS_InitClass(cx, global, obj, &AddPropertyClass, nullptr, 0, nullptr, nullptr,
48 nullptr, nullptr);
50 obj = JS::NewArrayObject(cx, 0);
51 CHECK(obj);
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);
59 CHECK(obj);
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.
65 EXEC(
66 "'use strict'; \n"
67 "for (var i = 0; i < arr.length; ++i) \n"
68 " arr[i].prop = 42; \n");
70 CHECK(callCount == ExpectedCount);
72 return true;
74 END_TEST(testAddPropertyHook)