diff options
author | chai <chaifix@163.com> | 2018-08-16 14:21:56 +0800 |
---|---|---|
committer | chai <chaifix@163.com> | 2018-08-16 14:21:56 +0800 |
commit | 8585c92b7d0744a1f1a39c872cf5096621161b6c (patch) | |
tree | 6aa02138f39f7b11ab17c7399064353092b8df0c /src/lua/thread/luaopen_Thread.cpp | |
parent | be9b27dbf550093b555ab3087c11b38c89ab9fd0 (diff) |
*update
Diffstat (limited to 'src/lua/thread/luaopen_Thread.cpp')
-rw-r--r-- | src/lua/thread/luaopen_Thread.cpp | 71 |
1 files changed, 35 insertions, 36 deletions
diff --git a/src/lua/thread/luaopen_Thread.cpp b/src/lua/thread/luaopen_Thread.cpp index 30eb343..2b4d01d 100644 --- a/src/lua/thread/luaopen_Thread.cpp +++ b/src/lua/thread/luaopen_Thread.cpp @@ -13,30 +13,28 @@ namespace lua int luaopen_thread(lua_State* L); - static inline Thread* checkThread(lua_State* L) + static inline Ref<Thread>& checkThread(lua_State* L) { Proxy* proxy = (Proxy*)luax_checktype(L, 1, JIN_THREAD_THREAD); - if (proxy != nullptr) - return (Thread*)proxy->object; - return nullptr; + return proxy->getRef<Thread>(); } static int threadRunner(void* t) { - Thread* thread = (Thread*)t; + Ref<Thread>& ref = *(Ref<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); + ref.retain(); + proxy->bind(&ref, JIN_THREAD_THREAD); luax_setfield(L, -2, "_curThread"); - luax_dostring(L, thread->code.c_str()); + luax_dostring(L, (*ref).code.c_str()); luax_close(L); return 0; } - + static int l_thread_gc(lua_State* L) { Proxy* proxy = (Proxy*)luax_checktype(L, 1, JIN_THREAD_THREAD); @@ -46,66 +44,66 @@ namespace lua static int l_start(lua_State* L) { - Thread* t = checkThread(L); - bool result = t->start(t); + Ref<Thread>& ref = checkThread(L); + bool result = (*ref).start(&ref); luax_pushboolean(L, result); return 1; } static int l_wait(lua_State* L) { - Thread* t = checkThread(L); - t->wait(); + Ref<Thread>& ref = checkThread(L); + (*ref).wait(); return 0; } static int l_send(lua_State* L) { - Thread* t = checkThread(L); + Ref<Thread>& ref = 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); + (*ref).send(slot, real); } else if (luax_isbooleanstrict(L, vp)) { bool bol = luax_checkbool(L, vp); - t->send(slot, bol); + (*ref).send(slot, bol); } else if (luax_isstringstrict(L, vp)) { const char* str = luax_checkstring(L, vp); - t->send(slot, str); + (*ref).send(slot, str); } else if (luax_isuserdata(L, vp)) { void* p = luax_touserdata(L, vp); - t->send(slot, p); + (*ref).send(slot, p); } else if (luax_islightuserdata(L, vp)) { void* p = luax_tolightuserdata(L, vp); - t->send(slot, p); + (*ref).send(slot, p); } return 0; } static int l_receive(lua_State* L) { - Thread* t = checkThread(L); + Ref<Thread>& ref = checkThread(L); int slot = luax_checkinteger(L, 2); - bool result = t->receive(slot); + bool result = (*ref).receive(slot); luax_pushboolean(L, result); return 1; } static int l_fetch(lua_State* L) { - Thread* t = checkThread(L); + Ref<Thread>& ref = checkThread(L); int slot = luax_checkinteger(L, 2); - Thread::Variant v = t->fetch(slot); + Thread::Variant v = (*ref).fetch(slot); switch (v.type) { case thread::Thread::Variant::INTERGER: @@ -127,8 +125,8 @@ namespace lua 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); + p->reference->retain(); + proxy->bind(p->reference, p->type); break; } @@ -137,9 +135,9 @@ namespace lua static int l_demand(lua_State* L) { - Thread* t = checkThread(L); + Ref<Thread>& ref = checkThread(L); int slot = luax_checkinteger(L, 2); - Thread::Variant v = t->demand(slot); + Thread::Variant v = (*ref).demand(slot); switch (v.type) { case thread::Thread::Variant::INTERGER: @@ -161,8 +159,8 @@ namespace lua 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); + p->reference->retain(); + proxy->bind(p->reference, p->type); break; } @@ -171,24 +169,24 @@ namespace lua static int l_remove(lua_State* L) { - Thread* t = checkThread(L); + Ref<Thread>& ref = checkThread(L); int slot = luax_checkinteger(L, 2); - t->remove(slot); + (*ref).remove(slot); return 0; } static int l_getName(lua_State* L) { - Thread* t = checkThread(L); - const char* name = t->getName(); + Ref<Thread>& ref = checkThread(L); + const char* name = (*ref).getName(); luax_pushstring(L, name); return 1; } static int l_isRunning(lua_State* L) { - Thread* t = checkThread(L); - bool running = t->isRunning(); + Ref<Thread>& ref = checkThread(L); + bool running = (*ref).isRunning(); luax_pushboolean(L, running); return 1; } @@ -221,7 +219,8 @@ namespace lua const char* code = luax_checkstring(L, 2); Proxy* proxy = (Proxy*)luax_newinstance(L, JIN_THREAD_THREAD, sizeof(Proxy)); Thread* thread = new Thread(name, code, threadRunner); - proxy->bind(thread, JIN_THREAD_THREAD); + Ref<Thread>* ref = new Ref<Thread>(thread); + proxy->bind(ref, JIN_THREAD_THREAD); return 1; } |