1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
|
#include "lua/luax.h"
#include "libjin/jin.h"
#include "../luaopen_types.h"
namespace jin
{
namespace lua
{
static int luaopen_thread(lua_State* L);
class Thread : public jin::thread::Thread
{
public:
Thread(std::string _name, std::string _code, jin::thread::Thread::ThreadRunner runner)
: jin::thread::Thread(name, runner)
, name(_name)
, code(_code)
{
}
const std::string name;
const std::string code;
};
static inline Thread* checkThread(lua_State* L)
{
Proxy* proxy = (Proxy*)luax_checktype(L, 1, TYPE_THREAD);
if (proxy != nullptr)
return (Thread*)proxy->object;
return nullptr;
}
static int threadRunner(void* p)
{
Thread* thread = (Thread*)p;
lua_State* L = lua_open();
luax_openlibs(L);
luaopen_thread(L);
luax_dostring(L, thread->code.c_str(), thread->code.length(),thread->name.c_str());
}
static int l_gc(lua_State* L)
{
return 1;
}
static int l_start(lua_State* L)
{
Thread* t = checkThread(L);
bool result = t->start();
luax_pushboolean(L, result);
return 1;
}
static int l_wait(lua_State* L)
{
Thread* t = checkThread(L);
t->wait();
return 0;
}
static int l_send(lua_State* L)
{
Thread* t = checkThread(L);
const char* name = luax_checkstring(L, 2);
if (luax_isnumber(L, 3))
{
float real = luax_checknumber(L, 3);
t->send(name, real);
}
else if (luax_isboolean(L, 3))
{
bool bol = luax_checkbool(L, 3);
t->send(name, bol);
}
else if (luax_isstring(L, 3))
{
const char* str = luax_checkstring(L, 3);
t->send(name, str);
}
return 0;
}
static int l_receive(lua_State* L)
{
Thread* t = checkThread(L);
const char* name = luax_checkstring(L, 2);
bool result = t->receive(name);
luax_pushboolean(L, result);
return 1;
}
static int l_fetch(lua_State* L)
{
Thread* t = checkThread(L);
const char* name = luax_checkstring(L, 2);
Thread::Value v = t->fetch(name);
if (v.type == Thread::Value::INTEGER)
{
luax_pushinteger(L, v.integer);
}
else if (v.type == Thread::Value::BOOL)
{
luax_pushboolean(L, v.boolean);
}
else if (v.type == Thread::Value::STRING)
{
luax_pushstring(L, v.cstring);
}
else if (v.type == Thread::Value::POINTER)
{
}
return 1;
}
static int l_demand(lua_State* L)
{
return 1;
}
static int l_getName(lua_State* L)
{
return 1;
}
static int l_isRunning(lua_State* L)
{
return 1;
}
static const luaL_Reg thread_function[] = {
{ "__gc", l_gc },
{ "start", l_start },
{ "wait", l_wait },
{ "send", l_send },
{ "receive", l_receive },
{ "fetch", l_fetch },
{ "demand", l_demand },
{ "getName", l_getName },
{ "isRunning", l_isRunning },
{ 0, 0 }
};
static int luaopen_Thread(lua_State* L)
{
luax_newtype(L, TYPE_SOURCE, thread_function);
return 0;
}
// jin.thread.Thread(name)
static int l_newThread(lua_State* L)
{
const char* name = luax_checkstring(L, 1);
const char* code = luax_checkstring(L, 2);
Proxy* proxy = (Proxy*)luax_newinstance(L, TYPE_THREAD, sizeof(Proxy));
Thread* thread = new Thread(name, code, threadRunner);
proxy->bind(thread);
return 1;
}
static int l_getThread(lua_State* L)
{
}
static const luaL_Reg f[] = {
{ "Thread", l_newThread},
{ "getThread", l_getThread},
{ 0, 0 }
};
static int luaopen_thread(lua_State* L)
{
}
}
}
|