Skip to content

Commit

Permalink
Fix up LuaException
Browse files Browse the repository at this point in the history
  • Loading branch information
Victorious3 committed Aug 18, 2015
1 parent 61970df commit 59dca77
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import org.apache.commons.lang3.ArrayUtils;

import cpw.mods.fml.common.Optional;
import dan200.computercraft.api.lua.LuaException;
import dan200.computercraft.api.peripheral.IComputerAccess;

// TODO Test me! -- If it didn't work, it would probably be reported by now
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@

import cpw.mods.fml.common.Optional;
import dan200.computercraft.api.lua.ILuaContext;
import dan200.computercraft.api.lua.LuaException;
import dan200.computercraft.api.peripheral.IComputerAccess;
import dan200.computercraft.api.peripheral.IPeripheral;

Expand All @@ -36,40 +35,44 @@ public final String[] getMethodNames() {
return methodNames;
}

public static class LuaException extends RuntimeException {
public LuaException(String message) {
super(message);
}
}

@Override
@Optional.Method(modid = "ComputerCraft")
public final Object[] callMethod(IComputerAccess computer, ILuaContext context, int method, Object[] arguments) throws LuaException, InterruptedException {
public final Object[] callMethod(IComputerAccess computer, ILuaContext context, int method, Object[] arguments) throws dan200.computercraft.api.lua.LuaException, InterruptedException {
String name = (String) methods.keySet().toArray()[method];
return callMethod(name, arguments);
}

public final Object[] callMethod(String name, Object[] arguments) throws LuaException, InterruptedException {
try {
Method m = methods.get(name);
if (m == null) {
return null;
}
Class<?>[] paramTypes = m.getParameterTypes();
if (arguments.length != paramTypes.length) {
throw new LuaException("Illegal amount of parameters!");
}
for (int i = 0; i < paramTypes.length; i++) {
paramTypes[i] = Primitives.wrap(paramTypes[i]);
if (!paramTypes[i].isAssignableFrom(arguments[i].getClass()))
throw new LuaException("Illegal parameter at index " + i + ". Expected '" + paramTypes[i] + "', got '" + arguments[i].getClass() + "'.");
}
Object o = m.invoke(this, arguments);
if (o == null) {
return null;
} else if (m.getReturnType().isArray()) {
return (Object[]) o;
} else {
return new Object[] { o };
}
} catch (LuaException e) {
throw e;
return callMethod(name, arguments);
} catch (Exception e) {
throw new LuaException(e.getMessage());
throw new dan200.computercraft.api.lua.LuaException(e.getMessage());
}
}

public final Object[] callMethod(String name, Object[] arguments) throws Exception {
Method m = methods.get(name);
if (m == null) {
return null;
}
Class<?>[] paramTypes = m.getParameterTypes();
if (arguments.length != paramTypes.length) {
throw new LuaException("Illegal amount of parameters!");
}
for (int i = 0; i < paramTypes.length; i++) {
paramTypes[i] = Primitives.wrap(paramTypes[i]);
if (!paramTypes[i].isAssignableFrom(arguments[i].getClass()))
throw new LuaException("Illegal parameter at index " + i + ". Expected '" + paramTypes[i] + "', got '" + arguments[i].getClass() + "'.");
}
Object o = m.invoke(this, arguments);
if (o == null) {
return null;
} else if (m.getReturnType().isArray()) {
return (Object[]) o;
} else {
return new Object[] { o };
}
}

Expand Down

0 comments on commit 59dca77

Please sign in to comment.