| 1 |
/* tolua: functions to push C values. |
| 2 |
** Support code for Lua bindings. |
| 3 |
** Written by Waldemar Celes |
| 4 |
** TeCGraf/PUC-Rio |
| 5 |
** Apr 2003 |
| 6 |
*/ |
| 7 |
|
| 8 |
/* This code is free software; you can redistribute it and/or modify it. |
| 9 |
** The software provided hereunder is on an "as is" basis, and |
| 10 |
** the author has no obligation to provide maintenance, support, updates, |
| 11 |
** enhancements, or modifications. |
| 12 |
*/ |
| 13 |
|
| 14 |
#include "tolua++.h" |
| 15 |
#include "lauxlib.h" |
| 16 |
|
| 17 |
#include <stdlib.h> |
| 18 |
|
| 19 |
TOLUA_API void tolua_pushvalue (lua_State* L, int lo) |
| 20 |
{ |
| 21 |
lua_pushvalue(L,lo); |
| 22 |
} |
| 23 |
|
| 24 |
TOLUA_API void tolua_pushboolean (lua_State* L, int value) |
| 25 |
{ |
| 26 |
lua_pushboolean(L,value); |
| 27 |
} |
| 28 |
|
| 29 |
TOLUA_API void tolua_pushnumber (lua_State* L, lua_Number value) |
| 30 |
{ |
| 31 |
lua_pushnumber(L,value); |
| 32 |
} |
| 33 |
|
| 34 |
TOLUA_API void tolua_pushstring (lua_State* L, const char* value) |
| 35 |
{ |
| 36 |
if (value == NULL) |
| 37 |
lua_pushnil(L); |
| 38 |
else |
| 39 |
lua_pushstring(L,value); |
| 40 |
} |
| 41 |
|
| 42 |
TOLUA_API void tolua_pushuserdata (lua_State* L, void* value) |
| 43 |
{ |
| 44 |
if (value == NULL) |
| 45 |
lua_pushnil(L); |
| 46 |
else |
| 47 |
lua_pushlightuserdata(L,value); |
| 48 |
} |
| 49 |
|
| 50 |
TOLUA_API void tolua_pushusertype (lua_State* L, void* value, const char* type) |
| 51 |
{ |
| 52 |
if (value == NULL) |
| 53 |
lua_pushnil(L); |
| 54 |
else |
| 55 |
{ |
| 56 |
luaL_getmetatable(L, type); |
| 57 |
lua_pushstring(L,"tolua_ubox"); |
| 58 |
lua_rawget(L,-2); /* stack: mt ubox */ |
| 59 |
if (lua_isnil(L, -1)) { |
| 60 |
lua_pop(L, 1); |
| 61 |
lua_pushstring(L, "tolua_ubox"); |
| 62 |
lua_rawget(L, LUA_REGISTRYINDEX); |
| 63 |
}; |
| 64 |
lua_pushlightuserdata(L,value); |
| 65 |
lua_rawget(L,-2); /* stack: mt ubox ubox[u] */ |
| 66 |
if (lua_isnil(L,-1)) |
| 67 |
{ |
| 68 |
lua_pop(L,1); /* stack: mt ubox */ |
| 69 |
lua_pushlightuserdata(L,value); |
| 70 |
*(void**)lua_newuserdata(L,sizeof(void *)) = value; /* stack: mt ubox u newud */ |
| 71 |
lua_pushvalue(L,-1); /* stack: mt ubox u newud newud */ |
| 72 |
lua_insert(L,-4); /* stack: mt newud ubox u newud */ |
| 73 |
lua_rawset(L,-3); /* stack: mt newud ubox */ |
| 74 |
lua_pop(L,1); /* stack: mt newud */ |
| 75 |
/*luaL_getmetatable(L,type);*/ |
| 76 |
lua_pushvalue(L, -2); /* stack: mt newud mt */ |
| 77 |
lua_setmetatable(L,-2); /* stack: mt newud */ |
| 78 |
|
| 79 |
#ifdef LUA_VERSION_NUM |
| 80 |
lua_pushvalue(L, TOLUA_NOPEER); |
| 81 |
lua_setfenv(L, -2); |
| 82 |
#endif |
| 83 |
} |
| 84 |
else |
| 85 |
{ |
| 86 |
/* check the need of updating the metatable to a more specialized class */ |
| 87 |
lua_insert(L,-2); /* stack: mt ubox[u] ubox */ |
| 88 |
lua_pop(L,1); /* stack: mt ubox[u] */ |
| 89 |
lua_pushstring(L,"tolua_super"); |
| 90 |
lua_rawget(L,LUA_REGISTRYINDEX); /* stack: mt ubox[u] super */ |
| 91 |
lua_getmetatable(L,-2); /* stack: mt ubox[u] super mt */ |
| 92 |
lua_rawget(L,-2); /* stack: mt ubox[u] super super[mt] */ |
| 93 |
if (lua_istable(L,-1)) |
| 94 |
{ |
| 95 |
lua_pushstring(L,type); /* stack: mt ubox[u] super super[mt] type */ |
| 96 |
lua_rawget(L,-2); /* stack: mt ubox[u] super super[mt] flag */ |
| 97 |
if (lua_toboolean(L,-1) == 1) /* if true */ |
| 98 |
{ |
| 99 |
lua_pop(L,3); /* mt ubox[u]*/ |
| 100 |
lua_remove(L, -2); |
| 101 |
return; |
| 102 |
} |
| 103 |
} |
| 104 |
/* type represents a more specilized type */ |
| 105 |
/*luaL_getmetatable(L,type); // stack: mt ubox[u] super super[mt] flag mt */ |
| 106 |
lua_pushvalue(L, -5); /* stack: mt ubox[u] super super[mt] flag mt */ |
| 107 |
lua_setmetatable(L,-5); /* stack: mt ubox[u] super super[mt] flag */ |
| 108 |
lua_pop(L,3); /* stack: mt ubox[u] */ |
| 109 |
} |
| 110 |
lua_remove(L, -2); /* stack: ubox[u]*/ |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
TOLUA_API void tolua_pushusertype_and_takeownership (lua_State* L, void* value, const char* type) |
| 115 |
{ |
| 116 |
tolua_pushusertype(L,value,type); |
| 117 |
tolua_register_gc(L,lua_gettop(L)); |
| 118 |
} |
| 119 |
|
| 120 |
TOLUA_API void tolua_pushfieldvalue (lua_State* L, int lo, int index, int v) |
| 121 |
{ |
| 122 |
lua_pushnumber(L,index); |
| 123 |
lua_pushvalue(L,v); |
| 124 |
lua_settable(L,lo); |
| 125 |
} |
| 126 |
|
| 127 |
TOLUA_API void tolua_pushfieldboolean (lua_State* L, int lo, int index, int v) |
| 128 |
{ |
| 129 |
lua_pushnumber(L,index); |
| 130 |
lua_pushboolean(L,v); |
| 131 |
lua_settable(L,lo); |
| 132 |
} |
| 133 |
|
| 134 |
|
| 135 |
TOLUA_API void tolua_pushfieldnumber (lua_State* L, int lo, int index, lua_Number v) |
| 136 |
{ |
| 137 |
lua_pushnumber(L,index); |
| 138 |
tolua_pushnumber(L,v); |
| 139 |
lua_settable(L,lo); |
| 140 |
} |
| 141 |
|
| 142 |
TOLUA_API void tolua_pushfieldstring (lua_State* L, int lo, int index, const char* v) |
| 143 |
{ |
| 144 |
lua_pushnumber(L,index); |
| 145 |
tolua_pushstring(L,v); |
| 146 |
lua_settable(L,lo); |
| 147 |
} |
| 148 |
|
| 149 |
TOLUA_API void tolua_pushfielduserdata (lua_State* L, int lo, int index, void* v) |
| 150 |
{ |
| 151 |
lua_pushnumber(L,index); |
| 152 |
tolua_pushuserdata(L,v); |
| 153 |
lua_settable(L,lo); |
| 154 |
} |
| 155 |
|
| 156 |
TOLUA_API void tolua_pushfieldusertype (lua_State* L, int lo, int index, void* v, const char* type) |
| 157 |
{ |
| 158 |
lua_pushnumber(L,index); |
| 159 |
tolua_pushusertype(L,v,type); |
| 160 |
lua_settable(L,lo); |
| 161 |
} |
| 162 |
|
| 163 |
TOLUA_API void tolua_pushfieldusertype_and_takeownership (lua_State* L, int lo, int index, void* v, const char* type) |
| 164 |
{ |
| 165 |
lua_pushnumber(L,index); |
| 166 |
tolua_pushusertype(L,v,type); |
| 167 |
tolua_register_gc(L,lua_gettop(L)); |
| 168 |
lua_settable(L,lo); |
| 169 |
} |
| 170 |
|