aboutsummaryrefslogtreecommitdiff
path: root/src/lua
diff options
context:
space:
mode:
Diffstat (limited to 'src/lua')
-rw-r--r--src/lua/luaopen_types.h46
-rw-r--r--src/lua/net/lua_net_Buffer.h12
-rw-r--r--src/lua/net/lua_net_Socket.h64
-rw-r--r--src/lua/net/luaopen_Buffer.cpp8
-rw-r--r--src/lua/net/luaopen_Socket.cpp16
-rw-r--r--src/lua/net/luaopen_net.cpp14
-rw-r--r--src/lua/thread/luaopen_Thread.cpp16
-rw-r--r--src/lua/thread/luaopen_thread.cpp16
8 files changed, 164 insertions, 28 deletions
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;
}