comparison src/modules/thread/wrap_ThreadModule.cpp @ 1098:a74672ba1320 minor

Merge in channels from love-experiments Rewritten love.thread and changed communication method from weird system before, to channels.
author Bart van Strien <bart.bes+projects@gmail.com>
date Wed, 04 Jul 2012 17:15:16 +0200
parents
children ee1448bb9680
comparison
equal deleted inserted replaced
1097:4819322d9cdc 1098:a74672ba1320
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_ThreadModule.h"
22 #include "wrap_LuaThread.h"
23 #include "wrap_Channel.h"
24 #include <filesystem/File.h>
25 #include <filesystem/FileData.h>
26
27 namespace love
28 {
29 namespace thread
30 {
31 static ThreadModule *instance = 0;
32
33 int w_newThread(lua_State *L)
34 {
35 std::string name = "Thread code";
36 love::Data *data;
37 if (lua_isstring(L, 1))
38 luax_convobj(L, 1, "filesystem", "newFile");
39 if (luax_istype(L, 1, FILESYSTEM_FILE_T))
40 {
41 try
42 {
43 love::filesystem::File * file = luax_checktype<love::filesystem::File>(L, 1, "File", FILESYSTEM_FILE_T);
44 name = std::string("@") + file->getFilename();
45 data = file->read();
46 }
47 catch (love::Exception & e)
48 {
49 return luaL_error(L, "%s", e.what());
50 }
51 }
52 else if (luax_istype(L, 1, FILESYSTEM_FILE_DATA_T))
53 {
54 love::filesystem::FileData * fdata = luax_checktype<love::filesystem::FileData>(L, 1, "FileData", FILESYSTEM_FILE_DATA_T);
55 name = std::string("@") + fdata->getFilename();
56 data = fdata;
57 data->retain();
58 }
59 else
60 {
61 data = luax_checktype<love::Data>(L, 1, "Data", DATA_T);
62 data->retain();
63 }
64 LuaThread *t = instance->newThread(name, data);
65 // do not worry, file->read() returns retained data
66 data->release();
67 luax_newtype(L, "Thread", THREAD_THREAD_T, (void*)t);
68 return 1;
69 }
70
71 int w_newChannel(lua_State *L)
72 {
73 Channel *c = instance->newChannel();
74 luax_newtype(L, "Channel", THREAD_CHANNEL_T, (void*)c);
75 return 1;
76 }
77
78 int w_getChannel(lua_State *L)
79 {
80 std::string name = luax_checkstring(L, 1);
81 Channel *c = instance->getChannel(name);
82 luax_newtype(L, "Channel", THREAD_CHANNEL_T, (void*)c);
83 return 1;
84 }
85
86 // List of functions to wrap.
87 static const luaL_Reg module_functions[] = {
88 { "newThread", w_newThread },
89 { "newChannel", w_newChannel },
90 { "getChannel", w_getChannel },
91 { 0, 0 }
92 };
93
94 static const lua_CFunction types[] = {
95 luaopen_thread,
96 luaopen_channel,
97 0
98 };
99
100 extern "C" int luaopen_love_thread(lua_State *L)
101 {
102 if (instance == 0)
103 {
104 try
105 {
106 instance = new ThreadModule();
107 }
108 catch (Exception & e)
109 {
110 return luaL_error(L, "%s", e.what());
111 }
112 }
113 else
114 instance->retain();
115
116 WrappedModule w;
117 w.module = instance;
118 w.name = "thread";
119 w.flags = MODULE_T;
120 w.functions = module_functions;
121 w.types = types;
122
123 return luax_register_module(L, w);
124 }
125 }
126 }