aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--bin/config.lua12
-rw-r--r--bin/jin.exebin0 -> 800256 bytes
-rw-r--r--bin/main.lua207
-rw-r--r--src/3rdparty/luax/luax.h3
-rw-r--r--src/libjin/Thread/Thread.cpp7
-rw-r--r--src/libjin/Thread/Thread.h48
-rw-r--r--src/libjin/Utils/Log.h10
-rw-r--r--src/libjin/jin.h8
-rw-r--r--src/libjin/modules.h12
-rw-r--r--src/libjin/thread/thread.cpp7
-rw-r--r--src/libjin/thread/thread.h48
-rw-r--r--src/libjin/utils/log.h10
-rw-r--r--src/lua/luaopen_jin.cpp2
-rw-r--r--src/lua/thread/luaopen_Thread.cpp98
-rw-r--r--src/lua/thread/luaopen_thread.cpp98
-rw-r--r--src/main.cpp2
16 files changed, 310 insertions, 262 deletions
diff --git a/bin/config.lua b/bin/config.lua
index 36049b0..3e696b8 100644
--- a/bin/config.lua
+++ b/bin/config.lua
@@ -1,8 +1,8 @@
return
{
- width = 320 * 2,
- height = 240 * 2,
- fps = 60,
- vsync = false,
- title = "动态光照demo",
-} \ No newline at end of file
+ width = 512,
+ height = 400,
+ fullscreen = false,
+ resizable = true,
+ title = "Jin Modules"
+}
diff --git a/bin/jin.exe b/bin/jin.exe
new file mode 100644
index 0000000..682b120
--- /dev/null
+++ b/bin/jin.exe
Binary files differ
diff --git a/bin/main.lua b/bin/main.lua
index c0b0ce3..4ef3d14 100644
--- a/bin/main.lua
+++ b/bin/main.lua
@@ -1,162 +1,57 @@
-local shader = [[
-extern Image diffuse;
-extern vec2 mouse;
-extern number i ;
-
-vec4 effect(vec4 color, Image texture, vec2 texture_coords, vec2 pixel_coords)
-{
- vec3 light_vec = vec3(mouse,1);
- vec3 light_direction = light_vec - vec3(pixel_coords, 0);
- float distance = length(light_direction);
- light_direction = normalize(light_direction);
-
- vec3 normal = Texel(texture, texture_coords).xyz;
- normal.y = 1 - normal.y;
- normal = normalize(mix(vec3(-1), vec3(1), normal));
-
- //float attenuation = 1/(7e-5*pow(distance, 2));
- float attenuation = 5000/pow(distance, 2);
- //float attenuation = 1;
-
- float diffuse_term = clamp(attenuation * dot(normal, light_direction), 0.0, 1.0);
-
- vec3 dark_color = vec3(0.0, 0.0, 1);
- //vec3 light_color = vec3(0.6, 0.6, 0.0);
- vec3 light_color = vec3(0.8, 0.8, 0.0);
- vec3 ambient = mix(dark_color, light_color, diffuse_term) * 0.20;
-
- // the shaded cel has a light value of 0.5, the light cel has a light value of 1
- float cel_diffuse_term = smoothstep(0.49, 0.52, diffuse_term)/2 + 0.5;
- //float cel_diffuse_term = step(0.5, diffuse_term)/2 + 0.5;
-
- return vec4((cel_diffuse_term * Texel(diffuse, texture_coords).rgb) + ambient, Texel(texture, texture_coords).a);
- //return vec4(ambient+Texel(diffuse, texture_coords).rgb/100, Texel(texture, texture_coords).a);
-
-}
-]]
-
-local shader2 = [[
-extern Image diffuse;
-vec4 effect(vec4 color, Image texture, vec2 texture_coords, vec2 pixel_coords)
-{
- return Texel(diffuse, texture_coords);
-}
-]]
-
-local lighningShader = [[
-extern vec2 iResolution; // 分辨率, pixels
-extern number iTime; // 秒数, seconds
-// Lightning shader
-// rand,noise,fmb functions from https://www.shadertoy.com/view/Xsl3zN
-// jerome
-
-float rand(vec2 n) {
- return fract(sin(dot(n, vec2(12.9898, 4.1414))) * 43758.5453);
-}
-
-float noise(vec2 n) {
- const vec2 d = vec2(0.0, 1.0);
- vec2 b = floor(n), f = smoothstep(vec2(0.0), vec2(1.0), fract(n));
- return mix(mix(rand(b), rand(b + d.yx), f.x), mix(rand(b + d.xy), rand(b + d.yy), f.x), f.y);
-}
-
-float fbm(vec2 n) {
- float total = 0.0, amplitude = 1.0;
- for (int i = 0; i < 7; i++) {
- total += noise(n) * amplitude;
- n += n;
- amplitude *= 0.5;
- }
- return total;
-}
+local loghelper = require("loghelper")
+loghelper.strict(loghelper.LEVEL.INFO)
+local EventMsgCenter = require("EventMsgCenter.EventMsgCenter")
+local Events = require("EventMsgCenter.Events")
+local timer = require("timer.timer")
+_G["frame"] = 0
+
+local thread = nil
+
+jin.core.onLoad = function()
+ thread = jin.thread.Thread("Test", [[
+ local t = jin.thread.getThread()
+ local str = t:demand(2)
+ print(str)
+ t:send(3, "back data")
+ while true do
+ jin.time.sleep(1)
+ end
+ ]])
+ thread:start()
+ EventMsgCenter.registerMsg(Events.Player_Move, function(msg)
+ print(msg)
+ end)
+ timer.every(1.0, function()
+ loghelper.log(loghelper.LEVEL.INFO, _G["frame"] .. "fps")
+ EventMsgCenter.sendMsg(Events.Player_Move, _G["frame"])
+ _G["frame"] = 0
+ if thread:receive(3) then
+ print(thread:fetch(3))
+ end
+ end)
+ timer.after(4.0, function()
+ thread:send(2, "test thread data")
+ EventMsgCenter.unregisterAllMsgByEvent(Events.Player_Move)
+ end)
+end
-vec4 effect(vec4 outcolor, Image img, vec2 texture_coords, vec2 pixel_coords)
-//void mainImage( out vec4 fragColor, in vec2 fragCoord )
-{
- vec4 col = vec4(0,0,0,1);
- vec2 uv = pixel_coords.xy * 1.0 / iResolution.xy;
-
-
- // draw a line, left side is fixed
- vec2 t = uv * vec2(2.0,1.0) - iTime*3.0;
- vec2 t2 = (vec2(1,-1) + uv) * vec2(2.0,1.0) - iTime*3.0; // a second strand
-
- // draw the lines,
-// this make the left side fixed, can be useful
-// float ycenter = mix( 0.5, 0.25 + 0.25*fbm( t ), uv.x*4.0);
-// float ycenter2 = mix( 0.5, 0.25 + 0.25*fbm( t2 ), uv.x*4.0);
- float ycenter = fbm(t)*0.5;
- float ycenter2= fbm(t2)*0.5;
+jin.core.onEvent = function(e)
+ if e.type == "quit" then
+ jin.core.stop()
+ elseif e.type == "keydown" then
+ if e.key == "Escape" then
+ jin.core.stop()
+ end
+ end
+end
- // falloff
- float diff = abs(uv.y - ycenter);
- float c1 = 1.0 - mix(0.0,1.0,diff*20.0);
-
- float diff2 = abs(uv.y - ycenter2);
- float c2 = 1.0 - mix(0.0,1.0,diff2*20.0);
-
- float c = max(c1,c2);
- col = vec4(c*0.6,0.2*c2,c,1.0); // purple color
- //fragColor = col;
- return col;
-}
-]]
+jin.core.onUpdate = function(dt)
+ _G["frame"] = _G["frame"] + 1
+ timer.update(dt)
-local jg = jin.graphics
-local effect = jg.Shader(shader)
-local effect2 = jg.Shader(shader2)
-local lightning = jg.Shader(lighningShader)
-local diffuse = jg.Image("treestump_diffuse.png")
-local img = jg.Image("treestump.png")
-local img2 = jg.Image("lightning.png")
-local ww, wh = jg.size()
-src = jin.audio.Source("a.ogg")
-local scale = 2
-ww = ww / scale
-wh = wh / scale
-src:play()
-src:setLoop(true)
-src:setVolume(0.5)
-jin.audio.setVolume(0.1)
-jin.core.onLoad = function()
- jg.use(lightning)
- lightning:send("vec2", "iResolution", ww, wh)
- jg.unuse()
+ -- loghelper.log(loghelper.LEVEL.WARN, "版本" .. jin.revision())
end
-jin.core.onEvent = function(e)
- if e.type == "quit" then
- jin.core.stop()
- end
-end
-local mx, my
-local c = 0
-local t = 0
-jin.core.onUpdate = function(dt)
- t = t + dt
- mx, my = jin.mouse.position()
- mx = mx / 2
- my = my / 2
- my = wh - my
- c = c + 1
- if c == 50 then
- print(1/dt)
- c = 0
- end
-end
-img:setAnchor(16, 16)
-local cvs = jg.Canvas(320, 240)
-local i = 0
jin.core.onDraw = function()
--- if true then return end
- i = i + 0.001
- jg.bind(cvs)
- jg.draw(img, 10, 10)
- -- jg.use(lightning)
- -- lightning:send("number", "iTime", t)
- -- lightning:send("Image", "img", img2)
- -- jg.draw(img2, 20, 20)
- -- jg.unuse()
- jg.unbind()
- jg.draw(cvs, 0, 0, 2, 2)
-end
+
+end \ No newline at end of file
diff --git a/src/3rdparty/luax/luax.h b/src/3rdparty/luax/luax.h
index 8c9fcd8..4940f9f 100644
--- a/src/3rdparty/luax/luax.h
+++ b/src/3rdparty/luax/luax.h
@@ -32,6 +32,7 @@
#define LUAX_VERSION "0.1.0"
#define luax_newstate luaL_newstate
+#define luax_close lua_close
#define luax_openlibs luaL_openlibs
// load chunk but dont run it
#define luax_loadbuffer luaL_loadbuffer
@@ -40,7 +41,7 @@
#define luax_setglobal lua_setglobal
#define luax_pop lua_pop
#define luax_newtable lua_newtable
-
+#define luax_getglobal lua_getglobal
/**
*
*/
diff --git a/src/libjin/Thread/Thread.cpp b/src/libjin/Thread/Thread.cpp
index 374778b..064d3db 100644
--- a/src/libjin/Thread/Thread.cpp
+++ b/src/libjin/Thread/Thread.cpp
@@ -296,6 +296,13 @@ namespace thread
return v;
}
+ void Thread::remove(int slot)
+ {
+ lock();
+ common->remove(slot);
+ unlock();
+ }
+
} // thread
} // jin
diff --git a/src/libjin/Thread/Thread.h b/src/libjin/Thread/Thread.h
index aeb0d9e..37c978d 100644
--- a/src/libjin/Thread/Thread.h
+++ b/src/libjin/Thread/Thread.h
@@ -42,23 +42,36 @@ namespace thread
class Thread
{
public:
-
- union Variant
+ struct Variant
{
- int integer;
- bool boolean;
- char character;
- const char* cstring;
- void* pointer;
- float real;
-
- Variant() {};
- Variant(int i) : integer(i) {};
- Variant(float f) : real(f) {};
- Variant(bool b) : boolean(b) {};
- Variant(char c) : character(c) {};
- Variant(const char* s) : cstring(s) {};
- Variant(void* p) : pointer(p) {};
+ enum Type
+ {
+ NONE = 0,
+ INTERGER,
+ BOOLEAN,
+ CHARACTER,
+ CSTRING,
+ POINTER,
+ REAL,
+ };
+ Type type;
+ union
+ {
+ int integer;
+ bool boolean;
+ char character;
+ const char* cstring;
+ void* pointer;
+ float real;
+ };
+ Variant() :type(NONE) {};
+ Variant(const Variant& v){ memcpy(this, &v, sizeof(v)); }
+ Variant(int i) : integer(i), type(INTERGER) {};
+ Variant(float f) : real(f), type(REAL) {};
+ Variant(bool b) : boolean(b), type(BOOLEAN) {};
+ Variant(char c) : character(c), type(CHARACTER) {};
+ Variant(const char* s) : cstring(s), type(CSTRING) {};
+ Variant(void* p) : pointer(p), type(POINTER) {};
};
private:
@@ -76,6 +89,8 @@ namespace thread
static const int SLOT_ERROR = -1;
static const int SLOT_WARN = -2;
+ static const int SLOT_INFO = -3;
+ static const int SLOT_DEBUG = -4;
private:
std::map<int, Variant> share; // threads shared value
@@ -91,6 +106,7 @@ namespace thread
bool receive(int slot);
Variant fetch(int slot);
Variant demand(int slot);
+ void remove(int slot);
const char* getName();
bool isRunning();
void lock();
diff --git a/src/libjin/Utils/Log.h b/src/libjin/Utils/Log.h
index b047402..50ec3c8 100644
--- a/src/libjin/Utils/Log.h
+++ b/src/libjin/Utils/Log.h
@@ -65,19 +65,19 @@ void Loghelper::log(Level _level, const char* _fmt, ...)
switch (_level)
{
case LV_ERROR:
- levelStr = "Error: ";
+ levelStr = "[Jin Error]:";
break;
case LV_WARN:
- levelStr = "Warn: ";
+ levelStr = "[Jin Warn]:";
break;
case LV_INFO:
- levelStr = "Info: ";
+ levelStr = "[Jin Info]:";
break;
case LV_DEBUG:
- levelStr = "Debug: ";
+ levelStr = "[Jin Debug]:";
break;
default:
- levelStr = "Unknown: ";
+ levelStr = "[Jin Unknown]:";
break;
}
char buffer[FORMAT_MSG_BUFFER_SIZE + 1] = { 0 };
diff --git a/src/libjin/jin.h b/src/libjin/jin.h
index ad83fae..239fddd 100644
--- a/src/libjin/jin.h
+++ b/src/libjin/jin.h
@@ -15,9 +15,9 @@
#include "Time/Timer.h"
#include "Thread/Thread.h"
-const char* JIN_VERSION = "Jin 0.1";
-const char* JIN_AUTHOR = "Chai";
-const char* JIN_RELEASE = "Jin 0.1.1";
-const int JIN_VERSION_NUM = 101;
+#define JIN_VERSION "Jin 0.1";
+#define JIN_AUTHOR "Chai";
+#define JIN_RELEASE "Jin 0.1.1";
+#define JIN_VERSION_NUM 101;
#endif // __JIN_H \ No newline at end of file
diff --git a/src/libjin/modules.h b/src/libjin/modules.h
index 06c14df..13e288f 100644
--- a/src/libjin/modules.h
+++ b/src/libjin/modules.h
@@ -6,7 +6,7 @@
#define JIN_MODULES_AUDIO 1
#define JIN_AUDIO_SDLAUDIO 1
-#define JIN_AUDIO_OPENAL 1
+#define JIN_AUDIO_OPENAL 0
#define JIN_MODULES_RENDER 1
@@ -21,15 +21,15 @@
#define JIN_MODULES_NET 1
-#define JIN_MODULES_PHYSICS 1
+#define JIN_MODULES_PHYSICS 0
#define JIN_PHYSICS_BOX2D 1
#define JIN_PHYSICS_NEWTON 1
-#define JIN_MODULES_TILEMAP 1
+#define JIN_MODULES_TILEMAP 0
-#define JIN_MODULES_UI 1
+#define JIN_MODULES_UI 0
-#define JIN_MODULES_TOOLS 1
+#define JIN_MODULES_TOOLS 0
#define JIN_TOOLS_COMPONENT 1
#define JIN_TOOLS_EVENTMSGCENTER 1
#define JIN_TOOLS_XML 1
@@ -48,7 +48,7 @@
* Open libjin debug
*/
-#define JIN_DEBUG 1
+#define JIN_DEBUG 0
/*
* Operating system
diff --git a/src/libjin/thread/thread.cpp b/src/libjin/thread/thread.cpp
index 374778b..064d3db 100644
--- a/src/libjin/thread/thread.cpp
+++ b/src/libjin/thread/thread.cpp
@@ -296,6 +296,13 @@ namespace thread
return v;
}
+ void Thread::remove(int slot)
+ {
+ lock();
+ common->remove(slot);
+ unlock();
+ }
+
} // thread
} // jin
diff --git a/src/libjin/thread/thread.h b/src/libjin/thread/thread.h
index aeb0d9e..37c978d 100644
--- a/src/libjin/thread/thread.h
+++ b/src/libjin/thread/thread.h
@@ -42,23 +42,36 @@ namespace thread
class Thread
{
public:
-
- union Variant
+ struct Variant
{
- int integer;
- bool boolean;
- char character;
- const char* cstring;
- void* pointer;
- float real;
-
- Variant() {};
- Variant(int i) : integer(i) {};
- Variant(float f) : real(f) {};
- Variant(bool b) : boolean(b) {};
- Variant(char c) : character(c) {};
- Variant(const char* s) : cstring(s) {};
- Variant(void* p) : pointer(p) {};
+ enum Type
+ {
+ NONE = 0,
+ INTERGER,
+ BOOLEAN,
+ CHARACTER,
+ CSTRING,
+ POINTER,
+ REAL,
+ };
+ Type type;
+ union
+ {
+ int integer;
+ bool boolean;
+ char character;
+ const char* cstring;
+ void* pointer;
+ float real;
+ };
+ Variant() :type(NONE) {};
+ Variant(const Variant& v){ memcpy(this, &v, sizeof(v)); }
+ Variant(int i) : integer(i), type(INTERGER) {};
+ Variant(float f) : real(f), type(REAL) {};
+ Variant(bool b) : boolean(b), type(BOOLEAN) {};
+ Variant(char c) : character(c), type(CHARACTER) {};
+ Variant(const char* s) : cstring(s), type(CSTRING) {};
+ Variant(void* p) : pointer(p), type(POINTER) {};
};
private:
@@ -76,6 +89,8 @@ namespace thread
static const int SLOT_ERROR = -1;
static const int SLOT_WARN = -2;
+ static const int SLOT_INFO = -3;
+ static const int SLOT_DEBUG = -4;
private:
std::map<int, Variant> share; // threads shared value
@@ -91,6 +106,7 @@ namespace thread
bool receive(int slot);
Variant fetch(int slot);
Variant demand(int slot);
+ void remove(int slot);
const char* getName();
bool isRunning();
void lock();
diff --git a/src/libjin/utils/log.h b/src/libjin/utils/log.h
index b047402..50ec3c8 100644
--- a/src/libjin/utils/log.h
+++ b/src/libjin/utils/log.h
@@ -65,19 +65,19 @@ void Loghelper::log(Level _level, const char* _fmt, ...)
switch (_level)
{
case LV_ERROR:
- levelStr = "Error: ";
+ levelStr = "[Jin Error]:";
break;
case LV_WARN:
- levelStr = "Warn: ";
+ levelStr = "[Jin Warn]:";
break;
case LV_INFO:
- levelStr = "Info: ";
+ levelStr = "[Jin Info]:";
break;
case LV_DEBUG:
- levelStr = "Debug: ";
+ levelStr = "[Jin Debug]:";
break;
default:
- levelStr = "Unknown: ";
+ levelStr = "[Jin Unknown]:";
break;
}
char buffer[FORMAT_MSG_BUFFER_SIZE + 1] = { 0 };
diff --git a/src/lua/luaopen_jin.cpp b/src/lua/luaopen_jin.cpp
index 7f84467..4113ffe 100644
--- a/src/lua/luaopen_jin.cpp
+++ b/src/lua/luaopen_jin.cpp
@@ -18,6 +18,7 @@ namespace lua
extern int luaopen_filesystem(lua_State* L);
extern int luaopen_joypad(lua_State* L);
extern int luaopen_math(lua_State* L);
+ extern int luaopen_thread(lua_State* L);
static int l_getversion(lua_State* L)
{
@@ -70,6 +71,7 @@ namespace lua
{"audio", luaopen_audio},
{"joypad", luaopen_joypad},
{"math", luaopen_math},
+ {"thread", luaopen_thread},
{0, 0}
};
diff --git a/src/lua/thread/luaopen_Thread.cpp b/src/lua/thread/luaopen_Thread.cpp
index 99ddee1..4c38899 100644
--- a/src/lua/thread/luaopen_Thread.cpp
+++ b/src/lua/thread/luaopen_Thread.cpp
@@ -8,7 +8,7 @@ namespace jin
namespace lua
{
- static int luaopen_thread(lua_State* L);
+ int luaopen_thread(lua_State* L);
class Thread : public jin::thread::Thread
{
@@ -19,7 +19,9 @@ namespace jin
, code(_code)
{
}
+ static void threadRunner(jin::thread::Thread* t);
+ private:
const std::string name;
const std::string code;
};
@@ -32,18 +34,25 @@ namespace jin
return nullptr;
}
- static void threadRunner(jin::thread::Thread* t)
+ void Thread::threadRunner(jin::thread::Thread* t)
{
Thread* thread = (Thread*)t;
lua_State* L = lua_open();
luax_openlibs(L);
luaopen_jin(L);
+ luax_getglobal(L, MODULE_NAME);
+ Proxy* proxy = (Proxy*)luax_newinstance(L, TYPE_THREAD, sizeof(Proxy));
+ proxy->bind(thread);
+ luax_setfield(L, -2, "_curThread");
luax_dostring(L, thread->code.c_str());
+ luax_close(L);
}
static int l_gc(lua_State* L)
{
- return 1;
+ Thread* t = checkThread(L);
+ delete t;
+ return 0;
}
static int l_start(lua_State* L)
@@ -97,38 +106,74 @@ namespace jin
Thread* t = checkThread(L);
int slot = luax_checkinteger(L, 2);
Thread::Variant v = t->fetch(slot);
- //if (v.type == Thread::Value::INTEGER)
- //{
- // luax_pushinteger(L, v.integer);
- //}
- //else if (v.type == Thread::Value::BOOL)
- //{
- // luax_pushboolean(L, v.boolean);
- //}
- //else if (v.type == Thread::Value::STRING)
- //{
- // luax_pushstring(L, v.cstring);
- //}
- //else if (v.type == Thread::Value::POINTER)
- //{
- //}
+ switch (v.type)
+ {
+ case thread::Thread::Variant::INTERGER:
+ luax_pushinteger(L, v.integer);
+ break;
+
+ case thread::Thread::Variant::BOOLEAN:
+ luax_pushboolean(L, v.boolean);
+ break;
+
+ case thread::Thread::Variant::CSTRING:
+ luax_pushstring(L, v.cstring);
+ break;
+
+ case thread::Thread::Variant::REAL:
+ luax_pushnumber(L, v.real);
+ break;
+ }
return 1;
}
static int l_demand(lua_State* L)
{
Thread* t = checkThread(L);
-
+ int slot = luax_checkinteger(L, 2);
+ Thread::Variant v = t->demand(slot);
+ switch (v.type)
+ {
+ case thread::Thread::Variant::INTERGER:
+ luax_pushinteger(L, v.integer);
+ break;
+
+ case thread::Thread::Variant::BOOLEAN:
+ luax_pushboolean(L, v.boolean);
+ break;
+
+ case thread::Thread::Variant::CSTRING:
+ luax_pushstring(L, v.cstring);
+ break;
+
+ case thread::Thread::Variant::REAL:
+ luax_pushnumber(L, v.real);
+ break;
+ }
return 1;
}
+ static int l_remove(lua_State* L)
+ {
+ Thread* t = checkThread(L);
+ int slot = luax_checkinteger(L, 1);
+ t->remove(slot);
+ return 0;
+ }
+
static int l_getName(lua_State* L)
{
+ Thread* t = checkThread(L);
+ const char* name = t->getName();
+ luax_pushstring(L, name);
return 1;
}
static int l_isRunning(lua_State* L)
{
+ Thread* t = checkThread(L);
+ bool running = t->isRunning();
+ luax_pushboolean(L, running);
return 1;
}
@@ -140,6 +185,7 @@ namespace jin
{ "receive", l_receive },
{ "fetch", l_fetch },
{ "demand", l_demand },
+ { "remove", l_remove },
{ "getName", l_getName },
{ "isRunning", l_isRunning },
{ 0, 0 }
@@ -147,7 +193,7 @@ namespace jin
static int luaopen_Thread(lua_State* L)
{
- luax_newtype(L, TYPE_SOURCE, thread_function);
+ luax_newtype(L, TYPE_THREAD, thread_function);
return 0;
}
@@ -158,14 +204,16 @@ 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));
- Thread* thread = new Thread(name, code, threadRunner);
+ Thread* thread = new Thread(name, code, Thread::threadRunner);
proxy->bind(thread);
return 1;
}
static int l_getThread(lua_State* L)
{
-
+ luax_getglobal(L, MODULE_NAME);
+ luax_getfield(L, -1, "_curThread");
+ return 1;
}
static const luaL_Reg f[] = {
@@ -174,9 +222,13 @@ namespace jin
{ 0, 0 }
};
- static int luaopen_thread(lua_State* L)
+ int luaopen_thread(lua_State* L)
{
+ luaopen_Thread(L);
+
+ luax_newlib(L, f);
+ return 1;
}
}
} \ No newline at end of file
diff --git a/src/lua/thread/luaopen_thread.cpp b/src/lua/thread/luaopen_thread.cpp
index 99ddee1..4c38899 100644
--- a/src/lua/thread/luaopen_thread.cpp
+++ b/src/lua/thread/luaopen_thread.cpp
@@ -8,7 +8,7 @@ namespace jin
namespace lua
{
- static int luaopen_thread(lua_State* L);
+ int luaopen_thread(lua_State* L);
class Thread : public jin::thread::Thread
{
@@ -19,7 +19,9 @@ namespace jin
, code(_code)
{
}
+ static void threadRunner(jin::thread::Thread* t);
+ private:
const std::string name;
const std::string code;
};
@@ -32,18 +34,25 @@ namespace jin
return nullptr;
}
- static void threadRunner(jin::thread::Thread* t)
+ void Thread::threadRunner(jin::thread::Thread* t)
{
Thread* thread = (Thread*)t;
lua_State* L = lua_open();
luax_openlibs(L);
luaopen_jin(L);
+ luax_getglobal(L, MODULE_NAME);
+ Proxy* proxy = (Proxy*)luax_newinstance(L, TYPE_THREAD, sizeof(Proxy));
+ proxy->bind(thread);
+ luax_setfield(L, -2, "_curThread");
luax_dostring(L, thread->code.c_str());
+ luax_close(L);
}
static int l_gc(lua_State* L)
{
- return 1;
+ Thread* t = checkThread(L);
+ delete t;
+ return 0;
}
static int l_start(lua_State* L)
@@ -97,38 +106,74 @@ namespace jin
Thread* t = checkThread(L);
int slot = luax_checkinteger(L, 2);
Thread::Variant v = t->fetch(slot);
- //if (v.type == Thread::Value::INTEGER)
- //{
- // luax_pushinteger(L, v.integer);
- //}
- //else if (v.type == Thread::Value::BOOL)
- //{
- // luax_pushboolean(L, v.boolean);
- //}
- //else if (v.type == Thread::Value::STRING)
- //{
- // luax_pushstring(L, v.cstring);
- //}
- //else if (v.type == Thread::Value::POINTER)
- //{
- //}
+ switch (v.type)
+ {
+ case thread::Thread::Variant::INTERGER:
+ luax_pushinteger(L, v.integer);
+ break;
+
+ case thread::Thread::Variant::BOOLEAN:
+ luax_pushboolean(L, v.boolean);
+ break;
+
+ case thread::Thread::Variant::CSTRING:
+ luax_pushstring(L, v.cstring);
+ break;
+
+ case thread::Thread::Variant::REAL:
+ luax_pushnumber(L, v.real);
+ break;
+ }
return 1;
}
static int l_demand(lua_State* L)
{
Thread* t = checkThread(L);
-
+ int slot = luax_checkinteger(L, 2);
+ Thread::Variant v = t->demand(slot);
+ switch (v.type)
+ {
+ case thread::Thread::Variant::INTERGER:
+ luax_pushinteger(L, v.integer);
+ break;
+
+ case thread::Thread::Variant::BOOLEAN:
+ luax_pushboolean(L, v.boolean);
+ break;
+
+ case thread::Thread::Variant::CSTRING:
+ luax_pushstring(L, v.cstring);
+ break;
+
+ case thread::Thread::Variant::REAL:
+ luax_pushnumber(L, v.real);
+ break;
+ }
return 1;
}
+ static int l_remove(lua_State* L)
+ {
+ Thread* t = checkThread(L);
+ int slot = luax_checkinteger(L, 1);
+ t->remove(slot);
+ return 0;
+ }
+
static int l_getName(lua_State* L)
{
+ Thread* t = checkThread(L);
+ const char* name = t->getName();
+ luax_pushstring(L, name);
return 1;
}
static int l_isRunning(lua_State* L)
{
+ Thread* t = checkThread(L);
+ bool running = t->isRunning();
+ luax_pushboolean(L, running);
return 1;
}
@@ -140,6 +185,7 @@ namespace jin
{ "receive", l_receive },
{ "fetch", l_fetch },
{ "demand", l_demand },
+ { "remove", l_remove },
{ "getName", l_getName },
{ "isRunning", l_isRunning },
{ 0, 0 }
@@ -147,7 +193,7 @@ namespace jin
static int luaopen_Thread(lua_State* L)
{
- luax_newtype(L, TYPE_SOURCE, thread_function);
+ luax_newtype(L, TYPE_THREAD, thread_function);
return 0;
}
@@ -158,14 +204,16 @@ 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));
- Thread* thread = new Thread(name, code, threadRunner);
+ Thread* thread = new Thread(name, code, Thread::threadRunner);
proxy->bind(thread);
return 1;
}
static int l_getThread(lua_State* L)
{
-
+ luax_getglobal(L, MODULE_NAME);
+ luax_getfield(L, -1, "_curThread");
+ return 1;
}
static const luaL_Reg f[] = {
@@ -174,9 +222,13 @@ namespace jin
{ 0, 0 }
};
- static int luaopen_thread(lua_State* L)
+ int luaopen_thread(lua_State* L)
{
+ luaopen_Thread(L);
+
+ luax_newlib(L, f);
+ return 1;
}
}
} \ No newline at end of file
diff --git a/src/main.cpp b/src/main.cpp
index 256f39d..51b100f 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -42,9 +42,9 @@ int main(int argc, char* argv[])
#endif
#undef BUFFER_SIZE
luax_setfield_string(L, "_dir", buffer);
-
// boot
jin::lua::boot(L);
+ luax_close(L);
return 0;
} \ No newline at end of file