01: /*
02: * @(#)call.java 1.3 05/04/20
03: *
04: * Copyright (c) 2004-2005 Sun Microsystems, Inc. All Rights Reserved.
05: *
06: * See the file "LICENSE.txt" for information on usage and redistribution
07: * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
08: */
09: package org.pnuts.lib;
10:
11: import pnuts.lang.*;
12: import pnuts.lang.Runtime;
13: import java.util.*;
14:
15: public class call extends PnutsFunction {
16:
17: public call() {
18: super ("call");
19: }
20:
21: public call(String name) {
22: super (name);
23: }
24:
25: public boolean defined(int narg) {
26: return (narg == 1 || narg == 2);
27: }
28:
29: protected Object exec(Object args[], Context context) {
30: int nargs = args.length;
31: Object target;
32: Object a1 = null;
33: Object[] arguments;
34: if (nargs == 1) {
35: target = args[0];
36: } else if (nargs == 2) {
37: target = args[0];
38: a1 = args[1];
39: } else {
40: undefined(args, context);
41: return null;
42: }
43: if (a1 == null) {
44: arguments = new Object[] {};
45: } else if (a1 instanceof Object[]) {
46: arguments = (Object[]) a1;
47: } else if (a1 instanceof List) {
48: arguments = ((List) a1).toArray();
49: } else if (a1 instanceof Collection) {
50: ArrayList list = new ArrayList((Collection) a1);
51: arguments = list.toArray();
52: } else if (a1 instanceof Iterator) {
53: ArrayList list = new ArrayList();
54: for (Iterator it = (Iterator) a1; it.hasNext();) {
55: list.add(it.next());
56: }
57: arguments = list.toArray();
58: } else if (a1 instanceof Enumeration) {
59: ArrayList list = new ArrayList();
60: for (Enumeration en = (Enumeration) a1; en
61: .hasMoreElements();) {
62: list.add(en.nextElement());
63: }
64: arguments = list.toArray();
65: } else if (a1 instanceof Generator) {
66: Generator g = (Generator) a1;
67: final ArrayList list = new ArrayList();
68: g.apply(new PnutsFunction() {
69: protected Object exec(Object[] args, Context context) {
70: list.add(args[0]);
71: return null;
72: }
73: }, context);
74: arguments = list.toArray();
75: } else {
76: throw new IllegalArgumentException(a1.getClass() + " ("
77: + String.valueOf(a1) + ")");
78: }
79: return Runtime.call(context, target, arguments, null);
80: }
81:
82: public String toString() {
83: return "function call(callableOrClass {, arguments })";
84: }
85: }
|