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 :
5 : #include "tests.h"
6 : #include "jsxdrapi.h"
7 :
8 : /* Do the test a bunch of times, because sometimes we seem to randomly
9 : miss the propcache */
10 : static const int expectedCount = 100;
11 : static int callCount = 0;
12 :
13 : static JSBool
14 100 : addProperty(JSContext *cx, JSObject *obj, jsid id, jsval *vp)
15 : {
16 100 : callCount++;
17 100 : return true;
18 : }
19 :
20 : JSClass addPropertyClass = {
21 : "AddPropertyTester",
22 : 0,
23 : addProperty,
24 : JS_PropertyStub, /* delProperty */
25 : JS_PropertyStub, /* getProperty */
26 : JS_StrictPropertyStub, /* setProperty */
27 : JS_EnumerateStub,
28 : JS_ResolveStub,
29 : JS_ConvertStub
30 : };
31 :
32 4 : BEGIN_TEST(testAddPropertyHook)
33 : {
34 2 : jsvalRoot proto(cx);
35 1 : JSObject *obj = JS_NewObject(cx, NULL, NULL, NULL);
36 1 : CHECK(obj);
37 1 : proto = OBJECT_TO_JSVAL(obj);
38 : JS_InitClass(cx, global, obj, &addPropertyClass, NULL, 0, NULL, NULL, NULL,
39 1 : NULL);
40 :
41 2 : jsvalRoot arr(cx);
42 1 : obj = JS_NewArrayObject(cx, 0, NULL);
43 1 : CHECK(obj);
44 1 : arr = OBJECT_TO_JSVAL(obj);
45 :
46 1 : CHECK(JS_DefineProperty(cx, global, "arr", arr,
47 : JS_PropertyStub, JS_StrictPropertyStub,
48 : JSPROP_ENUMERATE));
49 :
50 101 : for (int i = 0; i < expectedCount; ++i) {
51 200 : jsvalRoot vobj(cx);
52 100 : obj = JS_NewObject(cx, &addPropertyClass, NULL, NULL);
53 100 : CHECK(obj);
54 100 : vobj = OBJECT_TO_JSVAL(obj);
55 100 : CHECK(JS_DefineElement(cx, JSVAL_TO_OBJECT(arr), i, vobj,
56 : JS_PropertyStub, JS_StrictPropertyStub,
57 : JSPROP_ENUMERATE));
58 : }
59 :
60 : // Now add a prop to each of the objects, but make sure to do
61 : // so at the same bytecode location so we can hit the propcache.
62 1 : EXEC("'use strict'; \n"
63 : "for (var i = 0; i < arr.length; ++i) \n"
64 : " arr[i].prop = 42; \n"
65 : );
66 :
67 1 : CHECK(callCount == expectedCount);
68 :
69 1 : return true;
70 : }
71 2 : END_TEST(testAddPropertyHook)
72 :
|