diff options
author | chai <chaifix@163.com> | 2018-08-06 16:22:00 +0800 |
---|---|---|
committer | chai <chaifix@163.com> | 2018-08-06 16:22:00 +0800 |
commit | 89e7a9ecfad9a54633eb3ebbab1d1f13ad78826f (patch) | |
tree | 1ba8a806af1b8e7750d6b637c296f08e13cf268e /src/lua/thread/luaopen_Thread.cpp | |
parent | ea9769944a2db3abe15f537dab67e16fdfc20bef (diff) |
*update
Diffstat (limited to 'src/lua/thread/luaopen_Thread.cpp')
-rw-r--r-- | src/lua/thread/luaopen_Thread.cpp | 180 |
1 files changed, 180 insertions, 0 deletions
diff --git a/src/lua/thread/luaopen_Thread.cpp b/src/lua/thread/luaopen_Thread.cpp index e69de29..f811bdf 100644 --- a/src/lua/thread/luaopen_Thread.cpp +++ b/src/lua/thread/luaopen_Thread.cpp @@ -0,0 +1,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) + { + + } + } +}
\ No newline at end of file |