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 | * Tests the JSClass::getProperty hook |
michael@0 | 5 | */ |
michael@0 | 6 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 7 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 8 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 9 | |
michael@0 | 10 | #include "jsapi-tests/tests.h" |
michael@0 | 11 | |
michael@0 | 12 | static int called_test_fn; |
michael@0 | 13 | static int called_test_prop_get; |
michael@0 | 14 | |
michael@0 | 15 | static bool test_prop_get( JSContext *cx, JS::HandleObject obj, JS::HandleId id, JS::MutableHandleValue vp ) |
michael@0 | 16 | { |
michael@0 | 17 | called_test_prop_get++; |
michael@0 | 18 | return true; |
michael@0 | 19 | } |
michael@0 | 20 | |
michael@0 | 21 | static bool |
michael@0 | 22 | PTest(JSContext* cx, unsigned argc, jsval *vp); |
michael@0 | 23 | |
michael@0 | 24 | static const JSClass ptestClass = { |
michael@0 | 25 | "PTest", |
michael@0 | 26 | JSCLASS_HAS_PRIVATE, |
michael@0 | 27 | |
michael@0 | 28 | JS_PropertyStub, // add |
michael@0 | 29 | JS_DeletePropertyStub, // delete |
michael@0 | 30 | test_prop_get, // get |
michael@0 | 31 | JS_StrictPropertyStub, // set |
michael@0 | 32 | JS_EnumerateStub, |
michael@0 | 33 | JS_ResolveStub, |
michael@0 | 34 | JS_ConvertStub |
michael@0 | 35 | }; |
michael@0 | 36 | |
michael@0 | 37 | static bool |
michael@0 | 38 | PTest(JSContext* cx, unsigned argc, jsval *vp) |
michael@0 | 39 | { |
michael@0 | 40 | JS::CallArgs args = JS::CallArgsFromVp(argc, vp); |
michael@0 | 41 | JSObject *obj = JS_NewObjectForConstructor(cx, &ptestClass, args); |
michael@0 | 42 | if (!obj) |
michael@0 | 43 | return false; |
michael@0 | 44 | args.rval().setObject(*obj); |
michael@0 | 45 | return true; |
michael@0 | 46 | } |
michael@0 | 47 | static bool test_fn(JSContext *cx, unsigned argc, jsval *vp) |
michael@0 | 48 | { |
michael@0 | 49 | called_test_fn++; |
michael@0 | 50 | return true; |
michael@0 | 51 | } |
michael@0 | 52 | |
michael@0 | 53 | static const JSFunctionSpec ptestFunctions[] = { |
michael@0 | 54 | JS_FS( "test_fn", test_fn, 0, 0 ), |
michael@0 | 55 | JS_FS_END |
michael@0 | 56 | }; |
michael@0 | 57 | |
michael@0 | 58 | BEGIN_TEST(testClassGetter_isCalled) |
michael@0 | 59 | { |
michael@0 | 60 | CHECK(JS_InitClass(cx, global, js::NullPtr(), &ptestClass, PTest, 0, |
michael@0 | 61 | nullptr, ptestFunctions, nullptr, nullptr)); |
michael@0 | 62 | |
michael@0 | 63 | EXEC("function check() { var o = new PTest(); o.test_fn(); o.test_value1; o.test_value2; o.test_value1; }"); |
michael@0 | 64 | |
michael@0 | 65 | for (int i = 1; i < 9; i++) { |
michael@0 | 66 | JS::RootedValue rval(cx); |
michael@0 | 67 | CHECK(JS_CallFunctionName(cx, global, "check", JS::HandleValueArray::empty(), |
michael@0 | 68 | &rval)); |
michael@0 | 69 | CHECK_SAME(INT_TO_JSVAL(called_test_fn), INT_TO_JSVAL(i)); |
michael@0 | 70 | CHECK_SAME(INT_TO_JSVAL(called_test_prop_get), INT_TO_JSVAL(4 * i)); |
michael@0 | 71 | } |
michael@0 | 72 | return true; |
michael@0 | 73 | } |
michael@0 | 74 | END_TEST(testClassGetter_isCalled) |