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 "jsdbgapi.h"
7 :
8 : static int emptyTrapCallCount = 0;
9 :
10 : static JSTrapStatus
11 11 : EmptyTrapHandler(JSContext *cx, JSScript *script, jsbytecode *pc, jsval *rval,
12 : jsval closure)
13 : {
14 11 : JS_GC(cx);
15 11 : if (JSVAL_IS_STRING(closure))
16 11 : ++emptyTrapCallCount;
17 11 : return JSTRAP_CONTINUE;
18 : }
19 :
20 4 : BEGIN_TEST(testTrap_gc)
21 : {
22 : static const char source[] =
23 : "var i = 0;\n"
24 : "var sum = 0;\n"
25 : "while (i < 10) {\n"
26 : " sum += i;\n"
27 : " ++i;\n"
28 : "}\n"
29 : "({ result: sum });\n"
30 : ;
31 :
32 : // compile
33 1 : JSScript *script = JS_CompileScript(cx, global, source, strlen(source), __FILE__, 1);
34 1 : CHECK(script);
35 :
36 : // execute
37 2 : jsvalRoot v2(cx);
38 1 : CHECK(JS_ExecuteScript(cx, global, script, v2.addr()));
39 1 : CHECK(JSVAL_IS_OBJECT(v2));
40 1 : CHECK_EQUAL(emptyTrapCallCount, 0);
41 :
42 : // Enable debug mode
43 1 : CHECK(JS_SetDebugMode(cx, JS_TRUE));
44 :
45 : static const char trapClosureText[] = "some trap closure";
46 :
47 : // scope JSScript usage to make sure that it is not used after
48 : // JS_ExecuteScript. This way we avoid using Anchor.
49 : JSString *trapClosure;
50 : {
51 1 : jsbytecode *line2 = JS_LineNumberToPC(cx, script, 1);
52 1 : CHECK(line2);
53 :
54 1 : jsbytecode *line6 = JS_LineNumberToPC(cx, script, 5);
55 1 : CHECK(line2);
56 :
57 1 : trapClosure = JS_NewStringCopyZ(cx, trapClosureText);
58 1 : CHECK(trapClosure);
59 1 : JS_SetTrap(cx, script, line2, EmptyTrapHandler, STRING_TO_JSVAL(trapClosure));
60 1 : JS_SetTrap(cx, script, line6, EmptyTrapHandler, STRING_TO_JSVAL(trapClosure));
61 :
62 1 : JS_GC(cx);
63 :
64 1 : CHECK(JS_FlatStringEqualsAscii(JS_ASSERT_STRING_IS_FLAT(trapClosure), trapClosureText));
65 : }
66 :
67 : // execute
68 1 : CHECK(JS_ExecuteScript(cx, global, script, v2.addr()));
69 1 : CHECK_EQUAL(emptyTrapCallCount, 11);
70 :
71 1 : JS_GC(cx);
72 :
73 1 : CHECK(JS_FlatStringEqualsAscii(JS_ASSERT_STRING_IS_FLAT(trapClosure), trapClosureText));
74 :
75 1 : return true;
76 : }
77 2 : END_TEST(testTrap_gc)
78 :
|