js/src/jsapi-tests/testTrap.cpp

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

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

mercurial