DXR is a code search and navigation tool aimed at making sense of large projects. It supports full-text and regex searches as well as structural queries.

Mercurial (5216dd412535)

VCS Links

Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
 * vim: set ts=8 sw=4 et tw=99:
 */

#include "tests.h"
#include "jsdbgapi.h"

static int emptyTrapCallCount = 0;

static JSTrapStatus
EmptyTrapHandler(JSContext *cx, JSScript *script, jsbytecode *pc, jsval *rval,
                 jsval closure)
{
    JS_GC(cx);
    if (JSVAL_IS_STRING(closure))
        ++emptyTrapCallCount;
    return JSTRAP_CONTINUE;
}

BEGIN_TEST(testTrap_gc)
{
    static const char source[] =
"var i = 0;\n"
"var sum = 0;\n"
"while (i < 10) {\n"
"    sum += i;\n"
"    ++i;\n"
"}\n"
"({ result: sum });\n"
        ;

    // compile
    JSScript *script = JS_CompileScript(cx, global, source, strlen(source), __FILE__, 1);
    CHECK(script);
    JSObject *scrobj = JS_NewScriptObject(cx, script);
    CHECK(scrobj);
    jsvalRoot v(cx, OBJECT_TO_JSVAL(scrobj));

    // execute
    jsvalRoot v2(cx);
    CHECK(JS_ExecuteScript(cx, global, script, v2.addr()));
    CHECK(JSVAL_IS_OBJECT(v2));
    CHECK(emptyTrapCallCount == 0);

    // Disable JIT for debugging
    JS_SetOptions(cx, JS_GetOptions(cx) & ~JSOPTION_JIT);

    // Enable debug mode
    CHECK(JS_SetDebugMode(cx, JS_TRUE));

    jsbytecode *line2 = JS_LineNumberToPC(cx, script, 1);
    CHECK(line2);

    jsbytecode *line6 = JS_LineNumberToPC(cx, script, 5);
    CHECK(line2);

    static const char trapClosureText[] = "some trap closure";
    JSString *trapClosure = JS_NewStringCopyZ(cx, trapClosureText);
    CHECK(trapClosure);
    JS_SetTrap(cx, script, line2, EmptyTrapHandler, STRING_TO_JSVAL(trapClosure));
    JS_SetTrap(cx, script, line6, EmptyTrapHandler, STRING_TO_JSVAL(trapClosure));

    JS_GC(cx);

    CHECK(JS_FlatStringEqualsAscii(JS_ASSERT_STRING_IS_FLAT(trapClosure), trapClosureText));

    // execute
    CHECK(JS_ExecuteScript(cx, global, script, v2.addr()));
    CHECK(emptyTrapCallCount == 11);

    JS_GC(cx);

    CHECK(JS_FlatStringEqualsAscii(JS_ASSERT_STRING_IS_FLAT(trapClosure), trapClosureText));

    return true;
}
END_TEST(testTrap_gc)