| 1 |
|
| 2 |
|
| 3 |
|
| 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 |
|
| 15 |
|
| 16 |
|
| 17 |
public class Utils |
| 18 |
{ |
| 19 |
|
| 20 |
|
| 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 |
|
| 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 |
|
| 41 |
|
| 42 |
public static void runWithOptimizationLevel(final ContextAction action, final int optimizationLevel) |
| 43 |
{ |
| 44 |
runWithOptimizationLevel(new ContextFactory(), action, optimizationLevel); |
| 45 |
} |
| 46 |
|
| 47 |
|
| 48 |
|
| 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 |
|
| 66 |
|
| 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 |
} |