|
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 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
5 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
7 |
|
8 #include "js/OldDebugAPI.h" |
|
9 #include "jsapi-tests/tests.h" |
|
10 |
|
11 static int emptyTrapCallCount = 0; |
|
12 |
|
13 static JSTrapStatus |
|
14 EmptyTrapHandler(JSContext *cx, JSScript *script, jsbytecode *pc, jsval *rval, |
|
15 jsval closureArg) |
|
16 { |
|
17 JS::RootedValue closure(cx, closureArg); |
|
18 JS_GC(JS_GetRuntime(cx)); |
|
19 if (JSVAL_IS_STRING(closure)) |
|
20 ++emptyTrapCallCount; |
|
21 return JSTRAP_CONTINUE; |
|
22 } |
|
23 |
|
24 BEGIN_TEST(testTrap_gc) |
|
25 { |
|
26 static const char source[] = |
|
27 "var i = 0;\n" |
|
28 "var sum = 0;\n" |
|
29 "while (i < 10) {\n" |
|
30 " sum += i;\n" |
|
31 " ++i;\n" |
|
32 "}\n" |
|
33 "({ result: sum });\n" |
|
34 ; |
|
35 |
|
36 // compile |
|
37 JS::CompileOptions options(cx); |
|
38 options.setFileAndLine(__FILE__, 1); |
|
39 JS::RootedScript script(cx, JS_CompileScript(cx, global, source, |
|
40 strlen(source), options)); |
|
41 CHECK(script); |
|
42 |
|
43 // execute |
|
44 JS::RootedValue v2(cx); |
|
45 CHECK(JS_ExecuteScript(cx, global, script, &v2)); |
|
46 CHECK(v2.isObject()); |
|
47 CHECK_EQUAL(emptyTrapCallCount, 0); |
|
48 |
|
49 // Enable debug mode |
|
50 CHECK(JS_SetDebugMode(cx, true)); |
|
51 |
|
52 static const char trapClosureText[] = "some trap closure"; |
|
53 |
|
54 // scope JSScript usage to make sure that it is not used after |
|
55 // JS_ExecuteScript. This way we avoid using Anchor. |
|
56 JS::RootedString trapClosure(cx); |
|
57 { |
|
58 jsbytecode *line2 = JS_LineNumberToPC(cx, script, 1); |
|
59 CHECK(line2); |
|
60 |
|
61 jsbytecode *line6 = JS_LineNumberToPC(cx, script, 5); |
|
62 CHECK(line2); |
|
63 |
|
64 trapClosure = JS_NewStringCopyZ(cx, trapClosureText); |
|
65 CHECK(trapClosure); |
|
66 JS::RootedValue closureValue(cx, JS::StringValue(trapClosure)); |
|
67 JS_SetTrap(cx, script, line2, EmptyTrapHandler, closureValue); |
|
68 JS_SetTrap(cx, script, line6, EmptyTrapHandler, closureValue); |
|
69 |
|
70 JS_GC(rt); |
|
71 |
|
72 CHECK(JS_FlatStringEqualsAscii(JS_ASSERT_STRING_IS_FLAT(trapClosure), trapClosureText)); |
|
73 } |
|
74 |
|
75 // execute |
|
76 CHECK(JS_ExecuteScript(cx, global, script, &v2)); |
|
77 CHECK_EQUAL(emptyTrapCallCount, 11); |
|
78 |
|
79 JS_GC(rt); |
|
80 |
|
81 CHECK(JS_FlatStringEqualsAscii(JS_ASSERT_STRING_IS_FLAT(trapClosure), trapClosureText)); |
|
82 |
|
83 return true; |
|
84 } |
|
85 END_TEST(testTrap_gc) |
|
86 |