michael@0: /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- michael@0: * vim: set ts=8 sts=4 et sw=4 tw=99: michael@0: */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "jsapi-tests/tests.h" michael@0: michael@0: static int callCount = 0; michael@0: michael@0: static bool michael@0: AddProperty(JSContext *cx, JS::HandleObject obj, JS::HandleId id, JS::MutableHandleValue vp) michael@0: { michael@0: callCount++; michael@0: return true; michael@0: } michael@0: michael@0: static const JSClass AddPropertyClass = { michael@0: "AddPropertyTester", michael@0: 0, michael@0: AddProperty, michael@0: JS_DeletePropertyStub, /* delProperty */ michael@0: JS_PropertyStub, /* getProperty */ michael@0: JS_StrictPropertyStub, /* setProperty */ michael@0: JS_EnumerateStub, michael@0: JS_ResolveStub, michael@0: JS_ConvertStub michael@0: }; michael@0: michael@0: BEGIN_TEST(testAddPropertyHook) michael@0: { michael@0: /* michael@0: * Do the test a bunch of times, because sometimes we seem to randomly michael@0: * miss the propcache. michael@0: */ michael@0: static const int ExpectedCount = 100; michael@0: michael@0: JS::RootedObject obj(cx, JS_NewObject(cx, nullptr, JS::NullPtr(), JS::NullPtr())); michael@0: CHECK(obj); michael@0: JS::RootedValue proto(cx, OBJECT_TO_JSVAL(obj)); michael@0: JS_InitClass(cx, global, obj, &AddPropertyClass, nullptr, 0, nullptr, nullptr, nullptr, michael@0: nullptr); michael@0: michael@0: obj = JS_NewArrayObject(cx, 0); michael@0: CHECK(obj); michael@0: JS::RootedValue arr(cx, OBJECT_TO_JSVAL(obj)); michael@0: michael@0: CHECK(JS_DefineProperty(cx, global, "arr", arr, JSPROP_ENUMERATE, michael@0: JS_PropertyStub, JS_StrictPropertyStub)); michael@0: michael@0: for (int i = 0; i < ExpectedCount; ++i) { michael@0: obj = JS_NewObject(cx, &AddPropertyClass, JS::NullPtr(), JS::NullPtr()); michael@0: CHECK(obj); michael@0: JS::RootedValue vobj(cx, OBJECT_TO_JSVAL(obj)); michael@0: JS::RootedObject arrObj(cx, JSVAL_TO_OBJECT(arr)); michael@0: CHECK(JS_DefineElement(cx, arrObj, i, vobj, michael@0: JS_PropertyStub, JS_StrictPropertyStub, michael@0: JSPROP_ENUMERATE)); michael@0: } michael@0: michael@0: // Now add a prop to each of the objects, but make sure to do michael@0: // so at the same bytecode location so we can hit the propcache. michael@0: EXEC("'use strict'; \n" michael@0: "for (var i = 0; i < arr.length; ++i) \n" michael@0: " arr[i].prop = 42; \n" michael@0: ); michael@0: michael@0: CHECK(callCount == ExpectedCount); michael@0: michael@0: return true; michael@0: } michael@0: END_TEST(testAddPropertyHook) michael@0: