diff options
Diffstat (limited to 'src/lua')
-rw-r--r-- | src/lua/embed/embed.h | 18 | ||||
-rw-r--r-- | src/lua/luaopen_types.h | 3 | ||||
-rw-r--r-- | src/lua/thread/luaopen_Thread.cpp | 180 | ||||
-rw-r--r-- | src/lua/thread/thread.cpp | 0 |
4 files changed, 192 insertions, 9 deletions
diff --git a/src/lua/embed/embed.h b/src/lua/embed/embed.h index 2ef8b75..33620b0 100644 --- a/src/lua/embed/embed.h +++ b/src/lua/embed/embed.h @@ -10,8 +10,8 @@ namespace embed /** * embed lua script to context. */ -#define embed(L, script, name) \ - if(luaL_loadbuffer(L, script, strlen(script), name) == 0)\ +#define embed(L, script, name)\ + if(luax_loadbuffer(L, script, strlen(script), name) == 0)\ lua_call(L, 0, 0); /** @@ -33,14 +33,14 @@ namespace embed // embed scripts const jin_Embed scripts[] = { - {"graphics.lua", graphics_lua}, - {"keyboard.lua", keyboard_lua}, - {"mouse.lua", mouse_lua}, - {"debug.lua", debug_lua}, - {"boot.lua", boot_lua}, - {0, 0} + { "graphics.lua", graphics_lua }, + { "keyboard.lua", keyboard_lua }, + { "mouse.lua", mouse_lua }, + { "debug.lua", debug_lua }, + { "boot.lua", boot_lua }, + { 0, 0 } }; - + // load all emebd lua scripts for (int i = 0; scripts[i].fname; ++i) embed(L, scripts[i].source, scripts[i].fname); diff --git a/src/lua/luaopen_types.h b/src/lua/luaopen_types.h index 8c1756f..15e618e 100644 --- a/src/lua/luaopen_types.h +++ b/src/lua/luaopen_types.h @@ -10,6 +10,9 @@ // audio module #define TYPE_SOURCE "Source" +// thread module +#define TYPE_THREAD "Thread" + namespace jin { namespace lua 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 diff --git a/src/lua/thread/thread.cpp b/src/lua/thread/thread.cpp new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/src/lua/thread/thread.cpp |