1/**
2 * Copyright (c) 2006-2023 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
21#include <memory>
22
23#include "Variant.h"
24#include "common/StringMap.h"
25
26namespace love
27{
28
29static Proxy *tryextractproxy(lua_State *L, int idx)
30{
31 Proxy *u = (Proxy *)lua_touserdata(L, idx);
32
33 if (u == nullptr || u->type == nullptr)
34 return nullptr;
35
36 // We could get rid of the dynamic_cast for more performance, but it would
37 // be less safe...
38 if (dynamic_cast<Object *>(u->object) != nullptr)
39 return u;
40
41 return nullptr;
42}
43
44Variant::Variant()
45 : type(NIL)
46{
47}
48
49Variant::Variant(bool boolean)
50 : type(BOOLEAN)
51{
52 data.boolean = boolean;
53}
54
55Variant::Variant(double number)
56 : type(NUMBER)
57{
58 data.number = number;
59}
60
61Variant::Variant(const char *str, size_t len)
62{
63 if (len <= MAX_SMALL_STRING_LENGTH)
64 {
65 type = SMALLSTRING;
66 memcpy(data.smallstring.str, str, len);
67 data.smallstring.len = (uint8) len;
68 }
69 else
70 {
71 type = STRING;
72 data.string = new SharedString(str, len);
73 }
74}
75
76Variant::Variant(const std::string &str)
77 : Variant(str.c_str(), str.length())
78{
79}
80
81Variant::Variant(void *lightuserdata)
82 : type(LUSERDATA)
83{
84 data.userdata = lightuserdata;
85}
86
87Variant::Variant(love::Type *lovetype, love::Object *object)
88 : type(LOVEOBJECT)
89{
90 data.objectproxy.type = lovetype;
91 data.objectproxy.object = object;
92
93 if (data.objectproxy.object != nullptr)
94 data.objectproxy.object->retain();
95}
96
97// Variant gets ownership of the vector.
98Variant::Variant(std::vector<std::pair<Variant, Variant>> *table)
99 : type(TABLE)
100{
101 data.table = new SharedTable(table);
102}
103
104Variant::Variant(const Variant &v)
105 : type(v.type)
106 , data(v.data)
107{
108 if (type == STRING)
109 data.string->retain();
110 else if (type == LOVEOBJECT && data.objectproxy.object != nullptr)
111 data.objectproxy.object->retain();
112 else if (type == TABLE)
113 data.table->retain();
114}
115
116Variant::Variant(Variant &&v)
117 : type(std::move(v.type))
118 , data(std::move(v.data))
119{
120 v.type = NIL;
121}
122
123Variant::~Variant()
124{
125 if (type == STRING)
126 data.string->release();
127 else if (type == LOVEOBJECT && data.objectproxy.object != nullptr)
128 data.objectproxy.object->release();
129 else if (type == TABLE)
130 data.table->release();
131}
132
133Variant &Variant::operator = (const Variant &v)
134{
135 if (v.type == STRING)
136 v.data.string->retain();
137 else if (v.type == LOVEOBJECT && v.data.objectproxy.object != nullptr)
138 v.data.objectproxy.object->retain();
139 else if (v.type == TABLE)
140 v.data.table->retain();
141
142 if (type == STRING)
143 data.string->release();
144 else if (type == LOVEOBJECT && data.objectproxy.object != nullptr)
145 data.objectproxy.object->release();
146 else if (type == TABLE)
147 data.table->release();
148
149 type = v.type;
150 data = v.data;
151
152 return *this;
153}
154
155Variant Variant::fromLua(lua_State *L, int n, std::set<const void*> *tableSet)
156{
157 size_t len;
158 const char *str;
159 Proxy *p = nullptr;
160
161 if (n < 0) // Fix the stack position, we might modify it later
162 n += lua_gettop(L) + 1;
163
164 switch (lua_type(L, n))
165 {
166 case LUA_TBOOLEAN:
167 return Variant(luax_toboolean(L, n));
168 case LUA_TNUMBER:
169 return Variant(lua_tonumber(L, n));
170 case LUA_TSTRING:
171 str = lua_tolstring(L, n, &len);
172 return Variant(str, len);
173 case LUA_TLIGHTUSERDATA:
174 return Variant(lua_touserdata(L, n));
175 case LUA_TUSERDATA:
176 p = tryextractproxy(L, n);
177 if (p != nullptr)
178 return Variant(p->type, p->object);
179 else
180 {
181 luax_typerror(L, n, "love type");
182 return Variant();
183 }
184 case LUA_TNIL:
185 return Variant();
186 case LUA_TTABLE:
187 {
188 bool success = true;
189 std::set<const void *> topTableSet;
190 std::vector<std::pair<Variant, Variant>> *table = new std::vector<std::pair<Variant, Variant>>();
191
192 // We can use a pointer to a stack-allocated variable because it's
193 // never used after the stack-allocated variable is destroyed.
194 if (tableSet == nullptr)
195 tableSet = &topTableSet;
196
197 // Now make sure this table wasn't already serialised
198 const void *tablePointer = lua_topointer(L, n);
199 {
200 auto result = tableSet->insert(tablePointer);
201 if (!result.second) // insertion failed
202 throw love::Exception("Cycle detected in table");
203 }
204
205 size_t len = luax_objlen(L, -1);
206 if (len > 0)
207 table->reserve(len);
208
209 lua_pushnil(L);
210
211 while (lua_next(L, n))
212 {
213 table->emplace_back(fromLua(L, -2, tableSet), fromLua(L, -1, tableSet));
214 lua_pop(L, 1);
215
216 const auto &p = table->back();
217 if (p.first.getType() == UNKNOWN || p.second.getType() == UNKNOWN)
218 {
219 success = false;
220 break;
221 }
222 }
223
224 // And remove the table from the set again
225 tableSet->erase(tablePointer);
226
227 if (success)
228 return Variant(table);
229 else
230 delete table;
231 }
232 break;
233 }
234
235 Variant v;
236 v.type = UNKNOWN;
237 return v;
238}
239
240void Variant::toLua(lua_State *L) const
241{
242 switch (type)
243 {
244 case BOOLEAN:
245 lua_pushboolean(L, data.boolean);
246 break;
247 case NUMBER:
248 lua_pushnumber(L, data.number);
249 break;
250 case STRING:
251 lua_pushlstring(L, data.string->str, data.string->len);
252 break;
253 case SMALLSTRING:
254 lua_pushlstring(L, data.smallstring.str, data.smallstring.len);
255 break;
256 case LUSERDATA:
257 lua_pushlightuserdata(L, data.userdata);
258 break;
259 case LOVEOBJECT:
260 luax_pushtype(L, *data.objectproxy.type, data.objectproxy.object);
261 break;
262 case TABLE:
263 {
264 std::vector<std::pair<Variant, Variant>> *table = data.table->table;
265 int tsize = (int) table->size();
266
267 lua_createtable(L, 0, tsize);
268
269 for (int i = 0; i < tsize; ++i)
270 {
271 std::pair<Variant, Variant> &kv = (*table)[i];
272 kv.first.toLua(L);
273 kv.second.toLua(L);
274 lua_settable(L, -3);
275 }
276
277 break;
278 }
279 case NIL:
280 default:
281 lua_pushnil(L);
282 break;
283 }
284}
285
286} // love
287