Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 3 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 4 | |
michael@0 | 5 | #include "jsapi-tests/tests.h" |
michael@0 | 6 | |
michael@0 | 7 | #ifdef MOZ_TRACE_JSCALLS |
michael@0 | 8 | |
michael@0 | 9 | static int depth = 0; |
michael@0 | 10 | static int enters = 0; |
michael@0 | 11 | static int leaves = 0; |
michael@0 | 12 | static int interpreted = 0; |
michael@0 | 13 | |
michael@0 | 14 | static void |
michael@0 | 15 | funcTransition(const JSFunction *, |
michael@0 | 16 | const JSScript *, |
michael@0 | 17 | const JSContext *cx, |
michael@0 | 18 | int entering) |
michael@0 | 19 | { |
michael@0 | 20 | if (entering > 0) { |
michael@0 | 21 | ++depth; |
michael@0 | 22 | ++enters; |
michael@0 | 23 | ++interpreted; |
michael@0 | 24 | } else { |
michael@0 | 25 | --depth; |
michael@0 | 26 | ++leaves; |
michael@0 | 27 | } |
michael@0 | 28 | } |
michael@0 | 29 | |
michael@0 | 30 | static bool called2 = false; |
michael@0 | 31 | |
michael@0 | 32 | static void |
michael@0 | 33 | funcTransition2(const JSFunction *, const JSScript*, const JSContext*, int) |
michael@0 | 34 | { |
michael@0 | 35 | called2 = true; |
michael@0 | 36 | } |
michael@0 | 37 | |
michael@0 | 38 | static int overlays = 0; |
michael@0 | 39 | static JSFunctionCallback innerCallback = nullptr; |
michael@0 | 40 | static void |
michael@0 | 41 | funcTransitionOverlay(const JSFunction *fun, |
michael@0 | 42 | const JSScript *script, |
michael@0 | 43 | const JSContext *cx, |
michael@0 | 44 | int entering) |
michael@0 | 45 | { |
michael@0 | 46 | (*innerCallback)(fun, script, cx, entering); |
michael@0 | 47 | overlays++; |
michael@0 | 48 | } |
michael@0 | 49 | #endif |
michael@0 | 50 | |
michael@0 | 51 | BEGIN_TEST(testFuncCallback_bug507012) |
michael@0 | 52 | { |
michael@0 | 53 | #ifdef MOZ_TRACE_JSCALLS |
michael@0 | 54 | // Call funcTransition() whenever a Javascript method is invoked |
michael@0 | 55 | JS_SetFunctionCallback(cx, funcTransition); |
michael@0 | 56 | |
michael@0 | 57 | EXEC("x = 0; function f (n) { if (n > 1) { f(n - 1); } }"); |
michael@0 | 58 | interpreted = enters = leaves = depth = 0; |
michael@0 | 59 | |
michael@0 | 60 | // Check whether JS_Execute() tracking works |
michael@0 | 61 | EXEC("42"); |
michael@0 | 62 | CHECK_EQUAL(enters, 1); |
michael@0 | 63 | CHECK_EQUAL(leaves, 1); |
michael@0 | 64 | CHECK_EQUAL(depth, 0); |
michael@0 | 65 | interpreted = enters = leaves = depth = 0; |
michael@0 | 66 | |
michael@0 | 67 | // Check whether the basic function tracking works |
michael@0 | 68 | EXEC("f(1)"); |
michael@0 | 69 | CHECK_EQUAL(enters, 1+1); |
michael@0 | 70 | CHECK_EQUAL(leaves, 1+1); |
michael@0 | 71 | CHECK_EQUAL(depth, 0); |
michael@0 | 72 | |
michael@0 | 73 | // Can we switch to a different callback? |
michael@0 | 74 | enters = 777; |
michael@0 | 75 | JS_SetFunctionCallback(cx, funcTransition2); |
michael@0 | 76 | EXEC("f(1)"); |
michael@0 | 77 | CHECK(called2); |
michael@0 | 78 | CHECK_EQUAL(enters, 777); |
michael@0 | 79 | |
michael@0 | 80 | // Check whether we can turn off function tracing |
michael@0 | 81 | JS_SetFunctionCallback(cx, nullptr); |
michael@0 | 82 | EXEC("f(1)"); |
michael@0 | 83 | CHECK_EQUAL(enters, 777); |
michael@0 | 84 | interpreted = enters = leaves = depth = 0; |
michael@0 | 85 | |
michael@0 | 86 | // Check nested invocations |
michael@0 | 87 | JS_SetFunctionCallback(cx, funcTransition); |
michael@0 | 88 | enters = leaves = depth = 0; |
michael@0 | 89 | EXEC("f(3)"); |
michael@0 | 90 | CHECK_EQUAL(enters, 1+3); |
michael@0 | 91 | CHECK_EQUAL(leaves, 1+3); |
michael@0 | 92 | CHECK_EQUAL(depth, 0); |
michael@0 | 93 | interpreted = enters = leaves = depth = 0; |
michael@0 | 94 | |
michael@0 | 95 | // Check calls invoked while running on trace -- or now, perhaps on |
michael@0 | 96 | // IonMonkey's equivalent, if it ever starts to exist? |
michael@0 | 97 | EXEC("function g () { ++x; }"); |
michael@0 | 98 | interpreted = enters = leaves = depth = 0; |
michael@0 | 99 | EXEC("for (i = 0; i < 5000; ++i) { g(); }"); |
michael@0 | 100 | CHECK_EQUAL(enters, 1+5000); |
michael@0 | 101 | CHECK_EQUAL(leaves, 1+5000); |
michael@0 | 102 | CHECK_EQUAL(depth, 0); |
michael@0 | 103 | |
michael@0 | 104 | // Test nesting callbacks via JS_GetFunctionCallback() |
michael@0 | 105 | JS_SetFunctionCallback(cx, funcTransition); |
michael@0 | 106 | innerCallback = JS_GetFunctionCallback(cx); |
michael@0 | 107 | JS_SetFunctionCallback(cx, funcTransitionOverlay); |
michael@0 | 108 | |
michael@0 | 109 | EXEC("x = 0; function f (n) { if (n > 1) { f(n - 1); } }"); |
michael@0 | 110 | interpreted = enters = leaves = depth = overlays = 0; |
michael@0 | 111 | |
michael@0 | 112 | EXEC("42.5"); |
michael@0 | 113 | CHECK_EQUAL(enters, 1); |
michael@0 | 114 | CHECK_EQUAL(leaves, 1); |
michael@0 | 115 | CHECK_EQUAL(depth, 0); |
michael@0 | 116 | CHECK_EQUAL(overlays, enters + leaves); |
michael@0 | 117 | interpreted = enters = leaves = depth = overlays = 0; |
michael@0 | 118 | #endif |
michael@0 | 119 | |
michael@0 | 120 | // Uncomment this to validate whether you're hitting all runmodes (interp, |
michael@0 | 121 | // mjit, ...?) Unfortunately, that still doesn't cover all |
michael@0 | 122 | // transitions between the various runmodes, but it's a start. |
michael@0 | 123 | //JS_DumpAllProfiles(cx); |
michael@0 | 124 | |
michael@0 | 125 | return true; |
michael@0 | 126 | } |
michael@0 | 127 | |
michael@0 | 128 | // Make sure that the method jit is enabled. |
michael@0 | 129 | // We'll probably want to test in all modes. |
michael@0 | 130 | virtual |
michael@0 | 131 | JSContext *createContext() |
michael@0 | 132 | { |
michael@0 | 133 | JSContext *cx = JSAPITest::createContext(); |
michael@0 | 134 | if (!cx) |
michael@0 | 135 | return nullptr; |
michael@0 | 136 | JS::RuntimeOptionsRef(cx).setBaseline(true) |
michael@0 | 137 | .setIon(true); |
michael@0 | 138 | return cx; |
michael@0 | 139 | } |
michael@0 | 140 | |
michael@0 | 141 | END_TEST(testFuncCallback_bug507012) |