Variant.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. /**
  2. * Copyright (c) 2006-2016 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. static love::Type extractudatatype(lua_State *L, int idx)
  25. {
  26. Proxy *u = (Proxy *)lua_touserdata(L, idx);
  27. if (u == nullptr || u->type <= INVALID_ID || u->type >= TYPE_MAX_ENUM)
  28. return INVALID_ID;
  29. // We could get rid of the dynamic_cast for more performance, but it would
  30. // be less safe...
  31. if (dynamic_cast<Object *>(u->object) != nullptr)
  32. return u->type;
  33. return INVALID_ID;
  34. }
  35. Variant::Variant()
  36. : type(NIL)
  37. {
  38. }
  39. Variant::Variant(bool boolean)
  40. : type(BOOLEAN)
  41. {
  42. data.boolean = boolean;
  43. }
  44. Variant::Variant(double number)
  45. : type(NUMBER)
  46. {
  47. data.number = number;
  48. }
  49. Variant::Variant(const char *string, size_t len)
  50. {
  51. if (len <= MAX_SMALL_STRING_LENGTH)
  52. {
  53. type = SMALLSTRING;
  54. memcpy(data.smallstring.str, string, len);
  55. data.smallstring.len = (uint8) len;
  56. }
  57. else
  58. {
  59. type = STRING;
  60. data.string = new SharedString(string, len);
  61. }
  62. }
  63. Variant::Variant(void *userdata)
  64. : type(LUSERDATA)
  65. {
  66. data.userdata = userdata;
  67. }
  68. Variant::Variant(love::Type udatatype, void *userdata)
  69. : type(FUSERDATA)
  70. , udatatype(udatatype)
  71. {
  72. if (udatatype != INVALID_ID)
  73. {
  74. Proxy *p = (Proxy *) userdata;
  75. data.userdata = p->object;
  76. p->object->retain();
  77. }
  78. else
  79. data.userdata = userdata;
  80. }
  81. // Variant gets ownership of the vector.
  82. Variant::Variant(std::vector<std::pair<Variant, Variant>> *table)
  83. : type(TABLE)
  84. {
  85. data.table = new SharedTable(table);
  86. }
  87. Variant::Variant(const Variant &v)
  88. : type(v.type)
  89. , udatatype(v.udatatype)
  90. , data(v.data)
  91. {
  92. if (type == STRING)
  93. data.string->retain();
  94. else if (type == FUSERDATA)
  95. ((love::Object *) data.userdata)->retain();
  96. else if (type == TABLE)
  97. data.table->retain();
  98. }
  99. Variant::Variant(Variant &&v)
  100. : type(std::move(v.type))
  101. , udatatype(std::move(v.udatatype))
  102. , data(std::move(v.data))
  103. {
  104. v.type = NIL;
  105. }
  106. Variant::~Variant()
  107. {
  108. switch (type)
  109. {
  110. case STRING:
  111. data.string->release();
  112. break;
  113. case FUSERDATA:
  114. ((love::Object *) data.userdata)->release();
  115. break;
  116. case TABLE:
  117. data.table->release();
  118. break;
  119. default:
  120. break;
  121. }
  122. }
  123. Variant &Variant::operator = (const Variant &v)
  124. {
  125. if (v.type == STRING)
  126. v.data.string->retain();
  127. else if (v.type == FUSERDATA)
  128. ((love::Object *) v.data.userdata)->retain();
  129. else if (v.type == TABLE)
  130. v.data.table->retain();
  131. if (type == STRING)
  132. data.string->release();
  133. else if (type == FUSERDATA)
  134. ((love::Object *) v.data.userdata)->release();
  135. else if (type == TABLE)
  136. data.table->release();
  137. type = v.type;
  138. data = v.data;
  139. udatatype = v.udatatype;
  140. return *this;
  141. }
  142. bool Variant::fromLua(lua_State *L, int n, Variant *v, bool allowTables)
  143. {
  144. size_t len;
  145. const char *str;
  146. if (n < 0) // Fix the stack position, we might modify it later
  147. n += lua_gettop(L) + 1;
  148. switch (lua_type(L, n))
  149. {
  150. case LUA_TBOOLEAN:
  151. *v = Variant(luax_toboolean(L, n));
  152. return true;
  153. case LUA_TNUMBER:
  154. *v = Variant(lua_tonumber(L, n));
  155. return true;
  156. case LUA_TSTRING:
  157. str = lua_tolstring(L, n, &len);
  158. *v = Variant(str, len);
  159. return true;
  160. case LUA_TLIGHTUSERDATA:
  161. *v = Variant(lua_touserdata(L, n));
  162. return true;
  163. case LUA_TUSERDATA:
  164. *v = Variant(extractudatatype(L, n), lua_touserdata(L, n));
  165. return true;
  166. case LUA_TNIL:
  167. *v = Variant();
  168. return true;
  169. case LUA_TTABLE:
  170. if (allowTables)
  171. {
  172. bool success = true;
  173. std::vector<std::pair<Variant, Variant>> *table = new std::vector<std::pair<Variant, Variant>>();
  174. std::pair<Variant, Variant> pair;
  175. size_t len = luax_objlen(L, -1);
  176. if (len > 0)
  177. table->reserve(len);
  178. lua_pushnil(L);
  179. while (lua_next(L, n))
  180. {
  181. if (!fromLua(L, -2, &pair.first, false) || !fromLua(L, -1, &pair.second, false))
  182. {
  183. success = false;
  184. lua_pop(L, 2);
  185. break;
  186. }
  187. table->push_back(pair);
  188. lua_pop(L, 1);
  189. }
  190. if (success)
  191. {
  192. *v = Variant(table);
  193. return true;
  194. }
  195. else
  196. delete table;
  197. }
  198. break;
  199. }
  200. return false;
  201. }
  202. void Variant::toLua(lua_State *L) const
  203. {
  204. switch (type)
  205. {
  206. case BOOLEAN:
  207. lua_pushboolean(L, data.boolean);
  208. break;
  209. case NUMBER:
  210. lua_pushnumber(L, data.number);
  211. break;
  212. case STRING:
  213. lua_pushlstring(L, data.string->str, data.string->len);
  214. break;
  215. case SMALLSTRING:
  216. lua_pushlstring(L, data.smallstring.str, data.smallstring.len);
  217. break;
  218. case LUSERDATA:
  219. lua_pushlightuserdata(L, data.userdata);
  220. break;
  221. case FUSERDATA:
  222. if (udatatype != INVALID_ID)
  223. luax_pushtype(L, udatatype, (love::Object *) data.userdata);
  224. else
  225. lua_pushlightuserdata(L, data.userdata);
  226. // I know this is not the same
  227. // sadly, however, it's the most
  228. // I can do (at the moment).
  229. break;
  230. case TABLE:
  231. {
  232. std::vector<std::pair<Variant, Variant>> *table = data.table->table;
  233. lua_createtable(L, 0, (int) table->size());
  234. for (size_t i = 0; i < table->size(); ++i)
  235. {
  236. std::pair<Variant, Variant> &kv = table->at(i);
  237. kv.first.toLua(L);
  238. kv.second.toLua(L);
  239. lua_settable(L, -3);
  240. }
  241. break;
  242. }
  243. case NIL:
  244. default:
  245. lua_pushnil(L);
  246. break;
  247. }
  248. }
  249. } // love