|
1096
|
1 /**
|
|
|
2 * Copyright (c) 2006-2012 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 "runtime.h"
|
|
|
22
|
|
|
23 // LOVE
|
|
|
24 #include "Module.h"
|
|
|
25 #include "Object.h"
|
|
|
26 #include "Reference.h"
|
|
|
27 #include "StringMap.h"
|
|
|
28 #include "thread/threads.h"
|
|
|
29
|
|
|
30 // STD
|
|
|
31 #include <iostream>
|
|
|
32
|
|
|
33 namespace love
|
|
|
34 {
|
|
|
35
|
|
|
36 static thread::Mutex *gcmutex = 0;
|
|
|
37 void *_gcmutex = 0;
|
|
|
38 unsigned int _gcthread = 0;
|
|
|
39 /**
|
|
|
40 * Called when an object is collected. The object is released
|
|
|
41 * once in this function, possibly deleting it.
|
|
|
42 **/
|
|
|
43 static int w__gc(lua_State *L)
|
|
|
44 {
|
|
|
45 if (!gcmutex)
|
|
|
46 {
|
|
|
47 gcmutex = new thread::Mutex();
|
|
|
48 _gcmutex = (void *) gcmutex;
|
|
|
49 }
|
|
|
50 Proxy *p = (Proxy *)lua_touserdata(L, 1);
|
|
|
51 Object *t = (Object *)p->data;
|
|
|
52 if (p->own)
|
|
|
53 {
|
|
|
54 thread::Lock lock(gcmutex);
|
|
|
55 _gcthread = thread::ThreadBase::threadId();
|
|
|
56 t->release();
|
|
|
57 }
|
|
|
58 return 0;
|
|
|
59 }
|
|
|
60
|
|
|
61 static int w__tostring(lua_State *L)
|
|
|
62 {
|
|
|
63 lua_pushvalue(L, lua_upvalueindex(1));
|
|
|
64 return 1;
|
|
|
65 }
|
|
|
66
|
|
|
67 static int w__typeOf(lua_State *L)
|
|
|
68 {
|
|
|
69 Proxy *p = (Proxy *)lua_touserdata(L, 1);
|
|
|
70 Type t = luax_type(L, 2);
|
|
|
71 luax_pushboolean(L, p->flags[t]);
|
|
|
72 return 1;
|
|
|
73 }
|
|
|
74
|
|
|
75 static int w__eq(lua_State *L)
|
|
|
76 {
|
|
|
77 Proxy *p1 = (Proxy *)lua_touserdata(L, 1);
|
|
|
78 Proxy *p2 = (Proxy *)lua_touserdata(L, 2);
|
|
|
79 luax_pushboolean(L, p1->data == p2->data);
|
|
|
80 return 1;
|
|
|
81 }
|
|
|
82
|
|
|
83 Reference *luax_refif (lua_State *L, int type)
|
|
|
84 {
|
|
|
85 Reference *r = 0;
|
|
|
86
|
|
|
87 // Create a reference only if the test succeeds.
|
|
|
88 if (lua_type(L, -1) == type)
|
|
|
89 r = new Reference(L);
|
|
|
90 else // Pop the value even if it fails (but also if it succeeds).
|
|
|
91 lua_pop(L, 1);
|
|
|
92
|
|
|
93 return r;
|
|
|
94 }
|
|
|
95
|
|
|
96 void luax_printstack(lua_State *L)
|
|
|
97 {
|
|
|
98 for (int i = 1; i<=lua_gettop(L); i++)
|
|
|
99 {
|
|
|
100 std::cout << i << " - " << luaL_typename(L, i) << std::endl;
|
|
|
101 }
|
|
|
102 }
|
|
|
103
|
|
|
104 bool luax_toboolean(lua_State *L, int idx)
|
|
|
105 {
|
|
|
106 return (lua_toboolean(L, idx) != 0);
|
|
|
107 }
|
|
|
108
|
|
|
109 void luax_pushboolean(lua_State *L, bool b)
|
|
|
110 {
|
|
|
111 lua_pushboolean(L, b ? 1 : 0);
|
|
|
112 }
|
|
|
113
|
|
|
114 bool luax_optboolean(lua_State *L, int idx, bool b)
|
|
|
115 {
|
|
|
116 if (lua_isboolean(L, idx) == 1)
|
|
|
117 return (lua_toboolean(L, idx) == 1 ? true : false);
|
|
|
118 return b;
|
|
|
119 }
|
|
|
120
|
|
|
121 std::string luax_checkstring(lua_State *L, int idx)
|
|
|
122 {
|
|
|
123 size_t len;
|
|
|
124 const char *str = luaL_checklstring(L, idx, &len);
|
|
|
125 return std::string(str, len);
|
|
|
126 }
|
|
|
127
|
|
|
128 void luax_pushstring(lua_State *L, std::string str)
|
|
|
129 {
|
|
|
130 lua_pushlstring(L, str.data(), str.size());
|
|
|
131 }
|
|
|
132
|
|
|
133 int luax_assert_argc(lua_State *L, int min)
|
|
|
134 {
|
|
|
135 int argc = lua_gettop(L);
|
|
|
136 if (argc < min)
|
|
|
137 return luaL_error(L, "Incorrect number of arguments. Got [%d], expected at least [%d]", argc, min);
|
|
|
138 return 0;
|
|
|
139 }
|
|
|
140
|
|
|
141 int luax_assert_argc(lua_State *L, int min, int max)
|
|
|
142 {
|
|
|
143 int argc = lua_gettop(L);
|
|
|
144 if (argc < min || argc > max)
|
|
|
145 return luaL_error(L, "Incorrect number of arguments. Got [%d], expected [%d-%d]", argc, min, max);
|
|
|
146 return 0;
|
|
|
147 }
|
|
|
148
|
|
|
149 int luax_assert_function(lua_State *L, int n)
|
|
|
150 {
|
|
|
151 if (!lua_isfunction(L, n))
|
|
|
152 return luaL_error(L, "Argument must be of type \"function\".");
|
|
|
153 return 0;
|
|
|
154 }
|
|
|
155
|
|
|
156 int luax_register_module(lua_State *L, const WrappedModule &m)
|
|
|
157 {
|
|
|
158 // Put a reference to the C++ module in Lua.
|
|
|
159 luax_getregistry(L, REGISTRY_MODULES);
|
|
|
160
|
|
|
161 Proxy *p = (Proxy *)lua_newuserdata(L, sizeof(Proxy));
|
|
|
162 p->own = true;
|
|
|
163 p->data = m.module;
|
|
|
164 p->flags = m.flags;
|
|
|
165
|
|
|
166 luaL_newmetatable(L, m.module->getName());
|
|
|
167 lua_pushvalue(L, -1);
|
|
|
168 lua_setfield(L, -2, "__index");
|
|
|
169 lua_pushcfunction(L, w__gc);
|
|
|
170 lua_setfield(L, -2, "__gc");
|
|
|
171
|
|
|
172 lua_setmetatable(L, -2);
|
|
|
173 lua_setfield(L, -2, m.name); // _modules[name] = proxy
|
|
|
174 lua_pop(L, 1);
|
|
|
175
|
|
|
176 // Gets the love table.
|
|
|
177 luax_insistglobal(L, "love");
|
|
|
178
|
|
|
179 // Create new table for module.
|
|
|
180 lua_newtable(L);
|
|
|
181
|
|
|
182 // Register all the functions.
|
|
|
183 luaL_register(L, 0, m.functions);
|
|
|
184
|
|
|
185 // Register types.
|
|
|
186 if (m.types != 0)
|
|
|
187 for (const lua_CFunction *t = m.types; *t != 0; t++)
|
|
|
188 (*t)(L);
|
|
|
189
|
|
|
190 lua_pushvalue(L, -1);
|
|
|
191 lua_setfield(L, -3, m.name); // love.graphics = table
|
|
|
192 lua_remove(L, -2); // love
|
|
|
193
|
|
|
194 return 1;
|
|
|
195 }
|
|
|
196
|
|
|
197 int luax_preload(lua_State *L, lua_CFunction f, const char *name)
|
|
|
198 {
|
|
|
199 lua_getglobal(L, "package");
|
|
|
200 lua_getfield(L, -1, "preload");
|
|
|
201 lua_pushcfunction(L, f);
|
|
|
202 lua_setfield(L, -2, name);
|
|
|
203 lua_pop(L, 2);
|
|
|
204 return 0;
|
|
|
205 }
|
|
|
206
|
|
|
207 int luax_register_type(lua_State *L, const char *tname, const luaL_Reg *f)
|
|
|
208 {
|
|
|
209 luaL_newmetatable(L, tname);
|
|
|
210
|
|
|
211 // m.__index = m
|
|
|
212 lua_pushvalue(L, -1);
|
|
|
213 lua_setfield(L, -2, "__index");
|
|
|
214
|
|
|
215 // setup gc
|
|
|
216 lua_pushcfunction(L, w__gc);
|
|
|
217 lua_setfield(L, -2, "__gc");
|
|
|
218
|
|
|
219 // Add equality
|
|
|
220 lua_pushcfunction(L, w__eq);
|
|
|
221 lua_setfield(L, -2, "__eq");
|
|
|
222
|
|
|
223 // Add tostring function.
|
|
|
224 lua_pushstring(L, tname);
|
|
|
225 lua_pushcclosure(L, w__tostring, 1);
|
|
|
226 lua_setfield(L, -2, "__tostring");
|
|
|
227
|
|
|
228 // Add tostring to as type() as well.
|
|
|
229 lua_pushstring(L, tname);
|
|
|
230 lua_pushcclosure(L, w__tostring, 1);
|
|
|
231 lua_setfield(L, -2, "type");
|
|
|
232
|
|
|
233 // Add typeOf
|
|
|
234 lua_pushcfunction(L, w__typeOf);
|
|
|
235 lua_setfield(L, -2, "typeOf");
|
|
|
236
|
|
|
237 if (f != 0)
|
|
|
238 luaL_register(L, 0, f);
|
|
|
239
|
|
|
240 lua_pop(L, 1); // Pops metatable.
|
|
|
241 return 0;
|
|
|
242 }
|
|
|
243
|
|
|
244 int luax_table_insert(lua_State *L, int tindex, int vindex, int pos)
|
|
|
245 {
|
|
|
246 if (tindex < 0)
|
|
|
247 tindex = lua_gettop(L)+1+tindex;
|
|
|
248 if (vindex < 0)
|
|
|
249 vindex = lua_gettop(L)+1+vindex;
|
|
|
250 if (pos == -1)
|
|
|
251 {
|
|
|
252 lua_pushvalue(L, vindex);
|
|
|
253 lua_rawseti(L, tindex, lua_objlen(L, tindex)+1);
|
|
|
254 return 0;
|
|
|
255 }
|
|
|
256 else if (pos < 0)
|
|
|
257 pos = lua_objlen(L, tindex)+1+pos;
|
|
|
258 for (int i = lua_objlen(L, tindex)+1; i > pos; i--)
|
|
|
259 {
|
|
|
260 lua_rawgeti(L, tindex, i-1);
|
|
|
261 lua_rawseti(L, tindex, i);
|
|
|
262 }
|
|
|
263 lua_pushvalue(L, vindex);
|
|
|
264 lua_rawseti(L, tindex, pos);
|
|
|
265 return 0;
|
|
|
266 }
|
|
|
267
|
|
|
268 int luax_register_searcher(lua_State *L, lua_CFunction f, int pos)
|
|
|
269 {
|
|
|
270 // Add the package loader to the package.loaders table.
|
|
|
271 lua_getglobal(L, "package");
|
|
|
272
|
|
|
273 if (lua_isnil(L, -1))
|
|
|
274 return luaL_error(L, "Can't register searcher: package table does not exist.");
|
|
|
275
|
|
|
276 lua_getfield(L, -1, "loaders");
|
|
|
277
|
|
|
278 if (lua_isnil(L, -1))
|
|
|
279 return luaL_error(L, "Can't register searcher: package.loaders table does not exist.");
|
|
|
280
|
|
|
281 lua_pushcfunction(L, f);
|
|
|
282 luax_table_insert(L, -2, -1, pos);
|
|
|
283 lua_pop(L, 3);
|
|
|
284 return 0;
|
|
|
285 }
|
|
|
286
|
|
|
287 void luax_newtype(lua_State *L, const char *name, bits flags, void *data, bool own)
|
|
|
288 {
|
|
|
289 Proxy *u = (Proxy *)lua_newuserdata(L, sizeof(Proxy));
|
|
|
290
|
|
|
291 u->data = data;
|
|
|
292 u->flags = flags;
|
|
|
293 u->own = own;
|
|
|
294
|
|
|
295 luaL_newmetatable(L, name);
|
|
|
296 lua_setmetatable(L, -2);
|
|
|
297 }
|
|
|
298
|
|
|
299 bool luax_istype(lua_State *L, int idx, love::bits type)
|
|
|
300 {
|
|
|
301 if (lua_isuserdata(L, idx) == 0)
|
|
|
302 return false;
|
|
|
303
|
|
|
304 return ((((Proxy *)lua_touserdata(L, idx))->flags & type) == type);
|
|
|
305 }
|
|
|
306
|
|
|
307 int luax_getfunction(lua_State *L, const char *mod, const char *fn)
|
|
|
308 {
|
|
|
309 lua_getglobal(L, "love");
|
|
|
310 if (lua_isnil(L, -1)) return luaL_error(L, "Could not find global love!");
|
|
|
311 lua_getfield(L, -1, mod);
|
|
|
312 if (lua_isnil(L, -1)) return luaL_error(L, "Could not find love.%s!", mod);
|
|
|
313 lua_getfield(L, -1, fn);
|
|
|
314 if (lua_isnil(L, -1)) return luaL_error(L, "Could not find love.%s.%s!", mod, fn);
|
|
|
315
|
|
|
316 lua_remove(L, -2); // remove mod
|
|
|
317 lua_remove(L, -2); // remove fn
|
|
|
318 return 0;
|
|
|
319 }
|
|
|
320
|
|
|
321 int luax_convobj(lua_State *L, int idx, const char *mod, const char *fn)
|
|
|
322 {
|
|
|
323 // Convert string to a file.
|
|
|
324 luax_getfunction(L, mod, fn);
|
|
|
325 lua_pushvalue(L, idx); // The initial argument.
|
|
|
326 lua_call(L, 1, 1); // Call the function, one arg, one return value.
|
|
|
327 lua_replace(L, idx); // Replace the initial argument with the new object.
|
|
|
328 return 0;
|
|
|
329 }
|
|
|
330
|
|
|
331 int luax_convobj(lua_State *L, int idxs[], int n, const char *mod, const char *fn)
|
|
|
332 {
|
|
|
333 luax_getfunction(L, mod, fn);
|
|
|
334 for (int i = 0; i < n; i++)
|
|
|
335 {
|
|
|
336 lua_pushvalue(L, idxs[i]); // The arguments.
|
|
|
337 }
|
|
|
338 lua_call(L, n, 1); // Call the function, n args, one return value.
|
|
|
339 lua_replace(L, idxs[0]); // Replace the initial argument with the new object.
|
|
|
340 return 0;
|
|
|
341 }
|
|
|
342
|
|
|
343 int luax_strtofile(lua_State *L, int idx)
|
|
|
344 {
|
|
|
345 return luax_convobj(L, idx, "filesystem", "newFile");
|
|
|
346 }
|
|
|
347
|
|
|
348 int luax_filetodata(lua_State *L, int idx)
|
|
|
349 {
|
|
|
350 return luax_convobj(L, idx, "filesystem", "read");
|
|
|
351 }
|
|
|
352
|
|
|
353 int luax_insist(lua_State *L, int idx, const char *k)
|
|
|
354 {
|
|
|
355 // Convert to absolute index if necessary.
|
|
|
356 if (idx < 0 && idx > LUA_REGISTRYINDEX)
|
|
|
357 idx = lua_gettop(L) + ++idx;
|
|
|
358
|
|
|
359 lua_getfield(L, idx, k);
|
|
|
360
|
|
|
361 // Create if necessary.
|
|
|
362 if (!lua_istable(L, -1))
|
|
|
363 {
|
|
|
364 lua_pop(L, 1); // Pop the non-table.
|
|
|
365 lua_newtable(L);
|
|
|
366 lua_pushvalue(L, -1); // Duplicate the table to leave on top.
|
|
|
367 lua_setfield(L, idx, k); // lua_stack[idx][k] = lua_stack[-1] (table)
|
|
|
368 }
|
|
|
369
|
|
|
370 return 1;
|
|
|
371 }
|
|
|
372
|
|
|
373 int luax_insistglobal(lua_State *L, const char *k)
|
|
|
374 {
|
|
|
375 lua_getglobal(L, k);
|
|
|
376
|
|
|
377 if (!lua_istable(L, -1))
|
|
|
378 {
|
|
|
379 lua_pop(L, 1); // Pop the non-table.
|
|
|
380 lua_newtable(L);
|
|
|
381 lua_pushvalue(L, -1);
|
|
|
382 lua_setglobal(L, k);
|
|
|
383 }
|
|
|
384
|
|
|
385 return 1;
|
|
|
386 }
|
|
|
387
|
|
|
388 int luax_insistlove(lua_State *L, const char *k)
|
|
|
389 {
|
|
|
390 luax_insistglobal(L, "love");
|
|
|
391 luax_insist(L, -1, k);
|
|
|
392
|
|
|
393 // The love table should be replaced with the top stack
|
|
|
394 // item. Only the reqested table should remain on the stack.
|
|
|
395 lua_replace(L, -2);
|
|
|
396
|
|
|
397 return 1;
|
|
|
398 }
|
|
|
399
|
|
|
400 int luax_getregistry(lua_State *L, Registry r)
|
|
|
401 {
|
|
|
402 switch (r)
|
|
|
403 {
|
|
|
404 case REGISTRY_GC:
|
|
|
405 return luax_insistlove(L, "_gc");
|
|
|
406 case REGISTRY_MODULES:
|
|
|
407 return luax_insistlove(L, "_modules");
|
|
|
408 default:
|
|
|
409 return luaL_error(L, "Attempted to use invalid registry.");
|
|
|
410 }
|
|
|
411 }
|
|
|
412
|
|
|
413 StringMap<Type, TYPE_MAX_ENUM>::Entry typeEntries[] =
|
|
|
414 {
|
|
|
415 {"Invalid", INVALID_ID},
|
|
|
416
|
|
|
417 {"Object", OBJECT_ID},
|
|
|
418 {"Data", DATA_ID},
|
|
|
419 {"Module", MODULE_ID},
|
|
|
420
|
|
|
421 // Filesystem
|
|
|
422 {"File", FILESYSTEM_FILE_ID},
|
|
|
423 {"FileData", FILESYSTEM_FILE_DATA_ID},
|
|
|
424
|
|
|
425 // Font
|
|
|
426 {"GlyphData", FONT_GLYPH_DATA_ID},
|
|
|
427 {"Rasterizer", FONT_RASTERIZER_ID},
|
|
|
428
|
|
|
429 // Graphics
|
|
|
430 {"Drawable", GRAPHICS_DRAWABLE_ID},
|
|
|
431 {"Image", GRAPHICS_IMAGE_ID},
|
|
|
432 {"Quad", GRAPHICS_QUAD_ID},
|
|
|
433 {"Font", GRAPHICS_FONT_ID},
|
|
|
434 {"ParticleSystem", GRAPHICS_PARTICLE_SYSTEM_ID},
|
|
|
435 {"SpriteBatch", GRAPHICS_SPRITE_BATCH_ID},
|
|
|
436 {"Canvas", GRAPHICS_CANVAS_ID},
|
|
|
437
|
|
|
438 // Image
|
|
|
439 {"ImageData", IMAGE_IMAGE_DATA_ID},
|
|
|
440
|
|
|
441 // Audio
|
|
|
442 {"Source", AUDIO_SOURCE_ID},
|
|
|
443
|
|
|
444 // Sound
|
|
|
445 {"SoundData", SOUND_SOUND_DATA_ID},
|
|
|
446 {"Decoder", SOUND_DECODER_ID},
|
|
|
447
|
|
|
448 // Physics
|
|
|
449 {"World", PHYSICS_WORLD_ID},
|
|
|
450 {"Contact", PHYSICS_CONTACT_ID},
|
|
|
451 {"Body", PHYSICS_BODY_ID},
|
|
|
452 {"Shape", PHYSICS_SHAPE_ID},
|
|
|
453 {"CircleShape", PHYSICS_CIRCLE_SHAPE_ID},
|
|
|
454 {"PolygonShape", PHYSICS_POLYGON_SHAPE_ID},
|
|
|
455 {"Joint", PHYSICS_JOINT_ID},
|
|
|
456 {"MouseJoint", PHYSICS_MOUSE_JOINT_ID},
|
|
|
457 {"DistanceJoint", PHYSICS_DISTANCE_JOINT_ID},
|
|
|
458 {"PrismaticJoint", PHYSICS_PRISMATIC_JOINT_ID},
|
|
|
459 {"RevoluteJoint", PHYSICS_REVOLUTE_JOINT_ID},
|
|
|
460 {"PulleyJoint", PHYSICS_PULLEY_JOINT_ID},
|
|
|
461 {"GearJoint", PHYSICS_GEAR_JOINT_ID},
|
|
|
462
|
|
|
463 // Thread
|
|
|
464 {"Thread", THREAD_THREAD_ID},
|
|
|
465
|
|
|
466 // The modules themselves. Only add abstracted modules here.
|
|
|
467 {"filesystem", MODULE_FILESYSTEM_ID},
|
|
|
468 {"image", MODULE_IMAGE_ID},
|
|
|
469 {"sound", MODULE_SOUND_ID},
|
|
|
470 };
|
|
|
471
|
|
|
472 StringMap<Type, TYPE_MAX_ENUM> types(typeEntries, sizeof(typeEntries));
|
|
|
473
|
|
|
474 Type luax_type(lua_State *L, int idx)
|
|
|
475 {
|
|
|
476 Type t = INVALID_ID;
|
|
|
477 types.find(luaL_checkstring(L, idx), t);
|
|
|
478 return t;
|
|
|
479 }
|
|
|
480
|
|
|
481 } // love
|