Variant.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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. Variant Variant::fromLua(lua_State *L, int n, 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. return Variant(luax_toboolean(L, n));
  152. case LUA_TNUMBER:
  153. return Variant(lua_tonumber(L, n));
  154. case LUA_TSTRING:
  155. str = lua_tolstring(L, n, &len);
  156. return Variant(str, len);
  157. case LUA_TLIGHTUSERDATA:
  158. return Variant(lua_touserdata(L, n));
  159. case LUA_TUSERDATA:
  160. return Variant(extractudatatype(L, n), lua_touserdata(L, n));
  161. case LUA_TNIL:
  162. return Variant();
  163. case LUA_TTABLE:
  164. if (allowTables)
  165. {
  166. bool success = true;
  167. std::vector<std::pair<Variant, Variant>> *table = new std::vector<std::pair<Variant, Variant>>();
  168. size_t len = luax_objlen(L, -1);
  169. if (len > 0)
  170. table->reserve(len);
  171. lua_pushnil(L);
  172. while (lua_next(L, n))
  173. {
  174. table->emplace_back(fromLua(L, -2), fromLua(L, -1));
  175. lua_pop(L, 1);
  176. const auto &p = table->back();
  177. if (p.first.getType() == UNKNOWN || p.second.getType() == UNKNOWN)
  178. {
  179. success = false;
  180. break;
  181. }
  182. }
  183. if (success)
  184. return Variant(table);
  185. else
  186. delete table;
  187. }
  188. break;
  189. }
  190. Variant v;
  191. v.type = UNKNOWN;
  192. return v;
  193. }
  194. void Variant::toLua(lua_State *L) const
  195. {
  196. switch (type)
  197. {
  198. case BOOLEAN:
  199. lua_pushboolean(L, data.boolean);
  200. break;
  201. case NUMBER:
  202. lua_pushnumber(L, data.number);
  203. break;
  204. case STRING:
  205. lua_pushlstring(L, data.string->str, data.string->len);
  206. break;
  207. case SMALLSTRING:
  208. lua_pushlstring(L, data.smallstring.str, data.smallstring.len);
  209. break;
  210. case LUSERDATA:
  211. lua_pushlightuserdata(L, data.userdata);
  212. break;
  213. case FUSERDATA:
  214. if (udatatype != INVALID_ID)
  215. luax_pushtype(L, udatatype, (love::Object *) data.userdata);
  216. else
  217. lua_pushlightuserdata(L, data.userdata);
  218. // I know this is not the same
  219. // sadly, however, it's the most
  220. // I can do (at the moment).
  221. break;
  222. case TABLE:
  223. {
  224. std::vector<std::pair<Variant, Variant>> *table = data.table->table;
  225. int tsize = (int) table->size();
  226. lua_createtable(L, 0, tsize);
  227. for (int i = 0; i < tsize; ++i)
  228. {
  229. std::pair<Variant, Variant> &kv = (*table)[i];
  230. kv.first.toLua(L);
  231. kv.second.toLua(L);
  232. lua_settable(L, -3);
  233. }
  234. break;
  235. }
  236. case NIL:
  237. default:
  238. lua_pushnil(L);
  239. break;
  240. }
  241. }
  242. } // love