js/src/jsapi-tests/testAddPropertyPropcache.cpp

branch
TOR_BUG_9701
changeset 15
b8a032363ba2
equal deleted inserted replaced
-1:000000000000 0:3bcad258cfa3
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 * vim: set ts=8 sts=4 et sw=4 tw=99:
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 "jsapi-tests/tests.h"
9
10 static int callCount = 0;
11
12 static bool
13 AddProperty(JSContext *cx, JS::HandleObject obj, JS::HandleId id, JS::MutableHandleValue vp)
14 {
15 callCount++;
16 return true;
17 }
18
19 static const JSClass AddPropertyClass = {
20 "AddPropertyTester",
21 0,
22 AddProperty,
23 JS_DeletePropertyStub, /* delProperty */
24 JS_PropertyStub, /* getProperty */
25 JS_StrictPropertyStub, /* setProperty */
26 JS_EnumerateStub,
27 JS_ResolveStub,
28 JS_ConvertStub
29 };
30
31 BEGIN_TEST(testAddPropertyHook)
32 {
33 /*
34 * Do the test a bunch of times, because sometimes we seem to randomly
35 * miss the propcache.
36 */
37 static const int ExpectedCount = 100;
38
39 JS::RootedObject obj(cx, JS_NewObject(cx, nullptr, JS::NullPtr(), JS::NullPtr()));
40 CHECK(obj);
41 JS::RootedValue proto(cx, OBJECT_TO_JSVAL(obj));
42 JS_InitClass(cx, global, obj, &AddPropertyClass, nullptr, 0, nullptr, nullptr, nullptr,
43 nullptr);
44
45 obj = JS_NewArrayObject(cx, 0);
46 CHECK(obj);
47 JS::RootedValue arr(cx, OBJECT_TO_JSVAL(obj));
48
49 CHECK(JS_DefineProperty(cx, global, "arr", arr, JSPROP_ENUMERATE,
50 JS_PropertyStub, JS_StrictPropertyStub));
51
52 for (int i = 0; i < ExpectedCount; ++i) {
53 obj = JS_NewObject(cx, &AddPropertyClass, JS::NullPtr(), JS::NullPtr());
54 CHECK(obj);
55 JS::RootedValue vobj(cx, OBJECT_TO_JSVAL(obj));
56 JS::RootedObject arrObj(cx, JSVAL_TO_OBJECT(arr));
57 CHECK(JS_DefineElement(cx, arrObj, i, vobj,
58 JS_PropertyStub, JS_StrictPropertyStub,
59 JSPROP_ENUMERATE));
60 }
61
62 // Now add a prop to each of the objects, but make sure to do
63 // so at the same bytecode location so we can hit the propcache.
64 EXEC("'use strict'; \n"
65 "for (var i = 0; i < arr.length; ++i) \n"
66 " arr[i].prop = 42; \n"
67 );
68
69 CHECK(callCount == ExpectedCount);
70
71 return true;
72 }
73 END_TEST(testAddPropertyHook)
74

mercurial