aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/3rdparty/luax/luax.h15
-rw-r--r--src/libjin/Common/data.h32
-rw-r--r--src/libjin/Net/Socket.cpp2
-rw-r--r--src/libjin/Net/Socket.h2
-rw-r--r--src/libjin/Net/net.cpp24
-rw-r--r--src/libjin/Net/net.h30
-rw-r--r--src/lua/audio/luaopen_Source.cpp4
-rw-r--r--src/lua/audio/luaopen_audio.cpp4
-rw-r--r--src/lua/bit/luaopen_bit.cpp11
-rw-r--r--src/lua/embed/net.lua.h66
-rw-r--r--src/lua/graphics/luaopen_Canvas.cpp6
-rw-r--r--src/lua/graphics/luaopen_Font.cpp4
-rw-r--r--src/lua/graphics/luaopen_Image.cpp6
-rw-r--r--src/lua/graphics/luaopen_JSL.cpp10
-rw-r--r--src/lua/graphics/luaopen_graphics.cpp24
-rw-r--r--src/lua/luaopen_jin.cpp2
-rw-r--r--src/lua/luaopen_types.h21
-rw-r--r--src/lua/net/lua_net_Buffer.h92
-rw-r--r--src/lua/net/luaopen_Buffer.cpp214
-rw-r--r--src/lua/net/luaopen_Socket.cpp119
-rw-r--r--src/lua/net/luaopen_net.cpp70
-rw-r--r--src/lua/thread/luaopen_Thread.cpp51
-rw-r--r--src/lua/thread/luaopen_thread.cpp51
23 files changed, 510 insertions, 350 deletions
diff --git a/src/3rdparty/luax/luax.h b/src/3rdparty/luax/luax.h
index 69b9669..6ec4dc9 100644
--- a/src/3rdparty/luax/luax.h
+++ b/src/3rdparty/luax/luax.h
@@ -62,6 +62,8 @@
#define luax_checkinteger luaL_checkinteger
#define luax_checkstring luaL_checkstring
#define luax_checklstring luaL_checklstring
+#define luax_touserdata lua_touserdata
+#define luax_tolightuserdata lua_touserdata
//#define luax_checkbool luaL_checkinteger
inline bool luax_checkbool(lua_State *L, int numArg)
{
@@ -101,6 +103,17 @@ inline bool luax_checkbool(lua_State *L, int numArg)
#define luax_pushlstring lua_pushlstring
#define luax_pushinteger lua_pushinteger
#define luax_pushboolean lua_pushboolean
+#define luax_pushlightuserdata lua_pushlightuserdata
+
+//inline void luax_pushuserdata(lua_State* L, void* p)
+//{
+// /**
+// * https://stackoverflow.com/questions/15038796/lua-c-api-push-existing-pointers
+// */
+// void** box = (void**)lua_newuserdata(L, sizeof(p));
+// *box = p;
+//
+//}
#define luax_rawseti lua_rawseti
@@ -249,6 +262,8 @@ inline int luax_istype(lua_State* L, int idx, const char* tname)
#define luax_istable(L, i) luax_is(table, L, i)
#define luax_isnil(L, i) luax_is(nil, L, i)
#define luax_isboolean(L, i) luax_is(boolean, L, i)
+#define luax_isuserdata lua_isuserdata
+#define luax_islightuserdata lua_islightuserdata
inline int luax_isinteger(lua_State* L, int i)
{
if (!luax_isnumber(L, i))
diff --git a/src/libjin/Common/data.h b/src/libjin/Common/data.h
new file mode 100644
index 0000000..7fcc389
--- /dev/null
+++ b/src/libjin/Common/data.h
@@ -0,0 +1,32 @@
+#ifndef __JIN_COMMON_DATA_H
+#define __JIN_COMMON_DATA_H
+
+namespace jin
+{
+
+ class DataBuffer
+ {
+ public:
+ DataBuffer(int n)
+ : len(n)
+ {
+ buffer = new char[len];
+ memset(buffer, 0, len);
+ }
+ ~DataBuffer()
+ {
+ delete[] buffer;
+ }
+ char* operator&()
+ {
+ return buffer;
+ }
+
+ private:
+ char* buffer;
+ int len;
+ };
+
+} // jin
+
+#endif \ No newline at end of file
diff --git a/src/libjin/Net/Socket.cpp b/src/libjin/Net/Socket.cpp
index b7c621e..7e6fa7d 100644
--- a/src/libjin/Net/Socket.cpp
+++ b/src/libjin/Net/Socket.cpp
@@ -5,7 +5,7 @@ namespace jin
namespace net
{
- Socket::Socket(SocketInformation info)
+ Socket::Socket(const SocketInformation& info)
: tcpHandle(nullptr)
, udpHandle(nullptr)
{
diff --git a/src/libjin/Net/Socket.h b/src/libjin/Net/Socket.h
index fae6bd2..eb00605 100644
--- a/src/libjin/Net/Socket.h
+++ b/src/libjin/Net/Socket.h
@@ -26,7 +26,7 @@ namespace net
class Socket
{
public:
- Socket(SocketInformation socketInformation);
+ Socket(const SocketInformation& socketInformation);
Socket(SocketType type, unsigned short port);
Socket(SocketType type, unsigned int address, unsigned short port);
Socket(SocketType type, const char* address, unsigned short port);
diff --git a/src/libjin/Net/net.cpp b/src/libjin/Net/net.cpp
new file mode 100644
index 0000000..db39be7
--- /dev/null
+++ b/src/libjin/Net/net.cpp
@@ -0,0 +1,24 @@
+#include "Net.h"
+
+namespace jin
+{
+namespace net
+{
+
+ bool Net::initSystem(const SettingBase* setting)
+ {
+ #ifdef _WIN32
+ #if JIN_NET_TEKCOS
+ tk_init();
+ #endif
+ #endif
+ return true;
+ }
+
+ void Net::quitSystem()
+ {
+
+ }
+
+}
+}
diff --git a/src/libjin/Net/net.h b/src/libjin/Net/net.h
new file mode 100644
index 0000000..54ffede
--- /dev/null
+++ b/src/libjin/Net/net.h
@@ -0,0 +1,30 @@
+#ifndef __JIN_NET_H
+#define __JIN_NET_H
+#include "../modules.h"
+#if JIN_MODULES_NET
+
+#include "../Common/Subsystem.hpp"
+#include "Socket.h"
+
+namespace jin
+{
+namespace net
+{
+
+ class Net : public Subsystem<Net>
+ {
+ public:
+
+ protected:
+ Net() {};
+ ~Net() {};
+ SINGLETON(Net);
+ bool initSystem(const SettingBase* setting) override;
+ void quitSystem() override;
+ };
+
+}
+}
+
+#endif // JIN_MODULES_NET
+#endif // __JIN_NET_H \ No newline at end of file
diff --git a/src/lua/audio/luaopen_Source.cpp b/src/lua/audio/luaopen_Source.cpp
index cff3f7f..10aab4d 100644
--- a/src/lua/audio/luaopen_Source.cpp
+++ b/src/lua/audio/luaopen_Source.cpp
@@ -11,7 +11,7 @@ namespace lua
static inline SDLSource* checkSource(lua_State* L)
{
- Proxy* proxy = (Proxy*)luax_checktype(L, 1, TYPE_SOURCE);
+ Proxy* proxy = (Proxy*)luax_checktype(L, 1, JIN_AUDIO_SOURCE);
if (proxy != 0 && proxy != nullptr)
return (SDLSource*)proxy->object;
return nullptr;
@@ -108,7 +108,7 @@ namespace lua
int luaopen_Source(lua_State* L)
{
- luax_newtype(L, TYPE_SOURCE, f);
+ luax_newtype(L, JIN_AUDIO_SOURCE, f);
return 0;
}
diff --git a/src/lua/audio/luaopen_audio.cpp b/src/lua/audio/luaopen_audio.cpp
index 20a6bf4..33f0561 100644
--- a/src/lua/audio/luaopen_audio.cpp
+++ b/src/lua/audio/luaopen_audio.cpp
@@ -67,9 +67,9 @@ namespace lua
Buffer b;
fs->read(f, &b);
- Proxy* proxy = (Proxy*)luax_newinstance(L, TYPE_SOURCE, sizeof(Proxy));
+ Proxy* proxy = (Proxy*)luax_newinstance(L, JIN_AUDIO_SOURCE, sizeof(Proxy));
SDLSource* src = SDLSource::createSource(b.data, b.size);
- proxy->bind(src);
+ proxy->bind(src, JIN_AUDIO_SOURCE);
return 1;
}
diff --git a/src/lua/bit/luaopen_bit.cpp b/src/lua/bit/luaopen_bit.cpp
index e3a7bbb..d6c8197 100644
--- a/src/lua/bit/luaopen_bit.cpp
+++ b/src/lua/bit/luaopen_bit.cpp
@@ -71,17 +71,6 @@ namespace lua
{ "RS", l_rshift },
{ "INC", l_include },
- //{ "Buffer", l_newBuffer }, // ײ
-
- { "buffer", l_buffer },
- { "write", l_write },
- { "shift", l_shift },
-
- { "grabstring", l_grabstring },
- { "grabinteger", l_grabinteger },
- { "grabfloat", l_grabfloat },
- { "grabboolean", l_grabboolean },
-
// , offset
{ 0, 0 }
diff --git a/src/lua/embed/net.lua.h b/src/lua/embed/net.lua.h
index 3db9264..4d89dc7 100644
--- a/src/lua/embed/net.lua.h
+++ b/src/lua/embed/net.lua.h
@@ -1,70 +1,4 @@
/* net.lua */
static const char* net_lua = R"(
jin.net = jin.net or {}
-
---[[
-socketͨŵ
-* INT 32
-* FLOAT 32
-* BOOL 32
-* STRING --
-STRINGжҽ磬0β
-һЭ鶨ӣðЭͳһһluaļ
--- s2c_package.lua
-local INT = jin.net.dataType.INT
-local FLOAT = jin.net.dataType.FLOAT
-local BOOL = jin.net.dataType.BOOL
-local STRING = jin.net.dataType.STRING
-Skill = {
- id = INT,
- damage = FLOAT,
- range = FLOAT,
- description = STRING
-}
-local data, size = jin.net.serialize(Message.Skill, message)
-Socket:send(data, size)
-յʱ
-local data, size = Socket:receive()
-local message = jin.net.deserialize(Message.Skill, data, size)
-]]
-
-jin.net.dataType = {
- INT = 1,
- FLOAT = 2,
- BOOL = 3,
- STRING = 4
-}
-
-jin.net.dataSize = {
- INT = 4,
- FLOAT = 4,
- BOOL = 4,
- STRING = -1,
-}
-
-jin.net.deserialize = function(prototype, data, size)
- local message = {}
- local i = 1
- for k, t in pairs(prototype) do
- message[k] = data[i]
- i = i + 1
- end
- return message
-end
-
-jin.net.serialize = function(prototype, message)
- local data = ""
- local size = 0
- for i, v in pairs(message) do
- data, size = jin.bit.write(data, size, v)
- end
- return data, size
-end
-
--- Э
-jin.net.decode = function()
- local s = jin.bit.grabstring(buffer, size)
-end
-
)"; \ No newline at end of file
diff --git a/src/lua/graphics/luaopen_Canvas.cpp b/src/lua/graphics/luaopen_Canvas.cpp
index 1b76edd..808a977 100644
--- a/src/lua/graphics/luaopen_Canvas.cpp
+++ b/src/lua/graphics/luaopen_Canvas.cpp
@@ -11,7 +11,7 @@ namespace lua
static inline Canvas* checkCanvas(lua_State* L)
{
- Proxy* proxy = (Proxy*)luax_checktype(L, 1, TYPE_CANVAS);
+ Proxy* proxy = (Proxy*)luax_checktype(L, 1, JIN_GRAPHICS_CANVAS);
if (proxy != nullptr)
return (Canvas*)proxy->object;
return nullptr;
@@ -50,7 +50,7 @@ namespace lua
static int l_gc(lua_State* L)
{
- Proxy* proxy = (Proxy*)luax_checktype(L, 1, TYPE_CANVAS);
+ Proxy* proxy = (Proxy*)luax_checktype(L, 1, JIN_GRAPHICS_CANVAS);
Canvas* canvas = (Canvas*)proxy->object;
delete canvas;
return 0;
@@ -67,7 +67,7 @@ namespace lua
int luaopen_Canvas(lua_State* L)
{
- luax_newtype(L, TYPE_CANVAS, f);
+ luax_newtype(L, JIN_GRAPHICS_CANVAS, f);
return 0;
}
diff --git a/src/lua/graphics/luaopen_Font.cpp b/src/lua/graphics/luaopen_Font.cpp
index c0d4708..3de2981 100644
--- a/src/lua/graphics/luaopen_Font.cpp
+++ b/src/lua/graphics/luaopen_Font.cpp
@@ -16,7 +16,7 @@ namespace lua
static int l_box(lua_State* L)
{
- Font* font = (Font*)luax_checktype(L, 1, TYPE_FONT);
+ Font* font = (Font*)luax_checktype(L, 1, JIN_GRAPHICS_FONT);
const char* text = luax_checkstring(L, 2);
int fheight = luax_checknumber(L, 3);
int spacing = luax_checknumber(L, 4);
@@ -36,7 +36,7 @@ namespace lua
int luaopen_Font(lua_State* L)
{
- luax_newtype(L, TYPE_FONT, f);
+ luax_newtype(L, JIN_GRAPHICS_FONT, f);
return 0;
}
diff --git a/src/lua/graphics/luaopen_Image.cpp b/src/lua/graphics/luaopen_Image.cpp
index 546e6b1..0f97b2c 100644
--- a/src/lua/graphics/luaopen_Image.cpp
+++ b/src/lua/graphics/luaopen_Image.cpp
@@ -11,7 +11,7 @@ namespace lua
static inline Texture* checkTexture(lua_State* L)
{
- Proxy* proxy = (Proxy*)luax_checktype(L, 1, TYPE_IMAGE);
+ Proxy* proxy = (Proxy*)luax_checktype(L, 1, JIN_GRAPHICS_IMAGE);
if (proxy != nullptr)
return (Texture*)proxy->object;
return nullptr;
@@ -63,7 +63,7 @@ namespace lua
static int l_gc(lua_State* L)
{
- Proxy* proxy = (Proxy*)luax_checktype(L, 1, TYPE_IMAGE);
+ Proxy* proxy = (Proxy*)luax_checktype(L, 1, JIN_GRAPHICS_IMAGE);
Texture* img = (Texture*)proxy->object;
delete img;
return 0;
@@ -81,7 +81,7 @@ namespace lua
int luaopen_Image(lua_State* L)
{
- luax_newtype(L, TYPE_IMAGE, f);
+ luax_newtype(L, JIN_GRAPHICS_IMAGE, f);
return 0;
}
diff --git a/src/lua/graphics/luaopen_JSL.cpp b/src/lua/graphics/luaopen_JSL.cpp
index 7c59937..8d25178 100644
--- a/src/lua/graphics/luaopen_JSL.cpp
+++ b/src/lua/graphics/luaopen_JSL.cpp
@@ -11,7 +11,7 @@ namespace lua
static inline JSLProgram* checkJSLProgram(lua_State* L)
{
- Proxy* proxy = (Proxy*)luax_checktype(L, 1, TYPE_JSL);
+ Proxy* proxy = (Proxy*)luax_checktype(L, 1, JIN_GRAPHICS_SHADER);
if(proxy != nullptr)
return (JSLProgram*)proxy->object;
return nullptr;
@@ -66,14 +66,14 @@ namespace lua
}
case IMAGE:
{
- Proxy* proxy = (Proxy*)luax_checktype(L, 4, TYPE_IMAGE);
+ Proxy* proxy = (Proxy*)luax_checktype(L, 4, JIN_GRAPHICS_IMAGE);
Texture* tex = (Texture*)proxy->object;
jsl->sendTexture(variable, tex);
break;
}
case CANVAS:
{
- Proxy* proxy = (Proxy*)luax_checktype(L, 4, TYPE_IMAGE);
+ Proxy* proxy = (Proxy*)luax_checktype(L, 4, JIN_GRAPHICS_CANVAS);
Canvas* canvas = (Canvas*)proxy->object;
jsl->sendCanvas(variable, canvas);
break;
@@ -121,7 +121,7 @@ namespace lua
static int l_gc(lua_State* L)
{
- Proxy* proxy = (Proxy*)luax_checktype(L, 1, TYPE_JSL);
+ Proxy* proxy = (Proxy*)luax_checktype(L, 1, JIN_GRAPHICS_SHADER);
JSLProgram* jsl = (JSLProgram*)proxy->object;
delete jsl;
return 0;
@@ -138,7 +138,7 @@ namespace lua
*/
int luaopen_JSL(lua_State* L)
{
- luax_newtype(L, TYPE_JSL, f);
+ luax_newtype(L, JIN_GRAPHICS_SHADER, f);
return 0;
}
diff --git a/src/lua/graphics/luaopen_graphics.cpp b/src/lua/graphics/luaopen_graphics.cpp
index 2aa68e8..da91c51 100644
--- a/src/lua/graphics/luaopen_graphics.cpp
+++ b/src/lua/graphics/luaopen_graphics.cpp
@@ -77,9 +77,9 @@ namespace lua
Buffer b;
fs->read(f, &b);
- Proxy* proxy = (Proxy*)luax_newinstance(L, TYPE_IMAGE, sizeof(Proxy));
+ Proxy* proxy = (Proxy*)luax_newinstance(L, JIN_GRAPHICS_IMAGE, sizeof(Proxy));
Texture* img = Texture::createTexture(b.data, b.size);
- proxy->bind(img);
+ proxy->bind(img, JIN_GRAPHICS_IMAGE);
return 1;
}
@@ -89,10 +89,10 @@ namespace lua
*/
static int l_newShader(lua_State* L)
{
- Proxy* proxy = (Proxy*)luax_newinstance(L, TYPE_JSL, sizeof(Proxy));
+ Proxy* proxy = (Proxy*)luax_newinstance(L, JIN_GRAPHICS_SHADER, sizeof(Proxy));
const char* program = luax_checkstring(L, 1);
JSLProgram* jsl = JSLProgram::createJSLProgram(program);
- proxy->bind(jsl);
+ proxy->bind(jsl, JIN_GRAPHICS_SHADER);
return 1;
}
@@ -104,9 +104,9 @@ namespace lua
{
int w = luax_checknumber(L, 1);
int h = luax_checknumber(L, 2);
- Proxy* proxy = (Proxy*)luax_newinstance(L, TYPE_CANVAS, sizeof(Proxy));
+ Proxy* proxy = (Proxy*)luax_newinstance(L, JIN_GRAPHICS_CANVAS, sizeof(Proxy));
Canvas* cvs = Canvas::createCanvas(w, h);
- proxy->bind(cvs);
+ proxy->bind(cvs, JIN_GRAPHICS_CANVAS);
return 1;
}
@@ -145,13 +145,13 @@ namespace lua
float sx = luax_optnumber(L, 4, 1);
float sy = luax_optnumber(L, 5, 1);
float r = luax_optnumber(L, 6, 0);
- if (luax_istype(L, 1, TYPE_IMAGE))
+ if (luax_istype(L, 1, JIN_GRAPHICS_IMAGE))
{
Proxy* proxy = (Proxy*)luax_toudata(L, 1);
Texture* tex = (Texture*)proxy->object;
tex->draw(x, y, sx, sy, r);
}
- else if (luax_istype(L, 1, TYPE_CANVAS))
+ else if (luax_istype(L, 1, JIN_GRAPHICS_CANVAS))
{
Proxy* proxy = (Proxy*)luax_toudata(L, 1);
Canvas* p = (Canvas*)proxy->object;
@@ -204,7 +204,7 @@ namespace lua
Canvas::unbind();
return 0;
}
- Proxy* proxy = (Proxy*)luax_checktype(L, 1, TYPE_CANVAS);
+ Proxy* proxy = (Proxy*)luax_checktype(L, 1, JIN_GRAPHICS_CANVAS);
Canvas* c = (Canvas*)proxy->object;
c->bind();
return 0;
@@ -223,7 +223,7 @@ namespace lua
JSLProgram::unuse();
return 0;
}
- if (luax_istype(L, 1, TYPE_JSL))
+ if (luax_istype(L, 1, JIN_GRAPHICS_SHADER))
{
Proxy* proxy = (Proxy*)luax_toudata(L, 1);
JSLProgram* jsl = (JSLProgram*)proxy->object;
@@ -389,7 +389,7 @@ namespace lua
static int l_newFont(lua_State* L)
{
- Font* font = (Font*)luax_newinstance(L, TYPE_FONT, sizeof(Font));
+ Font* font = (Font*)luax_newinstance(L, JIN_GRAPHICS_FONT, sizeof(Font));
const char* path = luax_checkstring(L, 1);
Filesystem* fs = Filesystem::get();
Buffer b = {};
@@ -422,7 +422,7 @@ namespace lua
context.curFont = context.defaultFont;
return 0;
}
- Font* font = (Font*)luax_checktype(L, 1, TYPE_FONT);
+ Font* font = (Font*)luax_checktype(L, 1, JIN_GRAPHICS_FONT);
context.curFont = font;
return 0;
}
diff --git a/src/lua/luaopen_jin.cpp b/src/lua/luaopen_jin.cpp
index 0a258ec..ad5b193 100644
--- a/src/lua/luaopen_jin.cpp
+++ b/src/lua/luaopen_jin.cpp
@@ -68,7 +68,7 @@ namespace lua
{"mouse", luaopen_mouse},
{"keyboard", luaopen_keyboard},
{"filesystem", luaopen_filesystem},
- //{"net", luaopen_net},
+ {"net", luaopen_net},
{"audio", luaopen_audio},
{"joypad", luaopen_joypad},
{"math", luaopen_math},
diff --git a/src/lua/luaopen_types.h b/src/lua/luaopen_types.h
index 894af48..ba720ee 100644
--- a/src/lua/luaopen_types.h
+++ b/src/lua/luaopen_types.h
@@ -2,19 +2,20 @@
#define __JIN_M_TYPES_H
// graphics module
-#define TYPE_IMAGE "Image"
-#define TYPE_JSL "Shader"
-#define TYPE_CANVAS "Canvas"
-#define TYPE_FONT "Font"
+#define JIN_GRAPHICS_IMAGE "JIN_GRAPHICS_IMAGE"
+#define JIN_GRAPHICS_SHADER "JIN_GRAPHICS_SHADER"
+#define JIN_GRAPHICS_CANVAS "JIN_GRAPHICS_CANVAS"
+#define JIN_GRAPHICS_FONT "JIN_GRAPHICS_FONT"
// audio module
-#define TYPE_SOURCE "Source"
+#define JIN_AUDIO_SOURCE "JIN_AUDIO_SOURCE"
// thread module
-#define TYPE_THREAD "Thread"
+#define JIN_THREAD_THREAD "JIN_THREAD_THREAD"
// network module
-#define TYPE_SOCKET "Socket"
+#define JIN_NETWORK_SOCKET "JIN_NETWORK_SOCKET"
+#define JIN_NETWORK_BUFFER "JIN_NETWORK_BUFFER"
namespace jin
{
@@ -24,12 +25,14 @@ namespace lua
class Proxy
{
public:
- inline void bind(const void* obj)
+ inline void bind(const void* obj, const char* t)
{
object = obj;
+ type = t;
}
- const void* object;
+ const void* 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
new file mode 100644
index 0000000..51c7598
--- /dev/null
+++ b/src/lua/net/lua_net_Buffer.h
@@ -0,0 +1,92 @@
+#ifndef __JIN_LUA_NET_NETBUFFER_H
+#define __JIN_LUA_NET_NETBUFFER_H
+
+#include <cstring>
+#include <cstdlib>
+
+namespace jin
+{
+namespace lua
+{
+namespace net
+{
+
+ class Buffer
+ {
+ public:
+ Buffer(size_t s = 0)
+ {
+ size = s;
+ buffer = new char[size];
+ memset(buffer, 0, size);
+ }
+
+ Buffer(const char* data, size_t s)
+ {
+ size = s;
+ buffer = new char[size];
+ memcpy(buffer, data, size);
+ }
+
+ ~Buffer()
+ {
+ delete[] buffer;
+ buffer = nullptr;
+ size = 0;
+ }
+
+ void append(const void* data, size_t s)
+ {
+ if (data == nullptr)
+ return;
+ char* buf = buffer;
+ buffer = new char[size + s];
+ memcpy(buffer, buf, size);
+ memcpy(buffer + size, data, s);
+ delete[] buf;
+ size += s;
+ return;
+ }
+
+ const char* grabString(int* length, int offset = 0)
+ {
+ int l = offset;
+ for (; l < size; ++l)
+ {
+ if (buffer[l] == 0)
+ break;
+ }
+ *length = l - offset + 1;
+ char* str = (char*)malloc(*length);
+ memcpy(str, buffer + offset, *length);
+ return str;
+ }
+
+ int grabInteger(int* length, int offset = 0)
+ {
+ *length = sizeof(int);
+ return *((int*)buffer);
+ }
+
+ float grabFloat(int* length, int offset = 0)
+ {
+ *length = sizeof(float);
+ return *((float*)buffer);
+ }
+
+ bool grabBoolean(int* length, int offset = 0)
+ {
+ *length = sizeof(bool);
+ return *((bool*)buffer);
+ }
+
+ char* buffer;
+ size_t size;
+
+ };
+
+} // net
+} // lua
+} // jin
+
+#endif \ No newline at end of file
diff --git a/src/lua/net/luaopen_Buffer.cpp b/src/lua/net/luaopen_Buffer.cpp
index 6e10f25..3a8353b 100644
--- a/src/lua/net/luaopen_Buffer.cpp
+++ b/src/lua/net/luaopen_Buffer.cpp
@@ -1,6 +1,7 @@
#include "lua/luax.h"
#include "../luaopen_types.h"
#include "libjin/jin.h"
+#include "lua_net_Buffer.h"
namespace jin
{
@@ -9,183 +10,120 @@ namespace lua
namespace net
{
- class Buffer
+ static inline Buffer* checkNetBuffer(lua_State* L)
{
- public:
- Buffer(size_t size);
- Buffer(const char* data, size_t size);
- ~Buffer()
- {
- }
-
- void append(char* data, size_t size)
- {
-
- }
-
- const char* grabString(int offset = 0)
- {
- int l = offset;
- for (; l < size; ++l)
- {
- if (buffer[l] == 0)
- {
-
- }
- }
- return nullptr;
- }
-
- int grabInteger(int offset = 0)
- {
-
- }
-
- float grabfloat(int offset = 0)
- {
-
- }
-
- bool grabboolean(int offset = 0)
- {
-
- }
-
- private:
- char* buffer;
- size_t size;
-
- };
-
- static int l_buffer(lua_State* L)
- {
- int size = luax_checkinteger(L, 1);
- char* buffer = (char*)malloc(size);
- memset(buffer, 0, size);
- luax_pushlstring(L, buffer, size);
- return 1;
+ Proxy* proxy = (Proxy*)luax_checktype(L, 1, JIN_NETWORK_BUFFER);
+ if (proxy != 0 && proxy != nullptr)
+ return (Buffer*)proxy->object;
+ return nullptr;
}
- // return buffer, size
- static int l_write(lua_State* L)
+ // net.Buffer:append(value) -> value_length
+ static int l_append(lua_State* L)
{
- const char* data = luax_checklstring(L, 1, NULL);
- int len = luax_checkinteger(L, 2);
- if (luax_isinteger(L, 3))
+ Buffer* buffer = checkNetBuffer(L);
+ const int vp = 2;
+ if (luax_isinteger(L, vp))
{
- int n = luax_checkinteger(L, 3);
- int size = len + sizeof(n);
- char* buffer = (char*)malloc(size);
- memcpy(buffer, data, len);
- memcpy(buffer + len, &n, sizeof(n));
- //free((void*)data);
- luax_pushlstring(L, buffer, size);
+ int n = luax_checkinteger(L, vp);
+ int size = sizeof(n);
+ buffer->append(&n, size);
luax_pushinteger(L, size);
- return 2;
+ return 1;
}
- else if (luax_isfloat(L, 3))
+ else if (luax_isfloat(L, vp))
{
- float n = luax_checknumber(L, 3);
- int size = len + sizeof(n);
- char* buffer = (char*)malloc(size);
- memcpy(buffer, data, len);
- memcpy(buffer + len, &n, sizeof(n));
- //free((void*)data);
- luax_pushlstring(L, buffer, size);
+ float n = luax_checknumber(L, vp);
+ int size = sizeof(n);
+ buffer->append(&n, size);
luax_pushinteger(L, size);
- return 2;
+ return 1;
}
- else if (luax_isboolean(L, 3))
+ else if (luax_isboolean(L, vp))
{
- bool b = luax_checkbool(L, 3);
- int size = len + sizeof(b);
- char* buffer = (char*)malloc(size);
- memcpy(buffer, data, len);
- memcpy(buffer + len, &b, sizeof(b));
- //free((void*)data);
- luax_pushlstring(L, buffer, size);
+ bool n = luax_checkbool(L, vp);
+ int size = sizeof(n);
+ buffer->append(&n, size);
luax_pushinteger(L, size);
- return 2;
+ return 1;
}
- else if (luax_isstring(L, 3))
+ else if (luax_isstring(L, vp))
{
- const char* s = luax_checkstring(L, 3);
- int l = strlen(s) + 1; // with \0
- int size = len + l;
- char* buffer = (char*)malloc(size);
- memcpy(buffer, data, len);
- memcpy(buffer + len, s, l);
- luax_pushlstring(L, buffer, size);
+ const char* str = luax_checkstring(L, vp);
+ int size = strlen(str) + 1;
+ buffer->append(str, size);
luax_pushinteger(L, size);
- return 2;
+ return 1;
}
else
{
- luax_typerror(L, 3, "number, bool or string");
+ luax_typerror(L, vp, "number, bool or string");
return 0;
}
}
- // jin.shift(buffer, shift)
- static int l_shift(lua_State* L)
+ // net.Buffer:grabString(offset) -> string, length
+ static int l_grabString(lua_State* L)
{
- const char* buffer = luax_checklstring(L, 1, NULL);
- int size = luax_checkinteger(L, 2);
- int shift = luax_checkinteger(L, 3);
- int ss = size - shift;
- luax_pushlstring(L, buffer + shift, ss);
- luax_pushinteger(L, ss);
+ Buffer* buffer = checkNetBuffer(L);
+ int offset = luax_checkinteger(L, 2);
+ int len;
+ const char* str = buffer->grabString(&len, offset);
+ luax_pushstring(L, str);
+ luax_pushinteger(L, len);
return 2;
}
- // jin.bit.grabstring(buffer, size)
- static int l_grabstring(lua_State* L)
+ // net.Buffer:grabInteger(offset) -> integer, length
+ static int l_grabInteger(lua_State* L)
{
- const char* buffer = luax_checklstring(L, 1, NULL);
- int size = luax_checkinteger(L, 2);
- int l = 0;
- for (; l < size; ++l)
- {
- if (buffer[l] == 0)
- {
- int len = l + 1;
- char* str = (char*)malloc(len);
- memcpy(str, buffer, len);
- luax_pushstring(L, str);
- luax_pushinteger(L, l);
- return 2;
- }
- }
- return 0;
+ Buffer* buffer = checkNetBuffer(L);
+ int offset = luax_checkinteger(L, 2);
+ int len;
+ int integer = buffer->grabInteger(&len, offset);
+ luax_pushinteger(L, integer);
+ luax_pushinteger(L, len);
+ return 1;
}
- static int l_grabinteger(lua_State* L)
+ static int l_grabFloat(lua_State* L)
{
- const char* buffer = luax_checklstring(L, 1, NULL);
- int size = luax_checkinteger(L, 2);
- int n = *((int*)buffer);
- luax_pushinteger(L, n);
+ Buffer* buffer = checkNetBuffer(L);
+ int offset = luax_checkinteger(L, 2);
+ int len;
+ float floatv = buffer->grabFloat(&len, offset);
+ luax_pushnumber(L, floatv);
+ luax_pushinteger(L, len);
return 1;
}
- static int l_grabfloat(lua_State* L)
+ static int l_grabBoolean(lua_State* L)
{
- const char* buffer = luax_checklstring(L, 1, NULL);
- int size = luax_checkinteger(L, 2);
- float n = *((float*)buffer);
- luax_pushnumber(L, n);
+ Buffer* buffer = checkNetBuffer(L);
+ int offset = luax_checkinteger(L, 2);
+ int len;
+ bool boolean = buffer->grabBoolean(&len, offset);
+ luax_pushboolean(L, boolean);
+ luax_pushinteger(L, len);
return 1;
}
- static int l_grabboolean(lua_State* L)
+ static const luaL_Reg netbuffer_function[] = {
+ { "append", l_append },
+ { "grabString", l_grabString },
+ { "grabInteger", l_grabInteger },
+ { "grabBoolean", l_grabBoolean },
+ { "grabFloat", l_grabFloat },
+ { 0, 0 }
+ };
+
+} // net
+
+ int luaopen_Buffer(lua_State* L)
{
- const char* buffer = luax_checklstring(L, 1, NULL);
- int size = luax_checkinteger(L, 2);
- bool n = *((bool*)buffer);
- luax_pushboolean(L, n);
- return 1;
+ luax_newtype(L, JIN_NETWORK_BUFFER, net::netbuffer_function);
+ return 0;
}
-} // net
} // lua
} // jin \ No newline at end of file
diff --git a/src/lua/net/luaopen_Socket.cpp b/src/lua/net/luaopen_Socket.cpp
index 7dbfb33..395729a 100644
--- a/src/lua/net/luaopen_Socket.cpp
+++ b/src/lua/net/luaopen_Socket.cpp
@@ -1,120 +1,109 @@
#include "lua/luax.h"
#include "../luaopen_types.h"
#include "libjin/jin.h"
+#include "lua_net_Buffer.h"
namespace jin
{
namespace lua
{
- /**
- * л
- * int
- * float
- * bool
- *
- * һmessageһtable
- */
- class DenseBuffer
- {
- public:
- DenseBuffer(int len)
- {
- this->len = len;
- buffer = new char[len];
- memset(buffer, 0, len);
- }
- ~DenseBuffer()
- {
- delete[] buffer;
- }
- char* operator&()
- {
- return buffer;
- }
- int size()
- {
- return len;
- }
-
- private:
- int len;
- char* buffer;
- };
-
+
using namespace jin::net;
- static inline Socket* checkSocket(lua_State* L)
+ const int BUFFER_SIZE = 1024;
+
+ static inline Socket* checkSocket(lua_State* L, int pos = 1)
{
- Proxy* proxy = (Proxy*)luax_checktype(L, 1, TYPE_SOCKET);
+ Proxy* proxy = (Proxy*)luax_checktype(L, pos, JIN_NETWORK_SOCKET);
if (proxy != 0 && proxy != nullptr)
return (Socket*)proxy->object;
return nullptr;
}
- /**
- +---------------+
- | message table | -1
- +---------------+
- | ... | -2
- +---------------+
- | ... | ...
- */
- static char* serialize(lua_State* L)
+ static inline net::Buffer* checkNetBuffer(lua_State* L, int pos = 1)
{
- if (!luax_istable(L, -1))
- luax_typerror(L, -1, "table");
- DenseBuffer* buffer;
-
+ Proxy* proxy = (Proxy*)luax_checktype(L, pos, JIN_NETWORK_BUFFER);
+ if (proxy != 0 && proxy != nullptr)
+ return (net::Buffer*)proxy->object;
+ return nullptr;
}
+ // return net.Buffer
static int l_accept(lua_State* L)
{
Socket* socket = checkSocket(L);
-
+ Socket* client = socket->accept();
+ Proxy* proxy = (Proxy*)luax_newinstance(L, JIN_NETWORK_SOCKET, sizeof(Proxy));
+ proxy->bind(client, JIN_NETWORK_SOCKET);
+ return 1;
}
+ // return net.Buffer
static int l_receive(lua_State* L)
{
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));
+ proxy->bind(netBuffer, JIN_NETWORK_BUFFER);
+ return 1;
}
- static int l_receive(lua_State* L)
+ // Socket:receiveFrom(address, port)
+ static int l_receiveFrom(lua_State* L)
{
Socket* socket = checkSocket(L);
-
+ int address = luax_checkinteger(L, 2);
+ int port = luax_checkinteger(L, 3);
+ char buffer[BUFFER_SIZE];
+ 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));
+ proxy->bind(netBuffer, JIN_NETWORK_BUFFER);
+ return 1;
}
-
+
+ // Socket:send(net.Buffer) -> data_length
static int l_send(lua_State* L)
{
Socket* socket = checkSocket(L);
- DenseBuffer buffer(1024);
- socket->send(&buffer, buffer.size());
+ net::Buffer* buffer = checkNetBuffer(L, 2);
+ int len = socket->send(buffer->buffer, buffer->size);
+ luax_pushinteger(L, len);
+ return 1;
}
- // thread:sendTo(address, port, table)
+ // Socket:sendTo(address, port, net.Buffer)
static int l_sendTo(lua_State* L)
{
Socket* socket = checkSocket(L);
-
+ int address = luax_checkinteger(L, 2);
+ int port = luax_checkinteger(L, 3);
+ net::Buffer* buffer = checkNetBuffer(L, 4);
+ socket->sendTo(buffer->buffer, buffer->size, address, port);
+ return 0;
}
static int l_close(lua_State* L)
{
Socket* socket = checkSocket(L);
-
+ socket->close();
+ return 0;
}
static int l_configBlocking(lua_State* L)
{
Socket* socket = checkSocket(L);
-
+ bool blocking = luax_checkbool(L, 2);
+ socket->configureBlocking(blocking);
+ return 0;
}
static const luaL_Reg socket_function[] = {
{ "accept", l_accept },
{ "receive", l_receive },
- { "receiveFrom", l_receive },
+ { "receiveFrom", l_receiveFrom },
{ "send", l_send },
{ "sendTo", l_sendTo },
{ "close", l_close },
@@ -124,9 +113,9 @@ namespace lua
int luaopen_Socket(lua_State* L)
{
- luax_newtype(L, TYPE_SOURCE, socket_function);
+ luax_newtype(L, JIN_NETWORK_SOCKET, socket_function);
return 0;
}
-}
-} \ No newline at end of file
+} // lua
+} // jin \ No newline at end of file
diff --git a/src/lua/net/luaopen_net.cpp b/src/lua/net/luaopen_net.cpp
index 8d67487..4bbb7e8 100644
--- a/src/lua/net/luaopen_net.cpp
+++ b/src/lua/net/luaopen_net.cpp
@@ -1,31 +1,95 @@
#include "lua/luax.h"
#include "libjin/jin.h"
+#include "../luaopen_types.h"
+#include "lua_net_Buffer.h"
namespace jin
{
namespace lua
{
+ using namespace jin::net;
+
static int l_initNetwork(lua_State* L)
{
jin::net::Net::get()->init();
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)
+ {
+ 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;
+ }
+ // type, port
+ if (luax_gettop(L) == 2)
+ {
+ info.port = luax_checkinteger(L, 2);
+ }
+ // type, address, port
+ else if (luax_gettop(L) == 3)
+ {
+ if (luax_isstring(L, 2))
+ info.address = tk_strtohl(luax_checkstring(L, 2));
+ else if(luax_isinteger(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->bind(socket, JIN_NETWORK_SOCKET);
+ return 1;
+ }
+
+ // jin.net.Buffer()
+ static int l_Buffer(lua_State* L)
+ {
+ int size = luax_checkinteger(L, 1);
+ Proxy* proxy = (Proxy*)luax_newinstance(L, JIN_NETWORK_BUFFER, sizeof(Proxy));
+ net::Buffer* buffer = new net::Buffer();
+ proxy->bind(buffer, JIN_NETWORK_BUFFER);
+ return 1;
+ }
static const luaL_Reg f[] = {
- { "init", l_initNetwork},
+ { "init", l_initNetwork },
+ { "toSocket", l_toSocket },
+ { "Socket", l_Socket },
+ { "Buffer", l_Buffer },
{ 0, 0 }
};
extern int luaopen_Socket(lua_State* L);
+ extern int luaopen_Buffer(lua_State* L);
// only tcp
int luaopen_net(lua_State* L)
{
+ luaopen_Socket(L);
+ luaopen_Buffer(L);
+
luax_newlib(L, f);
return 1;
}
-}
-} \ No newline at end of file
+} // lua
+} // jin \ No newline at end of file
diff --git a/src/lua/thread/luaopen_Thread.cpp b/src/lua/thread/luaopen_Thread.cpp
index 4c38899..b16a114 100644
--- a/src/lua/thread/luaopen_Thread.cpp
+++ b/src/lua/thread/luaopen_Thread.cpp
@@ -28,7 +28,7 @@ namespace jin
static inline Thread* checkThread(lua_State* L)
{
- Proxy* proxy = (Proxy*)luax_checktype(L, 1, TYPE_THREAD);
+ Proxy* proxy = (Proxy*)luax_checktype(L, 1, JIN_THREAD_THREAD);
if (proxy != nullptr)
return (Thread*)proxy->object;
return nullptr;
@@ -41,8 +41,8 @@ namespace jin
luax_openlibs(L);
luaopen_jin(L);
luax_getglobal(L, MODULE_NAME);
- Proxy* proxy = (Proxy*)luax_newinstance(L, TYPE_THREAD, sizeof(Proxy));
- proxy->bind(thread);
+ Proxy* proxy = (Proxy*)luax_newinstance(L, JIN_THREAD_THREAD, sizeof(Proxy));
+ proxy->bind(thread, JIN_THREAD_THREAD);
luax_setfield(L, -2, "_curThread");
luax_dostring(L, thread->code.c_str());
luax_close(L);
@@ -74,21 +74,32 @@ namespace jin
{
Thread* t = checkThread(L);
int slot = luax_checkinteger(L, 2);
- if (luax_isnumber(L, 3))
+ const int vp = 3;
+ if (luax_isnumber(L, vp))
{
- float real = luax_checknumber(L, 3);
+ float real = luax_checknumber(L, vp);
t->send(slot, real);
}
- else if (luax_isboolean(L, 3))
+ else if (luax_isboolean(L, vp))
{
- bool bol = luax_checkbool(L, 3);
+ bool bol = luax_checkbool(L, vp);
t->send(slot, bol);
}
- else if (luax_isstring(L, 3))
+ else if (luax_isstring(L, vp))
{
- const char* str = luax_checkstring(L, 3);
+ const char* str = luax_checkstring(L, vp);
t->send(slot, str);
}
+ else if (luax_isuserdata(L, vp))
+ {
+ void* p = luax_touserdata(L, vp);
+ t->send(slot, p);
+ }
+ else if (luax_islightuserdata(L, vp))
+ {
+ void* p = luax_tolightuserdata(L, vp);
+ t->send(slot, p);
+ }
return 0;
}
@@ -123,6 +134,13 @@ namespace jin
case thread::Thread::Variant::REAL:
luax_pushnumber(L, v.real);
break;
+
+ case thread::Thread::Variant::POINTER:
+ Proxy* p = (Proxy*)v.pointer;
+ Proxy* proxy = (Proxy*)luax_newinstance(L, p->type, sizeof(Proxy));
+ proxy->bind(p->object, p->type);
+ break;
+
}
return 1;
}
@@ -149,6 +167,13 @@ namespace jin
case thread::Thread::Variant::REAL:
luax_pushnumber(L, v.real);
break;
+
+ case thread::Thread::Variant::POINTER:
+ Proxy* p = (Proxy*)v.pointer;
+ Proxy* proxy = (Proxy*)luax_newinstance(L, p->type, sizeof(Proxy));
+ proxy->bind(p->object, p->type);
+ break;
+
}
return 1;
}
@@ -190,10 +215,10 @@ namespace jin
{ "isRunning", l_isRunning },
{ 0, 0 }
};
-
+
static int luaopen_Thread(lua_State* L)
{
- luax_newtype(L, TYPE_THREAD, thread_function);
+ luax_newtype(L, JIN_THREAD_THREAD, thread_function);
return 0;
}
@@ -203,9 +228,9 @@ namespace jin
{
const char* name = luax_checkstring(L, 1);
const char* code = luax_checkstring(L, 2);
- Proxy* proxy = (Proxy*)luax_newinstance(L, TYPE_THREAD, sizeof(Proxy));
+ Proxy* proxy = (Proxy*)luax_newinstance(L, JIN_THREAD_THREAD, sizeof(Proxy));
Thread* thread = new Thread(name, code, Thread::threadRunner);
- proxy->bind(thread);
+ 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 4c38899..b16a114 100644
--- a/src/lua/thread/luaopen_thread.cpp
+++ b/src/lua/thread/luaopen_thread.cpp
@@ -28,7 +28,7 @@ namespace jin
static inline Thread* checkThread(lua_State* L)
{
- Proxy* proxy = (Proxy*)luax_checktype(L, 1, TYPE_THREAD);
+ Proxy* proxy = (Proxy*)luax_checktype(L, 1, JIN_THREAD_THREAD);
if (proxy != nullptr)
return (Thread*)proxy->object;
return nullptr;
@@ -41,8 +41,8 @@ namespace jin
luax_openlibs(L);
luaopen_jin(L);
luax_getglobal(L, MODULE_NAME);
- Proxy* proxy = (Proxy*)luax_newinstance(L, TYPE_THREAD, sizeof(Proxy));
- proxy->bind(thread);
+ Proxy* proxy = (Proxy*)luax_newinstance(L, JIN_THREAD_THREAD, sizeof(Proxy));
+ proxy->bind(thread, JIN_THREAD_THREAD);
luax_setfield(L, -2, "_curThread");
luax_dostring(L, thread->code.c_str());
luax_close(L);
@@ -74,21 +74,32 @@ namespace jin
{
Thread* t = checkThread(L);
int slot = luax_checkinteger(L, 2);
- if (luax_isnumber(L, 3))
+ const int vp = 3;
+ if (luax_isnumber(L, vp))
{
- float real = luax_checknumber(L, 3);
+ float real = luax_checknumber(L, vp);
t->send(slot, real);
}
- else if (luax_isboolean(L, 3))
+ else if (luax_isboolean(L, vp))
{
- bool bol = luax_checkbool(L, 3);
+ bool bol = luax_checkbool(L, vp);
t->send(slot, bol);
}
- else if (luax_isstring(L, 3))
+ else if (luax_isstring(L, vp))
{
- const char* str = luax_checkstring(L, 3);
+ const char* str = luax_checkstring(L, vp);
t->send(slot, str);
}
+ else if (luax_isuserdata(L, vp))
+ {
+ void* p = luax_touserdata(L, vp);
+ t->send(slot, p);
+ }
+ else if (luax_islightuserdata(L, vp))
+ {
+ void* p = luax_tolightuserdata(L, vp);
+ t->send(slot, p);
+ }
return 0;
}
@@ -123,6 +134,13 @@ namespace jin
case thread::Thread::Variant::REAL:
luax_pushnumber(L, v.real);
break;
+
+ case thread::Thread::Variant::POINTER:
+ Proxy* p = (Proxy*)v.pointer;
+ Proxy* proxy = (Proxy*)luax_newinstance(L, p->type, sizeof(Proxy));
+ proxy->bind(p->object, p->type);
+ break;
+
}
return 1;
}
@@ -149,6 +167,13 @@ namespace jin
case thread::Thread::Variant::REAL:
luax_pushnumber(L, v.real);
break;
+
+ case thread::Thread::Variant::POINTER:
+ Proxy* p = (Proxy*)v.pointer;
+ Proxy* proxy = (Proxy*)luax_newinstance(L, p->type, sizeof(Proxy));
+ proxy->bind(p->object, p->type);
+ break;
+
}
return 1;
}
@@ -190,10 +215,10 @@ namespace jin
{ "isRunning", l_isRunning },
{ 0, 0 }
};
-
+
static int luaopen_Thread(lua_State* L)
{
- luax_newtype(L, TYPE_THREAD, thread_function);
+ luax_newtype(L, JIN_THREAD_THREAD, thread_function);
return 0;
}
@@ -203,9 +228,9 @@ namespace jin
{
const char* name = luax_checkstring(L, 1);
const char* code = luax_checkstring(L, 2);
- Proxy* proxy = (Proxy*)luax_newinstance(L, TYPE_THREAD, sizeof(Proxy));
+ Proxy* proxy = (Proxy*)luax_newinstance(L, JIN_THREAD_THREAD, sizeof(Proxy));
Thread* thread = new Thread(name, code, Thread::threadRunner);
- proxy->bind(thread);
+ proxy->bind(thread, JIN_THREAD_THREAD);
return 1;
}