Search in sources :

Example 1 with Undefined

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

the class JavascriptExecutor method execute.

@Override
public Object execute(Map<String, Object> variables) throws EvaluationException {
    Context cx = Context.enter();
    try {
        Scriptable scope = cx.newObject(prototype);
        scope.setPrototype(prototype);
        if (variables != null) {
            for (String k : variables.keySet()) {
                scope.put(k, scope, variables.get(k));
            }
        }
        Object out = compiledScript.exec(cx, scope);
        if (out instanceof NativeJavaObject) {
            return ((NativeJavaObject) out).unwrap();
        } else if (out instanceof Undefined) {
            return null;
        } else {
            return out;
        }
    } catch (RhinoException e) {
        throw new EvaluationException("Failed to execute script, see nested exception.", e);
    } finally {
        Context.exit();
    }
}
Also used : Context(org.mozilla.javascript.Context) Undefined(org.mozilla.javascript.Undefined) NativeJavaObject(org.mozilla.javascript.NativeJavaObject) RhinoException(org.mozilla.javascript.RhinoException) EvaluationException(org.neo4j.server.rest.domain.EvaluationException) Scriptable(org.mozilla.javascript.Scriptable) NativeJavaObject(org.mozilla.javascript.NativeJavaObject)

Example 2 with Undefined

use of org.mozilla.javascript.Undefined in project stetho by facebook.

the class JsRuntimeReplFactoryBuilder method importVariables.

private void importVariables(@NonNull ScriptableObject scope) throws StethoJsException {
    // Define the variables
    for (Map.Entry<String, Object> entrySet : mVariables.entrySet()) {
        String varName = entrySet.getKey();
        Object varValue = entrySet.getValue();
        try {
            Object jsValue;
            if (varValue instanceof Scriptable || varValue instanceof Undefined) {
                jsValue = varValue;
            } else {
                jsValue = Context.javaToJS(varValue, scope);
            }
            ScriptableObject.putProperty(scope, varName, jsValue);
        } catch (Exception e) {
            throw new StethoJsException(e, "Failed to setup variable: %s", varName);
        }
    }
}
Also used : Undefined(org.mozilla.javascript.Undefined) ScriptableObject(org.mozilla.javascript.ScriptableObject) Scriptable(org.mozilla.javascript.Scriptable) HashMap(java.util.HashMap) Map(java.util.Map)

Example 3 with Undefined

use of org.mozilla.javascript.Undefined 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)

Aggregations

Scriptable (org.mozilla.javascript.Scriptable)3 Undefined (org.mozilla.javascript.Undefined)3 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Context (org.mozilla.javascript.Context)1 NativeJavaObject (org.mozilla.javascript.NativeJavaObject)1 RhinoException (org.mozilla.javascript.RhinoException)1 ScriptableObject (org.mozilla.javascript.ScriptableObject)1 EvaluationException (org.neo4j.server.rest.domain.EvaluationException)1