diff options
Diffstat (limited to 'src/lua/thread')
-rw-r--r-- | src/lua/thread/luaopen_Thread.cpp | 50 | ||||
-rw-r--r-- | src/lua/thread/luaopen_thread.cpp | 182 |
2 files changed, 207 insertions, 25 deletions
diff --git a/src/lua/thread/luaopen_Thread.cpp b/src/lua/thread/luaopen_Thread.cpp index c08c262..99ddee1 100644 --- a/src/lua/thread/luaopen_Thread.cpp +++ b/src/lua/thread/luaopen_Thread.cpp @@ -32,9 +32,9 @@ namespace jin return nullptr; } - static int threadRunner(void* p) + static void threadRunner(jin::thread::Thread* t) { - Thread* thread = (Thread*)p; + Thread* thread = (Thread*)t; lua_State* L = lua_open(); luax_openlibs(L); luaopen_jin(L); @@ -64,21 +64,21 @@ namespace jin static int l_send(lua_State* L) { Thread* t = checkThread(L); - const char* name = luax_checkstring(L, 2); + int slot = luax_checkinteger(L, 2); if (luax_isnumber(L, 3)) { float real = luax_checknumber(L, 3); - t->send(name, real); + t->send(slot, real); } else if (luax_isboolean(L, 3)) { bool bol = luax_checkbool(L, 3); - t->send(name, bol); + t->send(slot, bol); } else if (luax_isstring(L, 3)) { const char* str = luax_checkstring(L, 3); - t->send(name, str); + t->send(slot, str); } return 0; } @@ -86,8 +86,8 @@ namespace jin static int l_receive(lua_State* L) { Thread* t = checkThread(L); - const char* name = luax_checkstring(L, 2); - bool result = t->receive(name); + int slot = luax_checkinteger(L, 2); + bool result = t->receive(slot); luax_pushboolean(L, result); return 1; } @@ -95,23 +95,23 @@ namespace jin 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) - { - } + 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; } 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 |