Search in sources :

Example 71 with Scriptable

use of org.mozilla.javascript.Scriptable in project jslint4java by happygiraffe.

the class Util method listValue.

/**
     * Convert a JavaScript array into a Java {@link List}. You must provide a
     * converter which will be called on each value in order to convert it to a
     * Java object.
     *
     * @param <T>
     *            The type of every array member.
     * @param name
     *            The name of the array.
     * @param scope
     *            The scope which contains the array.
     * @param c
     *            A {@link Converter} instance to change the JavaScript object
     *            into a Java one.
     * @return A {@link List} of Java objects.
     */
static <T> List<T> listValue(String name, Scriptable scope, Converter<T> c) {
    Object val = scope.get(name, scope);
    if (val == UniqueTag.NOT_FOUND || val instanceof Undefined) {
        return new ArrayList<T>();
    }
    Scriptable ary = (Scriptable) val;
    int count = intValue("length", ary);
    List<T> list = new ArrayList<T>(count);
    for (int i = 0; i < count; i++) {
        list.add(c.convert(ary.get(i, ary)));
    }
    return list;
}
Also used : Undefined(org.mozilla.javascript.Undefined) ArrayList(java.util.ArrayList) Scriptable(org.mozilla.javascript.Scriptable)

Example 72 with Scriptable

use of org.mozilla.javascript.Scriptable in project SmartZPN by andforce.

the class PacScriptParser method runScript.

private String runScript(String js, String functionName, Object[] functionParams) {
    Context rhino = Context.enter();
    rhino.setOptimizationLevel(-1);
    try {
        Scriptable scope = rhino.initStandardObjects();
        rhino.evaluateString(scope, js, "JavaScript", js.split("\n").length, null);
        Function function = (Function) scope.get(functionName, scope);
        Object result = function.call(rhino, scope, scope, functionParams);
        if (result instanceof String) {
            return (String) result;
        } else if (result instanceof NativeJavaObject) {
            return (String) ((NativeJavaObject) result).getDefaultValue(String.class);
        } else if (result instanceof NativeObject) {
            return (String) ((NativeObject) result).getDefaultValue(String.class);
        }
        return result.toString();
    } finally {
        Context.exit();
    }
}
Also used : Context(org.mozilla.javascript.Context) NativeObject(org.mozilla.javascript.NativeObject) Function(org.mozilla.javascript.Function) NativeObject(org.mozilla.javascript.NativeObject) NativeJavaObject(org.mozilla.javascript.NativeJavaObject) ScriptableObject(org.mozilla.javascript.ScriptableObject) Scriptable(org.mozilla.javascript.Scriptable) NativeJavaObject(org.mozilla.javascript.NativeJavaObject)

Example 73 with Scriptable

use of org.mozilla.javascript.Scriptable in project neo4j by neo4j.

the class JavascriptExecutor method createPrototype.

private Scriptable createPrototype(Context cx) {
    Scriptable proto = cx.initStandardObjects();
    Scriptable topLevel = new ImporterTopLevel(cx);
    proto.setParentScope(topLevel);
    return proto;
}
Also used : ImporterTopLevel(org.mozilla.javascript.ImporterTopLevel) Scriptable(org.mozilla.javascript.Scriptable)

Example 74 with Scriptable

use of org.mozilla.javascript.Scriptable in project scriptographer by scriptographer.

the class ExtendedJavaClass method get.

public Object get(String name, Scriptable start) {
    // We are completely redefining get here without relying on
    // NativeJavaClass' implementation, in order to add more JS
    // like behavior.
    // When used as a constructor, ScriptRuntime.newObject() asks
    // for our prototype to create an object of the correct type.
    // We don't really care what the object is, since we're returning
    // one constructed out of whole cloth, so we return null.
    boolean isProto = name.equals("prototype");
    if (!isProto) {
        if (members.has(name, true)) {
            return members.get(this, name, javaObject, true);
        }
        // to be more logical / java-like. TODO: Is this a Rhino bug?
        if (staticFieldAndMethods != null) {
            Object result = staticFieldAndMethods.get(name);
            if (result != null)
                return result;
        }
    }
    if (properties != null && properties.containsKey(name)) {
        // see whether this object defines the property.
        return properties.get(name);
    } else if (isProto) {
        // getPrototype creates prototype Objects on the fly:
        return getInstancePrototype();
    } else {
        Scriptable proto = getPrototype();
        if (proto != null) {
            Object result = proto.get(name, start);
            if (result != Scriptable.NOT_FOUND)
                return result;
        }
    }
    // Experimental: look for nested classes by appending $name to
    // current class' name.
    Class<?> nestedClass = findNestedClass(getClassObject(), name);
    if (nestedClass != null) {
        ExtendedJavaClass nestedValue = new ExtendedJavaClass(ScriptableObject.getTopLevelScope(this), nestedClass, properties != null);
        nestedValue.setParentScope(this);
        return nestedValue;
    }
    return Scriptable.NOT_FOUND;
}
Also used : NativeObject(org.mozilla.javascript.NativeObject) ScriptableObject(org.mozilla.javascript.ScriptableObject) Scriptable(org.mozilla.javascript.Scriptable)

Example 75 with Scriptable

use of org.mozilla.javascript.Scriptable in project scriptographer by scriptographer.

the class ExtendedJavaObject method get.

public Object get(String name, Scriptable start) {
    // defined in the underlying Java object
    if (properties != null && properties.containsKey(name))
        return properties.get(name);
    if (changeReceiver != null)
        fetchChangeReceiver();
    Scriptable prototype = getPrototype();
    Object result = prototype.get(name, this);
    if (result != Scriptable.NOT_FOUND)
        return result;
    result = members.get(this, name, javaObject, false);
    if (result != Scriptable.NOT_FOUND) {
        if (javaObject instanceof ChangeReceiver)
            handleChangeEmitter(result, name);
        return result;
    }
    if (name.equals("prototype"))
        return prototype;
    return Scriptable.NOT_FOUND;
}
Also used : ChangeReceiver(com.scratchdisk.script.ChangeReceiver) NativeJavaObject(org.mozilla.javascript.NativeJavaObject) ScriptableObject(org.mozilla.javascript.ScriptableObject) Scriptable(org.mozilla.javascript.Scriptable)

Aggregations

Scriptable (org.mozilla.javascript.Scriptable)90 Context (org.mozilla.javascript.Context)44 ScriptableObject (org.mozilla.javascript.ScriptableObject)30 ContextAction (org.mozilla.javascript.ContextAction)12 NativeJavaObject (org.mozilla.javascript.NativeJavaObject)7 NativeObject (org.mozilla.javascript.NativeObject)7 Function (org.mozilla.javascript.Function)6 Map (java.util.Map)5 Test (org.junit.Test)5 RhinoException (org.mozilla.javascript.RhinoException)4 InputStreamReader (java.io.InputStreamReader)3 ArrayList (java.util.ArrayList)3 IdentityHashMap (java.util.IdentityHashMap)3 ScriptContext (javax.script.ScriptContext)3 Callable (org.mozilla.javascript.Callable)3 ContextFactory (org.mozilla.javascript.ContextFactory)3 NativeJavaClass (org.mozilla.javascript.NativeJavaClass)3 ReadOnlyList (com.scratchdisk.list.ReadOnlyList)2 ArgumentReader (com.scratchdisk.script.ArgumentReader)2 WeakIdentityHashMap (com.scratchdisk.util.WeakIdentityHashMap)2