Variant.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. /**
  2. * Copyright (c) 2006-2013 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. static inline void delete_table(std::vector<std::pair<Variant*, Variant*> > *table)
  41. {
  42. while (!table->empty())
  43. {
  44. std::pair<Variant*, Variant*> &kv = table->back();
  45. kv.first->release();
  46. kv.second->release();
  47. table->pop_back();
  48. }
  49. delete table;
  50. }
  51. Variant::Variant()
  52. : type(NIL)
  53. , data()
  54. {
  55. }
  56. Variant::Variant(bool boolean)
  57. : type(BOOLEAN)
  58. {
  59. data.boolean = boolean;
  60. }
  61. Variant::Variant(double number)
  62. : type(NUMBER)
  63. {
  64. data.number = number;
  65. }
  66. Variant::Variant(const char *string, size_t len)
  67. : type(STRING)
  68. {
  69. char *buf = new char[len+1];
  70. memset(buf, 0, len+1);
  71. memcpy(buf, string, len);
  72. data.string.str = buf;
  73. data.string.len = len;
  74. }
  75. Variant::Variant(char c)
  76. : type(CHARACTER)
  77. {
  78. data.character = c;
  79. }
  80. Variant::Variant(void *userdata)
  81. : type(LUSERDATA)
  82. {
  83. data.userdata = userdata;
  84. }
  85. Variant::Variant(love::Type udatatype, void *userdata)
  86. : type(FUSERDATA)
  87. , udatatype(udatatype)
  88. {
  89. if (udatatype != INVALID_ID)
  90. {
  91. Proxy *p = (Proxy *) userdata;
  92. flags = p->flags;
  93. data.userdata = p->data;
  94. ((love::Object *) data.userdata)->retain();
  95. }
  96. else
  97. data.userdata = userdata;
  98. }
  99. // Variant gets ownership of the vector.
  100. Variant::Variant(std::vector<std::pair<Variant*, Variant*> > *table)
  101. : type(TABLE)
  102. {
  103. data.table = table;
  104. }
  105. Variant::~Variant()
  106. {
  107. switch (type)
  108. {
  109. case STRING:
  110. delete[] data.string.str;
  111. break;
  112. case FUSERDATA:
  113. ((love::Object *) data.userdata)->release();
  114. break;
  115. case TABLE:
  116. delete_table(data.table);
  117. default:
  118. break;
  119. }
  120. }
  121. Variant *Variant::fromLua(lua_State *L, int n, bool allowTables)
  122. {
  123. Variant *v = NULL;
  124. size_t len;
  125. const char *str;
  126. if (n < 0) // Fix the stack position, we might modify it later
  127. n += lua_gettop(L) + 1;
  128. switch (lua_type(L, n))
  129. {
  130. case LUA_TBOOLEAN:
  131. v = new Variant(luax_toboolean(L, n));
  132. break;
  133. case LUA_TNUMBER:
  134. v = new Variant(lua_tonumber(L, n));
  135. break;
  136. case LUA_TSTRING:
  137. str = lua_tolstring(L, n, &len);
  138. v = new Variant(str, len);
  139. break;
  140. case LUA_TLIGHTUSERDATA:
  141. v = new Variant(lua_touserdata(L, n));
  142. break;
  143. case LUA_TUSERDATA:
  144. v = new Variant(extractudatatype(L, n), lua_touserdata(L, n));
  145. break;
  146. case LUA_TNIL:
  147. v = new Variant();
  148. break;
  149. case LUA_TTABLE:
  150. if (allowTables)
  151. {
  152. bool success = true;
  153. std::vector<std::pair<Variant*, Variant*> > *table = new std::vector<std::pair<Variant*, Variant*> >();
  154. lua_pushnil(L);
  155. while (lua_next(L, n))
  156. {
  157. Variant *key = fromLua(L, -2, false);
  158. if (!key)
  159. {
  160. success = false;
  161. lua_pop(L, 2);
  162. break;
  163. }
  164. Variant *value = fromLua(L, -1, false);
  165. if (!value)
  166. {
  167. delete key;
  168. success = false;
  169. lua_pop(L, 2);
  170. break;
  171. }
  172. table->push_back(std::make_pair(key, value));
  173. lua_pop(L, 1);
  174. }
  175. if (success)
  176. v = new Variant(table);
  177. else
  178. delete_table(table);
  179. }
  180. break;
  181. }
  182. return v;
  183. }
  184. void Variant::toLua(lua_State *L)
  185. {
  186. switch (type)
  187. {
  188. case BOOLEAN:
  189. lua_pushboolean(L, data.boolean);
  190. break;
  191. case CHARACTER:
  192. lua_pushlstring(L, &data.character, 1);
  193. break;
  194. case NUMBER:
  195. lua_pushnumber(L, data.number);
  196. break;
  197. case STRING:
  198. lua_pushlstring(L, data.string.str, data.string.len);
  199. break;
  200. case LUSERDATA:
  201. lua_pushlightuserdata(L, data.userdata);
  202. break;
  203. case FUSERDATA:
  204. if (udatatype != INVALID_ID)
  205. {
  206. const char *name = NULL;
  207. love::types.find(udatatype, name);
  208. ((love::Object *) data.userdata)->retain();
  209. luax_newtype(L, name, flags, data.userdata);
  210. }
  211. else
  212. lua_pushlightuserdata(L, data.userdata);
  213. // I know this is not the same
  214. // sadly, however, it's the most
  215. // I can do (at the moment).
  216. break;
  217. case TABLE:
  218. lua_newtable(L);
  219. for (int i = 0; i < data.table->size(); ++i)
  220. {
  221. std::pair<Variant*, Variant*> &kv = data.table->at(i);
  222. kv.first->toLua(L);
  223. kv.second->toLua(L);
  224. lua_settable(L, -3);
  225. }
  226. break;
  227. case NIL:
  228. default:
  229. lua_pushnil(L);
  230. break;
  231. }
  232. }
  233. } // love