Search in sources :

Example 11 with Script

use of org.mozilla.javascript.Script in project hackpad by dropbox.

the class GeneratedMethodNameTest method doTest.

public void doTest(final String scriptCode) throws Exception {
    final Context cx = ContextFactory.getGlobal().enterContext();
    try {
        Scriptable topScope = cx.initStandardObjects();
        topScope.put("javaNameGetter", topScope, new JavaNameGetter());
        Script script = cx.compileString(scriptCode, "myScript", 1, null);
        script.exec(cx, topScope);
    } finally {
        Context.exit();
    }
}
Also used : Context(org.mozilla.javascript.Context) Script(org.mozilla.javascript.Script) Scriptable(org.mozilla.javascript.Scriptable)

Example 12 with Script

use of org.mozilla.javascript.Script in project hackpad by dropbox.

the class Bug482203 method testJavaApi.

public void testJavaApi() throws Exception {
    Context cx = Context.enter();
    try {
        cx.setOptimizationLevel(-1);
        Script script = cx.compileReader(new InputStreamReader(Bug482203.class.getResourceAsStream("conttest.js")), "", 1, null);
        Scriptable scope = cx.initStandardObjects();
        cx.executeScriptWithContinuations(script, scope);
        for (; ; ) {
            Object cont = ScriptableObject.getProperty(scope, "c");
            if (cont == null) {
                break;
            }
            cx.resumeContinuation(cont, scope, null);
        }
    } finally {
        Context.exit();
    }
}
Also used : Context(org.mozilla.javascript.Context) Script(org.mozilla.javascript.Script) InputStreamReader(java.io.InputStreamReader) ScriptableObject(org.mozilla.javascript.ScriptableObject) Scriptable(org.mozilla.javascript.Scriptable)

Example 13 with Script

use of org.mozilla.javascript.Script in project hackpad by dropbox.

the class Bug421071Test method compileScript.

private Script compileScript() {
    String scriptSource = "importPackage(java.util);\n" + "var searchmon = 3;\n" + "var searchday = 10;\n" + "var searchyear = 2008;\n" + "var searchwkday = 0;\n" + "\n" + "var myDate = Calendar.getInstance();\n // this is a java.util.Calendar" + "myDate.set(Calendar.MONTH, searchmon);\n" + "myDate.set(Calendar.DATE, searchday);\n" + "myDate.set(Calendar.YEAR, searchyear);\n" + "searchwkday.value = myDate.get(Calendar.DAY_OF_WEEK);";
    Script script;
    Context context = factory.enterContext();
    try {
        script = context.compileString(scriptSource, "testScript", 1, null);
        return script;
    } finally {
        Context.exit();
    }
}
Also used : Context(org.mozilla.javascript.Context) Script(org.mozilla.javascript.Script)

Example 14 with Script

use of org.mozilla.javascript.Script in project OpenAM by OpenRock.

the class RhinoScriptEngine method compile.

/**
     * {@inheritDoc}
     */
@Override
public CompiledScript compile(final Reader reader) throws ScriptException {
    Reject.ifNull(reader);
    final Context context = factory.getContext();
    try {
        // Use configured ScriptContext from parent class, if specified
        final String filename = getFilename(getContext());
        final Script compiledScript = context.compileReader(reader, filename, 1, null);
        return new RhinoCompiledScript(this, compiledScript);
    } catch (RhinoException ex) {
        throw convertException(ex);
    } catch (IOException ex) {
        throw new ScriptException(ex);
    } finally {
        factory.releaseContext(context);
    }
}
Also used : Context(org.mozilla.javascript.Context) ScriptContext(javax.script.ScriptContext) Script(org.mozilla.javascript.Script) CompiledScript(javax.script.CompiledScript) ScriptException(javax.script.ScriptException) RhinoException(org.mozilla.javascript.RhinoException) IOException(java.io.IOException)

Example 15 with Script

use of org.mozilla.javascript.Script in project jaggery by wso2.

the class ModuleManager method initScripts.

