diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/libjin/3rdparty/tekcos/tekcos.c | 56 | ||||
-rw-r--r-- | src/libjin/3rdparty/tekcos/tekcos.h | 6 | ||||
-rw-r--r-- | src/libjin/Net/Socket.cpp | 53 | ||||
-rw-r--r-- | src/libjin/Net/Socket.h | 17 | ||||
-rw-r--r-- | src/lua/luaopen_types.h | 46 | ||||
-rw-r--r-- | src/lua/net/lua_net_Buffer.h | 12 | ||||
-rw-r--r-- | src/lua/net/lua_net_Socket.h | 64 | ||||
-rw-r--r-- | src/lua/net/luaopen_Buffer.cpp | 8 | ||||
-rw-r--r-- | src/lua/net/luaopen_Socket.cpp | 16 | ||||
-rw-r--r-- | src/lua/net/luaopen_net.cpp | 14 | ||||
-rw-r--r-- | src/lua/thread/luaopen_Thread.cpp | 16 | ||||
-rw-r--r-- | src/lua/thread/luaopen_thread.cpp | 16 |
12 files changed, 233 insertions, 91 deletions
diff --git a/src/libjin/3rdparty/tekcos/tekcos.c b/src/libjin/3rdparty/tekcos/tekcos.c index a1fc94e..0eed75b 100644 --- a/src/libjin/3rdparty/tekcos/tekcos.c +++ b/src/libjin/3rdparty/tekcos/tekcos.c @@ -91,12 +91,11 @@ const char* tk_hltostr(uint32 ip) /* TCP socket */ /********************************************/ -tk_TCPsocket* tk_tcp_open(tk_IPaddress ip) +tk_TCPsocket tk_tcp_open(tk_IPaddress ip) { - tk_TCPsocket* sk; - sk = (tk_TCPsocket*)malloc(sizeof(tk_TCPsocket)); - sk->id = socket(AF_INET, SOCK_STREAM, 0); - if (sk->id == INVALID_SOCKET) + tk_TCPsocket sk; + sk.id = socket(AF_INET, SOCK_STREAM, 0); + if (sk.id == INVALID_SOCKET) { state = TK_COULDNETCREATESOCKET; goto error; @@ -115,36 +114,36 @@ tk_TCPsocket* tk_tcp_open(tk_IPaddress ip) if (ip.host != INADDR_NONE && ip.host != INADDR_ANY) { addr.sin_addr.s_addr = htonl(ip.host); - if (connect(sk->id, (const struct sockaddr*)&addr, sizeof(addr)) == SOCKET_ERROR) + if (connect(sk.id, (const struct sockaddr*)&addr, sizeof(addr)) == SOCKET_ERROR) { state = TK_CONNECTFAILED; goto error; } - sk->type = SOCKET_TCLIENT; + sk.type = SOCKET_TCLIENT; } else { addr.sin_addr.s_addr = htonl(INADDR_ANY); - if (bind(sk->id, (const struct sockaddr*)&addr, sizeof(addr)) == SOCKET_ERROR) + if (bind(sk.id, (const struct sockaddr*)&addr, sizeof(addr)) == SOCKET_ERROR) { state = TK_BINDSOCKETFAILED; goto error; } - if (listen(sk->id, 8) == SOCKET_ERROR) + if (listen(sk.id, 8) == SOCKET_ERROR) { state = TK_LISTENSOCKETFAILED; goto error; } - sk->type = SOCKET_TSERVER; + sk.type = SOCKET_TSERVER; } - sk->remote.host = ntohl(addr.sin_addr.s_addr); - sk->remote.port = ntohs(addr.sin_port); + sk.remote.host = ntohl(addr.sin_addr.s_addr); + sk.remote.port = ntohs(addr.sin_port); return sk; error: - return 0; + return sk; } int tk_tcp_close(tk_TCPsocket* sk) @@ -217,10 +216,11 @@ error: return 0; } -tk_TCPsocket* tk_tcp_accept(tk_TCPsocket* server) +tk_TCPsocket tk_tcp_accept(tk_TCPsocket* server) { // client socket - tk_TCPsocket* csk = (tk_TCPsocket*) malloc(sizeof(tk_TCPsocket)); + tk_TCPsocket csk; + memset(&csk, 0, sizeof(csk)); if (server->type != SOCKET_TSERVER) { state = TK_WRONGSOCKETTPYE; @@ -229,21 +229,21 @@ tk_TCPsocket* tk_tcp_accept(tk_TCPsocket* server) struct sockaddr_in addr; memset(&addr, 0, sizeof(addr)); int addr_len = sizeof(addr); - csk->id = accept(server->id, (struct sockaddr *)&addr, &addr_len); - if (csk->id == INVALID_SOCKET) + csk.id = accept(server->id, (struct sockaddr *)&addr, &addr_len); + if (csk.id == INVALID_SOCKET) { state = TK_INVALIDSOCKET; goto error; } - csk->remote.host = ntohl(addr.sin_addr.s_addr); - csk->remote.port = ntohs(addr.sin_port); + csk.remote.host = ntohl(addr.sin_addr.s_addr); + csk.remote.port = ntohs(addr.sin_port); - csk->type = SOCKET_TCLIENT; + csk.type = SOCKET_TCLIENT; return csk; error: - return 0; + return csk; } int tk_tcp_nonblocking(tk_TCPsocket* sk) @@ -304,12 +304,12 @@ int tk_tcp_blocking(tk_TCPsocket* sk) * ***************************************************/ -tk_UDPsocket* tk_udp_open(uint16 portnumber) +tk_UDPsocket tk_udp_open(uint16 portnumber) { - tk_UDPsocket* sk; - sk = (tk_UDPsocket*)malloc(sizeof(tk_UDPsocket)); - sk->id = socket(AF_INET, SOCK_DGRAM, 0); - if (sk->id == INVALID_SOCKET) + tk_UDPsocket sk; + memset(&sk, 0, sizeof(sk)); + sk.id = socket(AF_INET, SOCK_DGRAM, 0); + if (sk.id == INVALID_SOCKET) { state = TK_COULDNETCREATESOCKET; goto error; @@ -325,12 +325,12 @@ tk_UDPsocket* tk_udp_open(uint16 portnumber) addr.sin_addr.s_addr = htonl(INADDR_ANY); addr.sin_port = htons(portnumber); addr.sin_family = AF_INET; - bind(sk->id, (const struct sockaddr*)&addr, sizeof(addr)); + bind(sk.id, (const struct sockaddr*)&addr, sizeof(addr)); return sk; } error: - return 0; + return sk; } int tk_udp_sendto(tk_UDPsocket* sk, tk_UDPpack* pack) diff --git a/src/libjin/3rdparty/tekcos/tekcos.h b/src/libjin/3rdparty/tekcos/tekcos.h index 0ec22fd..c78771a 100644 --- a/src/libjin/3rdparty/tekcos/tekcos.h +++ b/src/libjin/3rdparty/tekcos/tekcos.h @@ -55,11 +55,11 @@ typedef struct // INADDR_ANY, creeate a listenning server socket, // otherwise, connect to a remote server with given // ip address. -tk_TCPsocket* tk_tcp_open(tk_IPaddress ip); +tk_TCPsocket tk_tcp_open(tk_IPaddress ip); int tk_tcp_close(tk_TCPsocket* sk); int tk_tcp_send(tk_TCPsocket* client, const void* buffer, int bsize, int* len); int tk_tcp_recv(tk_TCPsocket* client, char* buffer, int bsize, int* len); -tk_TCPsocket* tk_tcp_accept(tk_TCPsocket* server); +tk_TCPsocket tk_tcp_accept(tk_TCPsocket* server); int tk_tcp_nonblocking(tk_TCPsocket* sk); int tk_tcp_blocking(tk_TCPsocket* sk); @@ -82,7 +82,7 @@ typedef struct char* data; // data }tk_UDPpack; -tk_UDPsocket* tk_udp_open(uint16 portnumber); +tk_UDPsocket tk_udp_open(uint16 portnumber); int tk_udp_close(tk_UDPsocket* sk); int tk_udp_sendto(tk_UDPsocket* sk, tk_UDPpack* pack); int tk_udp_recvfrom(tk_UDPsocket* sk, tk_UDPpack* pack); diff --git a/src/libjin/Net/Socket.cpp b/src/libjin/Net/Socket.cpp index 7e6fa7d..b28e4fc 100644 --- a/src/libjin/Net/Socket.cpp +++ b/src/libjin/Net/Socket.cpp @@ -4,22 +4,25 @@ namespace jin { namespace net { + Socket::Socket(const Socket& socket) + : handle(socket.handle) + , type(socket.type) + { + } Socket::Socket(const SocketInformation& info) - : tcpHandle(nullptr) - , udpHandle(nullptr) + : type(info.type) { - type = info.type; if (type == SocketType::TCP) { tk_IPaddress ip; ip.host = info.address; ip.port = info.port; - tcpHandle = tk_tcp_open(ip); + handle.tcpHandle = tk_tcp_open(ip); } else if (type == SocketType::UDP) { - udpHandle = tk_udp_open(info.port); + handle.udpHandle = tk_udp_open(info.port); } } @@ -32,12 +35,12 @@ namespace net #if JIN_NET_TEKCOS ip.host = tk_strtohl(address); ip.port = port; - tcpHandle = tk_tcp_open(ip); + handle.tcpHandle = tk_tcp_open(ip); #endif } else if (type == SocketType::UDP) { - udpHandle = tk_udp_open(port); + handle.udpHandle = tk_udp_open(port); } } @@ -49,11 +52,11 @@ namespace net tk_IPaddress ip; ip.host = address; ip.port = port; - tcpHandle = tk_tcp_open(ip); + handle.tcpHandle = tk_tcp_open(ip); } else if (type == SocketType::UDP) { - udpHandle = tk_udp_open(port); + handle.udpHandle = tk_udp_open(port); } } @@ -65,26 +68,24 @@ namespace net tk_IPaddress ip; ip.host = 0; ip.port = port; - tcpHandle = tk_tcp_open(ip); + handle.tcpHandle = tk_tcp_open(ip); } else if (type == SocketType::UDP) { - udpHandle = tk_udp_open(port); + handle.udpHandle = tk_udp_open(port); } } #if JIN_NET_TEKCOS - Socket::Socket(tk_TCPsocket* handle) - : tcpHandle(handle) - , udpHandle(nullptr) + Socket::Socket(tk_TCPsocket tcphandle) { + handle.tcpHandle = tcphandle; } - Socket::Socket(tk_UDPsocket* handle) - : tcpHandle(nullptr) - , udpHandle(handle) + Socket::Socket(tk_UDPsocket udphandle) { + handle.udpHandle = udphandle; } #endif // JIN_NET_TEKCOS @@ -99,9 +100,9 @@ namespace net return; #if JIN_NET_TEKCOS if (blocking) - tk_tcp_blocking(tcpHandle); + tk_tcp_blocking(&handle.tcpHandle); else - tk_tcp_nonblocking(tcpHandle); + tk_tcp_nonblocking(&handle.tcpHandle); #endif } @@ -111,7 +112,7 @@ namespace net return nullptr; Socket* client; #if JIN_NET_TEKCOS - tk_TCPsocket* socket = tk_tcp_accept(tcpHandle); + tk_TCPsocket socket = tk_tcp_accept(&handle.tcpHandle); client = new Socket(socket); #endif return client; @@ -123,7 +124,7 @@ namespace net return 0; #if JIN_NET_TEKCOS int len; - tk_tcp_recv(tcpHandle, buffer, size, &len); + tk_tcp_recv(&handle.tcpHandle, buffer, size, &len); return len; #endif } @@ -134,7 +135,7 @@ namespace net return 0; #if JIN_NET_TEKCOS int len; - tk_tcp_send(tcpHandle, buffer, size, &len); + tk_tcp_send(&handle.tcpHandle, buffer, size, &len); return len; #endif } @@ -149,7 +150,7 @@ namespace net pack.len = size; pack.ip.host = address; pack.ip.port = port; - tk_udp_sendto(udpHandle, &pack); + tk_udp_sendto(&handle.udpHandle, &pack); #endif } @@ -164,7 +165,7 @@ namespace net pack.len = size; pack.ip.host = address; pack.ip.port = port; - tk_udp_recvfrom(udpHandle, &pack); + tk_udp_recvfrom(&handle.udpHandle, &pack); return pack.len; #endif } @@ -174,13 +175,13 @@ namespace net if (type == SocketType::TCP) { #if JIN_NET_TEKCOS - tk_tcp_close(tcpHandle); + tk_tcp_close(&handle.tcpHandle); #endif } else if (type == SocketType::UDP) { #if JIN_NET_TEKCOS - tk_udp_close(udpHandle); + tk_udp_close(&handle.udpHandle); #endif } } diff --git a/src/libjin/Net/Socket.h b/src/libjin/Net/Socket.h index eb00605..720113e 100644 --- a/src/libjin/Net/Socket.h +++ b/src/libjin/Net/Socket.h @@ -26,6 +26,8 @@ namespace net class Socket { public: + Socket() {}; + Socket(const Socket& socket); Socket(const SocketInformation& socketInformation); Socket(SocketType type, unsigned short port); Socket(SocketType type, unsigned int address, unsigned short port); @@ -40,13 +42,16 @@ namespace net int receiveFrom(char* buffer, int size, unsigned int address, unsigned int port); void close(); - private: + protected: #if JIN_NET_TEKCOS - Socket(tk_TCPsocket* tcpHandle); - Socket(tk_UDPsocket* udpHandle); - tk_TCPsocket* tcpHandle; - tk_UDPsocket* udpHandle; - #endif + Socket(tk_TCPsocket tcpHandle); + Socket(tk_UDPsocket udpHandle); + union + { + tk_TCPsocket tcpHandle; + tk_UDPsocket udpHandle; + } handle; + #endif SocketType type; }; diff --git a/src/lua/luaopen_types.h b/src/lua/luaopen_types.h index eef4a3e..587c023 100644 --- a/src/lua/luaopen_types.h +++ b/src/lua/luaopen_types.h @@ -22,16 +22,58 @@ namespace jin namespace lua { + class Object + { + public: + Object() + : count(1) + { + } + + int getReferenceCount() const + { + return count; + } + void retain() + { + ++count; + } + void release() + { + if (--count <= 0) + delete this; + } + + protected: + virtual ~Object() = 0 + { + } + + private: + // reference count + int count; + }; + class Proxy { public: - inline void bind(const void* obj, const char* t) + void bind(Object* obj, const char* t) { object = obj; type = t; } - const void* object; // acctual object binded + void release() + { + if (object != nullptr) + { + object->release(); + object = nullptr; + type = nullptr; + } + } + + Object* object; // acctual object binded const char* type; // type name and metatable name }; diff --git a/src/lua/net/lua_net_Buffer.h b/src/lua/net/lua_net_Buffer.h index 7fba37f..6af3078 100644 --- a/src/lua/net/lua_net_Buffer.h +++ b/src/lua/net/lua_net_Buffer.h @@ -3,6 +3,7 @@ #include <cstring> #include <cstdlib> +#include "../luaopen_types.h" namespace jin { @@ -11,7 +12,7 @@ namespace lua namespace net { - class Buffer + class Buffer : public Object { public: Buffer(size_t s = 0) @@ -30,9 +31,12 @@ namespace net ~Buffer() { - delete[] buffer; - buffer = nullptr; - size = 0; + if (buffer != nullptr) + { + delete[] buffer; + buffer = nullptr; + size = 0; + } } void append(const void* data, size_t s) diff --git a/src/lua/net/lua_net_Socket.h b/src/lua/net/lua_net_Socket.h new file mode 100644 index 0000000..e02fb5b --- /dev/null +++ b/src/lua/net/lua_net_Socket.h @@ -0,0 +1,64 @@ +#ifndef __JIN_LUA_NET_SOCKET_H +#define __JIN_LUA_NET_SOCKET_H +#include "libjin/jin.h" +#include "../luaopen_types.h" + +namespace jin +{ +namespace lua +{ +namespace net +{ + + class Socket : public jin::net::Socket + , public Object + { + public: + typedef jin::net::SocketInformation SocketInformation; + typedef jin::net::SocketType SocketType; + + Socket(jin::net::Socket base) + : jin::net::Socket(base) + { + } + + Socket(const SocketInformation& socketInformation) + : jin::net::Socket(socketInformation) + { + } + + Socket(SocketType type, unsigned short port) + : jin::net::Socket(type, port) + { + } + + Socket(SocketType type, unsigned int address, unsigned short port) + : jin::net::Socket(type, address) + { + } + + Socket(SocketType type, const char* address, unsigned short port) + : jin::net::Socket(type, address, port) + { + } + + Socket * accept() + { + jin::net::Socket* base = jin::net::Socket::accept(); + Socket* socket = new Socket(*base); + delete base; + return socket; + } + + private: + ~Socket() + { + } + + }; + +} +} +} + +#endif
\ No newline at end of file diff --git a/src/lua/net/luaopen_Buffer.cpp b/src/lua/net/luaopen_Buffer.cpp index 9278544..68e377b 100644 --- a/src/lua/net/luaopen_Buffer.cpp +++ b/src/lua/net/luaopen_Buffer.cpp @@ -108,7 +108,15 @@ namespace net return 2; } + static int l_gc(lua_State* L) + { + Proxy* proxy = (Proxy*)luax_checktype(L, 1, JIN_NETWORK_BUFFER); + proxy->release<Buffer>(); + return 0; + } + static const luaL_Reg netbuffer_function[] = { + { "__gc", l_gc }, { "append", l_append }, { "grabString", l_grabString }, { "grabInteger", l_grabInteger }, diff --git a/src/lua/net/luaopen_Socket.cpp b/src/lua/net/luaopen_Socket.cpp index 395729a..327cda1 100644 --- a/src/lua/net/luaopen_Socket.cpp +++ b/src/lua/net/luaopen_Socket.cpp @@ -2,13 +2,14 @@ #include "../luaopen_types.h" #include "libjin/jin.h" #include "lua_net_Buffer.h" +#include "lua_net_Socket.h" namespace jin { namespace lua { - - using namespace jin::net; + + using namespace jin::lua::net; const int BUFFER_SIZE = 1024; @@ -34,6 +35,7 @@ namespace lua Socket* socket = checkSocket(L); Socket* client = socket->accept(); Proxy* proxy = (Proxy*)luax_newinstance(L, JIN_NETWORK_SOCKET, sizeof(Proxy)); + client->retain(); proxy->bind(client, JIN_NETWORK_SOCKET); return 1; } @@ -46,6 +48,7 @@ namespace lua int size = socket->receive(buffer, BUFFER_SIZE); net::Buffer* netBuffer = new net::Buffer(buffer, size); Proxy* proxy = (Proxy*)luax_newinstance(L, JIN_NETWORK_BUFFER, sizeof(Proxy)); + netBuffer->retain(); proxy->bind(netBuffer, JIN_NETWORK_BUFFER); return 1; } @@ -60,6 +63,7 @@ namespace lua int size = socket->receiveFrom(buffer, BUFFER_SIZE, address, port); net::Buffer* netBuffer = new net::Buffer(buffer, size); Proxy* proxy = (Proxy*)luax_newinstance(L, JIN_NETWORK_BUFFER, sizeof(Proxy)); + netBuffer->retain(); proxy->bind(netBuffer, JIN_NETWORK_BUFFER); return 1; } @@ -100,7 +104,15 @@ namespace lua return 0; } + static int l_gc(lua_State* L) + { + Proxy* proxy = (Proxy*)luax_checktype(L, 1, JIN_NETWORK_SOCKET); + proxy->release(); + return 0; + } + static const luaL_Reg socket_function[] = { + { "__gc", l_gc }, { "accept", l_accept }, { "receive", l_receive }, { "receiveFrom", l_receiveFrom }, diff --git a/src/lua/net/luaopen_net.cpp b/src/lua/net/luaopen_net.cpp index 89a79f3..28313ce 100644 --- a/src/lua/net/luaopen_net.cpp +++ b/src/lua/net/luaopen_net.cpp @@ -2,13 +2,13 @@ #include "libjin/jin.h" #include "../luaopen_types.h" #include "lua_net_Buffer.h" +#include "lua_net_Socket.h" namespace jin { namespace lua { - - using namespace jin::net; + using namespace jin::lua::net; static int l_initNetwork(lua_State* L) { @@ -16,15 +16,6 @@ namespace lua return 1; } - // jin.net.toSocket(lightuserdata) - static int l_toSocket(lua_State*L) - { - Proxy* proxy = (Proxy*)luax_checktype(L, 1, JIN_NETWORK_SOCKET); - //Proxy * socketProxy = (Proxy*)luax_newinstance(L, JIN_NETWORK_SOCKET, sizeof(Proxy)); - - return 1; - } - // jin.net.Socket() static int l_Socket(lua_State* L) { @@ -71,7 +62,6 @@ namespace lua static const luaL_Reg f[] = { { "init", l_initNetwork }, - { "toSocket", l_toSocket }, { "Socket", l_Socket }, { "Buffer", l_Buffer }, { 0, 0 } diff --git a/src/lua/thread/luaopen_Thread.cpp b/src/lua/thread/luaopen_Thread.cpp index 393a29d..4eabc07 100644 --- a/src/lua/thread/luaopen_Thread.cpp +++ b/src/lua/thread/luaopen_Thread.cpp @@ -11,6 +11,7 @@ namespace jin int luaopen_thread(lua_State* L); class Thread : public jin::thread::Thread + , public Object { public: Thread(std::string _name, std::string _code, jin::thread::Thread::ThreadRunner runner) @@ -22,6 +23,9 @@ namespace jin static void threadRunner(jin::thread::Thread* t); private: + ~Thread() + {} + const std::string name; const std::string code; }; @@ -42,16 +46,17 @@ namespace jin 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); luax_setfield(L, -2, "_curThread"); luax_dostring(L, thread->code.c_str()); luax_close(L); } - static int l_gc(lua_State* L) + static int l_thread_gc(lua_State* L) { - Thread* t = checkThread(L); - delete t; + Proxy* proxy = (Proxy*)luax_checktype(L, 1, JIN_THREAD_THREAD); + proxy->release(); return 0; } @@ -138,6 +143,7 @@ namespace jin 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); break; @@ -171,6 +177,7 @@ namespace jin 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); break; @@ -203,7 +210,7 @@ namespace jin } static const luaL_Reg thread_function[] = { - { "__gc", l_gc }, + { "__gc", l_thread_gc }, { "start", l_start }, { "wait", l_wait }, { "send", l_send }, @@ -230,6 +237,7 @@ namespace jin const char* code = luax_checkstring(L, 2); Proxy* proxy = (Proxy*)luax_newinstance(L, JIN_THREAD_THREAD, sizeof(Proxy)); Thread* thread = new Thread(name, code, Thread::threadRunner); + thread->retain(); proxy->bind(thread, JIN_THREAD_THREAD); return 1; } diff --git a/src/lua/thread/luaopen_thread.cpp b/src/lua/thread/luaopen_thread.cpp index 393a29d..4eabc07 100644 --- a/src/lua/thread/luaopen_thread.cpp +++ b/src/lua/thread/luaopen_thread.cpp @@ -11,6 +11,7 @@ namespace jin int luaopen_thread(lua_State* L); class Thread : public jin::thread::Thread + , public Object { public: Thread(std::string _name, std::string _code, jin::thread::Thread::ThreadRunner runner) @@ -22,6 +23,9 @@ namespace jin static void threadRunner(jin::thread::Thread* t); private: + ~Thread() + {} + const std::string name; const std::string code; }; @@ -42,16 +46,17 @@ namespace jin 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); luax_setfield(L, -2, "_curThread"); luax_dostring(L, thread->code.c_str()); luax_close(L); } - static int l_gc(lua_State* L) + static int l_thread_gc(lua_State* L) { - Thread* t = checkThread(L); - delete t; + Proxy* proxy = (Proxy*)luax_checktype(L, 1, JIN_THREAD_THREAD); + proxy->release(); return 0; } @@ -138,6 +143,7 @@ namespace jin 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); break; @@ -171,6 +177,7 @@ namespace jin 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); break; @@ -203,7 +210,7 @@ namespace jin } static const luaL_Reg thread_function[] = { - { "__gc", l_gc }, + { "__gc", l_thread_gc }, { "start", l_start }, { "wait", l_wait }, { "send", l_send }, @@ -230,6 +237,7 @@ namespace jin const char* code = luax_checkstring(L, 2); Proxy* proxy = (Proxy*)luax_newinstance(L, JIN_THREAD_THREAD, sizeof(Proxy)); Thread* thread = new Thread(name, code, Thread::threadRunner); + thread->retain(); proxy->bind(thread, JIN_THREAD_THREAD); return 1; } |