From 9fa8b26e2d8dda72c861fafa8c82a620d9b7a69a Mon Sep 17 00:00:00 2001 From: Ian Farmer Date: Fri, 30 May 2008 18:31:47 +0000 Subject: [PATCH] Add some helper functions for argument validation. --- src/core/org/luaj/vm/LuaState.java | 57 ++++++++++++++++++++++++++++++ version.properties | 2 +- 2 files changed, 58 insertions(+), 1 deletion(-) diff --git a/src/core/org/luaj/vm/LuaState.java b/src/core/org/luaj/vm/LuaState.java index 9890afcd..8d01fa6b 100644 --- a/src/core/org/luaj/vm/LuaState.java +++ b/src/core/org/luaj/vm/LuaState.java @@ -2604,7 +2604,64 @@ public class LuaState extends Lua { return topointer(index).toJavaBoxedLong(); } + // ================= Error Reporting Functions ================= + /** + * Report an error with an argument. + * + * @param narg + * Stack index of the bad argument + * @param extramsg + * String to include in error message + */ + public void argerror(int narg, String extramsg) { + // TODO: port ldebug.c and provide mode useful error messages. + error("bad argument #" + (narg - 1) + " (" + extramsg + ")"); + } + + /** + * Report a type error. + * + * @param narg + * Stack index of the bad argument + * @param typename + * Name of the type that was expected + */ + public void typerror(int narg, String typename) { + argerror(narg, typename + " expected, got " + typename(narg)); + } + + /** + * Report a type error. + * + * @param narg + * Stack index of the bad argument + * @param typenum + * Constant value specifying the type of argument that was + * expected (i.e. LUA_TSTRING). + */ + public void typerror(int narg, int typenum) { + typerror(narg, TYPE_NAMES[typenum]); + } + + /** + * Check that the type of userdata on the stack matches the required type, + * and if so, return the Java Object the userdata value points to. + * + * @param ud + * Stack index of the argument to check + * @param expected + * Class that the userdata is expected to have an instance of. + */ + public Object checkudata(int ud, Class expected) { + Object p = touserdata(ud); + if (expected.isInstance(p)) { + return p; + } + typerror(ud, expected.getName()); + return null; + } + /** * Method to indicate a vm internal error has occurred. * Generally, this is not recoverable, so we convert to diff --git a/version.properties b/version.properties index bb7e0d2f..19912f55 100644 --- a/version.properties +++ b/version.properties @@ -1 +1 @@ -version: 0.35 \ No newline at end of file +version: 0.36