diff options
author | chai <chaifix@163.com> | 2018-08-14 14:56:47 +0800 |
---|---|---|
committer | chai <chaifix@163.com> | 2018-08-14 14:56:47 +0800 |
commit | 5c9af043503f92852a1a765b6ecfbc1aea24d2e9 (patch) | |
tree | eb371092c4137a672e7bfc13dc56ee777623ebfe /src | |
parent | 5162f84be0a4deb447c6ba1226722b049335d525 (diff) |
*update
Diffstat (limited to 'src')
38 files changed, 1192 insertions, 618 deletions
diff --git a/src/libjin/Audio/SDL/SDLSource.h b/src/libjin/Audio/SDL/SDLSource.h index 9a3dd9b..dd792c7 100644 --- a/src/libjin/Audio/SDL/SDLSource.h +++ b/src/libjin/Audio/SDL/SDLSource.h @@ -45,7 +45,7 @@ namespace audio inline void handle(SDLSourceManager* manager, SDLSourceCommand* cmd); inline void process(void* buffer, size_t size); - private: + protected: SDLSource(); diff --git a/src/libjin/Graphics/Canvas.h b/src/libjin/Graphics/Canvas.h index bccb3f6..0d5635e 100644 --- a/src/libjin/Graphics/Canvas.h +++ b/src/libjin/Graphics/Canvas.h @@ -22,7 +22,7 @@ namespace graphics static bool hasbind(GLint fbo); - private: + protected: Canvas(int w, int h); diff --git a/src/libjin/Graphics/JSL.h b/src/libjin/Graphics/JSL.h index 3872802..df942fd 100644 --- a/src/libjin/Graphics/JSL.h +++ b/src/libjin/Graphics/JSL.h @@ -22,7 +22,7 @@ namespace graphics static JSLProgram* createJSLProgram(const char* program); - ~JSLProgram(); + virtual ~JSLProgram(); inline void JSLProgram::use() { @@ -49,7 +49,7 @@ namespace graphics return currentJSLProgram; } - private: + protected: JSLProgram(const char* program); diff --git a/src/libjin/Thread/Thread.cpp b/src/libjin/Thread/Thread.cpp index 064d3db..2be44a1 100644 --- a/src/libjin/Thread/Thread.cpp +++ b/src/libjin/Thread/Thread.cpp @@ -253,7 +253,7 @@ namespace thread mutex->unlock(); } - void Thread::send(int slot, Variant value) + void Thread::send(int slot, const Variant& value) { lock(); common->set(slot, value); diff --git a/src/libjin/Thread/Thread.h b/src/libjin/Thread/Thread.h index 37c978d..5b74f23 100644 --- a/src/libjin/Thread/Thread.h +++ b/src/libjin/Thread/Thread.h @@ -102,7 +102,7 @@ namespace thread ~Thread(); bool start(); void wait(); - void send(int slot, Variant value); + void send(int slot, const Variant& value); bool receive(int slot); Variant fetch(int slot); Variant demand(int slot); @@ -112,7 +112,7 @@ namespace thread void lock(); void unlock(); - private: + protected: #if JIN_THREAD_SDL SDL_Thread* handle; // SDL thread #elif JIN_THREAD_CPP diff --git a/src/libjin/Thread/thread.cpp b/src/libjin/Thread/thread.cpp index 064d3db..2be44a1 100644 --- a/src/libjin/Thread/thread.cpp +++ b/src/libjin/Thread/thread.cpp @@ -253,7 +253,7 @@ namespace thread mutex->unlock(); } - void Thread::send(int slot, Variant value) + void Thread::send(int slot, const Variant& value) { lock(); common->set(slot, value); diff --git a/src/libjin/Thread/thread.h b/src/libjin/Thread/thread.h index 37c978d..5b74f23 100644 --- a/src/libjin/Thread/thread.h +++ b/src/libjin/Thread/thread.h @@ -102,7 +102,7 @@ namespace thread ~Thread(); bool start(); void wait(); - void send(int slot, Variant value); + void send(int slot, const Variant& value); bool receive(int slot); Variant fetch(int slot); Variant demand(int slot); @@ -112,7 +112,7 @@ namespace thread void lock(); void unlock(); - private: + protected: #if JIN_THREAD_SDL SDL_Thread* handle; // SDL thread #elif JIN_THREAD_CPP diff --git a/src/libjin/thread/thread.cpp b/src/libjin/thread/thread.cpp index 064d3db..2be44a1 100644 --- a/src/libjin/thread/thread.cpp +++ b/src/libjin/thread/thread.cpp @@ -253,7 +253,7 @@ namespace thread mutex->unlock(); } - void Thread::send(int slot, Variant value) + void Thread::send(int slot, const Variant& value) { lock(); common->set(slot, value); diff --git a/src/libjin/thread/thread.h b/src/libjin/thread/thread.h index 37c978d..5b74f23 100644 --- a/src/libjin/thread/thread.h +++ b/src/libjin/thread/thread.h @@ -102,7 +102,7 @@ namespace thread ~Thread(); bool start(); void wait(); - void send(int slot, Variant value); + void send(int slot, const Variant& value); bool receive(int slot); Variant fetch(int slot); Variant demand(int slot); @@ -112,7 +112,7 @@ namespace thread void lock(); void unlock(); - private: + protected: #if JIN_THREAD_SDL SDL_Thread* handle; // SDL thread #elif JIN_THREAD_CPP diff --git a/src/lua/audio/Audio.h b/src/lua/audio/Audio.h new file mode 100644 index 0000000..3d22a90 --- /dev/null +++ b/src/lua/audio/Audio.h @@ -0,0 +1,19 @@ +#ifndef __JIN_LUA_AUDIO_AUDIO_H +#define __JIN_LUA_AUDIO_AUDIO_H +#include "libjin/jin.h" +#include "../luaopen_types.h" + +namespace jin +{ +namespace lua +{ +namespace audio +{ + + typedef jin::audio::SDLAudio Audio; + +} +} +} + +#endif#pragma once diff --git a/src/lua/audio/Source.h b/src/lua/audio/Source.h new file mode 100644 index 0000000..094ad12 --- /dev/null +++ b/src/lua/audio/Source.h @@ -0,0 +1,93 @@ +#ifndef __JIN_LUA_AUDIO_SOURCE_H +#define __JIN_LUA_AUDIO_SOURCE_H +#include "libjin/jin.h" +#include "../luaopen_types.h" + +namespace jin +{ +namespace lua +{ +namespace audio +{ + + class Source : public Object + { + public: + void play() + { + source->play(); + } + + void stop() + { + source->stop(); + } + + void pause() + { + source->pause(); + } + + void resume() + { + source->resume(); + } + + void rewind() + { + source->rewind(); + } + + bool isStopped() const + { + return source->isStopped(); + } + + bool isPaused() const + { + return source->isPaused(); + } + + void setPitch(float pitch) + { + source->setPitch(pitch); + } + + void setVolume(float volume) + { + source->setVolume(volume); + } + + bool setLoop(bool loop) + { + return source->setLoop(loop); + } + + void setRate(float rate) + { + source->setRate(rate); + } + + inline static Source * createSource(void* mem, size_t size) + { + Source* src = new Source; + src->source = jin::audio::SDLSource::createSource(mem, size); + return src; + } + + private: + ~Source() + { + source->stop(); + delete source; + } + + jin::audio::SDLSource* source; + + }; + +} // audio +} // lua +} // jin + +#endif
\ No newline at end of file diff --git a/src/lua/audio/luaopen_Source.cpp b/src/lua/audio/luaopen_Source.cpp index 10aab4d..8881620 100644 --- a/src/lua/audio/luaopen_Source.cpp +++ b/src/lua/audio/luaopen_Source.cpp @@ -1,60 +1,60 @@ #include "lua/luax.h" -#include "libjin/jin.h" #include "../luaopen_types.h" +#include "Source.h" namespace jin { namespace lua { - using namespace jin::audio; + using audio::Source; - static inline SDLSource* checkSource(lua_State* L) + static inline Source* checkSource(lua_State* L) { Proxy* proxy = (Proxy*)luax_checktype(L, 1, JIN_AUDIO_SOURCE); if (proxy != 0 && proxy != nullptr) - return (SDLSource*)proxy->object; + return (Source*)proxy->object; return nullptr; } static int l_play(lua_State* L) { - SDLSource* src = checkSource(L); + Source* src = checkSource(L); src->play(); return 0; } static int l_stop(lua_State* L) { - SDLSource* src = checkSource(L); + Source* src = checkSource(L); src->stop(); return 0; } static int l_pause(lua_State* L) { - SDLSource* src = checkSource(L); + Source* src = checkSource(L); src->pause(); return 0; } static int l_rewind(lua_State* L) { - SDLSource* src = checkSource(L); + Source* src = checkSource(L); src->rewind(); return 0; } static int l_resume(lua_State* L) { - SDLSource* src = checkSource(L); + Source* src = checkSource(L); src->resume(); return 0; } static int l_isStop(lua_State* L) { - SDLSource* src = checkSource(L); + Source* src = checkSource(L); bool isStop = src->isStopped(); luax_pushboolean(L, isStop); return 1; @@ -62,7 +62,7 @@ namespace lua static int l_isPaused(lua_State* L) { - SDLSource* src = checkSource(L); + Source* src = checkSource(L); bool isPaused = src->isPaused(); luax_pushboolean(L, isPaused); return 1; @@ -70,7 +70,7 @@ namespace lua static int l_setVolume(lua_State* L) { - SDLSource* src = checkSource(L); + Source* src = checkSource(L); float volume = luax_checknumber(L, 2); src->setVolume(volume); return 0; @@ -78,7 +78,7 @@ namespace lua static int l_setLoop(lua_State* L) { - SDLSource* src = checkSource(L); + Source* src = checkSource(L); bool loop = luax_checkbool(L, 2); src->setLoop(loop); return 0; @@ -86,9 +86,8 @@ namespace lua static int l_gc(lua_State* L) { - SDLSource* src = checkSource(L); - src->stop(); - delete src; + Proxy* proxy = (Proxy*)luax_checktype(L, 1, JIN_AUDIO_SOURCE); + proxy->release(); return 0; } diff --git a/src/lua/audio/luaopen_audio.cpp b/src/lua/audio/luaopen_audio.cpp index 33f0561..2bb2f3a 100644 --- a/src/lua/audio/luaopen_audio.cpp +++ b/src/lua/audio/luaopen_audio.cpp @@ -1,20 +1,21 @@ #include "lua/luax.h" #include "../luaopen_types.h" -#include "libjin/jin.h" +#include "Source.h" +#include "Audio.h" namespace jin { namespace lua { using namespace jin::filesystem; - using namespace jin::audio; + using namespace jin::lua::audio; static int l_init(lua_State* L) { - SDLAudio::Setting setting; + Audio::Setting setting; setting.samplerate = 44100; setting.samples = 44100; - if (! SDLAudio::get()->init(&setting)) + if (!Audio::get()->init(&setting)) { luax_error(L, "could not init audio"); luax_pushboolean(L, false); @@ -26,32 +27,32 @@ namespace lua static int l_play(lua_State* L) { - SDLAudio::get()->play(); + Audio::get()->play(); return 0; } static int l_stop(lua_State* L) { - SDLAudio::get()->stop(); + Audio::get()->stop(); return 0; } static int l_pause(lua_State* L) { - SDLAudio::get()->pause(); + Audio::get()->pause(); return 0; } static int l_resume(lua_State* L) { - SDLAudio::get()->resume(); + Audio::get()->resume(); return 0; } static int l_setVolume(lua_State* L) { float volume = luax_checknumber(L, 1); - SDLAudio::get()->setVolume(volume); + Audio::get()->setVolume(volume); return 0; } @@ -66,16 +67,15 @@ namespace lua } Buffer b; fs->read(f, &b); - Proxy* proxy = (Proxy*)luax_newinstance(L, JIN_AUDIO_SOURCE, sizeof(Proxy)); - SDLSource* src = SDLSource::createSource(b.data, b.size); + Source* src = Source::createSource(b.data, b.size); proxy->bind(src, JIN_AUDIO_SOURCE); return 1; } static int l_destroy(lua_State* L) { - SDLAudio* audio = SDLAudio::get(); + Audio* audio = Audio::get(); audio->quit(); return 0; } diff --git a/src/lua/graphics/Canvas.cpp b/src/lua/graphics/Canvas.cpp new file mode 100644 index 0000000..c4918f0 --- /dev/null +++ b/src/lua/graphics/Canvas.cpp @@ -0,0 +1,24 @@ +#include "Canvas.h" + +namespace jin +{ +namespace lua +{ +namespace graphics +{ + + Canvas* Canvas::createCanvas(int w, int h) + { + Canvas* canvas = new Canvas(); + canvas->canvas = jin::graphics::Canvas::createCanvas(w, h); + return canvas; + } + + void Canvas::unbind() + { + jin::graphics::Canvas::unbind(); + } + +} +} +}
\ No newline at end of file diff --git a/src/lua/graphics/Canvas.h b/src/lua/graphics/Canvas.h new file mode 100644 index 0000000..d80ff79 --- /dev/null +++ b/src/lua/graphics/Canvas.h @@ -0,0 +1,59 @@ +#ifndef __JIN_LUA_GRAPHICS_CANVAS_H +#define __JIN_LUA_GRAPHICS_CANVAS_H +#include "libjin/jin.h" +#include "../luaopen_types.h" + +namespace jin +{ +namespace lua +{ +namespace graphics +{ + + class Canvas : public Object + { + public: + static Canvas* createCanvas(int w, int h); + + int getWidth() + { + return canvas->getWidth(); + } + int getHeight() + { + return canvas->getHeight(); + } + void setAnchor(int x, int y) + { + canvas->setAnchor(x, y); + } + inline const jin::graphics::Canvas* getRawCanvas() const + { + return canvas; + } + void bind() + { + canvas->bind(); + } + + void draw(int x, int y, float sx, float sy, float r) + { + canvas->draw(x, y, sx, sy, r); + } + + static void unbind(); + + private: + ~Canvas() + { + delete canvas; + } + jin::graphics::Canvas* canvas; + + }; + +} // graphics +} // lua +} // jin + +#endif
\ No newline at end of file diff --git a/src/lua/graphics/Color.h b/src/lua/graphics/Color.h new file mode 100644 index 0000000..3621329 --- /dev/null +++ b/src/lua/graphics/Color.h @@ -0,0 +1,19 @@ +#ifndef __JIN_LUA_GRAPHICS_COLOR_H +#define __JIN_LUA_GRAPHICS_COLOR_H +#include "libjin/jin.h" +#include "../luaopen_types.h" + +namespace jin +{ +namespace lua +{ +namespace graphics +{ + + typedef jin::graphics::color color; + +} // graphics +} // lua +} // jin + +#endif
\ No newline at end of file diff --git a/src/lua/graphics/Font.h b/src/lua/graphics/Font.h new file mode 100644 index 0000000..b3b66a8 --- /dev/null +++ b/src/lua/graphics/Font.h @@ -0,0 +1,52 @@ +#ifndef __JIN_LUA_GRAPHICS_FONT_H +#define __JIN_LUA_GRAPHICS_FONT_H +#include "libjin/jin.h" +#include "../luaopen_types.h" + +namespace jin +{ +namespace lua +{ +namespace graphics +{ + + class Font : public Object + { + public: + Font() + { + font = new jin::graphics::Font(); + } + + void box(const char* str, int fheight, int spacing, int lheight, int* w, int * h) + { + font->box(str, fheight, spacing, lheight, w, h); + } + void loadb(const unsigned char* data) + { + font->loadb(data); + } + void render( + const char* text, // rendered text + float x, float y, // render position + int fheight, // font height + int spacing, // font spacing + int lheight) // line height + { + font->render(text, x, y, fheight, spacing, lheight); + } + private: + ~Font() + { + delete font; + } + + jin::graphics::Font* font; + + }; + +} // graphics +} // lua +} // jin + +#endif
\ No newline at end of file diff --git a/src/lua/graphics/Image.cpp b/src/lua/graphics/Image.cpp new file mode 100644 index 0000000..43a83c4 --- /dev/null +++ b/src/lua/graphics/Image.cpp @@ -0,0 +1,26 @@ +#include "Image.h" + +namespace jin +{ +namespace lua +{ +namespace graphics +{ + + Image* Image::createImage(const char* file) + { + Image* image = new Image(); + image->image = jin::graphics::Texture::createTexture(file); + return image; + } + + Image* Image::createImage(const void* mem, size_t size) + { + Image* image = new Image(); + image->image = jin::graphics::Texture::createTexture(mem, size); + return image; + } + +} +} +}
\ No newline at end of file diff --git a/src/lua/graphics/Image.h b/src/lua/graphics/Image.h new file mode 100644 index 0000000..689793a --- /dev/null +++ b/src/lua/graphics/Image.h @@ -0,0 +1,59 @@ +#ifndef __JIN_LUA_GRAPHICS_IMAGE_H +#define __JIN_LUA_GRAPHICS_IMAGE_H +#include "libjin/jin.h" +#include "../luaopen_types.h" + +namespace jin +{ +namespace lua +{ +namespace graphics +{ + + class Image : public Object + { + public: + static Image* createImage(const char* file); + static Image* createImage(const void* mem, size_t size); + + int getWidth() + { + return image->getWidth(); + } + int getHeight() + { + return image->getHeight(); + } + void setAnchor(int x, int y) + { + image->setAnchor(x, y); + } + jin::graphics::color getPixel(int x, int y) + { + return image->getPixel(x, y); + } + inline const jin::graphics::Texture* getRawImage() const + { + return image; + } + + void draw(int x, int y, float sx, float sy, float r) + { + image->draw(x, y, sx, sy, r); + } + + private: + ~Image() + { + delete image; + } + + jin::graphics::Texture* image; + + }; + +} // graphics +} // lua +} // jin + +#endif
\ No newline at end of file diff --git a/src/lua/graphics/JSL.cpp b/src/lua/graphics/JSL.cpp new file mode 100644 index 0000000..9df19a9 --- /dev/null +++ b/src/lua/graphics/JSL.cpp @@ -0,0 +1,19 @@ +#include "JSL.h" + +namespace jin +{ +namespace lua +{ +namespace graphics +{ + + JSLProgram* JSLProgram::currentJSLProgram = nullptr; + JSLProgram* JSLProgram::createJSLProgram(const char* program) + { + JSLProgram* jslprogram = new JSLProgram(); + jslprogram->jslprogram = jin::graphics::JSLProgram::createJSLProgram(program); + return jslprogram; + } +} +} +}
\ No newline at end of file diff --git a/src/lua/graphics/JSL.h b/src/lua/graphics/JSL.h new file mode 100644 index 0000000..828fac0 --- /dev/null +++ b/src/lua/graphics/JSL.h @@ -0,0 +1,90 @@ +#ifndef __JIN_LUA_GRAPHICS_JSL_H +#define __JIN_LUA_GRAPHICS_JSL_H +#include "libjin/jin.h" +#include "../luaopen_types.h" +#include "Image.h" +#include "Color.h" +#include "Canvas.h" + +namespace jin +{ +namespace lua +{ +namespace graphics +{ + + class JSLProgram : public Object + { + public: + static JSLProgram* createJSLProgram(const char* program); + + inline void use() + { + currentJSLProgram = this; + jslprogram->use(); + } + + static inline void unuse() + { + currentJSLProgram = nullptr; + jin::graphics::JSLProgram::unuse(); + } + + void sendFloat(const char* name, float number) + { + jslprogram->sendFloat(name, number); + } + + void sendImage(const char* name, const Image* image) + { + jslprogram->sendTexture(name, image->getRawImage()); + } + + void sendVec2(const char* name, float x, float y) + { + jslprogram->sendVec2(name, x, y); + } + + void sendVec3(const char* name, float x, float y, float z) + { + jslprogram->sendVec3(name, x, y, z); + } + + void sendVec4(const char* name, float x, float y, float z, float w) + { + jslprogram->sendVec4(name, x, y, z, w); + } + + void sendCanvas(const char* name, const Canvas* canvas) + { + jslprogram->sendCanvas(name, canvas->getRawCanvas()); + } + + void sendColor(const char* name, const lua::graphics::color* col) + { + jslprogram->sendColor(name, col); + } + + static inline JSLProgram* getCurrentJSL() + { + return currentJSLProgram; + } + + private: + + static JSLProgram* currentJSLProgram; + + ~JSLProgram() + { + delete jslprogram; + } + + jin::graphics::JSLProgram * jslprogram; + + }; + +} // graphics +} // lua +} // jin + +#endif
\ No newline at end of file diff --git a/src/lua/graphics/graphics.h b/src/lua/graphics/graphics.h new file mode 100644 index 0000000..bd9bec1 --- /dev/null +++ b/src/lua/graphics/graphics.h @@ -0,0 +1,18 @@ +#ifndef __JIN_LUA_GRAPHICS_GRAPHICS_H +#define __JIN_LUA_GRAPHICS_GRAPHICS_H +#include "libjin/jin.h" +#include "../luaopen_types.h" + +namespace jin +{ +namespace lua +{ +namespace graphics +{ + + typedef jin::graphics::RENDER_MODE RENDER_MODE; + +} +} +} +#endif
\ No newline at end of file diff --git a/src/lua/graphics/luaopen_Canvas.cpp b/src/lua/graphics/luaopen_Canvas.cpp index 808a977..d08b181 100644 --- a/src/lua/graphics/luaopen_Canvas.cpp +++ b/src/lua/graphics/luaopen_Canvas.cpp @@ -1,13 +1,13 @@ #include "lua/luax.h" #include "lua/luaopen_types.h" -#include "libjin/jin.h" +#include "Canvas.h" namespace jin { namespace lua { - using namespace jin::graphics; + using namespace lua::graphics; static inline Canvas* checkCanvas(lua_State* L) { @@ -51,8 +51,7 @@ namespace lua static int l_gc(lua_State* L) { Proxy* proxy = (Proxy*)luax_checktype(L, 1, JIN_GRAPHICS_CANVAS); - Canvas* canvas = (Canvas*)proxy->object; - delete canvas; + proxy->release(); return 0; } diff --git a/src/lua/graphics/luaopen_Font.cpp b/src/lua/graphics/luaopen_Font.cpp index 3de2981..3643412 100644 --- a/src/lua/graphics/luaopen_Font.cpp +++ b/src/lua/graphics/luaopen_Font.cpp @@ -11,12 +11,15 @@ namespace lua static int l_gc(lua_State* L) { + Proxy* proxy = (Proxy*)luax_newinstance(L, JIN_GRAPHICS_FONT, sizeof(Proxy)); + proxy->release(); return 0; } static int l_box(lua_State* L) { - Font* font = (Font*)luax_checktype(L, 1, JIN_GRAPHICS_FONT); + Proxy* proxy = (Proxy*)luax_newinstance(L, JIN_GRAPHICS_FONT, sizeof(Proxy)); + Font* font = (Font*)proxy->object; const char* text = luax_checkstring(L, 2); int fheight = luax_checknumber(L, 3); int spacing = luax_checknumber(L, 4); @@ -41,5 +44,5 @@ namespace lua return 0; } -} -}
\ No newline at end of file +} // lua +} // jin
\ No newline at end of file diff --git a/src/lua/graphics/luaopen_Image.cpp b/src/lua/graphics/luaopen_Image.cpp index 0f97b2c..8d89a80 100644 --- a/src/lua/graphics/luaopen_Image.cpp +++ b/src/lua/graphics/luaopen_Image.cpp @@ -1,39 +1,40 @@ #include "lua/luax.h" -#include "libjin/jin.h" #include "lua/luaopen_types.h" +#include "Image.h" +#include "Color.h" namespace jin { namespace lua { - using namespace jin::graphics; + using namespace lua::graphics; - static inline Texture* checkTexture(lua_State* L) + static inline Image* checkImage(lua_State* L) { Proxy* proxy = (Proxy*)luax_checktype(L, 1, JIN_GRAPHICS_IMAGE); if (proxy != nullptr) - return (Texture*)proxy->object; + return (Image*)proxy->object; return nullptr; } static int l_getWidth(lua_State* L) { - Texture* i = checkTexture(L); + Image* i = checkImage(L); luax_pushnumber(L, i->getWidth()); return 1; } static int l_getHeight(lua_State *L) { - Texture* i = checkTexture(L); + Image* i = checkImage(L); luax_pushnumber(L, i->getHeight()); return 1; } static int l_getPixel(lua_State* L) { - Texture* i = checkTexture(L); + Image* i = checkImage(L); int x = luax_checknumber(L, 2); int y = luax_checknumber(L, 3); color c = i->getPixel(x, y); @@ -46,7 +47,7 @@ namespace lua static int l_setAnchor(lua_State* L) { - Texture* i = checkTexture(L); + Image* i = checkImage(L); int x = luax_checknumber(L, 2); int y = luax_checknumber(L, 3); i->setAnchor(x, y); @@ -55,7 +56,7 @@ namespace lua static int l_getSize(lua_State* L) { - Texture* i = checkTexture(L); + Image* i = checkImage(L); luax_pushnumber(L, i->getWidth()); luax_pushnumber(L, i->getHeight()); return 2; @@ -64,8 +65,7 @@ namespace lua static int l_gc(lua_State* L) { Proxy* proxy = (Proxy*)luax_checktype(L, 1, JIN_GRAPHICS_IMAGE); - Texture* img = (Texture*)proxy->object; - delete img; + proxy->release(); return 0; } diff --git a/src/lua/graphics/luaopen_JSL.cpp b/src/lua/graphics/luaopen_JSL.cpp index 8d25178..774f2b6 100644 --- a/src/lua/graphics/luaopen_JSL.cpp +++ b/src/lua/graphics/luaopen_JSL.cpp @@ -1,13 +1,16 @@ #include "lua/luax.h" -#include "libjin/jin.h" #include "lua/luaopen_types.h" +#include "Image.h" +#include "JSL.h" +#include "Canvas.h" +#include "Color.h" namespace jin { namespace lua { - - using namespace jin::graphics; + + using namespace lua::graphics; static inline JSLProgram* checkJSLProgram(lua_State* L) { @@ -67,8 +70,8 @@ namespace lua case IMAGE: { Proxy* proxy = (Proxy*)luax_checktype(L, 4, JIN_GRAPHICS_IMAGE); - Texture* tex = (Texture*)proxy->object; - jsl->sendTexture(variable, tex); + Image* tex = (Image*)proxy->object; + jsl->sendImage(variable, tex); break; } case CANVAS: @@ -122,8 +125,7 @@ namespace lua static int l_gc(lua_State* L) { Proxy* proxy = (Proxy*)luax_checktype(L, 1, JIN_GRAPHICS_SHADER); - JSLProgram* jsl = (JSLProgram*)proxy->object; - delete jsl; + proxy->release(); return 0; } diff --git a/src/lua/graphics/luaopen_graphics.cpp b/src/lua/graphics/luaopen_graphics.cpp index da91c51..31c5719 100644 --- a/src/lua/graphics/luaopen_graphics.cpp +++ b/src/lua/graphics/luaopen_graphics.cpp @@ -1,14 +1,23 @@ -#include "libjin/jin.h" #include "lua/luax.h" +#include "libjin/jin.h" #include "lua/luaopen_types.h" #include "lua/embed/graphics.lua.h" +#include "Canvas.h" +#include "Color.h" +#include "Font.h" +#include "Image.h" +#include "JSL.h" +#include "graphics.h" namespace jin { namespace lua { - using namespace jin::graphics; - using namespace jin::filesystem; + + using namespace lua::graphics; + using jin::graphics::Window; + using jin::filesystem::Filesystem; + using jin::filesystem::Buffer; /** * jin.graphics context, storge some module @@ -17,8 +26,8 @@ namespace lua static struct { color curRenderColor; - Font* curFont = 0; - Font* defaultFont = 0; + Font* curFont = nullptr; + Font* defaultFont = nullptr; } context; /** @@ -78,7 +87,7 @@ namespace lua fs->read(f, &b); Proxy* proxy = (Proxy*)luax_newinstance(L, JIN_GRAPHICS_IMAGE, sizeof(Proxy)); - Texture* img = Texture::createTexture(b.data, b.size); + Image* img = Image::createImage(b.data, b.size); proxy->bind(img, JIN_GRAPHICS_IMAGE); return 1; } @@ -148,7 +157,7 @@ namespace lua if (luax_istype(L, 1, JIN_GRAPHICS_IMAGE)) { Proxy* proxy = (Proxy*)luax_toudata(L, 1); - Texture* tex = (Texture*)proxy->object; + Image* tex = (Image*)proxy->object; tex->draw(x, y, sx, sy, r); } else if (luax_istype(L, 1, JIN_GRAPHICS_CANVAS)) @@ -251,9 +260,9 @@ namespace lua static RENDER_MODE strtomode(const char* str) { std::string s = std::string(str); - if (s == "fill") return FILL; - else if (s == "line") return LINE; - else return NONE; + if (s == "fill") return RENDER_MODE::FILL; + else if (s == "line") return RENDER_MODE::LINE; + else return RENDER_MODE::NONE; } /** @@ -264,7 +273,7 @@ namespace lua { int x = luax_checknumber(L, 1); int y = luax_checknumber(L, 2); - point(x, y); + jin::graphics::point(x, y); return 0; } @@ -275,7 +284,7 @@ namespace lua int y1 = luax_checknumber(L, 2); int x2 = luax_checknumber(L, 3); int y2 = luax_checknumber(L, 4); - line(x1, y1, x2, y2); + jin::graphics::line(x1, y1, x2, y2); return 0; } @@ -284,7 +293,7 @@ namespace lua { const char* modestr = luax_checkstring(L, 1); RENDER_MODE mode = strtomode(modestr); - if (mode != NONE) + if (mode != RENDER_MODE::NONE) { int x = luax_checknumber(L, 2); int y = luax_checknumber(L, 3); @@ -305,7 +314,7 @@ namespace lua { const char* modestr = luax_checkstring(L, 1); RENDER_MODE mode = strtomode(modestr); - if (mode != NONE) + if (mode != RENDER_MODE::NONE) { int x = luax_checknumber(L, 2); int y = luax_checknumber(L, 3); @@ -325,7 +334,7 @@ namespace lua { const char* modestr = luax_checkstring(L, 1); RENDER_MODE mode = strtomode(modestr); - if (mode != NONE) + if (mode != RENDER_MODE::NONE) { int x = luax_checknumber(L, 2); int y = luax_checknumber(L, 3); @@ -356,7 +365,7 @@ namespace lua const char* modestr = luax_checkstring(L, 1); int n = luax_checknumber(L, 2); RENDER_MODE mode = strtomode(modestr); - if (mode != NONE) + if (mode != RENDER_MODE::NONE) { if (!luax_istable(L, 3)) { @@ -389,18 +398,21 @@ namespace lua static int l_newFont(lua_State* L) { - Font* font = (Font*)luax_newinstance(L, JIN_GRAPHICS_FONT, sizeof(Font)); - const char* path = luax_checkstring(L, 1); - Filesystem* fs = Filesystem::get(); - Buffer b = {}; - if (!fs->exists(path)) + Proxy* proxy = (Proxy*)luax_newinstance(L, JIN_GRAPHICS_FONT, sizeof(Proxy)); + Font* font = new Font(); { - printf("Error: no such font %s\n", path); - exit(1); + const char* path = luax_checkstring(L, 1); + Filesystem* fs = Filesystem::get(); + Buffer b = {}; + if (!fs->exists(path)) + { + printf("Error: no such font %s\n", path); + exit(1); + } + fs->read(path, &b); + font->loadb((const unsigned char*)b.data); } - fs->read(path, &b); - font->loadb((const unsigned char*)b.data); - + proxy->bind(font, JIN_GRAPHICS_FONT); return 1; } diff --git a/src/lua/luaopen_types.h b/src/lua/luaopen_types.h index 587c023..74c658f 100644 --- a/src/lua/luaopen_types.h +++ b/src/lua/luaopen_types.h @@ -59,6 +59,9 @@ namespace lua public: void bind(Object* obj, const char* t) { + if (obj == nullptr) + return; + obj->retain(); object = obj; type = t; } diff --git a/src/lua/net/lua_net_Buffer.h b/src/lua/net/Buffer.h index 6af3078..035da5c 100644 --- a/src/lua/net/lua_net_Buffer.h +++ b/src/lua/net/Buffer.h @@ -16,15 +16,15 @@ namespace net { public: Buffer(size_t s = 0) + : size(s) { - size = s; buffer = new char[size]; memset(buffer, 0, size); } Buffer(const char* data, size_t s) + : size(s) { - size = s; buffer = new char[size]; memcpy(buffer, data, size); } diff --git a/src/lua/net/Socket.h b/src/lua/net/Socket.h new file mode 100644 index 0000000..5834092 --- /dev/null +++ b/src/lua/net/Socket.h @@ -0,0 +1,77 @@ +#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 +{ + + typedef jin::net::SocketInformation SocketInformation; + typedef jin::net::SocketType SocketType; + + class Socket : public Object + { + public: + Socket() {} + + Socket(SocketInformation info) + { + socket = new jin::net::Socket(info); + } + + void configureBlocking(bool blocking) + { + socket->configureBlocking(blocking); + } + + Socket* accept() + { + Socket* client = new Socket(); + client->socket = socket->accept(); + return client; + } + + int receive(char* buffer, int size) + { + return socket->receive(buffer, size); + } + + int send(char* buffer, int size) + { + return socket->send(buffer, size); + } + + void sendTo(char* buffer, int size, unsigned int address, unsigned int port) + { + socket->sendTo(buffer, size, address, port); + } + + int receiveFrom(char* buffer, int size, unsigned int address, unsigned int port) + { + return socket->receiveFrom(buffer, size, address, port); + } + + void close() + { + socket->close(); + } + + private: + jin::net::Socket* socket; + + ~Socket() + { + delete socket; + } + + }; + +} // net +} // lua +} // jin + +#endif
\ No newline at end of file diff --git a/src/lua/net/lua_net_Socket.h b/src/lua/net/lua_net_Socket.h deleted file mode 100644 index e02fb5b..0000000 --- a/src/lua/net/lua_net_Socket.h +++ /dev/null @@ -1,64 +0,0 @@ -#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 68e377b..0ab47cb 100644 --- a/src/lua/net/luaopen_Buffer.cpp +++ b/src/lua/net/luaopen_Buffer.cpp @@ -1,7 +1,7 @@ #include "lua/luax.h" #include "../luaopen_types.h" #include "libjin/jin.h" -#include "lua_net_Buffer.h" +#include "Buffer.h" namespace jin { @@ -111,7 +111,7 @@ namespace net static int l_gc(lua_State* L) { Proxy* proxy = (Proxy*)luax_checktype(L, 1, JIN_NETWORK_BUFFER); - proxy->release<Buffer>(); + proxy->release(); return 0; } diff --git a/src/lua/net/luaopen_Socket.cpp b/src/lua/net/luaopen_Socket.cpp index 327cda1..abcd8f2 100644 --- a/src/lua/net/luaopen_Socket.cpp +++ b/src/lua/net/luaopen_Socket.cpp @@ -1,8 +1,8 @@ #include "lua/luax.h" #include "../luaopen_types.h" #include "libjin/jin.h" -#include "lua_net_Buffer.h" -#include "lua_net_Socket.h" +#include "Buffer.h" +#include "Socket.h" namespace jin { @@ -35,7 +35,6 @@ 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; } @@ -48,7 +47,6 @@ 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; } @@ -63,7 +61,6 @@ 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; } @@ -112,7 +109,7 @@ namespace lua } static const luaL_Reg socket_function[] = { - { "__gc", l_gc }, + { "__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 28313ce..13d24dd 100644 --- a/src/lua/net/luaopen_net.cpp +++ b/src/lua/net/luaopen_net.cpp @@ -1,8 +1,8 @@ #include "lua/luax.h" #include "libjin/jin.h" #include "../luaopen_types.h" -#include "lua_net_Buffer.h" -#include "lua_net_Socket.h" +#include "Buffer.h" +#include "Socket.h" namespace jin { diff --git a/src/lua/thread/Thread.cpp b/src/lua/thread/Thread.cpp new file mode 100644 index 0000000..8df3255 --- /dev/null +++ b/src/lua/thread/Thread.cpp @@ -0,0 +1,14 @@ +#include "Thread.h" + +namespace jin +{ +namespace lua +{ +namespace thread +{ + + + +} // thread +} // lua +} // jin
\ No newline at end of file diff --git a/src/lua/thread/Thread.h b/src/lua/thread/Thread.h new file mode 100644 index 0000000..63f7524 --- /dev/null +++ b/src/lua/thread/Thread.h @@ -0,0 +1,94 @@ +#include "libjin/jin.h" +#include "../luaopen_types.h" + +namespace jin +{ +namespace lua +{ +namespace thread +{ + + class Thread : public Object + { + public: + typedef jin::thread::Thread::Variant Variant; + + Thread(std::string _name, std::string _code, jin::thread::Thread::ThreadRunner runner) + : name(_name) + , code(_code) + { + thread = new jin::thread::Thread(_name, runner); + } + + bool start() + { + return thread->start(); + } + + void wait() + { + thread->wait(); + } + + void send(int slot, const Variant& value) + { + thread->send(slot, value); + } + + bool receive(int slot) + { + return thread->receive(slot); + } + + Variant fetch(int slot) + { + return thread->fetch(slot); + } + + Variant demand(int slot) + { + return thread->demand(slot); + } + + void remove(int slot) + { + thread->remove(slot); + } + + const char* getName() + { + return name.c_str(); + } + + bool isRunning() + { + return thread->isRunning(); + } + + void lock() + { + thread->lock(); + } + + void unlock() + { + thread->unlock(); + } + + static void threadRunner(jin::thread::Thread* t); + + private: + ~Thread() + { + delete thread; + } + + jin::thread::Thread* thread; + + const std::string name; + const std::string code; + }; + +} // thread +} // 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 4eabc07..70a30ee 100644 --- a/src/lua/thread/luaopen_Thread.cpp +++ b/src/lua/thread/luaopen_Thread.cpp @@ -2,266 +2,246 @@ #include "libjin/jin.h" #include "../luaopen_jin.h" #include "../luaopen_types.h" +#include "Thread.h" namespace jin { - namespace lua +namespace lua +{ + + using thread::Thread; + + int luaopen_thread(lua_State* L); + + static inline Thread* checkThread(lua_State* L) + { + Proxy* proxy = (Proxy*)luax_checktype(L, 1, JIN_THREAD_THREAD); + if (proxy != nullptr) + return (Thread*)proxy->object; + return nullptr; + } + + 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, 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); + } + + static int l_thread_gc(lua_State* L) { + Proxy* proxy = (Proxy*)luax_checktype(L, 1, JIN_THREAD_THREAD); + proxy->release(); + return 0; + } - int luaopen_thread(lua_State* L); + static int l_start(lua_State* L) + { + Thread* t = checkThread(L); + bool result = t->start(); + luax_pushboolean(L, result); + return 1; + } - class Thread : public jin::thread::Thread - , public Object - { - public: - Thread(std::string _name, std::string _code, jin::thread::Thread::ThreadRunner runner) - : jin::thread::Thread(name, runner) - , name(_name) - , code(_code) - { - } - static void threadRunner(jin::thread::Thread* t); - - private: - ~Thread() - {} - - const std::string name; - const std::string code; - }; - - static inline Thread* checkThread(lua_State* L) - { - Proxy* proxy = (Proxy*)luax_checktype(L, 1, JIN_THREAD_THREAD); - if (proxy != nullptr) - return (Thread*)proxy->object; - return nullptr; - } + static int l_wait(lua_State* L) + { + Thread* t = checkThread(L); + t->wait(); + return 0; + } - void Thread::threadRunner(jin::thread::Thread* t) + static int l_send(lua_State* L) + { + Thread* t = checkThread(L); + int slot = luax_checkinteger(L, 2); + const int vp = 3; + if (luax_isnumberstrict(L, vp)) { - 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, 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); + float real = luax_checknumber(L, vp); + t->send(slot, real); } - - static int l_thread_gc(lua_State* L) + else if (luax_isbooleanstrict(L, vp)) { - Proxy* proxy = (Proxy*)luax_checktype(L, 1, JIN_THREAD_THREAD); - proxy->release(); - return 0; + bool bol = luax_checkbool(L, vp); + t->send(slot, bol); } - - static int l_start(lua_State* L) + else if (luax_isstringstrict(L, vp)) { - Thread* t = checkThread(L); - bool result = t->start(); - luax_pushboolean(L, result); - return 1; + const char* str = luax_checkstring(L, vp); + t->send(slot, str); } - - static int l_wait(lua_State* L) + else if (luax_isuserdata(L, vp)) { - Thread* t = checkThread(L); - t->wait(); - return 0; + void* p = luax_touserdata(L, vp); + t->send(slot, p); } - - static int l_send(lua_State* L) + else if (luax_islightuserdata(L, vp)) { - Thread* t = checkThread(L); - int slot = luax_checkinteger(L, 2); - const int vp = 3; - if (luax_isnumberstrict(L, vp)) - { - float real = luax_checknumber(L, vp); - t->send(slot, real); - } - else if (luax_isbooleanstrict(L, vp)) - { - bool bol = luax_checkbool(L, vp); - t->send(slot, bol); - } - else if (luax_isstringstrict(L, vp)) - { - 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; + void* p = luax_tolightuserdata(L, vp); + t->send(slot, p); } + return 0; + } - static int l_receive(lua_State* L) - { - Thread* t = checkThread(L); - int slot = luax_checkinteger(L, 2); - bool result = t->receive(slot); - luax_pushboolean(L, result); - return 1; - } + static int l_receive(lua_State* L) + { + Thread* t = checkThread(L); + int slot = luax_checkinteger(L, 2); + bool result = t->receive(slot); + luax_pushboolean(L, result); + return 1; + } - static int l_fetch(lua_State* L) + static int l_fetch(lua_State* L) + { + Thread* t = checkThread(L); + int slot = luax_checkinteger(L, 2); + Thread::Variant v = t->fetch(slot); + switch (v.type) { - Thread* t = checkThread(L); - int slot = luax_checkinteger(L, 2); - Thread::Variant v = t->fetch(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; - - 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; - - } - return 1; - } + case thread::Thread::Variant::INTERGER: + luax_pushinteger(L, v.integer); + break; - 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; - - 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; - - } - return 1; - } + case thread::Thread::Variant::BOOLEAN: + luax_pushboolean(L, v.boolean); + break; - static int l_remove(lua_State* L) - { - Thread* t = checkThread(L); - int slot = luax_checkinteger(L, 1); - t->remove(slot); - return 0; - } + case thread::Thread::Variant::CSTRING: + luax_pushstring(L, v.cstring); + break; + + 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; - static int l_getName(lua_State* L) - { - Thread* t = checkThread(L); - const char* name = t->getName(); - luax_pushstring(L, name); - return 1; } + return 1; + } - static int l_isRunning(lua_State* L) + 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) { - Thread* t = checkThread(L); - bool running = t->isRunning(); - luax_pushboolean(L, running); - return 1; + 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; + + 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; + } - static const luaL_Reg thread_function[] = { - { "__gc", l_thread_gc }, - { "start", l_start }, - { "wait", l_wait }, - { "send", l_send }, - { "receive", l_receive }, - { "fetch", l_fetch }, - { "demand", l_demand }, - { "remove", l_remove }, - { "getName", l_getName }, - { "isRunning", l_isRunning }, - { 0, 0 } - }; + 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; + } + + static const luaL_Reg thread_function[] = { + { "__gc", l_thread_gc }, + { "start", l_start }, + { "wait", l_wait }, + { "send", l_send }, + { "receive", l_receive }, + { "fetch", l_fetch }, + { "demand", l_demand }, + { "remove", l_remove }, + { "getName", l_getName }, + { "isRunning", l_isRunning }, + { 0, 0 } + }; - static int luaopen_Thread(lua_State* L) - { - luax_newtype(L, JIN_THREAD_THREAD, thread_function); + static int luaopen_Thread(lua_State* L) + { + luax_newtype(L, JIN_THREAD_THREAD, thread_function); - return 0; - } + return 0; + } - // jin.thread.Thread(name) - static int l_newThread(lua_State* L) - { - 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->retain(); - proxy->bind(thread, JIN_THREAD_THREAD); - return 1; - } + // jin.thread.Thread(name) + static int l_newThread(lua_State* L) + { + 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); + proxy->bind(thread, JIN_THREAD_THREAD); + return 1; + } - static int l_getThread(lua_State* L) - { - luax_getglobal(L, MODULE_NAME); - luax_getfield(L, -1, "_curThread"); - 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[] = { - { "Thread", l_newThread}, - { "getThread", l_getThread}, - { 0, 0 } - }; + static const luaL_Reg f[] = { + { "Thread", l_newThread}, + { "getThread", l_getThread}, + { 0, 0 } + }; - int luaopen_thread(lua_State* L) - { - luaopen_Thread(L); + int luaopen_thread(lua_State* L) + { + luaopen_Thread(L); - luax_newlib(L, f); + luax_newlib(L, f); - return 1; - } + 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 4eabc07..70a30ee 100644 --- a/src/lua/thread/luaopen_thread.cpp +++ b/src/lua/thread/luaopen_thread.cpp @@ -2,266 +2,246 @@ #include "libjin/jin.h" #include "../luaopen_jin.h" #include "../luaopen_types.h" +#include "Thread.h" namespace jin { - namespace lua +namespace lua +{ + + using thread::Thread; + + int luaopen_thread(lua_State* L); + + static inline Thread* checkThread(lua_State* L) + { + Proxy* proxy = (Proxy*)luax_checktype(L, 1, JIN_THREAD_THREAD); + if (proxy != nullptr) + return (Thread*)proxy->object; + return nullptr; + } + + 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, 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); + } + + static int l_thread_gc(lua_State* L) { + Proxy* proxy = (Proxy*)luax_checktype(L, 1, JIN_THREAD_THREAD); + proxy->release(); + return 0; + } - int luaopen_thread(lua_State* L); + static int l_start(lua_State* L) + { + Thread* t = checkThread(L); + bool result = t->start(); + luax_pushboolean(L, result); + return 1; + } - class Thread : public jin::thread::Thread - , public Object - { - public: - Thread(std::string _name, std::string _code, jin::thread::Thread::ThreadRunner runner) - : jin::thread::Thread(name, runner) - , name(_name) - , code(_code) - { - } - static void threadRunner(jin::thread::Thread* t); - - private: - ~Thread() - {} - - const std::string name; - const std::string code; - }; - - static inline Thread* checkThread(lua_State* L) - { - Proxy* proxy = (Proxy*)luax_checktype(L, 1, JIN_THREAD_THREAD); - if (proxy != nullptr) - return (Thread*)proxy->object; - return nullptr; - } + static int l_wait(lua_State* L) + { + Thread* t = checkThread(L); + t->wait(); + return 0; + } - void Thread::threadRunner(jin::thread::Thread* t) + static int l_send(lua_State* L) + { + Thread* t = checkThread(L); + int slot = luax_checkinteger(L, 2); + const int vp = 3; + if (luax_isnumberstrict(L, vp)) { - 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, 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); + float real = luax_checknumber(L, vp); + t->send(slot, real); } - - static int l_thread_gc(lua_State* L) + else if (luax_isbooleanstrict(L, vp)) { - Proxy* proxy = (Proxy*)luax_checktype(L, 1, JIN_THREAD_THREAD); - proxy->release(); - return 0; + bool bol = luax_checkbool(L, vp); + t->send(slot, bol); } - - static int l_start(lua_State* L) + else if (luax_isstringstrict(L, vp)) { - Thread* t = checkThread(L); - bool result = t->start(); - luax_pushboolean(L, result); - return 1; + const char* str = luax_checkstring(L, vp); + t->send(slot, str); } - - static int l_wait(lua_State* L) + else if (luax_isuserdata(L, vp)) { - Thread* t = checkThread(L); - t->wait(); - return 0; + void* p = luax_touserdata(L, vp); + t->send(slot, p); } - - static int l_send(lua_State* L) + else if (luax_islightuserdata(L, vp)) { - Thread* t = checkThread(L); - int slot = luax_checkinteger(L, 2); - const int vp = 3; - if (luax_isnumberstrict(L, vp)) - { - float real = luax_checknumber(L, vp); - t->send(slot, real); - } - else if (luax_isbooleanstrict(L, vp)) - { - bool bol = luax_checkbool(L, vp); - t->send(slot, bol); - } - else if (luax_isstringstrict(L, vp)) - { - 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; + void* p = luax_tolightuserdata(L, vp); + t->send(slot, p); } + return 0; + } - static int l_receive(lua_State* L) - { - Thread* t = checkThread(L); - int slot = luax_checkinteger(L, 2); - bool result = t->receive(slot); - luax_pushboolean(L, result); - return 1; - } + static int l_receive(lua_State* L) + { + Thread* t = checkThread(L); + int slot = luax_checkinteger(L, 2); + bool result = t->receive(slot); + luax_pushboolean(L, result); + return 1; + } - static int l_fetch(lua_State* L) + static int l_fetch(lua_State* L) + { + Thread* t = checkThread(L); + int slot = luax_checkinteger(L, 2); + Thread::Variant v = t->fetch(slot); + switch (v.type) { - Thread* t = checkThread(L); - int slot = luax_checkinteger(L, 2); - Thread::Variant v = t->fetch(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; - - 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; - - } - return 1; - } + case thread::Thread::Variant::INTERGER: + luax_pushinteger(L, v.integer); + break; - 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; - - 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; - - } - return 1; - } + case thread::Thread::Variant::BOOLEAN: + luax_pushboolean(L, v.boolean); + break; - static int l_remove(lua_State* L) - { - Thread* t = checkThread(L); - int slot = luax_checkinteger(L, 1); - t->remove(slot); - return 0; - } + case thread::Thread::Variant::CSTRING: + luax_pushstring(L, v.cstring); + break; + + 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; - static int l_getName(lua_State* L) - { - Thread* t = checkThread(L); - const char* name = t->getName(); - luax_pushstring(L, name); - return 1; } + return 1; + } - static int l_isRunning(lua_State* L) + 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) { - Thread* t = checkThread(L); - bool running = t->isRunning(); - luax_pushboolean(L, running); - return 1; + 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; + + 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; + } - static const luaL_Reg thread_function[] = { - { "__gc", l_thread_gc }, - { "start", l_start }, - { "wait", l_wait }, - { "send", l_send }, - { "receive", l_receive }, - { "fetch", l_fetch }, - { "demand", l_demand }, - { "remove", l_remove }, - { "getName", l_getName }, - { "isRunning", l_isRunning }, - { 0, 0 } - }; + 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; + } + + static const luaL_Reg thread_function[] = { + { "__gc", l_thread_gc }, + { "start", l_start }, + { "wait", l_wait }, + { "send", l_send }, + { "receive", l_receive }, + { "fetch", l_fetch }, + { "demand", l_demand }, + { "remove", l_remove }, + { "getName", l_getName }, + { "isRunning", l_isRunning }, + { 0, 0 } + }; - static int luaopen_Thread(lua_State* L) - { - luax_newtype(L, JIN_THREAD_THREAD, thread_function); + static int luaopen_Thread(lua_State* L) + { + luax_newtype(L, JIN_THREAD_THREAD, thread_function); - return 0; - } + return 0; + } - // jin.thread.Thread(name) - static int l_newThread(lua_State* L) - { - 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->retain(); - proxy->bind(thread, JIN_THREAD_THREAD); - return 1; - } + // jin.thread.Thread(name) + static int l_newThread(lua_State* L) + { + 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); + proxy->bind(thread, JIN_THREAD_THREAD); + return 1; + } - static int l_getThread(lua_State* L) - { - luax_getglobal(L, MODULE_NAME); - luax_getfield(L, -1, "_curThread"); - 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[] = { - { "Thread", l_newThread}, - { "getThread", l_getThread}, - { 0, 0 } - }; + static const luaL_Reg f[] = { + { "Thread", l_newThread}, + { "getThread", l_getThread}, + { 0, 0 } + }; - int luaopen_thread(lua_State* L) - { - luaopen_Thread(L); + int luaopen_thread(lua_State* L) + { + luaopen_Thread(L); - luax_newlib(L, f); + luax_newlib(L, f); - return 1; - } + return 1; } + +} }
\ No newline at end of file |