Mercurial > love-experiments
annotate src/common/runtime.cpp @ 1282:b7f097053d74
Updated copyright text for the new year
| author | Alex Szpakowski <slime73@gmail.com> |
|---|---|
| date | Tue, 05 Feb 2013 05:32:49 -0400 |
| parents | fb1bf6a0dede |
| children | 730ed5f01ab0 |
| rev | line source |
|---|---|
| 1096 | 1 /** |
|
1282
b7f097053d74
Updated copyright text for the new year
Alex Szpakowski <slime73@gmail.com>
parents:
1168
diff
changeset
|
2 * Copyright (c) 2006-2013 LOVE Development Team |
| 1096 | 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 | |
|
1117
3c6206da8bcf
Add Module::registerInstance() and Module::getInstance().
vrld <vrld@vrld.org>
parents:
1096
diff
changeset
|
194 // Register module instance |
|
3c6206da8bcf
Add Module::registerInstance() and Module::getInstance().
vrld <vrld@vrld.org>
parents:
1096
diff
changeset
|
195 Module::registerInstance(m.module); |
|
3c6206da8bcf
Add Module::registerInstance() and Module::getInstance().
vrld <vrld@vrld.org>
parents:
1096
diff
changeset
|
196 |
| 1096 | 197 return 1; |
| 198 } | |
| 199 | |
| 200 int luax_preload(lua_State *L, lua_CFunction f, const char *name) | |
| 201 { | |
| 202 lua_getglobal(L, "package"); | |
| 203 lua_getfield(L, -1, "preload"); | |
| 204 lua_pushcfunction(L, f); | |
| 205 lua_setfield(L, -2, name); | |
| 206 lua_pop(L, 2); | |
| 207 return 0; | |
| 208 } | |
| 209 | |
| 210 int luax_register_type(lua_State *L, const char *tname, const luaL_Reg *f) | |
| 211 { | |
| 212 luaL_newmetatable(L, tname); | |
| 213 | |
| 214 // m.__index = m | |
| 215 lua_pushvalue(L, -1); | |
| 216 lua_setfield(L, -2, "__index"); | |
| 217 | |
| 218 // setup gc | |
| 219 lua_pushcfunction(L, w__gc); | |
| 220 lua_setfield(L, -2, "__gc"); | |
| 221 | |
| 222 // Add equality | |
| 223 lua_pushcfunction(L, w__eq); | |
| 224 lua_setfield(L, -2, "__eq"); | |
| 225 | |
| 226 // Add tostring function. | |
| 227 lua_pushstring(L, tname); | |
| 228 lua_pushcclosure(L, w__tostring, 1); | |
| 229 lua_setfield(L, -2, "__tostring"); | |
| 230 | |
| 231 // Add tostring to as type() as well. | |
| 232 lua_pushstring(L, tname); | |
| 233 lua_pushcclosure(L, w__tostring, 1); | |
| 234 lua_setfield(L, -2, "type"); | |
| 235 | |
| 236 // Add typeOf | |
| 237 lua_pushcfunction(L, w__typeOf); | |
| 238 lua_setfield(L, -2, "typeOf"); | |
| 239 | |
| 240 if (f != 0) | |
| 241 luaL_register(L, 0, f); | |
| 242 | |
| 243 lua_pop(L, 1); // Pops metatable. | |
| 244 return 0; | |
| 245 } | |
| 246 | |
| 247 int luax_table_insert(lua_State *L, int tindex, int vindex, int pos) | |
| 248 { | |
| 249 if (tindex < 0) | |
| 250 tindex = lua_gettop(L)+1+tindex; | |
| 251 if (vindex < 0) | |
| 252 vindex = lua_gettop(L)+1+vindex; | |
| 253 if (pos == -1) | |
| 254 { | |
| 255 lua_pushvalue(L, vindex); | |
| 256 lua_rawseti(L, tindex, lua_objlen(L, tindex)+1); | |
| 257 return 0; | |
| 258 } | |
| 259 else if (pos < 0) | |
| 260 pos = lua_objlen(L, tindex)+1+pos; | |
| 261 for (int i = lua_objlen(L, tindex)+1; i > pos; i--) | |
| 262 { | |
| 263 lua_rawgeti(L, tindex, i-1); | |
| 264 lua_rawseti(L, tindex, i); | |
| 265 } | |
| 266 lua_pushvalue(L, vindex); | |
| 267 lua_rawseti(L, tindex, pos); | |
| 268 return 0; | |
| 269 } | |
| 270 | |
| 271 int luax_register_searcher(lua_State *L, lua_CFunction f, int pos) | |
| 272 { | |
| 273 // Add the package loader to the package.loaders table. | |
| 274 lua_getglobal(L, "package"); | |
| 275 | |
| 276 if (lua_isnil(L, -1)) | |
| 277 return luaL_error(L, "Can't register searcher: package table does not exist."); | |
| 278 | |
| 279 lua_getfield(L, -1, "loaders"); | |
| 280 | |
| 281 if (lua_isnil(L, -1)) | |
| 282 return luaL_error(L, "Can't register searcher: package.loaders table does not exist."); | |
| 283 | |
| 284 lua_pushcfunction(L, f); | |
| 285 luax_table_insert(L, -2, -1, pos); | |
| 286 lua_pop(L, 3); | |
| 287 return 0; | |
| 288 } | |
| 289 | |
| 290 void luax_newtype(lua_State *L, const char *name, bits flags, void *data, bool own) | |
| 291 { | |
| 292 Proxy *u = (Proxy *)lua_newuserdata(L, sizeof(Proxy)); | |
| 293 | |
| 294 u->data = data; | |
| 295 u->flags = flags; | |
| 296 u->own = own; | |
| 297 | |
| 298 luaL_newmetatable(L, name); | |
| 299 lua_setmetatable(L, -2); | |
| 300 } | |
| 301 | |
| 302 bool luax_istype(lua_State *L, int idx, love::bits type) | |
| 303 { | |
| 304 if (lua_isuserdata(L, idx) == 0) | |
| 305 return false; | |
| 306 | |
| 307 return ((((Proxy *)lua_touserdata(L, idx))->flags & type) == type); | |
| 308 } | |
| 309 | |
| 310 int luax_getfunction(lua_State *L, const char *mod, const char *fn) | |
| 311 { | |
| 312 lua_getglobal(L, "love"); | |
| 313 if (lua_isnil(L, -1)) return luaL_error(L, "Could not find global love!"); | |
| 314 lua_getfield(L, -1, mod); | |
| 315 if (lua_isnil(L, -1)) return luaL_error(L, "Could not find love.%s!", mod); | |
| 316 lua_getfield(L, -1, fn); | |
| 317 if (lua_isnil(L, -1)) return luaL_error(L, "Could not find love.%s.%s!", mod, fn); | |
| 318 | |
| 319 lua_remove(L, -2); // remove mod | |
| 320 lua_remove(L, -2); // remove fn | |
| 321 return 0; | |
| 322 } | |
| 323 | |
| 324 int luax_convobj(lua_State *L, int idx, const char *mod, const char *fn) | |
| 325 { | |
| 326 // Convert string to a file. | |
| 327 luax_getfunction(L, mod, fn); | |
| 328 lua_pushvalue(L, idx); // The initial argument. | |
| 329 lua_call(L, 1, 1); // Call the function, one arg, one return value. | |
| 330 lua_replace(L, idx); // Replace the initial argument with the new object. | |
| 331 return 0; | |
| 332 } | |
| 333 | |
| 334 int luax_convobj(lua_State *L, int idxs[], int n, const char *mod, const char *fn) | |
| 335 { | |
| 336 luax_getfunction(L, mod, fn); | |
| 337 for (int i = 0; i < n; i++) | |
| 338 { | |
| 339 lua_pushvalue(L, idxs[i]); // The arguments. | |
| 340 } | |
| 341 lua_call(L, n, 1); // Call the function, n args, one return value. | |
| 342 lua_replace(L, idxs[0]); // Replace the initial argument with the new object. | |
| 343 return 0; | |
| 344 } | |
| 345 | |
|
1168
fb1bf6a0dede
Catch errors in love.font.newRasterizer, add luax_pconvobj which pcalls, allowing love.graphics.newFont to clean up before re-erroring
Bart van Strien <bart.bes+projects@gmail.com>
parents:
1117
diff
changeset
|
346 int luax_pconvobj(lua_State *L, int idx, const char *mod, const char *fn) |
|
fb1bf6a0dede
Catch errors in love.font.newRasterizer, add luax_pconvobj which pcalls, allowing love.graphics.newFont to clean up before re-erroring
Bart van Strien <bart.bes+projects@gmail.com>
parents:
1117
diff
changeset
|
347 { |
|
fb1bf6a0dede
Catch errors in love.font.newRasterizer, add luax_pconvobj which pcalls, allowing love.graphics.newFont to clean up before re-erroring
Bart van Strien <bart.bes+projects@gmail.com>
parents:
1117
diff
changeset
|
348 // Convert string to a file. |
|
fb1bf6a0dede
Catch errors in love.font.newRasterizer, add luax_pconvobj which pcalls, allowing love.graphics.newFont to clean up before re-erroring
Bart van Strien <bart.bes+projects@gmail.com>
parents:
1117
diff
changeset
|
349 luax_getfunction(L, mod, fn); |
|
fb1bf6a0dede
Catch errors in love.font.newRasterizer, add luax_pconvobj which pcalls, allowing love.graphics.newFont to clean up before re-erroring
Bart van Strien <bart.bes+projects@gmail.com>
parents:
1117
diff
changeset
|
350 lua_pushvalue(L, idx); // The initial argument. |
|
fb1bf6a0dede
Catch errors in love.font.newRasterizer, add luax_pconvobj which pcalls, allowing love.graphics.newFont to clean up before re-erroring
Bart van Strien <bart.bes+projects@gmail.com>
parents:
1117
diff
changeset
|
351 int ret = lua_pcall(L, 1, 1, 0); // Call the function, one arg, one return value. |
|
fb1bf6a0dede
Catch errors in love.font.newRasterizer, add luax_pconvobj which pcalls, allowing love.graphics.newFont to clean up before re-erroring
Bart van Strien <bart.bes+projects@gmail.com>
parents:
1117
diff
changeset
|
352 if (ret == 0) |
|
fb1bf6a0dede
Catch errors in love.font.newRasterizer, add luax_pconvobj which pcalls, allowing love.graphics.newFont to clean up before re-erroring
Bart van Strien <bart.bes+projects@gmail.com>
parents:
1117
diff
changeset
|
353 lua_replace(L, idx); // Replace the initial argument with the new object. |
|
fb1bf6a0dede
Catch errors in love.font.newRasterizer, add luax_pconvobj which pcalls, allowing love.graphics.newFont to clean up before re-erroring
Bart van Strien <bart.bes+projects@gmail.com>
parents:
1117
diff
changeset
|
354 return ret; |
|
fb1bf6a0dede
Catch errors in love.font.newRasterizer, add luax_pconvobj which pcalls, allowing love.graphics.newFont to clean up before re-erroring
Bart van Strien <bart.bes+projects@gmail.com>
parents:
1117
diff
changeset
|
355 } |
|
fb1bf6a0dede
Catch errors in love.font.newRasterizer, add luax_pconvobj which pcalls, allowing love.graphics.newFont to clean up before re-erroring
Bart van Strien <bart.bes+projects@gmail.com>
parents:
1117
diff
changeset
|
356 |
|
fb1bf6a0dede
Catch errors in love.font.newRasterizer, add luax_pconvobj which pcalls, allowing love.graphics.newFont to clean up before re-erroring
Bart van Strien <bart.bes+projects@gmail.com>
parents:
1117
diff
changeset
|
357 int luax_pconvobj(lua_State *L, int idxs[], int n, const char *mod, const char *fn) |
|
fb1bf6a0dede
Catch errors in love.font.newRasterizer, add luax_pconvobj which pcalls, allowing love.graphics.newFont to clean up before re-erroring
Bart van Strien <bart.bes+projects@gmail.com>
parents:
1117
diff
changeset
|
358 { |
|
fb1bf6a0dede
Catch errors in love.font.newRasterizer, add luax_pconvobj which pcalls, allowing love.graphics.newFont to clean up before re-erroring
Bart van Strien <bart.bes+projects@gmail.com>
parents:
1117
diff
changeset
|
359 luax_getfunction(L, mod, fn); |
|
fb1bf6a0dede
Catch errors in love.font.newRasterizer, add luax_pconvobj which pcalls, allowing love.graphics.newFont to clean up before re-erroring
Bart van Strien <bart.bes+projects@gmail.com>
parents:
1117
diff
changeset
|
360 for (int i = 0; i < n; i++) |
|
fb1bf6a0dede
Catch errors in love.font.newRasterizer, add luax_pconvobj which pcalls, allowing love.graphics.newFont to clean up before re-erroring
Bart van Strien <bart.bes+projects@gmail.com>
parents:
1117
diff
changeset
|
361 { |
|
fb1bf6a0dede
Catch errors in love.font.newRasterizer, add luax_pconvobj which pcalls, allowing love.graphics.newFont to clean up before re-erroring
Bart van Strien <bart.bes+projects@gmail.com>
parents:
1117
diff
changeset
|
362 lua_pushvalue(L, idxs[i]); // The arguments. |
|
fb1bf6a0dede
Catch errors in love.font.newRasterizer, add luax_pconvobj which pcalls, allowing love.graphics.newFont to clean up before re-erroring
Bart van Strien <bart.bes+projects@gmail.com>
parents:
1117
diff
changeset
|
363 } |
|
fb1bf6a0dede
Catch errors in love.font.newRasterizer, add luax_pconvobj which pcalls, allowing love.graphics.newFont to clean up before re-erroring
Bart van Strien <bart.bes+projects@gmail.com>
parents:
1117
diff
changeset
|
364 int ret = lua_pcall(L, n, 1, 0); // Call the function, n args, one return value. |
|
fb1bf6a0dede
Catch errors in love.font.newRasterizer, add luax_pconvobj which pcalls, allowing love.graphics.newFont to clean up before re-erroring
Bart van Strien <bart.bes+projects@gmail.com>
parents:
1117
diff
changeset
|
365 if (ret == 0) |
|
fb1bf6a0dede
Catch errors in love.font.newRasterizer, add luax_pconvobj which pcalls, allowing love.graphics.newFont to clean up before re-erroring
Bart van Strien <bart.bes+projects@gmail.com>
parents:
1117
diff
changeset
|
366 lua_replace(L, idxs[0]); // Replace the initial argument with the new object. |
|
fb1bf6a0dede
Catch errors in love.font.newRasterizer, add luax_pconvobj which pcalls, allowing love.graphics.newFont to clean up before re-erroring
Bart van Strien <bart.bes+projects@gmail.com>
parents:
1117
diff
changeset
|
367 return ret; |
|
fb1bf6a0dede
Catch errors in love.font.newRasterizer, add luax_pconvobj which pcalls, allowing love.graphics.newFont to clean up before re-erroring
Bart van Strien <bart.bes+projects@gmail.com>
parents:
1117
diff
changeset
|
368 } |
|
fb1bf6a0dede
Catch errors in love.font.newRasterizer, add luax_pconvobj which pcalls, allowing love.graphics.newFont to clean up before re-erroring
Bart van Strien <bart.bes+projects@gmail.com>
parents:
1117
diff
changeset
|
369 |
| 1096 | 370 int luax_strtofile(lua_State *L, int idx) |
| 371 { | |
| 372 return luax_convobj(L, idx, "filesystem", "newFile"); | |
| 373 } | |
| 374 | |
| 375 int luax_filetodata(lua_State *L, int idx) | |
| 376 { | |
| 377 return luax_convobj(L, idx, "filesystem", "read"); | |
| 378 } | |
| 379 | |
| 380 int luax_insist(lua_State *L, int idx, const char *k) | |
| 381 { | |
| 382 // Convert to absolute index if necessary. | |
| 383 if (idx < 0 && idx > LUA_REGISTRYINDEX) | |
| 384 idx = lua_gettop(L) + ++idx; | |
| 385 | |
| 386 lua_getfield(L, idx, k); | |
| 387 | |
| 388 // Create if necessary. | |
| 389 if (!lua_istable(L, -1)) | |
| 390 { | |
| 391 lua_pop(L, 1); // Pop the non-table. | |
| 392 lua_newtable(L); | |
| 393 lua_pushvalue(L, -1); // Duplicate the table to leave on top. | |
| 394 lua_setfield(L, idx, k); // lua_stack[idx][k] = lua_stack[-1] (table) | |
| 395 } | |
| 396 | |
| 397 return 1; | |
| 398 } | |
| 399 | |
| 400 int luax_insistglobal(lua_State *L, const char *k) | |
| 401 { | |
| 402 lua_getglobal(L, k); | |
| 403 | |
| 404 if (!lua_istable(L, -1)) | |
| 405 { | |
| 406 lua_pop(L, 1); // Pop the non-table. | |
| 407 lua_newtable(L); | |
| 408 lua_pushvalue(L, -1); | |
| 409 lua_setglobal(L, k); | |
| 410 } | |
| 411 | |
| 412 return 1; | |
| 413 } | |
| 414 | |
| 415 int luax_insistlove(lua_State *L, const char *k) | |
| 416 { | |
| 417 luax_insistglobal(L, "love"); | |
| 418 luax_insist(L, -1, k); | |
| 419 | |
| 420 // The love table should be replaced with the top stack | |
| 421 // item. Only the reqested table should remain on the stack. | |
| 422 lua_replace(L, -2); | |
| 423 | |
| 424 return 1; | |
| 425 } | |
| 426 | |
| 427 int luax_getregistry(lua_State *L, Registry r) | |
| 428 { | |
| 429 switch (r) | |
| 430 { | |
| 431 case REGISTRY_GC: | |
| 432 return luax_insistlove(L, "_gc"); | |
| 433 case REGISTRY_MODULES: | |
| 434 return luax_insistlove(L, "_modules"); | |
| 435 default: | |
| 436 return luaL_error(L, "Attempted to use invalid registry."); | |
| 437 } | |
| 438 } | |
| 439 | |
| 440 StringMap<Type, TYPE_MAX_ENUM>::Entry typeEntries[] = | |
| 441 { | |
| 442 {"Invalid", INVALID_ID}, | |
| 443 | |
| 444 {"Object", OBJECT_ID}, | |
| 445 {"Data", DATA_ID}, | |
| 446 {"Module", MODULE_ID}, | |
| 447 | |
| 448 // Filesystem | |
| 449 {"File", FILESYSTEM_FILE_ID}, | |
| 450 {"FileData", FILESYSTEM_FILE_DATA_ID}, | |
| 451 | |
| 452 // Font | |
| 453 {"GlyphData", FONT_GLYPH_DATA_ID}, | |
| 454 {"Rasterizer", FONT_RASTERIZER_ID}, | |
| 455 | |
| 456 // Graphics | |
| 457 {"Drawable", GRAPHICS_DRAWABLE_ID}, | |
| 458 {"Image", GRAPHICS_IMAGE_ID}, | |
| 459 {"Quad", GRAPHICS_QUAD_ID}, | |
| 460 {"Font", GRAPHICS_FONT_ID}, | |
| 461 {"ParticleSystem", GRAPHICS_PARTICLE_SYSTEM_ID}, | |
| 462 {"SpriteBatch", GRAPHICS_SPRITE_BATCH_ID}, | |
| 463 {"Canvas", GRAPHICS_CANVAS_ID}, | |
| 464 | |
| 465 // Image | |
| 466 {"ImageData", IMAGE_IMAGE_DATA_ID}, | |
| 467 | |
| 468 // Audio | |
| 469 {"Source", AUDIO_SOURCE_ID}, | |
| 470 | |
| 471 // Sound | |
| 472 {"SoundData", SOUND_SOUND_DATA_ID}, | |
| 473 {"Decoder", SOUND_DECODER_ID}, | |
| 474 | |
| 475 // Physics | |
| 476 {"World", PHYSICS_WORLD_ID}, | |
| 477 {"Contact", PHYSICS_CONTACT_ID}, | |
| 478 {"Body", PHYSICS_BODY_ID}, | |
| 479 {"Shape", PHYSICS_SHAPE_ID}, | |
| 480 {"CircleShape", PHYSICS_CIRCLE_SHAPE_ID}, | |
| 481 {"PolygonShape", PHYSICS_POLYGON_SHAPE_ID}, | |
| 482 {"Joint", PHYSICS_JOINT_ID}, | |
| 483 {"MouseJoint", PHYSICS_MOUSE_JOINT_ID}, | |
| 484 {"DistanceJoint", PHYSICS_DISTANCE_JOINT_ID}, | |
| 485 {"PrismaticJoint", PHYSICS_PRISMATIC_JOINT_ID}, | |
| 486 {"RevoluteJoint", PHYSICS_REVOLUTE_JOINT_ID}, | |
| 487 {"PulleyJoint", PHYSICS_PULLEY_JOINT_ID}, | |
| 488 {"GearJoint", PHYSICS_GEAR_JOINT_ID}, | |
| 489 | |
| 490 // Thread | |
| 491 {"Thread", THREAD_THREAD_ID}, | |
| 492 | |
| 493 // The modules themselves. Only add abstracted modules here. | |
| 494 {"filesystem", MODULE_FILESYSTEM_ID}, | |
| 495 {"image", MODULE_IMAGE_ID}, | |
| 496 {"sound", MODULE_SOUND_ID}, | |
| 497 }; | |
| 498 | |
| 499 StringMap<Type, TYPE_MAX_ENUM> types(typeEntries, sizeof(typeEntries)); | |
| 500 | |
| 501 Type luax_type(lua_State *L, int idx) | |
| 502 { | |
| 503 Type t = INVALID_ID; | |
| 504 types.find(luaL_checkstring(L, idx), t); | |
| 505 return t; | |
| 506 } | |
| 507 | |
| 508 } // love |
