diff options
author | chai <chaifix@163.com> | 2018-08-07 00:17:24 +0800 |
---|---|---|
committer | chai <chaifix@163.com> | 2018-08-07 00:17:24 +0800 |
commit | e4eeedbd6faaef380b58236745856b50cebf4461 (patch) | |
tree | 9284fe65848c41526d1d429400c425e47b888504 /src/lua/thread/luaopen_thread.cpp | |
parent | f5e72dd12fc47f082a4f6d14090391410aa8a9f1 (diff) |
*update
Diffstat (limited to 'src/lua/thread/luaopen_thread.cpp')
-rw-r--r-- | src/lua/thread/luaopen_thread.cpp | 182 |
1 files changed, 182 insertions, 0 deletions
diff --git a/src/lua/thread/luaopen_thread.cpp b/src/lua/thread/luaopen_thread.cpp new file mode 100644 index 0000000..99ddee1 --- /dev/null +++ b/src/lua/thread/luaopen_thread.cpp @@ -0,0 +1,182 @@ +#include "lua/luax.h" +#include "libjin/jin.h" +#include "../luaopen_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 void threadRunner(jin::thread::Thread* t) + { + Thread* thread = (Thread*)t; + lua_State* L = lua_open(); + luax_openlibs(L); + luaopen_jin(L); + luax_dostring(L, thread->code.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); + int slot = luax_checkinteger(L, 2); + if (luax_isnumber(L, 3)) + { + float real = luax_checknumber(L, 3); + t->send(slot, real); + } + else if (luax_isboolean(L, 3)) + { + bool bol = luax_checkbool(L, 3); + t->send(slot, bol); + } + else if (luax_isstring(L, 3)) + { + const char* str = luax_checkstring(L, 3); + t->send(slot, str); + } + 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_fetch(lua_State* L) + { + Thread* t = checkThread(L); + int slot = luax_checkinteger(L, 2); + Thread::Variant v = t->fetch(slot); + //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) + { + Thread* t = checkThread(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 |