| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
package org.mozilla.javascript.tests; |
| 6 |
import org.mozilla.javascript.*; |
| 7 |
import java.util.Collections; |
| 8 |
import java.util.Map; |
| 9 |
|
| 10 |
public class Evaluator { |
| 11 |
|
| 12 |
public static Object eval(String source) { |
| 13 |
return eval(source, null); |
| 14 |
} |
| 15 |
|
| 16 |
public static Object eval(String source, String id, Scriptable object) { |
| 17 |
return eval(source, Collections.singletonMap(id, object)); |
| 18 |
} |
| 19 |
|
| 20 |
public static Object eval(String source, Map<String, Scriptable> bindings) { |
| 21 |
Context cx = ContextFactory.getGlobal().enterContext(); |
| 22 |
try { |
| 23 |
Scriptable scope = cx.initStandardObjects(); |
| 24 |
if (bindings != null) { |
| 25 |
for (Map.Entry<String, Scriptable> entry : bindings.entrySet()) { |
| 26 |
final Scriptable object = entry.getValue(); |
| 27 |
object.setParentScope(scope); |
| 28 |
scope.put(entry.getKey(), scope, object); |
| 29 |
} |
| 30 |
} |
| 31 |
return cx.evaluateString(scope, source, "source", 1, null); |
| 32 |
} finally { |
| 33 |
Context.exit(); |
| 34 |
} |
| 35 |
} |
| 36 |
} |