1/**
2 * Copyright (c) 2006-2010 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 "wrap_LuaThread.h"
22
23namespace love
24{
25namespace thread
26{
27
28LuaThread *luax_checkthread(lua_State *L, int idx)
29{
30 return luax_checktype<LuaThread>(L, idx);
31}
32
33int w_Thread_start(lua_State *L)
34{
35 LuaThread *t = luax_checkthread(L, 1);
36 std::vector<Variant> args;
37 int nargs = lua_gettop(L) - 1;
38
39 for (int i = 0; i < nargs; ++i)
40 {
41 luax_catchexcept(L, [&]() {
42 args.push_back(Variant::fromLua(L, i+2));
43 });
44
45 if (args.back().getType() == Variant::UNKNOWN)
46 {
47 args.clear();
48 return luaL_argerror(L, i+2, "boolean, number, string, love type, or flat table expected");
49 }
50 }
51
52 luax_pushboolean(L, t->start(args));
53 return 1;
54}
55
56int w_Thread_wait(lua_State *L)
57{
58 LuaThread *t = luax_checkthread(L, 1);
59 t->wait();
60 return 0;
61}
62
63int w_Thread_getError(lua_State *L)
64{
65 LuaThread *t = luax_checkthread(L, 1);
66 if (t->hasError())
67 luax_pushstring(L, t->getError());
68 else
69 lua_pushnil(L);
70 return 1;
71}
72
73int w_Thread_isRunning(lua_State *L)
74{
75 LuaThread *t = luax_checkthread(L, 1);
76 luax_pushboolean(L, t->isRunning());
77 return 1;
78}
79
80static const luaL_Reg w_Thread_functions[] =
81{
82 { "start", w_Thread_start },
83 { "wait", w_Thread_wait },
84 { "getError", w_Thread_getError },
85 { "isRunning", w_Thread_isRunning },
86 { 0, 0 }
87};
88
89extern "C" int luaopen_thread(lua_State *L)
90{
91 return luax_register_type(L, &LuaThread::type, w_Thread_functions, nullptr);
92}
93
94} // thread
95} // love
96