Variant.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. /**
  2. * Copyright (c) 2006-2017 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 == nullptr)
  28. return nullptr;
  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 nullptr;
  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 != nullptr)
  73. {
  74. Proxy *p = (Proxy *) userdata;
  75. data.userdata = p->object;
  76. if (p->object)
  77. p->object->retain();
  78. }
  79. else
  80. data.userdata = userdata;
  81. }
  82. // Variant gets ownership of the vector.
  83. Variant::Variant(std::vector<std::pair<Variant, Variant>> *table)
  84. : type(TABLE)
  85. {
  86. data.table = new SharedTable(table);
  87. }
  88. Variant::Variant(const Variant &v)
  89. : type(v.type)
  90. , udatatype(v.udatatype)
  91. , data(v.data)
  92. {
  93. if (type == STRING)
  94. data.string->retain();
  95. else if (type == FUSERDATA && data.userdata != nullptr)
  96. ((love::Object *) data.userdata)->retain();
  97. else if (type == TABLE)
  98. data.table->retain();
  99. }
  100. Variant::Variant(Variant &&v)
  101. : type(std::move(v.type))
  102. , udatatype(std::move(v.udatatype))
  103. , data(std::move(v.data))
  104. {
  105. v.type = NIL;
  106. }
  107. Variant::~Variant()
  108. {
  109. switch (type)
  110. {
  111. case STRING:
  112. data.string->release();
  113. break;
  114. case FUSERDATA:
  115. if (data.userdata != nullptr)
  116. ((love::Object *) data.userdata)->release();
  117. break;
  118. case TABLE:
  119. data.table->release();
  120. break;
  121. default:
  122. break;
  123. }
  124. }
  125. Variant &Variant::operator = (const Variant &v)
  126. {
  127. if (v.type == STRING)
  128. v.data.string->retain();
  129. else if (v.type == FUSERDATA && v.data.userdata != nullptr)
  130. ((love::Object *) v.data.userdata)->retain();
  131. else if (v.type == TABLE)
  132. v.data.table->retain();
  133. if (type == STRING)
  134. data.string->release();
  135. else if (type == FUSERDATA && v.data.userdata != nullptr)
  136. ((love::Object *) v.data.userdata)->release();
  137. else if (type == TABLE)
  138. data.table->release();
  139. type = v.type;
  140. data = v.data;
  141. udatatype = v.udatatype;
  142. return *this;
  143. }
  144. Variant Variant::fromLua(lua_State *L, int n, bool allowTables)
  145. {
  146. size_t len;
  147. const char *str;
  148. if (n < 0) // Fix the stack position, we might modify it later
  149. n += lua_gettop(L) + 1;
  150. switch (lua_type(L, n))
  151. {
  152. case LUA_TBOOLEAN:
  153. return Variant(luax_toboolean(L, n));
  154. case LUA_TNUMBER:
  155. return Variant(lua_tonumber(L, n));
  156. case LUA_TSTRING:
  157. str = lua_tolstring(L, n, &len);
  158. return Variant(str, len);
  159. case LUA_TLIGHTUSERDATA:
  160. return Variant(lua_touserdata(L, n));
  161. case LUA_TUSERDATA:
  162. return Variant(extractudatatype(L, n), lua_touserdata(L, n));
  163. case LUA_TNIL:
  164. return Variant();
  165. case LUA_TTABLE:
  166. if (allowTables)
  167. {
  168. bool success = true;
  169. std::vector<std::pair<Variant, Variant>> *table = new std::vector<std::pair<Variant, Variant>>();
  170. size_t len = luax_objlen(L, -1);
  171. if (len > 0)
  172. table->reserve(len);
  173. lua_pushnil(L);
  174. while (lua_next(L, n))
  175. {
  176. table->emplace_back(fromLua(L, -2), fromLua(L, -1));
  177. lua_pop(L, 1);
  178. const auto &p = table->back();
  179. if (p.first.getType() == UNKNOWN || p.second.getType() == UNKNOWN)
  180. {
  181. success = false;
  182. break;
  183. }
  184. }
  185. if (success)
  186. return Variant(table);
  187. else
  188. delete table;
  189. }
  190. break;
  191. }
  192. Variant v;
  193. v.type = UNKNOWN;
  194. return v;
  195. }
  196. void Variant::toLua(lua_State *L) const
  197. {
  198. switch (type)
  199. {
  200. case BOOLEAN:
  201. lua_pushboolean(L, data.boolean);
  202. break;
  203. case NUMBER:
  204. lua_pushnumber(L, data.number);
  205. break;
  206. case STRING:
  207. lua_pushlstring(L, data.string->str, data.string->len);
  208. break;
  209. case SMALLSTRING:
  210. lua_pushlstring(L, data.smallstring.str, data.smallstring.len);
  211. break;
  212. case LUSERDATA:
  213. lua_pushlightuserdata(L, data.userdata);
  214. break;
  215. case FUSERDATA:
  216. if (udatatype != nullptr)
  217. luax_pushtype(L, *udatatype, (love::Object *) data.userdata);
  218. else
  219. lua_pushlightuserdata(L, data.userdata);
  220. // I know this is not the same
  221. // sadly, however, it's the most
  222. // I can do (at the moment).
  223. break;
  224. case TABLE:
  225. {
  226. std::vector<std::pair<Variant, Variant>> *table = data.table->table;
  227. int tsize = (int) table->size();
  228. lua_createtable(L, 0, tsize);
  229. for (int i = 0; i < tsize; ++i)
  230. {
  231. std::pair<Variant, Variant> &kv = (*table)[i];
  232. kv.first.toLua(L);
  233. kv.second.toLua(L);
  234. lua_settable(L, -3);
  235. }
  236. break;
  237. }
  238. case NIL:
  239. default:
  240. lua_pushnil(L);
  241. break;
  242. }
  243. }
  244. } // love