Search in sources :

Example 56 with Context

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);
    }
}
Also used : Context(org.mozilla.javascript.Context) ArrayList(java.util.ArrayList) FileReader(java.io.FileReader) Scriptable(org.mozilla.javascript.Scriptable) File(java.io.File) Test(org.testng.annotations.Test)

Example 57 with Context

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;
}
Also used : Context(org.mozilla.javascript.Context) ContextFactory(org.mozilla.javascript.ContextFactory) ScriptableObject(org.mozilla.javascript.ScriptableObject) InputStreamReader(java.io.InputStreamReader) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) FileInputStream(java.io.FileInputStream)

Example 58 with Context

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();
    }
}
Also used : Context(org.mozilla.javascript.Context) Function(org.mozilla.javascript.Function) ScriptableObject(org.mozilla.javascript.ScriptableObject) Scriptable(org.mozilla.javascript.Scriptable)

Example 59 with Context

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();
    }
}
Also used : Context(org.mozilla.javascript.Context) ScriptableObject(org.mozilla.javascript.ScriptableObject) Rules(org.candlepin.model.Rules) Date(java.util.Date)

Example 60 with Context

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);
    }
}
Also used : Context(org.mozilla.javascript.Context) DocumentBuilder(de.tblsoft.solr.pipeline.bean.DocumentBuilder) ArrayList(java.util.ArrayList) Scriptable(org.mozilla.javascript.Scriptable) Document(de.tblsoft.solr.pipeline.bean.Document)

Aggregations

Context (org.mozilla.javascript.Context)152 Scriptable (org.mozilla.javascript.Scriptable)68 ScriptableObject (org.mozilla.javascript.ScriptableObject)62 ContextAction (org.mozilla.javascript.ContextAction)23 ContextFactory (org.mozilla.javascript.ContextFactory)22 Script (org.mozilla.javascript.Script)17 IOException (java.io.IOException)14 Function (org.mozilla.javascript.Function)13 ScriptContext (javax.script.ScriptContext)10 Test (org.junit.Test)8 RhinoException (org.mozilla.javascript.RhinoException)8 ArrayList (java.util.ArrayList)7 ContinuationPending (org.mozilla.javascript.ContinuationPending)7 NativeJavaObject (org.mozilla.javascript.NativeJavaObject)7 InputStreamReader (java.io.InputStreamReader)6 ScriptException (org.jaggeryjs.scriptengine.exceptions.ScriptException)6 JavaScriptException (org.mozilla.javascript.JavaScriptException)6 Date (java.util.Date)5 HashSet (java.util.HashSet)5 List (java.util.List)5