Variant.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /**
  2. * Copyright (c) 2006-2012 LOVE Development Team
  3. *
  4. * This software is provided 'as-is', without any express or implied
  5. * warranty. In no event will the authors be held liable for any damages
  6. * arising from the use of this software.
  7. *
  8. * Permission is granted to anyone to use this software for any purpose,
  9. * including commercial applications, and to alter it and redistribute it
  10. * freely, subject to the following restrictions:
  11. *
  12. * 1. The origin of this software must not be misrepresented; you must not
  13. * claim that you wrote the original software. If you use this software
  14. * in a product, an acknowledgment in the product documentation would be
  15. * appreciated but is not required.
  16. * 2. Altered source versions must be plainly marked as such, and must not be
  17. * misrepresented as being the original software.
  18. * 3. This notice may not be removed or altered from any source distribution.
  19. **/
  20. #include "Variant.h"
  21. #include <common/StringMap.h>
  22. namespace love
  23. {
  24. extern StringMap<Type, TYPE_MAX_ENUM> types;
  25. love::Type extractudatatype(lua_State * L, int idx)
  26. {
  27. Type t = INVALID_ID;
  28. if (!lua_isuserdata(L, idx))
  29. return t;
  30. if (luaL_getmetafield (L, idx, "__tostring") == 0)
  31. return t;
  32. lua_pushvalue(L, idx);
  33. int result = lua_pcall(L, 1, 1, 0);
  34. if (result == 0)
  35. types.find(lua_tostring(L, -1), t);
  36. if (result == 0 || result == LUA_ERRRUN)
  37. lua_pop(L, 1);
  38. return t;
  39. }
  40. Variant::Variant(bool boolean)
  41. {
  42. type = BOOLEAN;
  43. data.boolean = boolean;
  44. }
  45. Variant::Variant(double number)
  46. {
  47. type = NUMBER;
  48. data.number = number;
  49. }
  50. Variant::Variant(const char *string, size_t len)
  51. {
  52. type = STRING;
  53. char *buf = new char[len+1];
  54. memset(buf, 0, len+1);
  55. memcpy(buf, string, len);
  56. data.string.str = buf;
  57. data.string.len = len;
  58. }
  59. Variant::Variant(char c)
  60. {
  61. type = CHARACTER;
  62. data.character = c;
  63. }
  64. Variant::Variant(void *userdata)
  65. {
  66. type = LUSERDATA;
  67. data.userdata = userdata;
  68. }
  69. Variant::Variant(love::Type udatatype, void *userdata)
  70. {
  71. type = FUSERDATA;
  72. this->udatatype = udatatype;
  73. if (udatatype != INVALID_ID)
  74. {
  75. Proxy *p = (Proxy *) userdata;
  76. flags = p->flags;
  77. data.userdata = p->data;
  78. ((love::Object *) data.userdata)->retain();
  79. }
  80. else
  81. data.userdata = userdata;
  82. }
  83. Variant::~Variant()
  84. {
  85. switch(type)
  86. {
  87. case STRING:
  88. delete[] data.string.str;
  89. break;
  90. case FUSERDATA:
  91. ((love::Object *) data.userdata)->release();
  92. break;
  93. default:
  94. break;
  95. }
  96. }
  97. Variant *Variant::fromLua(lua_State *L, int n)
  98. {
  99. Variant *v = NULL;
  100. size_t len;
  101. const char *str;
  102. switch(lua_type(L, n))
  103. {
  104. case LUA_TBOOLEAN:
  105. v = new Variant(luax_toboolean(L, n));
  106. break;
  107. case LUA_TNUMBER:
  108. v = new Variant(lua_tonumber(L, n));
  109. break;
  110. case LUA_TSTRING:
  111. str = lua_tolstring(L, n, &len);
  112. v = new Variant(str, len);
  113. break;
  114. case LUA_TLIGHTUSERDATA:
  115. v = new Variant(lua_touserdata(L, n));
  116. break;
  117. case LUA_TUSERDATA:
  118. v = new Variant(extractudatatype(L, n), lua_touserdata(L, n));
  119. break;
  120. }
  121. return v;
  122. }
  123. void Variant::toLua(lua_State *L)
  124. {
  125. switch(type)
  126. {
  127. case BOOLEAN:
  128. lua_pushboolean(L, data.boolean);
  129. break;
  130. case CHARACTER:
  131. lua_pushlstring(L, &data.character, 1);
  132. break;
  133. case NUMBER:
  134. lua_pushnumber(L, data.number);
  135. break;
  136. case STRING:
  137. lua_pushlstring(L, data.string.str, data.string.len);
  138. break;
  139. case LUSERDATA:
  140. lua_pushlightuserdata(L, data.userdata);
  141. break;
  142. case FUSERDATA:
  143. if (udatatype != INVALID_ID)
  144. {
  145. const char *name = NULL;
  146. love::types.find(udatatype, name);
  147. ((love::Object *) data.userdata)->retain();
  148. luax_newtype(L, name, flags, data.userdata);
  149. }
  150. else
  151. lua_pushlightuserdata(L, data.userdata);
  152. // I know this is not the same
  153. // sadly, however, it's the most
  154. // I can do (at the moment).
  155. break;
  156. default:
  157. lua_pushnil(L);
  158. break;
  159. }
  160. }
  161. } // love