Imported Upstream version 1.0.0
[platform/upstream/js.git] / js / src / jsapi-tests / testClassGetter.cpp
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2  * vim: set ts=8 sw=4 et tw=99:
3  *
4  * Tests the JSClass::getProperty hook
5  */
6
7 #include "tests.h"
8
9 int called_test_fn;
10 int called_test_prop_get;
11
12 static JSBool test_prop_get( JSContext *cx, JSObject *obj, jsid id, jsval *vp )
13 {
14     called_test_prop_get++;
15     return JS_TRUE;
16 }
17
18 static JSBool
19 PTest(JSContext* cx, uintN argc, jsval *vp)
20 {
21     JSObject *obj = JS_NewObjectForConstructor(cx, vp);
22     if (!obj)
23         return JS_FALSE;
24     JS_SET_RVAL(cx, vp, OBJECT_TO_JSVAL(obj));
25     return JS_TRUE;
26 }
27
28 static JSClass ptestClass = {
29     "PTest",
30     JSCLASS_HAS_PRIVATE,
31
32     JS_PropertyStub,       // add
33     JS_PropertyStub,       // delete
34     test_prop_get,         // get
35     JS_StrictPropertyStub, // set
36     JS_EnumerateStub,
37     JS_ResolveStub,
38     JS_ConvertStub,
39     JS_FinalizeStub,
40     JSCLASS_NO_OPTIONAL_MEMBERS
41 };
42
43 static JSBool test_fn(JSContext *cx, uintN argc, jsval *vp)
44 {
45     called_test_fn++;
46     return JS_TRUE;
47 }
48
49 static JSFunctionSpec ptestFunctions[] = {
50     JS_FS( "test_fn", test_fn, 0, 0 ),
51     JS_FS_END
52 };
53
54 BEGIN_TEST(testClassGetter_isCalled)
55 {
56     CHECK(JS_InitClass(cx, JS_GetGlobalObject(cx), NULL, &ptestClass, PTest, 0,
57                        NULL, ptestFunctions, NULL, NULL));
58
59     EXEC("function check() { var o = new PTest(); o.test_fn(); o.test_value1; o.test_value2; o.test_value1; }");
60
61     for (int i = 1; i < 9; i++) {
62         jsvalRoot rval(cx);
63         CHECK(JS_CallFunctionName(cx, global, "check", 0, NULL, rval.addr()));
64         CHECK_SAME(INT_TO_JSVAL(called_test_fn), INT_TO_JSVAL(i));
65         CHECK_SAME(INT_TO_JSVAL(called_test_prop_get), INT_TO_JSVAL(4 * i));
66     }
67     return true;
68 }
69 END_TEST(testClassGetter_isCalled)