01: /*
02: * @(#)isFunction.java 1.2 04/12/06
03: *
04: * Copyright (c) 1997-2004 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.Context;
12: import pnuts.lang.PnutsFunction;
13:
14: public class isFunction extends PnutsFunction {
15:
16: public isFunction() {
17: super ("isFunction");
18: }
19:
20: public boolean defined(int narg) {
21: return (narg == 1 || narg == 2);
22: }
23:
24: protected Object exec(Object[] args, Context context) {
25: int nargs = args.length;
26: if (nargs == 1) {
27: return new Boolean(args[0] instanceof PnutsFunction);
28: } else if (nargs == 2) {
29: Object arg = args[0];
30: int arity = ((Integer) args[1]).intValue();
31: if (arg instanceof PnutsFunction) {
32: PnutsFunction func = (PnutsFunction) arg;
33: if (arity >= 0
34: && (func.defined(arity) || func.defined(-1))) {
35: return Boolean.TRUE;
36: }
37: }
38: return Boolean.FALSE;
39: } else {
40: undefined(args, context);
41: return null;
42: }
43: }
44:
45: public String toString() {
46: return "function isFunction(obj {, nargs })";
47: }
48: }
|