js/src/jsapi-tests/testAddPropertyPropcache.cpp

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

     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/. */
     8 #include "jsapi-tests/tests.h"
    10 static int callCount = 0;
    12 static bool
    13 AddProperty(JSContext *cx, JS::HandleObject obj, JS::HandleId id, JS::MutableHandleValue vp)
    14 {
    15     callCount++;
    16     return true;
    17 }
    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 };
    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;
    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);
    45     obj = JS_NewArrayObject(cx, 0);
    46     CHECK(obj);
    47     JS::RootedValue arr(cx, OBJECT_TO_JSVAL(obj));
    49     CHECK(JS_DefineProperty(cx, global, "arr", arr, JSPROP_ENUMERATE,
    50                             JS_PropertyStub, JS_StrictPropertyStub));
    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     }
    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          );
    69     CHECK(callCount == ExpectedCount);
    71     return true;
    72 }
    73 END_TEST(testAddPropertyHook)

mercurial