@SuppressFBWarnings("PATH_TRAVERSAL_IN")
private void initScripts(Module moduleObject, Context cx, JavaScriptModule module, boolean isCustom) throws ScriptException {
    String name = null;
    String path = null;
    JavaScriptScript script;
    List scriptList = moduleObject.getScripts();
    Iterator itr = scriptList.iterator();
    while (itr.hasNext()) {
        try {
            //process methods
            org.jaggeryjs.jaggery.core.Script scriptObject = (org.jaggeryjs.jaggery.core.Script) itr.next();
            name = scriptObject.getName();
            path = scriptObject.getPath();
            script = new JavaScriptScript(name);
            Reader reader;
            final String fileName;
            ScriptCachingContext sctx;
            if (isCustom) {
                String filteredPath = filterPath(path);
                fileName = modulesDir + File.separator + module.getName() + File.separator + filterPath(path);
                reader = new FileReader(fileName);
                int endIndex = filteredPath.lastIndexOf(File.separator);
                sctx = new ScriptCachingContext(String.valueOf(MultitenantConstants.SUPER_TENANT_ID), '<' + module.getName() + '>', filteredPath.substring(0, endIndex), filteredPath.substring(endIndex));
            } else {
                reader = new InputStreamReader(ModuleManager.class.getClassLoader().getResourceAsStream(path));
                fileName = modulesDir + File.separator + name;
                int endIndex = path.lastIndexOf('/');
                sctx = new ScriptCachingContext(String.valueOf(MultitenantConstants.SUPER_TENANT_ID), "<<" + name + ">>", '/' + path.substring(0, endIndex), path.substring(endIndex));
            }
            CacheManager cacheManager = new CacheManager(null);
            sctx.setSecurityDomain(new RhinoSecurityDomain() {

                @SuppressFBWarnings("PATH_TRAVERSAL_IN")
                @Override
                public CodeSource getCodeSource() throws ScriptException {
                    try {
                        URL url = new File(fileName).getCanonicalFile().toURI().toURL();
                        return new CodeSource(url, (Certificate[]) null);
                    } catch (MalformedURLException e) {
                        throw new ScriptException(e);
                    } catch (IOException e) {
                        throw new ScriptException(e);
                    }
                }
            });
            sctx.setSourceModifiedTime(1);
            Script cachedScript = cacheManager.getScriptObject(reader, sctx);
            if (cachedScript == null) {
                cacheManager.cacheScript(reader, sctx);
                cachedScript = cacheManager.getScriptObject(reader, sctx);
            }
            script.setScript(cachedScript);
            module.addScript(script);
        } catch (FileNotFoundException e) {
            String msg = "Error executing script. Script cannot be found, name : " + name + ", path : " + path;
            log.error(msg, e);
            throw new ScriptException(msg, e);
        }
    }
}
Also used : MalformedURLException(java.net.MalformedURLException) RhinoSecurityDomain(org.jaggeryjs.scriptengine.security.RhinoSecurityDomain) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings) URL(java.net.URL) ScriptException(org.jaggeryjs.scriptengine.exceptions.ScriptException) Iterator(java.util.Iterator) CacheManager(org.jaggeryjs.scriptengine.cache.CacheManager) List(java.util.List) Script(org.mozilla.javascript.Script) ScriptCachingContext(org.jaggeryjs.scriptengine.cache.ScriptCachingContext) CodeSource(java.security.CodeSource) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings)

Aggregations

Script (org.mozilla.javascript.Script)17 Context (org.mozilla.javascript.Context)12 ScriptableObject (org.mozilla.javascript.ScriptableObject)5 IOException (java.io.IOException)3 InputStreamReader (java.io.InputStreamReader)3 ContinuationPending (org.mozilla.javascript.ContinuationPending)3 RhinoException (org.mozilla.javascript.RhinoException)3 Scriptable (org.mozilla.javascript.Scriptable)3 CompiledScript (javax.script.CompiledScript)2 ScriptContext (javax.script.ScriptContext)2 ScriptException (javax.script.ScriptException)2 ScriptException (org.jaggeryjs.scriptengine.exceptions.ScriptException)2 ContextAction (org.mozilla.javascript.ContextAction)2 GeneratedClassLoader (org.mozilla.javascript.GeneratedClassLoader)2 SuppressFBWarnings (edu.umd.cs.findbugs.annotations.SuppressFBWarnings)1 BufferedReader (java.io.BufferedReader)1 PrintStream (java.io.PrintStream)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 UndeclaredThrowableException (java.lang.reflect.UndeclaredThrowableException)1 MalformedURLException (java.net.MalformedURLException)1