source: branches/1.1.x/src/org/armedbear/lisp/FuncallableStandardObject.java

Last change on this file was 14086, checked in by rschlatte, 12 years ago

Eliminate numberOfRequiredArgs attribute from standard generic function

  • calculate it when required instead
File size: 8.0 KB
Line 
1/*
2 * FuncallableStandardObject.java
3 *
4 * Copyright (C) 2003-2006 Peter Graves, 2012 Rudolf Schlatte
5 * $Id$
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
20 *
21 * As a special exception, the copyright holders of this library give you
22 * permission to link this library with independent modules to produce an
23 * executable, regardless of the license terms of these independent
24 * modules, and to copy and distribute the resulting executable under
25 * terms of your choice, provided that you also meet, for each linked
26 * independent module, the terms and conditions of the license of that
27 * module.  An independent module is a module which is not derived from
28 * or based on this library.  If you modify this library, you may extend
29 * this exception to your version of the library, but you are not
30 * obligated to do so.  If you do not wish to do so, delete this
31 * exception statement from your version.
32 */
33
34
35// TODO: swap-slots is currently handled by StandardObject, so doesn't
36// exchange the functions.
37package org.armedbear.lisp;
38
39import static org.armedbear.lisp.Lisp.*;
40
41public class FuncallableStandardObject extends StandardObject
42{
43  protected LispObject function;
44
45  protected FuncallableStandardObject()
46  {
47    super();
48  }
49
50
51  protected FuncallableStandardObject(Layout layout)
52  {
53    this(layout, layout.getLength());
54  }
55
56  protected FuncallableStandardObject(Layout layout, int length)
57  {
58    super(layout, length);
59  }
60
61
62  protected FuncallableStandardObject(LispClass cls, int length)
63  {
64    super(cls, length);
65  }
66
67  protected FuncallableStandardObject(LispClass cls)
68  {
69    super(cls);
70  }
71
72  @Override
73  public LispObject typep(LispObject type)
74  {
75    if (type == Symbol.COMPILED_FUNCTION)
76      {
77        if (function != null)
78          return function.typep(type);
79        else
80          return NIL;
81      }
82    if (type == Symbol.FUNCALLABLE_STANDARD_OBJECT)
83      return T;
84    if (type == StandardClass.FUNCALLABLE_STANDARD_OBJECT)
85      return T;
86    return super.typep(type);
87  }
88
89  @Override
90  public LispObject execute()
91  {
92    return function.execute();
93  }
94
95  @Override
96  public LispObject execute(LispObject arg)
97  {
98    return function.execute(arg);
99  }
100
101  @Override
102  public LispObject execute(LispObject first, LispObject second)
103
104  {
105    return function.execute(first, second);
106  }
107
108  @Override
109  public LispObject execute(LispObject first, LispObject second,
110                            LispObject third)
111
112  {
113    return function.execute(first, second, third);
114  }
115
116  @Override
117  public LispObject execute(LispObject first, LispObject second,
118                            LispObject third, LispObject fourth)
119
120  {
121    return function.execute(first, second, third, fourth);
122  }
123
124  @Override
125  public LispObject execute(LispObject first, LispObject second,
126                            LispObject third, LispObject fourth,
127                            LispObject fifth)
128
129  {
130    return function.execute(first, second, third, fourth,
131                            fifth);
132  }
133
134  @Override
135  public LispObject execute(LispObject first, LispObject second,
136                            LispObject third, LispObject fourth,
137                            LispObject fifth, LispObject sixth)
138
139  {
140    return function.execute(first, second, third, fourth,
141                            fifth, sixth);
142  }
143
144  @Override
145  public LispObject execute(LispObject first, LispObject second,
146                            LispObject third, LispObject fourth,
147                            LispObject fifth, LispObject sixth,
148                            LispObject seventh)
149
150  {
151    return function.execute(first, second, third, fourth,
152                            fifth, sixth, seventh);
153  }
154
155  @Override
156  public LispObject execute(LispObject first, LispObject second,
157                            LispObject third, LispObject fourth,
158                            LispObject fifth, LispObject sixth,
159                            LispObject seventh, LispObject eighth)
160
161  {
162    return function.execute(first, second, third, fourth,
163                            fifth, sixth, seventh, eighth);
164  }
165
166  @Override
167  public LispObject execute(LispObject[] args)
168  {
169    return function.execute(args);
170  }
171
172  private static final Primitive _ALLOCATE_FUNCALLABLE_INSTANCE
173    = new pf__allocate_funcallable_instance();
174  @DocString(name="%allocate-funcallable-instance",
175             args="class",
176             returns="instance")
177  private static final class pf__allocate_funcallable_instance extends Primitive
178  {
179    pf__allocate_funcallable_instance()
180    {
181      super("%allocate-funcallable-instance", PACKAGE_SYS, true, "class");
182    }
183    @Override
184    public LispObject execute(LispObject arg)
185    {
186      if (arg.typep(StandardClass.FUNCALLABLE_STANDARD_CLASS) != NIL) {
187        LispObject l = Symbol.CLASS_LAYOUT.execute(arg);
188        if (! (l instanceof Layout)) {
189          return error(new ProgramError("Invalid standard class layout for: " + arg.princToString()));
190        }
191        // KLUDGE (rudi 2012-03-17): make (make-instance
192        // 'standard-generic-function) work -- subsequent code expects
193        // the additional slots to be present.
194        return new StandardGenericFunction((Layout)l);
195      }
196      return type_error(arg, Symbol.FUNCALLABLE_STANDARD_CLASS);
197    }
198  };
199
200  // AMOP p. 230
201  private static final Primitive SET_FUNCALLABLE_INSTANCE_FUNCTION
202    = new pf_set_funcallable_instance_function();
203  @DocString(name="set-funcallable-instance-function",
204             args="funcallable-instance function",
205             returns="unspecified")
206  private static final class pf_set_funcallable_instance_function extends Primitive
207  {
208    pf_set_funcallable_instance_function()
209    {
210      super("set-funcallable-instance-function", PACKAGE_MOP, true,
211            "funcallable-instance function");
212    }
213    @Override
214    public LispObject execute(LispObject first, LispObject second)
215    {
216      checkFuncallableStandardObject(first).function = second;
217      return second;
218    }
219  };
220
221  private static final Primitive FUNCALLABLE_INSTANCE_FUNCTION
222    = new pf_funcallable_instance_function();
223  @DocString(name="funcallable-instance-function",
224             args="funcallable-instance",
225             returns="function")
226  private static final class pf_funcallable_instance_function extends Primitive
227  {
228    pf_funcallable_instance_function()
229    {
230      super("funcallable-instance-function", PACKAGE_MOP, false,
231            "funcallable-instance");
232    }
233    @Override
234    public LispObject execute(LispObject arg)
235    {
236      return checkFuncallableStandardObject(arg).function;
237    }
238  };
239
240
241  // Profiling.
242  private int callCount;
243  private int hotCount;
244
245  @Override
246  public final int getCallCount()
247  {
248    return callCount;
249  }
250
251  @Override
252  public void setCallCount(int n)
253  {
254    callCount = n;
255  }
256
257  @Override
258  public final void incrementCallCount()
259  {
260    ++callCount;
261  }
262
263  @Override
264  public final int getHotCount()
265  {
266    return hotCount;
267  }
268
269  @Override
270  public void setHotCount(int n)
271  {
272    hotCount = n;
273  }
274
275  @Override
276  public final void incrementHotCount()
277  {
278    ++hotCount;
279  }
280
281  public static final FuncallableStandardObject checkFuncallableStandardObject(LispObject obj)
282  {
283    if (obj instanceof FuncallableStandardObject)
284      return (FuncallableStandardObject) obj;
285    return (FuncallableStandardObject) // Not reached.
286      type_error(obj, Symbol.FUNCALLABLE_STANDARD_OBJECT);
287  }
288
289}
Note: See TracBrowser for help on using the repository browser.