aboutsummaryrefslogtreecommitdiff
path: root/src/lua
diff options
context:
space:
mode:
Diffstat (limited to 'src/lua')
-rw-r--r--src/lua/graphics/luaopen_Font.cpp2
-rw-r--r--src/lua/luaopen_types.h1
-rw-r--r--src/lua/net/Socket.h2
-rw-r--r--src/lua/net/luaopen_Buffer.cpp2
-rw-r--r--src/lua/net/luaopen_Socket.cpp2
-rw-r--r--src/lua/net/luaopen_net.cpp48
-rw-r--r--src/lua/thread/Thread.h14
-rw-r--r--src/lua/thread/luaopen_Thread.cpp10
-rw-r--r--src/lua/thread/luaopen_thread.cpp10
9 files changed, 51 insertions, 40 deletions
diff --git a/src/lua/graphics/luaopen_Font.cpp b/src/lua/graphics/luaopen_Font.cpp
index 3643412..8585d46 100644
--- a/src/lua/graphics/luaopen_Font.cpp
+++ b/src/lua/graphics/luaopen_Font.cpp
@@ -11,7 +11,7 @@ namespace lua
static int l_gc(lua_State* L)
{
- Proxy* proxy = (Proxy*)luax_newinstance(L, JIN_GRAPHICS_FONT, sizeof(Proxy));
+ Proxy* proxy = (Proxy*)luax_checktype(L, 1, JIN_GRAPHICS_FONT);
proxy->release();
return 0;
}
diff --git a/src/lua/luaopen_types.h b/src/lua/luaopen_types.h
index 74c658f..b32d849 100644
--- a/src/lua/luaopen_types.h
+++ b/src/lua/luaopen_types.h
@@ -61,7 +61,6 @@ namespace lua
{
if (obj == nullptr)
return;
- obj->retain();
object = obj;
type = t;
}
diff --git a/src/lua/net/Socket.h b/src/lua/net/Socket.h
index 5834092..e3ef9c2 100644
--- a/src/lua/net/Socket.h
+++ b/src/lua/net/Socket.h
@@ -16,7 +16,6 @@ namespace net
class Socket : public Object
{
public:
- Socket() {}
Socket(SocketInformation info)
{
@@ -63,6 +62,7 @@ namespace net
private:
jin::net::Socket* socket;
+ Socket() {}
~Socket()
{
delete socket;
diff --git a/src/lua/net/luaopen_Buffer.cpp b/src/lua/net/luaopen_Buffer.cpp
index 0ab47cb..6d42c86 100644
--- a/src/lua/net/luaopen_Buffer.cpp
+++ b/src/lua/net/luaopen_Buffer.cpp
@@ -116,7 +116,7 @@ namespace net
}
static const luaL_Reg netbuffer_function[] = {
- { "__gc", l_gc },
+ { "__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 abcd8f2..5ccdaa0 100644
--- a/src/lua/net/luaopen_Socket.cpp
+++ b/src/lua/net/luaopen_Socket.cpp
@@ -45,8 +45,8 @@ namespace lua
Socket* socket = checkSocket(L);
char buffer[BUFFER_SIZE] = {0};
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));
+ net::Buffer* netBuffer = new net::Buffer(buffer, size);
proxy->bind(netBuffer, JIN_NETWORK_BUFFER);
return 1;
}
diff --git a/src/lua/net/luaopen_net.cpp b/src/lua/net/luaopen_net.cpp
index 13d24dd..7ec9cc7 100644
--- a/src/lua/net/luaopen_net.cpp
+++ b/src/lua/net/luaopen_net.cpp
@@ -19,33 +19,35 @@ namespace lua
// jin.net.Socket()
static int l_Socket(lua_State* L)
{
- const char* socketType = luax_checkstring(L, 1);
SocketInformation info = { 0 };
- if (strcmp(socketType, "TCP") == 0)
- info.type = SocketType::TCP;
- else if (strcmp(socketType, "UDP") == 0)
- info.type = SocketType::UDP;
- else
{
- luax_error(L, "jin.net.Socket() first paramter wrong, must be TCP or UDP");
- return 0;
+ const char* socketType = luax_checkstring(L, 1);
+ if (strcmp(socketType, "TCP") == 0)
+ info.type = SocketType::TCP;
+ else if (strcmp(socketType, "UDP") == 0)
+ info.type = SocketType::UDP;
+ else
+ {
+ luax_error(L, "jin.net.Socket() first paramter wrong, must be TCP or UDP");
+ return 0;
+ }
+ // type, port
+ if (luax_gettop(L) == 2)
+ {
+ info.port = luax_checkinteger(L, 2);
+ }
+ // type, address, port
+ else if (luax_gettop(L) == 3)
+ {
+ if (luax_isstringstrict(L, 2))
+ info.address = tk_strtohl(luax_checkstring(L, 2));
+ else if (luax_isintegerstrict(L, 2))
+ info.address = luax_checkinteger(L, 2);
+ info.port = luax_checkinteger(L, 3);
+ }
}
- // type, port
- if (luax_gettop(L) == 2)
- {
- info.port = luax_checkinteger(L, 2);
- }
- // type, address, port
- else if (luax_gettop(L) == 3)
- {
- if (luax_isstringstrict(L, 2))
- info.address = tk_strtohl(luax_checkstring(L, 2));
- else if(luax_isintegerstrict(L, 2))
- info.address = luax_checkinteger(L, 2);
- info.port = luax_checkinteger(L, 3);
- }
- Proxy* proxy = (Proxy*)luax_newinstance(L, JIN_NETWORK_SOCKET, sizeof(Proxy));
Socket* socket = new Socket(info);
+ Proxy* proxy = (Proxy*)luax_newinstance(L, JIN_NETWORK_SOCKET, sizeof(Proxy));
proxy->bind(socket, JIN_NETWORK_SOCKET);
return 1;
}
diff --git a/src/lua/thread/Thread.h b/src/lua/thread/Thread.h
index 63f7524..6969fd8 100644
--- a/src/lua/thread/Thread.h
+++ b/src/lua/thread/Thread.h
@@ -12,17 +12,18 @@ namespace thread
{
public:
typedef jin::thread::Thread::Variant Variant;
+ typedef jin::thread::Thread::ThreadRunner ThreadRunner;
- Thread(std::string _name, std::string _code, jin::thread::Thread::ThreadRunner runner)
+ Thread(std::string _name, std::string _code, ThreadRunner runner)
: name(_name)
, code(_code)
{
thread = new jin::thread::Thread(_name, runner);
}
- bool start()
+ bool start(void* p)
{
- return thread->start();
+ return thread->start(p);
}
void wait()
@@ -75,7 +76,10 @@ namespace thread
thread->unlock();
}
- static void threadRunner(jin::thread::Thread* t);
+ static void threadRunner(Thread* t);
+
+ const std::string name;
+ const std::string code;
private:
~Thread()
@@ -85,8 +89,6 @@ namespace thread
jin::thread::Thread* thread;
- const std::string name;
- const std::string code;
};
} // thread
diff --git a/src/lua/thread/luaopen_Thread.cpp b/src/lua/thread/luaopen_Thread.cpp
index 70a30ee..b6fb33b 100644
--- a/src/lua/thread/luaopen_Thread.cpp
+++ b/src/lua/thread/luaopen_Thread.cpp
@@ -21,7 +21,7 @@ namespace lua
return nullptr;
}
- void Thread::threadRunner(jin::thread::Thread* t)
+ static int threadRunner(void* t)
{
Thread* thread = (Thread*)t;
lua_State* L = lua_open();
@@ -29,10 +29,12 @@ namespace lua
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);
+ return 0;
}
static int l_thread_gc(lua_State* L)
@@ -45,7 +47,7 @@ namespace lua
static int l_start(lua_State* L)
{
Thread* t = checkThread(L);
- bool result = t->start();
+ bool result = t->start(t);
luax_pushboolean(L, result);
return 1;
}
@@ -125,6 +127,7 @@ 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);
break;
@@ -158,6 +161,7 @@ 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);
break;
@@ -216,7 +220,7 @@ namespace lua
const char* name = luax_checkstring(L, 1);
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* thread = new Thread(name, code, threadRunner);
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 70a30ee..b6fb33b 100644
--- a/src/lua/thread/luaopen_thread.cpp
+++ b/src/lua/thread/luaopen_thread.cpp
@@ -21,7 +21,7 @@ namespace lua
return nullptr;
}
- void Thread::threadRunner(jin::thread::Thread* t)
+ static int threadRunner(void* t)
{
Thread* thread = (Thread*)t;
lua_State* L = lua_open();
@@ -29,10 +29,12 @@ namespace lua
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);
+ return 0;
}
static int l_thread_gc(lua_State* L)
@@ -45,7 +47,7 @@ namespace lua
static int l_start(lua_State* L)
{
Thread* t = checkThread(L);
- bool result = t->start();
+ bool result = t->start(t);
luax_pushboolean(L, result);
return 1;
}
@@ -125,6 +127,7 @@ 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);
break;
@@ -158,6 +161,7 @@ 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);
break;
@@ -216,7 +220,7 @@ namespace lua
const char* name = luax_checkstring(L, 1);
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* thread = new Thread(name, code, threadRunner);
proxy->bind(thread, JIN_THREAD_THREAD);
return 1;
}