diff options
| author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 1997-03-17 14:02:29 -0300 |
|---|---|---|
| committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 1997-03-17 14:02:29 -0300 |
| commit | eea734aa881002e90bd9130171a2b94cd9dc3267 (patch) | |
| tree | b2816a614fca723d8c0b06e96cd093438e6e098b /inout.c | |
| parent | b6d91e24e23edfe98ad732660fd456e91658edb9 (diff) | |
| download | lua-eea734aa881002e90bd9130171a2b94cd9dc3267.tar.gz lua-eea734aa881002e90bd9130171a2b94cd9dc3267.tar.bz2 lua-eea734aa881002e90bd9130171a2b94cd9dc3267.zip | |
new module 'auxlib' centralizes functions to get/check parameters.
Diffstat (limited to 'inout.c')
| -rw-r--r-- | inout.c | 61 |
1 files changed, 15 insertions, 46 deletions
| @@ -5,7 +5,7 @@ | |||
| 5 | ** Also provides some predefined lua functions. | 5 | ** Also provides some predefined lua functions. |
| 6 | */ | 6 | */ |
| 7 | 7 | ||
| 8 | char *rcs_inout="$Id: inout.c,v 2.44 1997/02/26 17:38:41 roberto Unstable roberto $"; | 8 | char *rcs_inout="$Id: inout.c,v 2.45 1997/03/11 18:44:28 roberto Exp roberto $"; |
| 9 | 9 | ||
| 10 | #include <stdio.h> | 10 | #include <stdio.h> |
| 11 | #include <string.h> | 11 | #include <string.h> |
| @@ -101,32 +101,6 @@ void lua_closestring (void) | |||
| 101 | } | 101 | } |
| 102 | 102 | ||
| 103 | 103 | ||
| 104 | static void check_arg (int cond, char *func) | ||
| 105 | { | ||
| 106 | if (!cond) | ||
| 107 | { | ||
| 108 | char buff[100]; | ||
| 109 | sprintf(buff, "incorrect argument to function `%s'", func); | ||
| 110 | lua_error(buff); | ||
| 111 | } | ||
| 112 | } | ||
| 113 | |||
| 114 | static char *check_string (int numArg, char *funcname) | ||
| 115 | { | ||
| 116 | lua_Object o = lua_getparam(numArg); | ||
| 117 | check_arg(lua_isstring(o), funcname); | ||
| 118 | return lua_getstring(o); | ||
| 119 | } | ||
| 120 | |||
| 121 | static int check_number (int numArg, char *funcname) | ||
| 122 | { | ||
| 123 | lua_Object o = lua_getparam(numArg); | ||
| 124 | check_arg(lua_isnumber(o), funcname); | ||
| 125 | return (int)lua_getnumber(o); | ||
| 126 | } | ||
| 127 | |||
| 128 | |||
| 129 | |||
| 130 | static int passresults (void) | 104 | static int passresults (void) |
| 131 | { | 105 | { |
| 132 | int arg = 0; | 106 | int arg = 0; |
| @@ -141,7 +115,7 @@ static int passresults (void) | |||
| 141 | */ | 115 | */ |
| 142 | static void lua_internaldostring (void) | 116 | static void lua_internaldostring (void) |
| 143 | { | 117 | { |
| 144 | if (lua_dostring(check_string(1, "dostring")) == 0) | 118 | if (lua_dostring(luaL_check_string(1, "dostring")) == 0) |
| 145 | if (passresults() == 0) | 119 | if (passresults() == 0) |
| 146 | lua_pushuserdata(NULL); /* at least one result to signal no errors */ | 120 | lua_pushuserdata(NULL); /* at least one result to signal no errors */ |
| 147 | } | 121 | } |
| @@ -151,13 +125,7 @@ static void lua_internaldostring (void) | |||
| 151 | */ | 125 | */ |
| 152 | static void lua_internaldofile (void) | 126 | static void lua_internaldofile (void) |
| 153 | { | 127 | { |
| 154 | lua_Object obj = lua_getparam (1); | 128 | char *fname = luaL_opt_string(1, NULL, "dofile"); |
| 155 | char *fname = NULL; | ||
| 156 | if (lua_isstring(obj)) | ||
| 157 | fname = lua_getstring(obj); | ||
| 158 | else if (obj != LUA_NOOBJECT) | ||
| 159 | lua_error("invalid argument to function `dofile'"); | ||
| 160 | /* else fname = NULL */ | ||
| 161 | if (lua_dofile(fname) == 0) | 129 | if (lua_dofile(fname) == 0) |
| 162 | if (passresults() == 0) | 130 | if (passresults() == 0) |
| 163 | lua_pushuserdata(NULL); /* at least one result to signal no errors */ | 131 | lua_pushuserdata(NULL); /* at least one result to signal no errors */ |
| @@ -247,15 +215,15 @@ static void luaI_assert (void) | |||
| 247 | static void luaI_setglobal (void) | 215 | static void luaI_setglobal (void) |
| 248 | { | 216 | { |
| 249 | lua_Object value = lua_getparam(2); | 217 | lua_Object value = lua_getparam(2); |
| 250 | check_arg(value != LUA_NOOBJECT, "setglobal"); | 218 | luaL_arg_check(value != LUA_NOOBJECT, "setglobal", 2, NULL); |
| 251 | lua_pushobject(value); | 219 | lua_pushobject(value); |
| 252 | lua_storeglobal(check_string(1, "setglobal")); | 220 | lua_storeglobal(luaL_check_string(1, "setglobal")); |
| 253 | lua_pushobject(value); /* return given value */ | 221 | lua_pushobject(value); /* return given value */ |
| 254 | } | 222 | } |
| 255 | 223 | ||
| 256 | static void luaI_getglobal (void) | 224 | static void luaI_getglobal (void) |
| 257 | { | 225 | { |
| 258 | lua_pushobject(lua_getglobal(check_string(1, "getglobal"))); | 226 | lua_pushobject(lua_getglobal(luaL_check_string(1, "getglobal"))); |
| 259 | } | 227 | } |
| 260 | 228 | ||
| 261 | #define MAXPARAMS 256 | 229 | #define MAXPARAMS 256 |
| @@ -265,8 +233,8 @@ static void luaI_call (void) | |||
| 265 | lua_Object arg = lua_getparam(2); | 233 | lua_Object arg = lua_getparam(2); |
| 266 | lua_Object temp, params[MAXPARAMS]; | 234 | lua_Object temp, params[MAXPARAMS]; |
| 267 | int narg, i; | 235 | int narg, i; |
| 268 | check_arg(lua_istable(arg), "call"); | 236 | luaL_arg_check(lua_isfunction(f), "call", 1, "function expected"); |
| 269 | check_arg(lua_isfunction(f), "call"); | 237 | luaL_arg_check(lua_istable(arg), "call", 2, "table expected"); |
| 270 | /* narg = arg.n */ | 238 | /* narg = arg.n */ |
| 271 | lua_pushobject(arg); | 239 | lua_pushobject(arg); |
| 272 | lua_pushstring("n"); | 240 | lua_pushstring("n"); |
| @@ -296,21 +264,22 @@ static void luaI_call (void) | |||
| 296 | static void luaIl_settag (void) | 264 | static void luaIl_settag (void) |
| 297 | { | 265 | { |
| 298 | lua_Object o = lua_getparam(1); | 266 | lua_Object o = lua_getparam(1); |
| 299 | check_arg(o != LUA_NOOBJECT, "settag"); | 267 | luaL_arg_check(o != LUA_NOOBJECT, "settag", 1, NULL); |
| 300 | lua_pushobject(o); | 268 | lua_pushobject(o); |
| 301 | lua_settag(check_number(2, "settag")); | 269 | lua_settag(luaL_check_number(2, "settag")); |
| 302 | } | 270 | } |
| 303 | 271 | ||
| 304 | static void luaIl_newtag (void) | 272 | static void luaIl_newtag (void) |
| 305 | { | 273 | { |
| 306 | lua_pushnumber(lua_newtag(check_string(1, "newtag"))); | 274 | lua_pushnumber(lua_newtag(luaL_check_string(1, "newtag"))); |
| 307 | } | 275 | } |
| 308 | 276 | ||
| 309 | static void basicindex (void) | 277 | static void basicindex (void) |
| 310 | { | 278 | { |
| 311 | lua_Object t = lua_getparam(1); | 279 | lua_Object t = lua_getparam(1); |
| 312 | lua_Object i = lua_getparam(2); | 280 | lua_Object i = lua_getparam(2); |
| 313 | check_arg(t != LUA_NOOBJECT && i != LUA_NOOBJECT, "basicindex"); | 281 | luaL_arg_check(t != LUA_NOOBJECT, "basicindex", 1, NULL); |
| 282 | luaL_arg_check(i != LUA_NOOBJECT, "basicindex", 2, NULL); | ||
| 314 | lua_pushobject(t); | 283 | lua_pushobject(t); |
| 315 | lua_pushobject(i); | 284 | lua_pushobject(i); |
| 316 | lua_pushobject(lua_basicindex()); | 285 | lua_pushobject(lua_basicindex()); |
| @@ -321,8 +290,8 @@ static void basicstoreindex (void) | |||
| 321 | lua_Object t = lua_getparam(1); | 290 | lua_Object t = lua_getparam(1); |
| 322 | lua_Object i = lua_getparam(2); | 291 | lua_Object i = lua_getparam(2); |
| 323 | lua_Object v = lua_getparam(3); | 292 | lua_Object v = lua_getparam(3); |
| 324 | check_arg(t != LUA_NOOBJECT && i != LUA_NOOBJECT && v != LUA_NOOBJECT, | 293 | luaL_arg_check(t != LUA_NOOBJECT && i != LUA_NOOBJECT && v != LUA_NOOBJECT, |
| 325 | "basicindex"); | 294 | "basicindex", 0, NULL); |
| 326 | lua_pushobject(t); | 295 | lua_pushobject(t); |
| 327 | lua_pushobject(i); | 296 | lua_pushobject(i); |
| 328 | lua_pushobject(v); | 297 | lua_pushobject(v); |
