Search in sources :

Example 1 with Callable

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

the class Bug482203 method testJsApi.

public void testJsApi() throws Exception {
    Context cx = Context.enter();
    cx.setOptimizationLevel(-1);
    Script script = cx.compileReader(new InputStreamReader(Bug482203.class.getResourceAsStream("conttest.js")), "", 1, null);
    Scriptable scope = cx.initStandardObjects();
    script.exec(cx, scope);
    for (; ; ) {
        Object cont = ScriptableObject.getProperty(scope, "c");
        if (cont == null) {
            break;
        }
        ((Callable) cont).call(cx, scope, scope, new Object[] { null });
    }
}
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) Callable(org.mozilla.javascript.Callable)

Example 2 with Callable

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

the class ExtendedJavaClass method construct.

public Scriptable construct(Context cx, Scriptable scope, Object[] args) {
    // If the normal constructor failed, try to see if the last
    // argument is a Callable or a NativeObject object. 
    // If it is a NativeObject, use it as a hashtable containing
    // fields to be added to the object. If it is a Callable,
    // call it on the object and again use its return value
    // as a hashtable if it is a NativeObject.
    Class classObject = getClassObject();
    int modifiers = classObject.getModifiers();
    NativeObject properties = null;
    Callable initialize = null;
    if (args.length > 0 && !Modifier.isInterface(modifiers) && !Modifier.isAbstract(modifiers)) {
        // Look at the last argument to find out if we need to do something
        // special. Possibilities: a object literal that defines fields to
        // be set, or a function that is executed on the object and of which
        // the result can be fields to be set.
        Object last = args[args.length - 1];
        // which might be arguments to methods...
        if (last instanceof Callable && !(last instanceof NativeJavaClass))
            initialize = (Callable) last;
        else if (last instanceof NativeObject) {
            // Now see if the constructor takes a Map as the last argument.
            // If so, the NativeObject will be converted to it thought
            // RhinoWrapFactory. Otherwise, the NativeObject is used
            // as the properties to be set on the instance after creation.
            MemberBox ctor = findConstructor(cx, args);
            if (ctor != null) {
                Class[] types = ctor.ctor().getParameterTypes();
                Class lastType = types[types.length - 1];
                // of which NativeObject's can be converted to.
                if (!ArgumentReader.class.isAssignableFrom(lastType) && !Map.class.isAssignableFrom(lastType)) {
                    properties = (NativeObject) last;
                    if (ScriptableObject.hasProperty(properties, "unwrap"))
                        properties = null;
                }
            } else {
                // There is no constructor that has to be checked, so it
                // can only be a properties list.
                properties = (NativeObject) last;
            }
            if (properties != null) {
                // Support initialize in the passed object literal too.
                Object obj = ScriptableObject.getProperty(properties, "initialize");
                if (obj instanceof Callable)
                    initialize = (Callable) obj;
            }
        }
        // will be found:
        if (initialize != null || properties != null) {
            Object[] newArgs = new Object[args.length - 1];
            for (int i = 0; i < newArgs.length; i++) newArgs[i] = args[i];
            args = newArgs;
        }
    }
    Scriptable obj = super.construct(cx, scope, args);
    // properties returned by initialize.
    if (properties != null)
        setProperties(obj, properties);
    // into the object after, if it is a NativeObject.
    if (initialize != null) {
        Object res = initialize.call(cx, scope, obj, args);
        if (res instanceof NativeObject)
            setProperties(obj, (NativeObject) res);
    }
    return obj;
}
Also used : NativeObject(org.mozilla.javascript.NativeObject) MemberBox(org.mozilla.javascript.MemberBox) NativeJavaClass(org.mozilla.javascript.NativeJavaClass) NativeObject(org.mozilla.javascript.NativeObject) ScriptableObject(org.mozilla.javascript.ScriptableObject) NativeJavaClass(org.mozilla.javascript.NativeJavaClass) ArgumentReader(com.scratchdisk.script.ArgumentReader) Scriptable(org.mozilla.javascript.Scriptable) IdentityHashMap(java.util.IdentityHashMap) Map(java.util.Map) HashMap(java.util.HashMap) Callable(org.mozilla.javascript.Callable)

Example 3 with Callable

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

the class RhinoEngine method handleOperator.

public Object handleOperator(Context cx, Scriptable scope, int operator, Object lhs, Object rhs) {
    // be overridden here!
    if (lhs instanceof String && (operator == Token.SUB || operator == Token.MUL || operator == Token.DIV))
        lhs = ScriptRuntime.toObject(cx, scope, lhs);
    // Now perform the magic
    if (lhs instanceof Scriptable) {
        String name = null;
        switch(operator) {
            case Token.ADD:
                name = "add";
                break;
            case Token.SUB:
                name = "subtract";
                break;
            case Token.MUL:
                name = "multiply";
                break;
            case Token.DIV:
                name = "divide";
                break;
            case Token.MOD:
                name = "modulo";
                break;
            case Token.EQ:
            case Token.NE:
                name = "equals";
                break;
        }
        if (name != null) {
            Scriptable scriptable = (Scriptable) lhs;
            Object obj = ScriptableObject.getProperty(scriptable, name);
            if (obj instanceof Callable) {
                Object result = ((Callable) obj).call(cx, scope, scriptable, new Object[] { rhs });
                if (operator == Token.EQ || operator == Token.NE) {
                    boolean value = ScriptRuntime.toBoolean(result);
                    if (operator == Token.NE)
                        value = !value;
                    return ScriptRuntime.wrapBoolean(value);
                } else {
                    return result;
                }
            }
        }
    }
    return null;
}
Also used : ScriptableObject(org.mozilla.javascript.ScriptableObject) Scriptable(org.mozilla.javascript.Scriptable) Callable(org.mozilla.javascript.Callable)

Example 4 with Callable

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

the class RhinoEngine method handleSignOperator.

public Object handleSignOperator(Context cx, Scriptable scope, int operator, Object rhs) {
    switch(operator) {
        case Token.NEG:
            // Wrap String as Scriptable, so we can access its prototype easily too.
            if (rhs instanceof String)
                rhs = ScriptRuntime.toObject(cx, scope, rhs);
            // Now perform the magic
            if (rhs instanceof Scriptable) {
                Scriptable scriptable = (Scriptable) rhs;
                Object obj = ScriptableObject.getProperty(scriptable, "negate");
                if (obj instanceof Callable)
                    return ((Callable) obj).call(cx, scope, scriptable, new Object[] {});
            }
            break;
        case Token.POS:
            // converted top + new Point(1, 2) by the interpreter...
            return rhs;
    }
    return null;
}
Also used : ScriptableObject(org.mozilla.javascript.ScriptableObject) Scriptable(org.mozilla.javascript.Scriptable) Callable(org.mozilla.javascript.Callable)

Aggregations

Callable (org.mozilla.javascript.Callable)4 Scriptable (org.mozilla.javascript.Scriptable)4 ScriptableObject (org.mozilla.javascript.ScriptableObject)4 ArgumentReader (com.scratchdisk.script.ArgumentReader)1 InputStreamReader (java.io.InputStreamReader)1 HashMap (java.util.HashMap)1 IdentityHashMap (java.util.IdentityHashMap)1 Map (java.util.Map)1 Context (org.mozilla.javascript.Context)1 MemberBox (org.mozilla.javascript.MemberBox)1 NativeJavaClass (org.mozilla.javascript.NativeJavaClass)1 NativeObject (org.mozilla.javascript.NativeObject)1 Script (org.mozilla.javascript.Script)1