diff options
author | chai <chaifix@163.com> | 2018-08-14 14:56:47 +0800 |
---|---|---|
committer | chai <chaifix@163.com> | 2018-08-14 14:56:47 +0800 |
commit | 5c9af043503f92852a1a765b6ecfbc1aea24d2e9 (patch) | |
tree | eb371092c4137a672e7bfc13dc56ee777623ebfe /src/lua/thread/luaopen_Thread.cpp | |
parent | 5162f84be0a4deb447c6ba1226722b049335d525 (diff) |
*update
Diffstat (limited to 'src/lua/thread/luaopen_Thread.cpp')
-rw-r--r-- | src/lua/thread/luaopen_Thread.cpp | 430 |
1 files changed, 205 insertions, 225 deletions
diff --git a/src/lua/thread/luaopen_Thread.cpp b/src/lua/thread/luaopen_Thread.cpp index 4eabc07..70a30ee 100644 --- a/src/lua/thread/luaopen_Thread.cpp +++ b/src/lua/thread/luaopen_Thread.cpp @@ -2,266 +2,246 @@ #include "libjin/jin.h" #include "../luaopen_jin.h" #include "../luaopen_types.h" +#include "Thread.h" namespace jin { - namespace lua +namespace lua +{ + + using thread::Thread; + + int luaopen_thread(lua_State* L); + + static inline Thread* checkThread(lua_State* L) + { + Proxy* proxy = (Proxy*)luax_checktype(L, 1, JIN_THREAD_THREAD); + if (proxy != nullptr) + return (Thread*)proxy->object; + return nullptr; + } + + void Thread::threadRunner(jin::thread::Thread* t) + { + Thread* thread = (Thread*)t; + lua_State* L = lua_open(); + luax_openlibs(L); + luaopen_jin(L); + luax_getglobal(L, MODULE_NAME); + Proxy* proxy = (Proxy*)luax_newinstance(L, JIN_THREAD_THREAD, sizeof(Proxy)); + proxy->bind(thread, JIN_THREAD_THREAD); + luax_setfield(L, -2, "_curThread"); + luax_dostring(L, thread->code.c_str()); + luax_close(L); + } + + static int l_thread_gc(lua_State* L) { + Proxy* proxy = (Proxy*)luax_checktype(L, 1, JIN_THREAD_THREAD); + proxy->release(); + return 0; + } - int luaopen_thread(lua_State* L); + static int l_start(lua_State* L) + { + Thread* t = checkThread(L); + bool result = t->start(); + luax_pushboolean(L, result); + return 1; + } - class Thread : public jin::thread::Thread - , public Object - { - public: - Thread(std::string _name, std::string _code, jin::thread::Thread::ThreadRunner runner) - : jin::thread::Thread(name, runner) - , name(_name) - , code(_code) - { - } - static void threadRunner(jin::thread::Thread* t); - - private: - ~Thread() - {} - - const std::string name; - const std::string code; - }; - - static inline Thread* checkThread(lua_State* L) - { - Proxy* proxy = (Proxy*)luax_checktype(L, 1, JIN_THREAD_THREAD); - if (proxy != nullptr) - return (Thread*)proxy->object; - return nullptr; - } + static int l_wait(lua_State* L) + { + Thread* t = checkThread(L); + t->wait(); + return 0; + } - void Thread::threadRunner(jin::thread::Thread* t) + static int l_send(lua_State* L) + { + Thread* t = checkThread(L); + int slot = luax_checkinteger(L, 2); + const int vp = 3; + if (luax_isnumberstrict(L, vp)) { - Thread* thread = (Thread*)t; - lua_State* L = lua_open(); - luax_openlibs(L); - luaopen_jin(L); - luax_getglobal(L, MODULE_NAME); - Proxy* proxy = (Proxy*)luax_newinstance(L, JIN_THREAD_THREAD, sizeof(Proxy)); - thread->retain(); - proxy->bind(thread, JIN_THREAD_THREAD); - luax_setfield(L, -2, "_curThread"); - luax_dostring(L, thread->code.c_str()); - luax_close(L); + float real = luax_checknumber(L, vp); + t->send(slot, real); } - - static int l_thread_gc(lua_State* L) + else if (luax_isbooleanstrict(L, vp)) { - Proxy* proxy = (Proxy*)luax_checktype(L, 1, JIN_THREAD_THREAD); - proxy->release(); - return 0; + bool bol = luax_checkbool(L, vp); + t->send(slot, bol); } - - static int l_start(lua_State* L) + else if (luax_isstringstrict(L, vp)) { - Thread* t = checkThread(L); - bool result = t->start(); - luax_pushboolean(L, result); - return 1; + const char* str = luax_checkstring(L, vp); + t->send(slot, str); } - - static int l_wait(lua_State* L) + else if (luax_isuserdata(L, vp)) { - Thread* t = checkThread(L); - t->wait(); - return 0; + void* p = luax_touserdata(L, vp); + t->send(slot, p); } - - static int l_send(lua_State* L) + else if (luax_islightuserdata(L, vp)) { - Thread* t = checkThread(L); - int slot = luax_checkinteger(L, 2); - const int vp = 3; - if (luax_isnumberstrict(L, vp)) - { - float real = luax_checknumber(L, vp); - t->send(slot, real); - } - else if (luax_isbooleanstrict(L, vp)) - { - bool bol = luax_checkbool(L, vp); - t->send(slot, bol); - } - else if (luax_isstringstrict(L, vp)) - { - const char* str = luax_checkstring(L, vp); - t->send(slot, str); - } - else if (luax_isuserdata(L, vp)) - { - void* p = luax_touserdata(L, vp); - t->send(slot, p); - } - else if (luax_islightuserdata(L, vp)) - { - void* p = luax_tolightuserdata(L, vp); - t->send(slot, p); - } - return 0; + void* p = luax_tolightuserdata(L, vp); + t->send(slot, p); } + return 0; + } - static int l_receive(lua_State* L) - { - Thread* t = checkThread(L); - int slot = luax_checkinteger(L, 2); - bool result = t->receive(slot); - luax_pushboolean(L, result); - return 1; - } + static int l_receive(lua_State* L) + { + Thread* t = checkThread(L); + int slot = luax_checkinteger(L, 2); + bool result = t->receive(slot); + luax_pushboolean(L, result); + return 1; + } - static int l_fetch(lua_State* L) + static int l_fetch(lua_State* L) + { + Thread* t = checkThread(L); + int slot = luax_checkinteger(L, 2); + Thread::Variant v = t->fetch(slot); + switch (v.type) { - Thread* t = checkThread(L); - int slot = luax_checkinteger(L, 2); - Thread::Variant v = t->fetch(slot); - switch (v.type) - { - case thread::Thread::Variant::INTERGER: - luax_pushinteger(L, v.integer); - break; - - case thread::Thread::Variant::BOOLEAN: - luax_pushboolean(L, v.boolean); - break; - - case thread::Thread::Variant::CSTRING: - luax_pushstring(L, v.cstring); - break; - - case thread::Thread::Variant::REAL: - luax_pushnumber(L, v.real); - break; - - case thread::Thread::Variant::POINTER: - Proxy* p = (Proxy*)v.pointer; - Proxy* proxy = (Proxy*)luax_newinstance(L, p->type, sizeof(Proxy)); - p->object->retain(); - proxy->bind(p->object, p->type); - break; - - } - return 1; - } + case thread::Thread::Variant::INTERGER: + luax_pushinteger(L, v.integer); + break; - static int l_demand(lua_State* L) - { - Thread* t = checkThread(L); - int slot = luax_checkinteger(L, 2); - Thread::Variant v = t->demand(slot); - switch (v.type) - { - case thread::Thread::Variant::INTERGER: - luax_pushinteger(L, v.integer); - break; - - case thread::Thread::Variant::BOOLEAN: - luax_pushboolean(L, v.boolean); - break; - - case thread::Thread::Variant::CSTRING: - luax_pushstring(L, v.cstring); - break; - - case thread::Thread::Variant::REAL: - luax_pushnumber(L, v.real); - break; - - case thread::Thread::Variant::POINTER: - Proxy* p = (Proxy*)v.pointer; - Proxy* proxy = (Proxy*)luax_newinstance(L, p->type, sizeof(Proxy)); - p->object->retain(); - proxy->bind(p->object, p->type); - break; - - } - return 1; - } + case thread::Thread::Variant::BOOLEAN: + luax_pushboolean(L, v.boolean); + break; - static int l_remove(lua_State* L) - { - Thread* t = checkThread(L); - int slot = luax_checkinteger(L, 1); - t->remove(slot); - return 0; - } + case thread::Thread::Variant::CSTRING: + luax_pushstring(L, v.cstring); + break; + + case thread::Thread::Variant::REAL: + luax_pushnumber(L, v.real); + break; + + case thread::Thread::Variant::POINTER: + Proxy* p = (Proxy*)v.pointer; + Proxy* proxy = (Proxy*)luax_newinstance(L, p->type, sizeof(Proxy)); + proxy->bind(p->object, p->type); + break; - static int l_getName(lua_State* L) - { - Thread* t = checkThread(L); - const char* name = t->getName(); - luax_pushstring(L, name); - return 1; } + return 1; + } - static int l_isRunning(lua_State* L) + static int l_demand(lua_State* L) + { + Thread* t = checkThread(L); + int slot = luax_checkinteger(L, 2); + Thread::Variant v = t->demand(slot); + switch (v.type) { - Thread* t = checkThread(L); - bool running = t->isRunning(); - luax_pushboolean(L, running); - return 1; + case thread::Thread::Variant::INTERGER: + luax_pushinteger(L, v.integer); + break; + + case thread::Thread::Variant::BOOLEAN: + luax_pushboolean(L, v.boolean); + break; + + case thread::Thread::Variant::CSTRING: + luax_pushstring(L, v.cstring); + break; + + case thread::Thread::Variant::REAL: + luax_pushnumber(L, v.real); + break; + + case thread::Thread::Variant::POINTER: + Proxy* p = (Proxy*)v.pointer; + Proxy* proxy = (Proxy*)luax_newinstance(L, p->type, sizeof(Proxy)); + proxy->bind(p->object, p->type); + break; + } + return 1; + } - static const luaL_Reg thread_function[] = { - { "__gc", l_thread_gc }, - { "start", l_start }, - { "wait", l_wait }, - { "send", l_send }, - { "receive", l_receive }, - { "fetch", l_fetch }, - { "demand", l_demand }, - { "remove", l_remove }, - { "getName", l_getName }, - { "isRunning", l_isRunning }, - { 0, 0 } - }; + static int l_remove(lua_State* L) + { + Thread* t = checkThread(L); + int slot = luax_checkinteger(L, 1); + t->remove(slot); + return 0; + } + + static int l_getName(lua_State* L) + { + Thread* t = checkThread(L); + const char* name = t->getName(); + luax_pushstring(L, name); + return 1; + } + + static int l_isRunning(lua_State* L) + { + Thread* t = checkThread(L); + bool running = t->isRunning(); + luax_pushboolean(L, running); + return 1; + } + + static const luaL_Reg thread_function[] = { + { "__gc", l_thread_gc }, + { "start", l_start }, + { "wait", l_wait }, + { "send", l_send }, + { "receive", l_receive }, + { "fetch", l_fetch }, + { "demand", l_demand }, + { "remove", l_remove }, + { "getName", l_getName }, + { "isRunning", l_isRunning }, + { 0, 0 } + }; - static int luaopen_Thread(lua_State* L) - { - luax_newtype(L, JIN_THREAD_THREAD, thread_function); + static int luaopen_Thread(lua_State* L) + { + luax_newtype(L, JIN_THREAD_THREAD, thread_function); - return 0; - } + 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, JIN_THREAD_THREAD, sizeof(Proxy)); - Thread* thread = new Thread(name, code, Thread::threadRunner); - thread->retain(); - proxy->bind(thread, JIN_THREAD_THREAD); - return 1; - } + // 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, JIN_THREAD_THREAD, sizeof(Proxy)); + Thread* thread = new Thread(name, code, Thread::threadRunner); + proxy->bind(thread, JIN_THREAD_THREAD); + return 1; + } - static int l_getThread(lua_State* L) - { - luax_getglobal(L, MODULE_NAME); - luax_getfield(L, -1, "_curThread"); - return 1; - } + static int l_getThread(lua_State* L) + { + luax_getglobal(L, MODULE_NAME); + luax_getfield(L, -1, "_curThread"); + return 1; + } - static const luaL_Reg f[] = { - { "Thread", l_newThread}, - { "getThread", l_getThread}, - { 0, 0 } - }; + static const luaL_Reg f[] = { + { "Thread", l_newThread}, + { "getThread", l_getThread}, + { 0, 0 } + }; - int luaopen_thread(lua_State* L) - { - luaopen_Thread(L); + int luaopen_thread(lua_State* L) + { + luaopen_Thread(L); - luax_newlib(L, f); + luax_newlib(L, f); - return 1; - } + return 1; } + +} }
\ No newline at end of file |