| [1129] | 1 | /* tolua: funcitons to convert to C types | 
|---|
 | 2 | ** Support code for Lua bindings. | 
|---|
 | 3 | ** Written by Waldemar Celes | 
|---|
 | 4 | ** TeCGraf/PUC-Rio | 
|---|
 | 5 | ** Apr 2003 | 
|---|
 | 6 | ** $Id: $ | 
|---|
 | 7 | */ | 
|---|
 | 8 |  | 
|---|
 | 9 | /* This code is free software; you can redistribute it and/or modify it. | 
|---|
 | 10 | ** The software provided hereunder is on an "as is" basis, and | 
|---|
 | 11 | ** the author has no obligation to provide maintenance, support, updates, | 
|---|
 | 12 | ** enhancements, or modifications. | 
|---|
 | 13 | */ | 
|---|
 | 14 |  | 
|---|
 | 15 | #include "tolua++.h" | 
|---|
 | 16 |  | 
|---|
 | 17 | #include <string.h> | 
|---|
 | 18 | #include <stdlib.h> | 
|---|
 | 19 |  | 
|---|
 | 20 | TOLUA_API double tolua_tonumber (lua_State* L, int narg, double def) | 
|---|
 | 21 | { | 
|---|
 | 22 |  return lua_gettop(L)<abs(narg) ? def : lua_tonumber(L,narg); | 
|---|
 | 23 | } | 
|---|
 | 24 |  | 
|---|
 | 25 | TOLUA_API const char* tolua_tostring (lua_State* L, int narg, const char* def) | 
|---|
 | 26 | { | 
|---|
 | 27 |  return lua_gettop(L)<abs(narg) ? def : lua_tostring(L,narg); | 
|---|
 | 28 | } | 
|---|
 | 29 |  | 
|---|
 | 30 | TOLUA_API void* tolua_touserdata (lua_State* L, int narg, void* def) | 
|---|
 | 31 | { | 
|---|
 | 32 |          | 
|---|
 | 33 |         /* return lua_gettop(L)<abs(narg) ? def : lua_touserdata(L,narg); */ | 
|---|
 | 34 |  | 
|---|
 | 35 |         if (lua_gettop(L)<abs(narg)) { | 
|---|
 | 36 |                 return def; | 
|---|
 | 37 |         }; | 
|---|
 | 38 |           | 
|---|
 | 39 |         if (lua_islightuserdata(L, narg)) { | 
|---|
 | 40 |          | 
|---|
 | 41 |                 return lua_touserdata(L,narg); | 
|---|
 | 42 |         }; | 
|---|
 | 43 |          | 
|---|
 | 44 |         return tolua_tousertype(L, narg, def); | 
|---|
 | 45 | } | 
|---|
 | 46 |  | 
|---|
 | 47 | extern int push_table_instance(lua_State* L, int lo); | 
|---|
 | 48 |  | 
|---|
 | 49 | TOLUA_API void* tolua_tousertype (lua_State* L, int narg, void* def) | 
|---|
 | 50 | { | 
|---|
 | 51 |  if (lua_gettop(L)<abs(narg)) | 
|---|
 | 52 |   return def; | 
|---|
 | 53 |  else | 
|---|
 | 54 |  { | 
|---|
 | 55 |   void* u; | 
|---|
 | 56 |   if (!lua_isuserdata(L, narg)) { | 
|---|
 | 57 |           if (!push_table_instance(L, narg)) return NULL; | 
|---|
 | 58 |   }; | 
|---|
 | 59 |   u = lua_touserdata(L,narg); | 
|---|
 | 60 |   return (u==NULL) ? NULL : *((void**)u); /* nil represents NULL */ | 
|---|
 | 61 |  } | 
|---|
 | 62 | } | 
|---|
 | 63 |  | 
|---|
 | 64 | TOLUA_API int tolua_tovalue (lua_State* L, int narg, int def) | 
|---|
 | 65 | { | 
|---|
 | 66 |  return lua_gettop(L)<abs(narg) ? def : narg; | 
|---|
 | 67 | } | 
|---|
 | 68 |  | 
