use of org.mozilla.javascript.Context in project wombat by PLOS.
the class AssetServiceTest method testCompiledJs.
@Test
public void testCompiledJs() throws Exception {
// First, we use a javascript runtime to execute some uncompiled javascript, to
// get the expected value.
Object expected;
{
Context jsContext = Context.enter();
jsContext.setOptimizationLevel(-1);
Scriptable jsScope = jsContext.initStandardObjects();
try (FileReader reader1 = new FileReader(new File(DATA_PATH + "test1.js"));
FileReader reader2 = new FileReader(new File(DATA_PATH + "test2.js"))) {
jsContext.evaluateReader(jsScope, reader1, "test1.js", 1, null);
expected = jsContext.evaluateReader(jsScope, reader2, "test2.js", 1, null);
assertEquals(expected, "magicValue");
}
}
// Now we compile the same javascript files, execute them, and check the value returned.
List<String> jsFiles = new ArrayList<>();
jsFiles.add("resource/js/test1.js");
jsFiles.add("resource/js/test2.js");
String compiledJsPath = assetService.getCompiledAssetLink(AssetService.AssetType.JS, jsFiles, siteSet.getSite("site1"));
String[] fields = compiledJsPath.split("/");
String basename = fields[fields.length - 1];
Context jsContext = Context.enter();
jsContext.setOptimizationLevel(-1);
Scriptable jsScope = jsContext.initStandardObjects();
File file = new File(runtimeConfiguration.getCompiledAssetDir() + File.separator + basename);
try (FileReader fr = new FileReader(file)) {
Object actual = jsContext.evaluateReader(jsScope, fr, basename, 1, null);
assertEquals(actual, expected);
}
}
use of org.mozilla.javascript.Context in project closure-templates by google.
the class EscapingConventionsTest method applyDirectiveInRhino.
private List<String> applyDirectiveInRhino(String directiveName, Iterable<String> toEscape, String soyUtilsPath) throws Exception {
List<String> output = Lists.newArrayList();
Context context = new ContextFactory().enterContext();
// Only running once.
context.setOptimizationLevel(-1);
ScriptableObject globalScope = context.initStandardObjects();
globalScope.defineProperty("navigator", Context.javaToJS(new Navigator(), globalScope), ScriptableObject.DONTENUM);
Reader soyutils = new InputStreamReader(new FileInputStream(soyUtilsPath), UTF_8);
try {
String basename = soyUtilsPath.substring(soyUtilsPath.lastIndexOf('/') + 1);
context.evaluateReader(globalScope, soyutils, basename, 1, null);
} finally {
soyutils.close();
}
globalScope.defineProperty("test_toEscape", ImmutableList.copyOf(toEscape), ScriptableObject.DONTENUM);
globalScope.defineProperty("test_output", output, ScriptableObject.DONTENUM);
context.evaluateString(globalScope, Joiner.on('\n').join("(function () {", " if (typeof goog !== 'undefined') {", // Make sure we get the innocuous value from filters and not an exception.
" goog.asserts.ENABLE_ASSERTS = goog.DEBUG = false;", " }", " for (var i = 0, n = test_toEscape.size(); i < n; ++i) {", " var raw = String(test_toEscape.get(i));", " var escaped = String(soy.$$" + directiveName + "(raw));", " test_output.add(raw);", " test_output.add(escaped);", " }", "})()"), // File name for JS traces.
getClass() + ":" + testName.getMethodName(), 1, null);
return output;
}
use of org.mozilla.javascript.Context in project candlepin by candlepin.
the class JsRunner method invokeMethod.
@SuppressWarnings("unchecked")
public <T> T invokeMethod(String method) throws NoSuchMethodException, RhinoException {
Scriptable localScope = Context.toObject(this.rulesNameSpace, scope);
Object func = ScriptableObject.getProperty(localScope, method);
if (!(func instanceof Function)) {
throw new NoSuchMethodException("no such javascript method: " + method);
}
Context context = Context.enter();
try {
return (T) unwrapReturnValue(((Function) func).call(context, scope, localScope, Context.emptyArgs));
} finally {
Context.exit();
}
}
use of org.mozilla.javascript.Context in project candlepin by candlepin.
the class JsRunnerProvider method compileRules.
public void compileRules(boolean forceRefresh) {
scriptLock.writeLock().lock();
try {
// Check to see if we need to recompile. we do this inside the write lock
// just to avoid race conditions where we might double compile
Date newUpdated = rulesCurator.getUpdated();
if (!forceRefresh && newUpdated.equals(this.currentRulesUpdated)) {
return;
}
log.info("Recompiling rules with timestamp: {}", newUpdated);
Context context = Context.enter();
context.setOptimizationLevel(9);
scope = context.initStandardObjects(null, true);
try {
Rules rules = rulesCurator.getRules();
rulesVersion = rules.getVersion();
rulesSource = rules.getRulesSource();
script = context.compileString(rules.getRules(), "rules", 1, null);
script.exec(context, scope);
((ScriptableObject) scope).sealObject();
this.currentRulesUpdated = newUpdated;
} finally {
Context.exit();
}
} finally {
scriptLock.writeLock().unlock();
}
}
use of org.mozilla.javascript.Context in project solr-cmd-utils by tblsoft.
the class JavaScriptFilter method document.
@Override
public void document(Document document) {
Context cx = Context.enter();
Scriptable scope = cx.initStandardObjects();
List<Document> output = new ArrayList<Document>();
ScriptableObject.putProperty(scope, "documentBuilder", Context.javaToJS(new DocumentBuilder(), scope));
ScriptableObject.putProperty(scope, "input", Context.javaToJS(document, scope));
ScriptableObject.putProperty(scope, "output", Context.javaToJS(output, scope));
cx.evaluateString(scope, script, filename, 1, null);
for (Document out : output) {
super.document(out);
}
}
Aggregations