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