|---|
 | 69 | TOLUA_API int tolua_toboolean (lua_State* L, int narg, int def) | 
|---|
 | 70 | { | 
|---|
 | 71 |  return lua_gettop(L)<abs(narg) ?  def : lua_toboolean(L,narg); | 
|---|
 | 72 | } | 
|---|
 | 73 |  | 
|---|
 | 74 | TOLUA_API double tolua_tofieldnumber (lua_State* L, int lo, int index, double def) | 
|---|
 | 75 | { | 
|---|
 | 76 |  double v; | 
|---|
 | 77 |  lua_pushnumber(L,index); | 
|---|
 | 78 |  lua_gettable(L,lo); | 
|---|
 | 79 |  v = lua_isnil(L,-1) ? def : lua_tonumber(L,-1); | 
|---|
 | 80 |  lua_pop(L,1); | 
|---|
 | 81 |  return v; | 
|---|
 | 82 | } | 
|---|
 | 83 |  | 
|---|
 | 84 | TOLUA_API const char* tolua_tofieldstring | 
|---|
 | 85 | (lua_State* L, int lo, int index, const char* def) | 
|---|
 | 86 | { | 
|---|
 | 87 |  const char* v; | 
|---|
 | 88 |  lua_pushnumber(L,index); | 
|---|
 | 89 |  lua_gettable(L,lo); | 
|---|
 | 90 |  v = lua_isnil(L,-1) ? def : lua_tostring(L,-1); | 
|---|
 | 91 |  lua_pop(L,1); | 
|---|
 | 92 |  return v; | 
|---|
 | 93 | } | 
|---|
 | 94 |  | 
|---|
 | 95 | TOLUA_API void* tolua_tofielduserdata (lua_State* L, int lo, int index, void* def) | 
|---|
 | 96 | { | 
|---|
 | 97 |  void* v; | 
|---|
 | 98 |  lua_pushnumber(L,index); | 
|---|
 | 99 |  lua_gettable(L,lo); | 
|---|
 | 100 |  v = lua_isnil(L,-1) ? def : lua_touserdata(L,-1); | 
|---|
 | 101 |  lua_pop(L,1); | 
|---|
 | 102 |  return v; | 
|---|
 | 103 | } | 
|---|
 | 104 |  | 
|---|
 | 105 | TOLUA_API void* tolua_tofieldusertype (lua_State* L, int lo, int index, void* def) | 
|---|
 | 106 | { | 
|---|
 | 107 |  void* v; | 
|---|
 | 108 |  lua_pushnumber(L,index); | 
|---|
 | 109 |  lua_gettable(L,lo); | 
|---|
 | 110 |  v = lua_isnil(L,-1) ? def : (*(void **)(lua_touserdata(L, -1))); /* lua_unboxpointer(L,-1); */ | 
|---|
 | 111 |  lua_pop(L,1); | 
|---|
 | 112 |  return v; | 
|---|
 | 113 | } | 
|---|
 | 114 |  | 
|---|
 | 115 | TOLUA_API int tolua_tofieldvalue (lua_State* L, int lo, int index, int def) | 
|---|
 | 116 | { | 
|---|
 | 117 |  int v; | 
|---|
 | 118 |  lua_pushnumber(L,index); | 
|---|
 | 119 |  lua_gettable(L,lo); | 
|---|
 | 120 |  v = lua_isnil(L,-1) ? def : lo; | 
|---|
 | 121 |  lua_pop(L,1); | 
|---|
 | 122 |  return v; | 
|---|
 | 123 | } | 
|---|
 | 124 |  | 
|---|
 | 125 | TOLUA_API int tolua_getfieldboolean (lua_State* L, int lo, int index, int def) | 
|---|
 | 126 | { | 
|---|
 | 127 |  int v; | 
|---|
 | 128 |  lua_pushnumber(L,index); | 
|---|
 | 129 |  lua_gettable(L,lo); | 
|---|
 | 130 |  v = lua_isnil(L,-1) ? 0 : lua_toboolean(L,-1); | 
|---|
 | 131 |  lua_pop(L,1); | 
|---|
 | 132 |  return v; | 
|---|
 | 133 | } | 
|---|