Search in sources :

Example 36 with ScriptableObject

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

the class CommandLineManager method include.

@SuppressFBWarnings("PATH_TRAVERSAL_IN")
public static void include(Context cx, Scriptable thisObj, Object[] args, Function funObj) throws ScriptException {
    String functionName = "include";
    int argsCount = args.length;
    if (argsCount != 1) {
        HostObjectUtil.invalidNumberOfArgs(CommonManager.HOST_OBJECT_NAME, functionName, argsCount, false);
    }
    if (!(args[0] instanceof String)) {
        HostObjectUtil.invalidArgsError(CommonManager.HOST_OBJECT_NAME, functionName, "1", "string", args[0], false);
    }
    JaggeryContext jaggeryContext = CommonManager.getJaggeryContext();
    Stack<String> includesCallstack = CommonManager.getCallstack(jaggeryContext);
    Map<String, Boolean> includedScripts = CommonManager.getIncludes(jaggeryContext);
    String parent = includesCallstack.lastElement();
    String fileURL = (String) args[0];
    if (CommonManager.isHTTP(fileURL) || CommonManager.isHTTP(parent)) {
        CommonManager.include(cx, thisObj, args, funObj);
        return;
    }
    ScriptableObject scope = jaggeryContext.getScope();
    RhinoEngine engine = jaggeryContext.getEngine();
    if (fileURL.startsWith("/")) {
        fileURL = includesCallstack.firstElement() + fileURL;
    } else {
        fileURL = FilenameUtils.getFullPath(parent) + fileURL;
    }
    fileURL = FilenameUtils.normalize(fileURL);
    if (includesCallstack.search(fileURL) != -1) {
        return;
    }
    ScriptReader source = null;
    try {
        source = new ScriptReader(new FileInputStream(fileURL));
        includedScripts.put(fileURL, true);
        includesCallstack.push(fileURL);
        engine.exec(source, scope, null);
        includesCallstack.pop();
    } catch (FileNotFoundException e) {
        log.error(e.getMessage(), e);
        throw new ScriptException(e);
    }
}
Also used : ScriptException(org.jaggeryjs.scriptengine.exceptions.ScriptException) ScriptableObject(org.mozilla.javascript.ScriptableObject) ScriptReader(org.jaggeryjs.jaggery.core.ScriptReader) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings)

Example 37 with ScriptableObject

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

the class DatabaseHostObject method processResults.

private static Scriptable processResults(Context cx, Scriptable scope, DatabaseHostObject db, ResultSet results, boolean keyed) throws SQLException, ScriptException {
    List<ScriptableObject> rows = new ArrayList<ScriptableObject>();
    while (results.next()) {
        ScriptableObject row;
        ResultSetMetaData rsmd = results.getMetaData();
        if (keyed) {
            row = (ScriptableObject) db.context.newObject(db);
            for (int i = 0; i < rsmd.getColumnCount(); i++) {
                String columnName = rsmd.getColumnLabel(i + 1);
                Object columnValue = getValue(db, results, i + 1, rsmd.getColumnType(i + 1));
                row.put(columnName, row, columnValue);
            }
        } else {
            row = (ScriptableObject) cx.newArray(scope, rsmd.getColumnCount());
            for (int i = 0; i < rsmd.getColumnCount(); i++) {
                Object columnValue = getValue(db, results, i + 1, rsmd.getColumnType(i + 1));
                row.put(i + 1, row, columnValue);
            }
        }
        rows.add(row);
    }
    return db.context.newArray(db, rows.toArray());
}
Also used : ScriptableObject(org.mozilla.javascript.ScriptableObject) StreamHostObject(org.jaggeryjs.hostobjects.stream.StreamHostObject) NativeObject(org.mozilla.javascript.NativeObject) ScriptableObject(org.mozilla.javascript.ScriptableObject)

Example 38 with ScriptableObject

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

the class RhinoScriptEngine method getScope.

/**
     * Builds a Rhino variable scope that includes all of the scopes defined in the given script context as well as
     * the standard Rhino top-level environment. Also binds the variable {@code context} to point to the JSR 223
     * ScriptContext object, as per the JSR 223 spec.
     *
     * @param context the Rhino context to build the scope for.
     * @param scriptContext the JSR 223 script context.
     * @return a Rhino scope containing the given ScriptContext bindings and standard Rhino top-level bindings.
     */
private Scriptable getScope(final Context context, final ScriptContext scriptContext) {
    final Scriptable scope = new ScriptContextScope(scriptContext);
    final ScriptableObject topLevel = context.initStandardObjects();
    scope.setPrototype(topLevel);
    scope.put("context", scope, scriptContext);
    return scope;
}
Also used : ScriptableObject(org.mozilla.javascript.ScriptableObject) Scriptable(org.mozilla.javascript.Scriptable)

Example 39 with ScriptableObject

use of org.mozilla.javascript.ScriptableObject in project quorrabot by GloriousEggroll.

the class Script method load.

public void load() throws IOException {
    if (killed) {
        return;
    }
    context = Context.enter();
    ScriptableObject scope = context.initStandardObjects(global, false);
    scope.defineProperty("$", global, 0);
    scope.defineProperty("$api", ScriptApi.instance(), 0);
    scope.defineProperty("$script", this, 0);
    scope.defineProperty("$var", vars, 0);
    context.evaluateString(scope, FileUtils.readFileToString(file), file.getName(), 1, null);
}
Also used : ScriptableObject(org.mozilla.javascript.ScriptableObject)

Example 40 with ScriptableObject

use of org.mozilla.javascript.ScriptableObject in project sling by apache.

the class AsyncExtractor method extract.

public void extract(Object jsObj, UnaryCallback unaryCallback) {
    if (!isPromise(jsObj)) {
        unaryCallback.invoke(jsObj);
    }
    if (jsObj instanceof AsyncContainer) {
        ((AsyncContainer) jsObj).addListener(unaryCallback);
    }
    if (jsObj instanceof ScriptableObject) {
        ScriptableObject scriptableObject = (ScriptableObject) jsObj;
        decodeJSPromise(scriptableObject, unaryCallback);
    }
}
Also used : ScriptableObject(org.mozilla.javascript.ScriptableObject)

Aggregations

ScriptableObject (org.mozilla.javascript.ScriptableObject)44 Context (org.mozilla.javascript.Context)20 ScriptReader (org.jaggeryjs.jaggery.core.ScriptReader)8 ScriptException (org.jaggeryjs.scriptengine.exceptions.ScriptException)8 ContextAction (org.mozilla.javascript.ContextAction)6 ContextFactory (org.mozilla.javascript.ContextFactory)6 Scriptable (org.mozilla.javascript.Scriptable)6 JaggeryContext (org.jaggeryjs.scriptengine.engine.JaggeryContext)5 RhinoEngine (org.jaggeryjs.scriptengine.engine.RhinoEngine)5 IOException (java.io.IOException)4 SuppressFBWarnings (edu.umd.cs.findbugs.annotations.SuppressFBWarnings)3 StringReader (java.io.StringReader)3 Method (java.lang.reflect.Method)3 List (java.util.List)3 ServletContext (javax.servlet.ServletContext)3 Function (org.mozilla.javascript.Function)3 Function (com.google.common.base.Function)2 PrintWriter (java.io.PrintWriter)2 MalformedURLException (java.net.MalformedURLException)2 URL (java.net.URL)2