Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- |
michael@0 | 2 | * vim: set ts=8 sts=4 et sw=4 tw=99: |
michael@0 | 3 | */ |
michael@0 | 4 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 5 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 6 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 7 | |
michael@0 | 8 | #include "jsapi-tests/tests.h" |
michael@0 | 9 | |
michael@0 | 10 | static int callCount = 0; |
michael@0 | 11 | |
michael@0 | 12 | static bool |
michael@0 | 13 | AddProperty(JSContext *cx, JS::HandleObject obj, JS::HandleId id, JS::MutableHandleValue vp) |
michael@0 | 14 | { |
michael@0 | 15 | callCount++; |
michael@0 | 16 | return true; |
michael@0 | 17 | } |
michael@0 | 18 | |
michael@0 | 19 | static const JSClass AddPropertyClass = { |
michael@0 | 20 | "AddPropertyTester", |
michael@0 | 21 | 0, |
michael@0 | 22 | AddProperty, |
michael@0 | 23 | JS_DeletePropertyStub, /* delProperty */ |
michael@0 | 24 | JS_PropertyStub, /* getProperty */ |
michael@0 | 25 | JS_StrictPropertyStub, /* setProperty */ |
michael@0 | 26 | JS_EnumerateStub, |
michael@0 | 27 | JS_ResolveStub, |
michael@0 | 28 | JS_ConvertStub |
michael@0 | 29 | }; |
michael@0 | 30 | |
michael@0 | 31 | BEGIN_TEST(testAddPropertyHook) |
michael@0 | 32 | { |
michael@0 | 33 | /* |
michael@0 | 34 | * Do the test a bunch of times, because sometimes we seem to randomly |
michael@0 | 35 | * miss the propcache. |
michael@0 | 36 | */ |
michael@0 | 37 | static const int ExpectedCount = 100; |
michael@0 | 38 | |
michael@0 | 39 | JS::RootedObject obj(cx, JS_NewObject(cx, nullptr, JS::NullPtr(), JS::NullPtr())); |
michael@0 | 40 | CHECK(obj); |
michael@0 | 41 | JS::RootedValue proto(cx, OBJECT_TO_JSVAL(obj)); |
michael@0 | 42 | JS_InitClass(cx, global, obj, &AddPropertyClass, nullptr, 0, nullptr, nullptr, nullptr, |
michael@0 | 43 | nullptr); |
michael@0 | 44 | |
michael@0 | 45 | obj = JS_NewArrayObject(cx, 0); |
michael@0 | 46 | CHECK(obj); |
michael@0 | 47 | JS::RootedValue arr(cx, OBJECT_TO_JSVAL(obj)); |
michael@0 | 48 | |
michael@0 | 49 | CHECK(JS_DefineProperty(cx, global, "arr", arr, JSPROP_ENUMERATE, |
michael@0 | 50 | JS_PropertyStub, JS_StrictPropertyStub)); |
michael@0 | 51 | |
michael@0 | 52 | for (int i = 0; i < ExpectedCount; ++i) { |
michael@0 | 53 | obj = JS_NewObject(cx, &AddPropertyClass, JS::NullPtr(), JS::NullPtr()); |
michael@0 | 54 | CHECK(obj); |
michael@0 | 55 | JS::RootedValue vobj(cx, OBJECT_TO_JSVAL(obj)); |
michael@0 | 56 | JS::RootedObject arrObj(cx, JSVAL_TO_OBJECT(arr)); |
michael@0 | 57 | CHECK(JS_DefineElement(cx, arrObj, i, vobj, |
michael@0 | 58 | JS_PropertyStub, JS_StrictPropertyStub, |
michael@0 | 59 | JSPROP_ENUMERATE)); |
michael@0 | 60 | } |
michael@0 | 61 | |
michael@0 | 62 | // Now add a prop to each of the objects, but make sure to do |
michael@0 | 63 | // so at the same bytecode location so we can hit the propcache. |
michael@0 | 64 | EXEC("'use strict'; \n" |
michael@0 | 65 | "for (var i = 0; i < arr.length; ++i) \n" |
michael@0 | 66 | " arr[i].prop = 42; \n" |
michael@0 | 67 | ); |
michael@0 | 68 | |
michael@0 | 69 | CHECK(callCount == ExpectedCount); |
michael@0 | 70 | |
michael@0 | 71 | return true; |
michael@0 | 72 | } |
michael@0 | 73 | END_TEST(testAddPropertyHook) |
michael@0 | 74 |