source: branches/0.17.x/abcl/src/org/armedbear/lisp/JHandler.java

Last change on this file was 12255, checked in by ehuelsmann, 16 years ago

Rename ConditionThrowable? to ControlTransfer? and remove

try/catch blocks which don't have anything to do with
non-local transfer of control.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 5.4 KB
Line 
1/*
2 * JHandler.java
3 *
4 * Copyright (C) 2003-2005 Andras Simon, Peter Graves
5 * $Id: JHandler.java 12255 2009-11-06 22:36:32Z ehuelsmann $
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
34package org.armedbear.lisp;
35
36import java.util.HashMap;
37import java.util.Map;
38import java.util.WeakHashMap;
39
40public final class JHandler extends Lisp
41{
42    private static final Map<Object,Map<String,Entry>> table =
43       new WeakHashMap<Object,Map<String,Entry>>();
44
45    public static void callLisp (String s, Object o)
46    {
47        callLisp(s, o, "");
48    }
49
50    public static void callLisp (String s, Object o, String s1)
51    {
52        callLisp(s, o, s1, new int[] {});
53    }
54
55    public static void callLisp (String s, Object o, String s1, int ai[]) {
56        callLisp(s, o, new String[] { s1 }, ai);
57    }
58
59    public static void callLisp (String s, Object o, String as[])
60    {
61        callLisp(s, o, as, new int[] {});
62    }
63
64    public static void callLisp (String s, Object o, String as[], int ai[])
65    {
66        if (table.containsKey(o)) {
67            Map<String,Entry> entryTable =  (Map<String,Entry>)table.get(o);
68            if (entryTable.containsKey(s)) {
69                Function f = ((Entry)entryTable.get(s)).getHandler();
70                LispObject data = ((Entry)entryTable.get(s)).getData();
71                Fixnum count = ((Entry)entryTable.get(s)).getCount();
72                Fixnum[] lispAi = new Fixnum[ai.length];
73                for (int i = 0; i < ai.length; i++) {
74                    lispAi[i] = Fixnum.getInstance(ai[i]);
75                }
76                LispObject lispAiVector = new SimpleVector(lispAi);
77                SimpleString[] lispAs = new SimpleString[as.length];
78                for (int i = 0; i < as.length; i++) {
79                    lispAs[i] = new SimpleString(as[i]);
80                }
81                LispObject lispAsVector = new SimpleVector(lispAs);
82                LispObject[] args = new LispObject[] //FIXME: count -> seq_num
83                { data, new JavaObject(o), lispAiVector, lispAsVector, Keyword.internKeyword(s), count };
84                f.execute(args);
85            }
86        }
87    }
88
89    // jregister-handler1 object event handler data count
90    private static final Primitive _JREGISTER_HANDLER =
91        new Primitive("%jregister-handler", PACKAGE_JAVA)
92    {
93        @Override
94        public LispObject execute(LispObject[] args)
95        {
96            if (args.length != 5)
97                return error(new WrongNumberOfArgumentsException(this));
98            Map<String,Entry> entryTable = null;
99            Object object = args[0].javaInstance();
100            String event = ((Symbol)args[1]).getName();
101            if (!table.containsKey(object)) {
102                entryTable = new HashMap<String,Entry>();
103                table.put(object,entryTable);
104            } else {
105                entryTable = (Map<String,Entry>)table.get(object);
106            }
107            Entry entry = new Entry((Function) args[2], args[3], event, entryTable);
108            if (args[4] != NIL)
109                entry.addCount(((Fixnum)args[4]).value);
110            entryTable.put(event,entry);
111            return T;
112        }
113    };
114
115    private static class Entry
116    {
117        Function handler;
118        LispObject data;
119        int count = -1;
120        Map<String,Entry> entryTable;
121        String event;
122
123        public Entry (Function handler, LispObject data, String event,
124                      Map<String,Entry> entryTable)
125        {
126            this.entryTable = entryTable;
127            this.event = event;
128            this.handler = handler;
129            this.data = data;
130        }
131
132        public Function getHandler ()
133        {
134            return handler;
135        }
136
137        public void addData (LispObject data)
138        {
139            this.data = data;
140        }
141
142        public LispObject getData ()
143        {
144            return data;
145        }
146
147        public void addCount (int count)
148        {
149            this.count = count;
150        }
151
152        public Fixnum getCount ()
153        {
154            if (count == 0)
155                entryTable.remove(event);
156            return (Fixnum.getInstance (count--));
157        }
158    }
159}
Note: See TracBrowser for help on using the repository browser.