~ubuntu-branches/ubuntu/saucy/mozjs17/saucy

« back to all changes in this revision

Viewing changes to js/src/jsapi-tests/testTrap.cpp

  • Committer: Package Import Robot
  • Author(s): Rico Tzschichholz
  • Date: 2013-05-25 12:24:23 UTC
  • Revision ID: package-import@ubuntu.com-20130525122423-zmxucrhtensw90xy
Tags: upstream-17.0.0
ImportĀ upstreamĀ versionĀ 17.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
/* 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
 
 
9
#include "tests.h"
 
10
#include "jsdbgapi.h"
 
11
 
 
12
static int emptyTrapCallCount = 0;
 
13
 
 
14
static JSTrapStatus
 
15
EmptyTrapHandler(JSContext *cx, JSScript *script, jsbytecode *pc, jsval *rval,
 
16
                 jsval closure)
 
17
{
 
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::RootedScript script(cx, JS_CompileScript(cx, global, source, strlen(source), __FILE__, 1));
 
38
    CHECK(script);
 
39
 
 
40
    // execute
 
41
    JS::RootedValue v2(cx);
 
42
    CHECK(JS_ExecuteScript(cx, global, script, v2.address()));
 
43
    CHECK(v2.isObject());
 
44
    CHECK_EQUAL(emptyTrapCallCount, 0);
 
45
 
 
46
    // Enable debug mode
 
47
    CHECK(JS_SetDebugMode(cx, JS_TRUE));
 
48
 
 
49
    static const char trapClosureText[] = "some trap closure";
 
50
 
 
51
    // scope JSScript  usage to make sure that it is not used after
 
52
    // JS_ExecuteScript. This way we avoid using Anchor.
 
53
    JS::RootedString trapClosure(cx);
 
54
    {
 
55
        jsbytecode *line2 = JS_LineNumberToPC(cx, script, 1);
 
56
        CHECK(line2);
 
57
 
 
58
        jsbytecode *line6 = JS_LineNumberToPC(cx, script, 5);
 
59
        CHECK(line2);
 
60
 
 
61
        trapClosure = JS_NewStringCopyZ(cx, trapClosureText);
 
62
        CHECK(trapClosure);
 
63
        JS_SetTrap(cx, script, line2, EmptyTrapHandler, STRING_TO_JSVAL(trapClosure));
 
64
        JS_SetTrap(cx, script, line6, EmptyTrapHandler, STRING_TO_JSVAL(trapClosure));
 
65
 
 
66
        JS_GC(rt);
 
67
 
 
68
        CHECK(JS_FlatStringEqualsAscii(JS_ASSERT_STRING_IS_FLAT(trapClosure), trapClosureText));
 
69
    }
 
70
 
 
71
    // execute
 
72
    CHECK(JS_ExecuteScript(cx, global, script, v2.address()));
 
73
    CHECK_EQUAL(emptyTrapCallCount, 11);
 
74
 
 
75
    JS_GC(rt);
 
76
 
 
77
    CHECK(JS_FlatStringEqualsAscii(JS_ASSERT_STRING_IS_FLAT(trapClosure), trapClosureText));
 
78
 
 
79
    return true;
 
80
}
 
81
END_TEST(testTrap_gc)
 
82