1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
|
/* tolua: get & push functions.
** Support code for Lua bindings.
** Written by Waldemar Celes
** TeCGraf/PUC-Rio
** Jul 1998
** $Id: tolua_gp.c,v 1.2 2001/11/26 23:00:27 darkgod Exp $
*/
/* This code is free software; you can redistribute it and/or modify it.
** The software provided hereunder is on an "as is" basis, and
** the author has no obligation to provide maintenance, support, updates,
** enhancements, or modifications.
*/
#include "tolua.h"
#include "tolua_tm.h"
#include <string.h>
#include <stdlib.h>
long tolua_getnumber (lua_State* L, int narg, long def)
{
return lua_gettop(L)<abs(narg) ? def : lua_tonumber(L,narg);
}
const char* tolua_getstring (lua_State* L, int narg, const char* def)
{
return lua_gettop(L)<abs(narg) ? def : lua_tostring(L,narg);
}
void* tolua_getuserdata (lua_State* L, int narg, void* def)
{
return lua_gettop(L)<abs(narg) ? def : lua_touserdata(L,narg);
}
void* tolua_getusertype (lua_State* L, int narg, void* def)
{
return lua_gettop(L)<abs(narg) ? def : lua_touserdata(L,narg);
}
int tolua_getvalue (lua_State* L, int narg, int def)
{
return lua_gettop(L)<abs(narg) ? def : narg;
}
int tolua_getbool (lua_State* L, int narg, int def)
{
return lua_gettop(L)<abs(narg) ?
def :
lua_isnil(L,narg) ? 0 : lua_tonumber(L,narg)!=0;
}
long tolua_getfieldnumber (lua_State* L, int lo, int index, long def)
{
long v;
lua_pushnumber(L,index);
lua_gettable(L,lo);
v = lua_isnil(L,-1) ? def : lua_tonumber(L,-1);
lua_pop(L,1);
return v;
}
const char* tolua_getfieldstring
(lua_State* L, int lo, int index, const char* def)
{
const char* v;
lua_pushnumber(L,index);
lua_gettable(L,lo);
v = lua_isnil(L,-1) ? def : lua_tostring(L,-1);
lua_pop(L,1);
return v;
}
void* tolua_getfielduserdata (lua_State* L, int lo, int index, void* def)
{
void* v;
lua_pushnumber(L,index);
lua_gettable(L,lo);
v = lua_isnil(L,-1) ? def : lua_touserdata(L,-1);
lua_pop(L,1);
return v;
}
void* tolua_getfieldusertype (lua_State* L, int lo, int index, void* def)
{
void* v;
lua_pushnumber(L,index);
lua_gettable(L,lo);
v = lua_isnil(L,-1) ? def : lua_touserdata(L,-1);
lua_pop(L,1);
return v;
}
int tolua_getfieldvalue (lua_State* L, int lo, int index, int def)
{
int v;
lua_pushnumber(L,index);
lua_gettable(L,lo);
v = lua_isnil(L,-1) ? def : lo;
lua_pop(L,1);
return v;
}
int tolua_getfieldbool (lua_State* L, int lo, int index, int def)
{
int v;
lua_pushnumber(L,index);
lua_gettable(L,lo);
v = lua_isnil(L,-1) ? 0 : lua_tonumber(L,-1)!=0;
lua_pop(L,1);
return v;
}
void tolua_pushnumber (lua_State* L, long value)
{
lua_pushnumber(L,value);
}
void tolua_pushstring (lua_State* L, const char* value)
{
if (value == NULL)
lua_pushnil(L);
else
lua_pushstring(L,value);
}
void tolua_pushuserdata (lua_State* L, void* value)
{
if (value == NULL)
lua_pushnil(L);
else
lua_pushuserdata(L,value);
}
void tolua_pushusertype (lua_State* L, void* value, int tag)
{
if (value == NULL)
lua_pushnil(L);
else
lua_pushusertag(L,value,tag);
}
void tolua_pushvalue (lua_State* L, int lo)
{
lua_pushvalue(L,lo);
}
void tolua_pushbool (lua_State* L, int value)
{
if (value)
lua_pushnumber(L,(long)value);
else
lua_pushnil(L);
}
void tolua_pushfieldnumber (lua_State* L, int lo, int index, long v)
{
lua_pushnumber(L,index);
tolua_pushnumber(L,v);
lua_settable(L,lo);
}
void tolua_pushfieldstring (lua_State* L, int lo, int index, char* v)
{
lua_pushnumber(L,index);
tolua_pushstring(L,v);
lua_settable(L,lo);
}
void tolua_pushfielduserdata (lua_State* L, int lo, int index, void* v)
{
lua_pushnumber(L,index);
tolua_pushuserdata(L,v);
lua_settable(L,lo);
}
void tolua_pushfieldusertype (lua_State* L, int lo, int index, void* v, int tag)
{
lua_pushnumber(L,index);
tolua_pushusertype(L,v,tag);
lua_settable(L,lo);
}
void tolua_pushfieldvalue (lua_State* L, int lo, int index, int v)
{
lua_pushnumber(L,index);
lua_pushvalue(L,v);
lua_settable(L,lo);
}
void tolua_pushfieldbool (lua_State* L, int lo, int index, int v)
{
lua_pushnumber(L,index);
tolua_pushbool(L,v);
lua_settable(L,lo);
}
|