aboutsummaryrefslogtreecommitdiff
path: root/lapi.c
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2003-10-07 17:13:41 -0300
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2003-10-07 17:13:41 -0300
commitf04fe526cd9de3e5460b614b2ff06268ad51872d (patch)
tree9e9fd4fc0bae4858d74c04a22d8ce5748191d5a9 /lapi.c
parent21947deddc5976536665cd2397d7d5c9e6bd7e48 (diff)
downloadlua-f04fe526cd9de3e5460b614b2ff06268ad51872d.tar.gz
lua-f04fe526cd9de3e5460b614b2ff06268ad51872d.tar.bz2
lua-f04fe526cd9de3e5460b614b2ff06268ad51872d.zip
new functions `lua_tointeger' and lua_pushinteger'
Diffstat (limited to 'lapi.c')
-rw-r--r--lapi.c29
1 files changed, 28 insertions, 1 deletions
diff --git a/lapi.c b/lapi.c
index 33eed2eb..b40f984a 100644
--- a/lapi.c
+++ b/lapi.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lapi.c,v 1.243 2003/08/25 20:00:50 roberto Exp roberto $ 2** $Id: lapi.c,v 1.244 2003/08/27 21:01:44 roberto Exp roberto $
3** Lua API 3** Lua API
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -27,6 +27,12 @@
27#include "lvm.h" 27#include "lvm.h"
28 28
29 29
30/* function to convert a lua_Number to lua_Integer (with any rounding method) */
31#ifndef lua_number2integer
32#define lua_number2integer(i,n) ((i)=(lua_Integer)(n))
33#endif
34
35
30const char lua_ident[] = 36const char lua_ident[] =
31 "$Lua: " LUA_VERSION " " LUA_COPYRIGHT " $\n" 37 "$Lua: " LUA_VERSION " " LUA_COPYRIGHT " $\n"
32 "$Authors: " LUA_AUTHORS " $\n" 38 "$Authors: " LUA_AUTHORS " $\n"
@@ -289,6 +295,19 @@ LUA_API lua_Number lua_tonumber (lua_State *L, int idx) {
289} 295}
290 296
291 297
298LUA_API lua_Integer lua_tointeger (lua_State *L, int idx) {
299 TObject n;
300 const TObject *o = luaA_index(L, idx);
301 if (tonumber(o, &n)) {
302 lua_Integer res;
303 lua_number2integer(res, nvalue(o));
304 return res;
305 }
306 else
307 return 0;
308}
309
310
292LUA_API int lua_toboolean (lua_State *L, int idx) { 311LUA_API int lua_toboolean (lua_State *L, int idx) {
293 const TObject *o = luaA_index(L, idx); 312 const TObject *o = luaA_index(L, idx);
294 return !l_isfalse(o); 313 return !l_isfalse(o);
@@ -382,6 +401,14 @@ LUA_API void lua_pushnumber (lua_State *L, lua_Number n) {
382} 401}
383 402
384 403
404LUA_API void lua_pushinteger (lua_State *L, lua_Integer n) {
405 lua_lock(L);
406 setnvalue(L->top, cast(lua_Number, n));
407 api_incr_top(L);
408 lua_unlock(L);
409}
410
411
385LUA_API void lua_pushlstring (lua_State *L, const char *s, size_t len) { 412LUA_API void lua_pushlstring (lua_State *L, const char *s, size_t len) {
386 lua_lock(L); 413 lua_lock(L);
387 luaC_checkGC(L); 414 luaC_checkGC(L);