js/src/jsapi-tests/testTrap.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/js/src/jsapi-tests/testTrap.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,86 @@
     1.4 +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
     1.5 + * vim: set ts=8 sts=4 et sw=4 tw=99:
     1.6 + */
     1.7 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.8 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.9 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
    1.10 +
    1.11 +#include "js/OldDebugAPI.h"
    1.12 +#include "jsapi-tests/tests.h"
    1.13 +
    1.14 +static int emptyTrapCallCount = 0;
    1.15 +
    1.16 +static JSTrapStatus
    1.17 +EmptyTrapHandler(JSContext *cx, JSScript *script, jsbytecode *pc, jsval *rval,
    1.18 +                 jsval closureArg)
    1.19 +{
    1.20 +    JS::RootedValue closure(cx, closureArg);
    1.21 +    JS_GC(JS_GetRuntime(cx));
    1.22 +    if (JSVAL_IS_STRING(closure))
    1.23 +        ++emptyTrapCallCount;
    1.24 +    return JSTRAP_CONTINUE;
    1.25 +}
    1.26 +
    1.27 +BEGIN_TEST(testTrap_gc)
    1.28 +{
    1.29 +    static const char source[] =
    1.30 +"var i = 0;\n"
    1.31 +"var sum = 0;\n"
    1.32 +"while (i < 10) {\n"
    1.33 +"    sum += i;\n"
    1.34 +"    ++i;\n"
    1.35 +"}\n"
    1.36 +"({ result: sum });\n"
    1.37 +        ;
    1.38 +
    1.39 +    // compile
    1.40 +    JS::CompileOptions options(cx);
    1.41 +    options.setFileAndLine(__FILE__, 1);
    1.42 +    JS::RootedScript script(cx, JS_CompileScript(cx, global, source,
    1.43 +                                                 strlen(source), options));
    1.44 +    CHECK(script);
    1.45 +
    1.46 +    // execute
    1.47 +    JS::RootedValue v2(cx);
    1.48 +    CHECK(JS_ExecuteScript(cx, global, script, &v2));
    1.49 +    CHECK(v2.isObject());
    1.50 +    CHECK_EQUAL(emptyTrapCallCount, 0);
    1.51 +
    1.52 +    // Enable debug mode
    1.53 +    CHECK(JS_SetDebugMode(cx, true));
    1.54 +
    1.55 +    static const char trapClosureText[] = "some trap closure";
    1.56 +
    1.57 +    // scope JSScript  usage to make sure that it is not used after
    1.58 +    // JS_ExecuteScript. This way we avoid using Anchor.
    1.59 +    JS::RootedString trapClosure(cx);
    1.60 +    {
    1.61 +        jsbytecode *line2 = JS_LineNumberToPC(cx, script, 1);
    1.62 +        CHECK(line2);
    1.63 +
    1.64 +        jsbytecode *line6 = JS_LineNumberToPC(cx, script, 5);
    1.65 +        CHECK(line2);
    1.66 +
    1.67 +        trapClosure = JS_NewStringCopyZ(cx, trapClosureText);
    1.68 +        CHECK(trapClosure);
    1.69 +        JS::RootedValue closureValue(cx, JS::StringValue(trapClosure));
    1.70 +        JS_SetTrap(cx, script, line2, EmptyTrapHandler, closureValue);
    1.71 +        JS_SetTrap(cx, script, line6, EmptyTrapHandler, closureValue);
    1.72 +
    1.73 +        JS_GC(rt);
    1.74 +
    1.75 +        CHECK(JS_FlatStringEqualsAscii(JS_ASSERT_STRING_IS_FLAT(trapClosure), trapClosureText));
    1.76 +    }
    1.77 +
    1.78 +    // execute
    1.79 +    CHECK(JS_ExecuteScript(cx, global, script, &v2));
    1.80 +    CHECK_EQUAL(emptyTrapCallCount, 11);
    1.81 +
    1.82 +    JS_GC(rt);
    1.83 +
    1.84 +    CHECK(JS_FlatStringEqualsAscii(JS_ASSERT_STRING_IS_FLAT(trapClosure), trapClosureText));
    1.85 +
    1.86 +    return true;
    1.87 +}
    1.88 +END_TEST(testTrap_gc)
    1.89 +

mercurial