Debian Codegraph @

rhino/testsrc/org/mozilla/javascript/tests/Utils.java

raw symbols SCIP
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4
5 package org.mozilla.javascript.tests;
6
7 import org.mozilla.javascript.Context;
8 import org.mozilla.javascript.ContextAction;
9 import org.mozilla.javascript.ContextFactory;
10 import org.mozilla.javascript.Scriptable;
11
12
13 /**
14 * Misc utilities to make test code easier.
15 * @author Marc Guillemot
16 */
17 public class Utils
18 {
19 /**
20 * Runs the action successively with all available optimization levels
21 */
22 public static void runWithAllOptimizationLevels(final ContextAction action)
23 {
24 runWithOptimizationLevel(action, -1);
25 runWithOptimizationLevel(action, 0);
26 runWithOptimizationLevel(action, 1);
27 }
28
29 /**
30 * Runs the action successively with all available optimization levels
31 */
32 public static void runWithAllOptimizationLevels(final ContextFactory contextFactory, final ContextAction action)
33 {
34 runWithOptimizationLevel(contextFactory, action, -1);
35 runWithOptimizationLevel(contextFactory, action, 0);
36 runWithOptimizationLevel(contextFactory, action, 1);
37 }
38
39 /**
40 * Runs the provided action at the given optimization level
41 */
42 public static void runWithOptimizationLevel(final ContextAction action, final int optimizationLevel)
43 {
44 runWithOptimizationLevel(new ContextFactory(), action, optimizationLevel);
45 }
46
47 /**
48 * Runs the provided action at the given optimization level
49 */
50 public static void runWithOptimizationLevel(final ContextFactory contextFactory, final ContextAction action, final int optimizationLevel)
51 {
52 final Context cx = contextFactory.enterContext();
53 try
54 {
55 cx.setOptimizationLevel(optimizationLevel);
56 action.run(cx);
57 }
58 finally
59 {
60 Context.exit();
61 }
62 }
63
64 /**
65 * Execute the provided script in a fresh context as "myScript.js".
66 * @param script the script code
67 */
68 static void executeScript(final String script, final int optimizationLevel) {
69 final ContextAction action = new ContextAction()
70 {
71 public Object run(Context cx) {
72 final Scriptable scope = cx.initStandardObjects();
73 return cx.evaluateString(scope, script, "myScript.js", 1, null);
74 }
75 };
76 Utils.runWithOptimizationLevel(action, optimizationLevel);
77 }
78 }