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: * Tests the JSClass::getProperty hook 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 called_test_fn; michael@0: static int called_test_prop_get; michael@0: michael@0: static bool test_prop_get( JSContext *cx, JS::HandleObject obj, JS::HandleId id, JS::MutableHandleValue vp ) michael@0: { michael@0: called_test_prop_get++; michael@0: return true; michael@0: } michael@0: michael@0: static bool michael@0: PTest(JSContext* cx, unsigned argc, jsval *vp); michael@0: michael@0: static const JSClass ptestClass = { michael@0: "PTest", michael@0: JSCLASS_HAS_PRIVATE, michael@0: michael@0: JS_PropertyStub, // add michael@0: JS_DeletePropertyStub, // delete michael@0: test_prop_get, // get michael@0: JS_StrictPropertyStub, // set michael@0: JS_EnumerateStub, michael@0: JS_ResolveStub, michael@0: JS_ConvertStub michael@0: }; michael@0: michael@0: static bool michael@0: PTest(JSContext* cx, unsigned argc, jsval *vp) michael@0: { michael@0: JS::CallArgs args = JS::CallArgsFromVp(argc, vp); michael@0: JSObject *obj = JS_NewObjectForConstructor(cx, &ptestClass, args); michael@0: if (!obj) michael@0: return false; michael@0: args.rval().setObject(*obj); michael@0: return true; michael@0: } michael@0: static bool test_fn(JSContext *cx, unsigned argc, jsval *vp) michael@0: { michael@0: called_test_fn++; michael@0: return true; michael@0: } michael@0: michael@0: static const JSFunctionSpec ptestFunctions[] = { michael@0: JS_FS( "test_fn", test_fn, 0, 0 ), michael@0: JS_FS_END michael@0: }; michael@0: michael@0: BEGIN_TEST(testClassGetter_isCalled) michael@0: { michael@0: CHECK(JS_InitClass(cx, global, js::NullPtr(), &ptestClass, PTest, 0, michael@0: nullptr, ptestFunctions, nullptr, nullptr)); michael@0: michael@0: EXEC("function check() { var o = new PTest(); o.test_fn(); o.test_value1; o.test_value2; o.test_value1; }"); michael@0: michael@0: for (int i = 1; i < 9; i++) { michael@0: JS::RootedValue rval(cx); michael@0: CHECK(JS_CallFunctionName(cx, global, "check", JS::HandleValueArray::empty(), michael@0: &rval)); michael@0: CHECK_SAME(INT_TO_JSVAL(called_test_fn), INT_TO_JSVAL(i)); michael@0: CHECK_SAME(INT_TO_JSVAL(called_test_prop_get), INT_TO_JSVAL(4 * i)); michael@0: } michael@0: return true; michael@0: } michael@0: END_TEST(testClassGetter_isCalled)