1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/js/src/jsapi-tests/testClassGetter.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,74 @@ 1.4 +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- 1.5 + * vim: set ts=8 sts=4 et sw=4 tw=99: 1.6 + * 1.7 + * Tests the JSClass::getProperty hook 1.8 + */ 1.9 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.10 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.11 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.12 + 1.13 +#include "jsapi-tests/tests.h" 1.14 + 1.15 +static int called_test_fn; 1.16 +static int called_test_prop_get; 1.17 + 1.18 +static bool test_prop_get( JSContext *cx, JS::HandleObject obj, JS::HandleId id, JS::MutableHandleValue vp ) 1.19 +{ 1.20 + called_test_prop_get++; 1.21 + return true; 1.22 +} 1.23 + 1.24 +static bool 1.25 +PTest(JSContext* cx, unsigned argc, jsval *vp); 1.26 + 1.27 +static const JSClass ptestClass = { 1.28 + "PTest", 1.29 + JSCLASS_HAS_PRIVATE, 1.30 + 1.31 + JS_PropertyStub, // add 1.32 + JS_DeletePropertyStub, // delete 1.33 + test_prop_get, // get 1.34 + JS_StrictPropertyStub, // set 1.35 + JS_EnumerateStub, 1.36 + JS_ResolveStub, 1.37 + JS_ConvertStub 1.38 +}; 1.39 + 1.40 +static bool 1.41 +PTest(JSContext* cx, unsigned argc, jsval *vp) 1.42 +{ 1.43 + JS::CallArgs args = JS::CallArgsFromVp(argc, vp); 1.44 + JSObject *obj = JS_NewObjectForConstructor(cx, &ptestClass, args); 1.45 + if (!obj) 1.46 + return false; 1.47 + args.rval().setObject(*obj); 1.48 + return true; 1.49 +} 1.50 +static bool test_fn(JSContext *cx, unsigned argc, jsval *vp) 1.51 +{ 1.52 + called_test_fn++; 1.53 + return true; 1.54 +} 1.55 + 1.56 +static const JSFunctionSpec ptestFunctions[] = { 1.57 + JS_FS( "test_fn", test_fn, 0, 0 ), 1.58 + JS_FS_END 1.59 +}; 1.60 + 1.61 +BEGIN_TEST(testClassGetter_isCalled) 1.62 +{ 1.63 + CHECK(JS_InitClass(cx, global, js::NullPtr(), &ptestClass, PTest, 0, 1.64 + nullptr, ptestFunctions, nullptr, nullptr)); 1.65 + 1.66 + EXEC("function check() { var o = new PTest(); o.test_fn(); o.test_value1; o.test_value2; o.test_value1; }"); 1.67 + 1.68 + for (int i = 1; i < 9; i++) { 1.69 + JS::RootedValue rval(cx); 1.70 + CHECK(JS_CallFunctionName(cx, global, "check", JS::HandleValueArray::empty(), 1.71 + &rval)); 1.72 + CHECK_SAME(INT_TO_JSVAL(called_test_fn), INT_TO_JSVAL(i)); 1.73 + CHECK_SAME(INT_TO_JSVAL(called_test_prop_get), INT_TO_JSVAL(4 * i)); 1.74 + } 1.75 + return true; 1.76 +} 1.77 +END_TEST(testClassGetter_isCalled)