Search in sources :

Example 11 with Wrapper

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

the class RhinoScript method execute.

public Object execute(Scope scope) throws ScriptException {
    try {
        Context cx = Context.getCurrentContext();
        Object result;
        // TODO: Typecast to RhinoScope can be wrong, e.g. when calling
        // from another language
        result = script.exec(cx, ((RhinoScope) scope).getScope());
        if (result instanceof Wrapper)
            result = ((Wrapper) result).unwrap();
        return result;
    } catch (Throwable t) {
        throw new RhinoScriptException(engine, t);
    }
}
Also used : Context(org.mozilla.javascript.Context) Wrapper(org.mozilla.javascript.Wrapper)

Example 12 with Wrapper

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

the class MapAdapter method get.

public Object get(Object key) {
    Object value;
    if (key instanceof Integer)
        value = ScriptableObject.getProperty(object, ((Integer) key).intValue());
    else if (key instanceof String)
        value = ScriptableObject.getProperty(object, (String) key);
    else
        value = null;
    if (value instanceof Wrapper)
        value = ((Wrapper) value).unwrap();
    else if (value == ScriptableObject.NOT_FOUND)
        value = null;
    else if (value instanceof NativeArray) {
        // Convert to a normal array
        // TODO: see if we need to convert the other way in put?
        NativeArray array = (NativeArray) value;
        int length = (int) array.getLength();
        Object[] list = new Object[length];
        for (int i = 0; i < length; i++) {
            Object obj = array.get(i, array);
            if (obj instanceof Wrapper)
                obj = ((Wrapper) obj).unwrap();
            list[i] = obj;
        }
        return list;
    }
    return value;
}
Also used : NativeArray(org.mozilla.javascript.NativeArray) Wrapper(org.mozilla.javascript.Wrapper) ScriptableObject(org.mozilla.javascript.ScriptableObject)

Example 13 with Wrapper

use of org.mozilla.javascript.Wrapper in project jmeter by apache.

the class BSFJavaScriptEngine method call.

/**
 * Return an object from an extension.
 * @param object Object on which to make the call (ignored).
 * @param method The name of the method to call.
 * @param args an array of arguments to be
 * passed to the extension, which may be either
 * Vectors of Nodes, or Strings.
 */
@Override
public Object call(Object object, String method, Object[] args) throws BSFException {
    Object retval = null;
    Context cx;
    try {
        cx = Context.enter();
        // REMIND: convert arg list Vectors here?
        Object fun = global.get(method, global);
        // Any way to make these arguments *sensible?
        if (fun == Scriptable.NOT_FOUND) {
            throw new EvaluatorException("function " + method + " not found.", "none", 0);
        }
        cx.setOptimizationLevel(-1);
        cx.setGeneratingDebug(false);
        cx.setGeneratingSource(false);
        cx.setOptimizationLevel(0);
        cx.setDebugger(null, null);
        retval = ((Function) fun).call(cx, global, global, args);
        if (retval instanceof Wrapper) {
            retval = ((Wrapper) retval).unwrap();
        }
    } catch (Throwable t) {
        // NOSONAR We handle correctly Error case in function
        handleError(t);
    } finally {
        Context.exit();
    }
    return retval;
}
Also used : Context(org.mozilla.javascript.Context) Wrapper(org.mozilla.javascript.Wrapper) EvaluatorException(org.mozilla.javascript.EvaluatorException) NativeJavaObject(org.mozilla.javascript.NativeJavaObject)

Aggregations

Wrapper (org.mozilla.javascript.Wrapper)13 ScriptableObject (org.mozilla.javascript.ScriptableObject)7 Context (org.mozilla.javascript.Context)4 Scriptable (org.mozilla.javascript.Scriptable)4 NativeArray (org.mozilla.javascript.NativeArray)3 XMLObject (org.mozilla.javascript.xml.XMLObject)3 BigDecimal (java.math.BigDecimal)2 BigInteger (java.math.BigInteger)2 ConsString (org.mozilla.javascript.ConsString)2 NativeJavaObject (org.mozilla.javascript.NativeJavaObject)2 NativeObject (org.mozilla.javascript.NativeObject)2 List (com.scratchdisk.list.List)1 ReadOnlyList (com.scratchdisk.list.ReadOnlyList)1 ReadOnlyStringIndexList (com.scratchdisk.list.ReadOnlyStringIndexList)1 StringIndexList (com.scratchdisk.list.StringIndexList)1 OutputStreamWriter (java.io.OutputStreamWriter)1 PrintWriter (java.io.PrintWriter)1 StringWriter (java.io.StringWriter)1 Date (java.util.Date)1 HashMap (java.util.HashMap)1