8865f3ba2b7e45eb5bf84b3c741a6d24a38cf976
[org.ibex.core.git] / src / org / xwt / js / JSCallable.java
1 package org.xwt.js;
2
3 /** anything that is callable with the () operator and wasn't compiled from JS code */
4 public abstract class JSCallable extends JSObj {
5     public Object call0(Object method) throws JS.Exn {
6         JSArray args = new JSArray();
7         return call(method, args);
8     }
9     public Object call1(Object method, Object arg1) throws JS.Exn {
10         JSArray args = new JSArray();
11         args.addElement(arg1);
12         return call(method, args);
13     }
14     public Object call2(Object method, Object arg1, Object arg2) throws JS.Exn {
15         JSArray args = new JSArray();
16         args.addElement(arg1);
17         args.addElement(arg2);
18         return call(method, args);
19     }
20     public Object call(Object method, JSArray args) throws JS.Exn {
21         if (method != null) {
22             Object meth = get(method);
23             if (meth == null) throw new JS.Exn("attempt to invoke the null method");
24             if (!(meth instanceof JSCallable)) throw new JS.Exn("cannot call a " + meth.getClass().getName());
25             switch (args.size()) {
26                 case 0: return ((JSCallable)meth).call0(null);
27                 case 1: return ((JSCallable)meth).call1(null, args.elementAt(0));
28                 case 2: return ((JSCallable)meth).call2(null, args.elementAt(0), args.elementAt(1));
29                 default: return ((JSCallable)meth).call(null, args);
30             }
31         }
32         throw new JS.Exn("cannot invoke method " + method + " on object " + this.getClass().getName() + " with " + args.size() + " arguments");
33     }
34 }
35