1 // -*- Mode: Java; indent-tabs-mode: t; tab-width: 4 -*-
2 // ---------------------------------------------------------------------------
4 // Copyright (C) Stephanie Gawroriski <xer@multiphasicapps.net>
5 // ---------------------------------------------------------------------------
6 // SquirrelJME is under the GNU General Public License v3+, or later.
7 // See license.mkd for licensing and copyright information.
8 // ---------------------------------------------------------------------------
10 package cc
.squirreljme
.vm
.springcoat
;
12 import cc
.squirreljme
.jvm
.mle
.AtomicShelf
;
13 import cc
.squirreljme
.jvm
.mle
.DebugShelf
;
14 import cc
.squirreljme
.jvm
.mle
.JarPackageShelf
;
15 import cc
.squirreljme
.jvm
.mle
.MathShelf
;
16 import cc
.squirreljme
.jvm
.mle
.ObjectShelf
;
17 import cc
.squirreljme
.jvm
.mle
.PencilShelf
;
18 import cc
.squirreljme
.jvm
.mle
.ReferenceShelf
;
19 import cc
.squirreljme
.jvm
.mle
.ReflectionShelf
;
20 import cc
.squirreljme
.jvm
.mle
.RuntimeShelf
;
21 import cc
.squirreljme
.jvm
.mle
.TaskShelf
;
22 import cc
.squirreljme
.jvm
.mle
.TerminalShelf
;
23 import cc
.squirreljme
.jvm
.mle
.ThreadShelf
;
24 import cc
.squirreljme
.jvm
.mle
.TypeShelf
;
25 import cc
.squirreljme
.jvm
.mle
.UIFormShelf
;
26 import cc
.squirreljme
.jvm
.mle
.exceptions
.MLECallError
;
27 import cc
.squirreljme
.vm
.springcoat
.exceptions
.SpringMLECallError
;
28 import cc
.squirreljme
.vm
.springcoat
.exceptions
.SpringVirtualMachineException
;
30 import java
.util
.TreeMap
;
31 import net
.multiphasicapps
.classfile
.ClassName
;
32 import net
.multiphasicapps
.classfile
.MethodNameAndType
;
39 public enum MLEDispatcher
40 implements MLEDispatcherKey
42 /** {@link AtomicShelf}. */
43 ATOMIC("cc/squirreljme/jvm/mle/AtomicShelf",
46 /** {@link DebugShelf}. */
47 DEBUG("cc/squirreljme/jvm/mle/DebugShelf",
50 /** {@link JarPackageShelf}. */
51 JAR_PACKAGE("cc/squirreljme/jvm/mle/JarPackageShelf",
52 MLEJarPackage
.values()),
54 /** {@link MathShelf}. */
55 MATH("cc/squirreljme/jvm/mle/MathShelf",
58 /** {@link ObjectShelf}. */
59 OBJECT("cc/squirreljme/jvm/mle/ObjectShelf",
62 /** {@link PencilShelf}. */
63 PENCIL("cc/squirreljme/jvm/mle/PencilShelf",
66 /** {@link ReferenceShelf}. */
67 REFERENCE("cc/squirreljme/jvm/mle/ReferenceShelf",
68 MLEReference
.values()),
70 /** {@link ReflectionShelf}. */
71 REFLECTION("cc/squirreljme/jvm/mle/ReflectionShelf",
72 MLEReflection
.values()),
74 /** {@link RuntimeShelf}. */
75 RUNTIME("cc/squirreljme/jvm/mle/RuntimeShelf",
78 /** {@link TaskShelf}. */
79 TASK("cc/squirreljme/jvm/mle/TaskShelf",
82 /** {@link TerminalShelf}. */
83 TERMINAL("cc/squirreljme/jvm/mle/TerminalShelf",
84 MLETerminal
.values()),
86 /** {@link ThreadShelf}. */
87 THREAD("cc/squirreljme/jvm/mle/ThreadShelf",
90 /** {@link TypeShelf}. */
91 TYPE("cc/squirreljme/jvm/mle/TypeShelf",
94 /** {@link UIFormShelf}. */
95 UI_FORM("cc/squirreljme/jvm/mle/UIFormShelf",
101 /** The function tree. */
102 private static Map
<String
, Map
<String
, MLEDispatcherTarget
>> _fnTree
;
104 /** The dispatcher key. */
105 protected final String key
;
107 /** The functions. */
108 private final MLEFunction
[] _functions
;
112 Map
<String
, Map
<String
, MLEDispatcherTarget
>> functionTree
=
115 // Build the function tree
116 for (MLEDispatcher dispatch
: MLEDispatcher
.values())
118 Map
<String
, MLEDispatcherTarget
> subTree
= new TreeMap
<>();
120 for (MLEFunction function
: dispatch
._functions
)
121 subTree
.put(function
.key(), function
);
123 functionTree
.put(dispatch
.key(), subTree
);
126 MLEDispatcher
._fnTree
= functionTree
;
130 * Initializes the dispatcher info.
132 * @param __key The key.
133 * @param __functions Functions for the dispatcher.
134 * @throws NullPointerException On null arguments.
137 MLEDispatcher(String __key
, MLEFunction
[] __functions
)
138 throws NullPointerException
140 if (__key
== null || __functions
== null)
141 throw new NullPointerException("NARG");
144 this._functions
= __functions
;
158 * Handles the dispatching of the native method.
160 * @param __thread The current thread this is acting under.
161 * @param __class The native class being called.
162 * @param __func The method being called.
163 * @param __args The arguments to the call.
164 * @return The resulting object returned by the dispatcher.
165 * @throws NullPointerException On null arguments.
168 public static Object
dispatch(SpringThreadWorker __thread
,
169 ClassName __class
, MethodNameAndType __func
, Object
... __args
)
170 throws NullPointerException
172 if (__thread
== null || __class
== null)
173 throw new NullPointerException("NARG");
176 Map
<String
, MLEDispatcherTarget
> subTree
= MLEDispatcher
._fnTree
.get(
179 throw new SpringVirtualMachineException(String
.format(
180 "Unknown MLE Shelf: %s", __class
));
182 // Find the target function
183 MLEDispatcherTarget target
= subTree
.get(__func
.toString());
185 throw new SpringVirtualMachineException(String
.format(
186 "Unknown MLE Shelf Function: %s::%s", __class
, __func
));
191 return target
.handle(__thread
, __args
);
193 catch (MLECallError e
)
195 throw new SpringMLECallError(
196 String
.format("Unwrapped MLECallError calling %s:%s",
197 __class
, __func
), e
);