Remove Assembly.
[SquirrelJME.git] / emulators / springcoat-vm / src / main / java / cc / squirreljme / vm / springcoat / MLEDispatcher.java
blobde5e1c4ca7bc9462efaaba0a1d72bdf22748e7e5
1 // -*- Mode: Java; indent-tabs-mode: t; tab-width: 4 -*-
2 // ---------------------------------------------------------------------------
3 // SquirrelJME
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;
29 import java.util.Map;
30 import java.util.TreeMap;
31 import net.multiphasicapps.classfile.ClassName;
32 import net.multiphasicapps.classfile.MethodNameAndType;
34 /**
35 * Not Described.
37 * @since 2020/06/18
39 public enum MLEDispatcher
40 implements MLEDispatcherKey
42 /** {@link AtomicShelf}. */
43 ATOMIC("cc/squirreljme/jvm/mle/AtomicShelf",
44 MLEAtomic.values()),
46 /** {@link DebugShelf}. */
47 DEBUG("cc/squirreljme/jvm/mle/DebugShelf",
48 MLEDebug.values()),
50 /** {@link JarPackageShelf}. */
51 JAR_PACKAGE("cc/squirreljme/jvm/mle/JarPackageShelf",
52 MLEJarPackage.values()),
54 /** {@link MathShelf}. */
55 MATH("cc/squirreljme/jvm/mle/MathShelf",
56 MLEMath.values()),
58 /** {@link ObjectShelf}. */
59 OBJECT("cc/squirreljme/jvm/mle/ObjectShelf",
60 MLEObject.values()),
62 /** {@link PencilShelf}. */
63 PENCIL("cc/squirreljme/jvm/mle/PencilShelf",
64 MLEPencil.values()),
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",
76 MLERuntime.values()),
78 /** {@link TaskShelf}. */
79 TASK("cc/squirreljme/jvm/mle/TaskShelf",
80 MLETask.values()),
82 /** {@link TerminalShelf}. */
83 TERMINAL("cc/squirreljme/jvm/mle/TerminalShelf",
84 MLETerminal.values()),
86 /** {@link ThreadShelf}. */
87 THREAD("cc/squirreljme/jvm/mle/ThreadShelf",
88 MLEThread.values()),
90 /** {@link TypeShelf}. */
91 TYPE("cc/squirreljme/jvm/mle/TypeShelf",
92 MLEType.values()),
94 /** {@link UIFormShelf}. */
95 UI_FORM("cc/squirreljme/jvm/mle/UIFormShelf",
96 MLEUIForm.values()),
98 /* End. */
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;
110 static
112 Map<String, Map<String, MLEDispatcherTarget>> functionTree =
113 new TreeMap<>();
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.
135 * @since 2020/06/18
137 MLEDispatcher(String __key, MLEFunction[] __functions)
138 throws NullPointerException
140 if (__key == null || __functions == null)
141 throw new NullPointerException("NARG");
143 this.key = __key;
144 this._functions = __functions;
148 * {@inheritDoc}
149 * @since 2020/06/18
151 @Override
152 public String key()
154 return this.key;
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.
166 * @since 2020/05/30
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");
175 // Find the sub-tree
176 Map<String, MLEDispatcherTarget> subTree = MLEDispatcher._fnTree.get(
177 __class.toString());
178 if (subTree == null)
179 throw new SpringVirtualMachineException(String.format(
180 "Unknown MLE Shelf: %s", __class));
182 // Find the target function
183 MLEDispatcherTarget target = subTree.get(__func.toString());
184 if (target == null)
185 throw new SpringVirtualMachineException(String.format(
186 "Unknown MLE Shelf Function: %s::%s", __class, __func));
188 // Call it
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);