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