diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2003-10-07 17:13:41 -0300 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2003-10-07 17:13:41 -0300 |
commit | f04fe526cd9de3e5460b614b2ff06268ad51872d (patch) | |
tree | 9e9fd4fc0bae4858d74c04a22d8ce5748191d5a9 /lauxlib.c | |
parent | 21947deddc5976536665cd2397d7d5c9e6bd7e48 (diff) | |
download | lua-f04fe526cd9de3e5460b614b2ff06268ad51872d.tar.gz lua-f04fe526cd9de3e5460b614b2ff06268ad51872d.tar.bz2 lua-f04fe526cd9de3e5460b614b2ff06268ad51872d.zip |
new functions `lua_tointeger' and lua_pushinteger'
Diffstat (limited to 'lauxlib.c')
-rw-r--r-- | lauxlib.c | 27 |
1 files changed, 21 insertions, 6 deletions
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lauxlib.c,v 1.103 2003/10/01 16:50:53 roberto Exp roberto $ | 2 | ** $Id: lauxlib.c,v 1.104 2003/10/02 20:31:17 roberto Exp roberto $ |
3 | ** Auxiliary functions for building Lua libraries | 3 | ** Auxiliary functions for building Lua libraries |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -197,6 +197,21 @@ LUALIB_API lua_Number luaL_optnumber (lua_State *L, int narg, lua_Number def) { | |||
197 | } | 197 | } |
198 | 198 | ||
199 | 199 | ||
200 | LUALIB_API lua_Integer luaL_checkinteger (lua_State *L, int narg) { | ||
201 | lua_Integer d = lua_tointeger(L, narg); | ||
202 | if (d == 0 && !lua_isnumber(L, narg)) /* avoid extra test when d is not 0 */ | ||
203 | tag_error(L, narg, LUA_TNUMBER); | ||
204 | return d; | ||
205 | } | ||
206 | |||
207 | |||
208 | LUALIB_API lua_Integer luaL_optinteger (lua_State *L, int narg, | ||
209 | lua_Integer def) { | ||
210 | if (lua_isnoneornil(L, narg)) return def; | ||
211 | else return luaL_checkinteger(L, narg); | ||
212 | } | ||
213 | |||
214 | |||
200 | LUALIB_API int luaL_getmetafield (lua_State *L, int obj, const char *event) { | 215 | LUALIB_API int luaL_getmetafield (lua_State *L, int obj, const char *event) { |
201 | if (!lua_getmetatable(L, obj)) /* no metatable? */ | 216 | if (!lua_getmetatable(L, obj)) /* no metatable? */ |
202 | return 0; | 217 | return 0; |
@@ -257,7 +272,7 @@ LUALIB_API void luaL_openlib (lua_State *L, const char *libname, | |||
257 | */ | 272 | */ |
258 | 273 | ||
259 | static int checkint (lua_State *L, int topop) { | 274 | static int checkint (lua_State *L, int topop) { |
260 | int n = (int)lua_tonumber(L, -1); | 275 | int n = (int)lua_tointeger(L, -1); |
261 | if (n == 0 && !lua_isnumber(L, -1)) n = -1; | 276 | if (n == 0 && !lua_isnumber(L, -1)) n = -1; |
262 | lua_pop(L, topop); | 277 | lua_pop(L, topop); |
263 | return n; | 278 | return n; |
@@ -286,13 +301,13 @@ void luaL_setn (lua_State *L, int t, int n) { | |||
286 | lua_rawget(L, t); | 301 | lua_rawget(L, t); |
287 | if (checkint(L, 1) >= 0) { /* is there a numeric field `n'? */ | 302 | if (checkint(L, 1) >= 0) { /* is there a numeric field `n'? */ |
288 | lua_pushliteral(L, "n"); /* use it */ | 303 | lua_pushliteral(L, "n"); /* use it */ |
289 | lua_pushnumber(L, (lua_Number)n); | 304 | lua_pushinteger(L, n); |
290 | lua_rawset(L, t); | 305 | lua_rawset(L, t); |
291 | } | 306 | } |
292 | else { /* use `sizes' */ | 307 | else { /* use `sizes' */ |
293 | getsizes(L); | 308 | getsizes(L); |
294 | lua_pushvalue(L, t); | 309 | lua_pushvalue(L, t); |
295 | lua_pushnumber(L, (lua_Number)n); | 310 | lua_pushinteger(L, n); |
296 | lua_rawset(L, -3); /* sizes[t] = n */ | 311 | lua_rawset(L, -3); /* sizes[t] = n */ |
297 | lua_pop(L, 1); /* remove `sizes' */ | 312 | lua_pop(L, 1); /* remove `sizes' */ |
298 | } | 313 | } |
@@ -425,7 +440,7 @@ LUALIB_API int luaL_ref (lua_State *L, int t) { | |||
425 | return LUA_REFNIL; /* `nil' has a unique fixed reference */ | 440 | return LUA_REFNIL; /* `nil' has a unique fixed reference */ |
426 | } | 441 | } |
427 | lua_rawgeti(L, t, FREELIST_REF); /* get first free element */ | 442 | lua_rawgeti(L, t, FREELIST_REF); /* get first free element */ |
428 | ref = (int)lua_tonumber(L, -1); /* ref = t[FREELIST_REF] */ | 443 | ref = (int)lua_tointeger(L, -1); /* ref = t[FREELIST_REF] */ |
429 | lua_pop(L, 1); /* remove it from stack */ | 444 | lua_pop(L, 1); /* remove it from stack */ |
430 | if (ref != 0) { /* any free element? */ | 445 | if (ref != 0) { /* any free element? */ |
431 | lua_rawgeti(L, t, ref); /* remove it from list */ | 446 | lua_rawgeti(L, t, ref); /* remove it from list */ |
@@ -448,7 +463,7 @@ LUALIB_API void luaL_unref (lua_State *L, int t, int ref) { | |||
448 | t = abs_index(L, t); | 463 | t = abs_index(L, t); |
449 | lua_rawgeti(L, t, FREELIST_REF); | 464 | lua_rawgeti(L, t, FREELIST_REF); |
450 | lua_rawseti(L, t, ref); /* t[ref] = t[FREELIST_REF] */ | 465 | lua_rawseti(L, t, ref); /* t[ref] = t[FREELIST_REF] */ |
451 | lua_pushnumber(L, (lua_Number)ref); | 466 | lua_pushinteger(L, ref); |
452 | lua_rawseti(L, t, FREELIST_REF); /* t[FREELIST_REF] = ref */ | 467 | lua_rawseti(L, t, FREELIST_REF); /* t[FREELIST_REF] = ref */ |
453 | } | 468 | } |
454 | } | 469 | } |