js/src/jsapi-tests/testAddPropertyPropcache.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/js/src/jsapi-tests/testAddPropertyPropcache.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,74 @@
     1.4 +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
     1.5 + * vim: set ts=8 sts=4 et sw=4 tw=99:
     1.6 + */
     1.7 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.8 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.9 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
    1.10 +
    1.11 +#include "jsapi-tests/tests.h"
    1.12 +
    1.13 +static int callCount = 0;
    1.14 +
    1.15 +static bool
    1.16 +AddProperty(JSContext *cx, JS::HandleObject obj, JS::HandleId id, JS::MutableHandleValue vp)
    1.17 +{
    1.18 +    callCount++;
    1.19 +    return true;
    1.20 +}
    1.21 +
    1.22 +static const JSClass AddPropertyClass = {
    1.23 +    "AddPropertyTester",
    1.24 +    0,
    1.25 +    AddProperty,
    1.26 +    JS_DeletePropertyStub,   /* delProperty */
    1.27 +    JS_PropertyStub,         /* getProperty */
    1.28 +    JS_StrictPropertyStub,   /* setProperty */
    1.29 +    JS_EnumerateStub,
    1.30 +    JS_ResolveStub,
    1.31 +    JS_ConvertStub
    1.32 +};
    1.33 +
    1.34 +BEGIN_TEST(testAddPropertyHook)
    1.35 +{
    1.36 +    /*
    1.37 +     * Do the test a bunch of times, because sometimes we seem to randomly
    1.38 +     * miss the propcache.
    1.39 +     */
    1.40 +    static const int ExpectedCount = 100;
    1.41 +
    1.42 +    JS::RootedObject obj(cx, JS_NewObject(cx, nullptr, JS::NullPtr(), JS::NullPtr()));
    1.43 +    CHECK(obj);
    1.44 +    JS::RootedValue proto(cx, OBJECT_TO_JSVAL(obj));
    1.45 +    JS_InitClass(cx, global, obj, &AddPropertyClass, nullptr, 0, nullptr, nullptr, nullptr,
    1.46 +                 nullptr);
    1.47 +
    1.48 +    obj = JS_NewArrayObject(cx, 0);
    1.49 +    CHECK(obj);
    1.50 +    JS::RootedValue arr(cx, OBJECT_TO_JSVAL(obj));
    1.51 +
    1.52 +    CHECK(JS_DefineProperty(cx, global, "arr", arr, JSPROP_ENUMERATE,
    1.53 +                            JS_PropertyStub, JS_StrictPropertyStub));
    1.54 +
    1.55 +    for (int i = 0; i < ExpectedCount; ++i) {
    1.56 +        obj = JS_NewObject(cx, &AddPropertyClass, JS::NullPtr(), JS::NullPtr());
    1.57 +        CHECK(obj);
    1.58 +        JS::RootedValue vobj(cx, OBJECT_TO_JSVAL(obj));
    1.59 +        JS::RootedObject arrObj(cx, JSVAL_TO_OBJECT(arr));
    1.60 +        CHECK(JS_DefineElement(cx, arrObj, i, vobj,
    1.61 +                               JS_PropertyStub, JS_StrictPropertyStub,
    1.62 +                               JSPROP_ENUMERATE));
    1.63 +    }
    1.64 +
    1.65 +    // Now add a prop to each of the objects, but make sure to do
    1.66 +    // so at the same bytecode location so we can hit the propcache.
    1.67 +    EXEC("'use strict';                                     \n"
    1.68 +         "for (var i = 0; i < arr.length; ++i)              \n"
    1.69 +         "  arr[i].prop = 42;                               \n"
    1.70 +         );
    1.71 +
    1.72 +    CHECK(callCount == ExpectedCount);
    1.73 +
    1.74 +    return true;
    1.75 +}
    1.76 +END_TEST(testAddPropertyHook)
    1.77 +

mercurial