diff options
Diffstat (limited to 'src')
34 files changed, 290 insertions, 1042 deletions
diff --git a/src/lua/audio/Audio.h b/src/lua/audio/Audio.h deleted file mode 100644 index 3d22a90..0000000 --- a/src/lua/audio/Audio.h +++ /dev/null @@ -1,19 +0,0 @@ -#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/luaopen_Source.cpp b/src/lua/audio/luaopen_Source.cpp index 8881620..b5db88f 100644 --- a/src/lua/audio/luaopen_Source.cpp +++ b/src/lua/audio/luaopen_Source.cpp @@ -1,86 +1,84 @@ #include "lua/luax.h" #include "../luaopen_types.h" -#include "Source.h" +#include "libjin/jin.h" namespace jin { namespace lua { - using audio::Source; + using namespace jin::audio; - static inline Source* checkSource(lua_State* L) + static inline Ref<Source>& checkSource(lua_State* L) { Proxy* proxy = (Proxy*)luax_checktype(L, 1, JIN_AUDIO_SOURCE); - if (proxy != 0 && proxy != nullptr) - return (Source*)proxy->object; - return nullptr; + return proxy->getRef<Source>(); } static int l_play(lua_State* L) { - Source* src = checkSource(L); - src->play(); + Ref<Source>& ref = checkSource(L); + (*ref).play(); return 0; } static int l_stop(lua_State* L) { - Source* src = checkSource(L); - src->stop(); + Ref<Source>& ref = checkSource(L); + (*ref).stop(); return 0; } static int l_pause(lua_State* L) { - Source* src = checkSource(L); - src->pause(); + Ref<Source>& ref = checkSource(L); + (*ref).pause(); return 0; } static int l_rewind(lua_State* L) { - Source* src = checkSource(L); - src->rewind(); + Ref<Source>& ref = checkSource(L); + (*ref).rewind(); return 0; } static int l_resume(lua_State* L) { - Source* src = checkSource(L); - src->resume(); + Ref<Source>& ref = checkSource(L); + (*ref).resume(); return 0; } static int l_isStop(lua_State* L) { - Source* src = checkSource(L); - bool isStop = src->isStopped(); + Ref<Source>& ref = checkSource(L); + bool isStop = (*ref).isStopped(); luax_pushboolean(L, isStop); return 1; } static int l_isPaused(lua_State* L) { - Source* src = checkSource(L); - bool isPaused = src->isPaused(); + Ref<Source>& ref = checkSource(L); + bool isPaused = (*ref).isPaused(); luax_pushboolean(L, isPaused); return 1; } static int l_setVolume(lua_State* L) { - Source* src = checkSource(L); + Ref<Source>& ref = checkSource(L); float volume = luax_checknumber(L, 2); - src->setVolume(volume); + (*ref).setVolume(volume); return 0; } static int l_setLoop(lua_State* L) { - Source* src = checkSource(L); + Ref<Source>& ref = checkSource(L); bool loop = luax_checkbool(L, 2); - src->setLoop(loop); + (*ref).setLoop(loop); return 0; } diff --git a/src/lua/audio/luaopen_audio.cpp b/src/lua/audio/luaopen_audio.cpp index 2bb2f3a..5edfe7b 100644 --- a/src/lua/audio/luaopen_audio.cpp +++ b/src/lua/audio/luaopen_audio.cpp @@ -1,18 +1,21 @@ #include "lua/luax.h" #include "../luaopen_types.h" -#include "Source.h" -#include "Audio.h" +#include "libjin/jin.h" namespace jin { namespace lua { - using namespace jin::filesystem; - using namespace jin::lua::audio; + + using namespace jin::audio; + using namespace jin::filesystem; + + typedef SDLAudio Audio; + typedef SDLSource Source; static int l_init(lua_State* L) { - Audio::Setting setting; + Audio::Setting setting; setting.samplerate = 44100; setting.samples = 44100; if (!Audio::get()->init(&setting)) @@ -69,7 +72,8 @@ namespace lua fs->read(f, &b); Proxy* proxy = (Proxy*)luax_newinstance(L, JIN_AUDIO_SOURCE, sizeof(Proxy)); Source* src = Source::createSource(b.data, b.size); - proxy->bind(src, JIN_AUDIO_SOURCE); + Ref<Source>* ref = new Ref<Source>(src); + proxy->bind(ref, JIN_AUDIO_SOURCE); return 1; } diff --git a/src/lua/embed/boot.lua b/src/lua/embed/boot.lua deleted file mode 100644 index e649737..0000000 --- a/src/lua/embed/boot.lua +++ /dev/null @@ -1,118 +0,0 @@ -jin._argv[2] = jin._argv[2] or '.' -jin.filesystem.init() -jin.filesystem.mount(jin._argv[2]) - -local conf = {} -if jin.filesystem.exist("config.lua") then - conf = require "config" -end -conf.width = conf.width or 600 -conf.height = conf.height or 500 -conf.fps = conf.fps or 60 -conf.vsync = conf.vsync or false -conf.title = conf.title or ("jin v" .. jin.version()) -conf.resizable = conf.resizable or false -conf.fullscreen = conf.fullscreen or false - --- initialize subsystems -jin.graphics.init(conf) -jin.audio.init() - --- open debug mode, must after jin.graphics.init -if jin._argv[3] == '-d' then - jin.debug.init() -end - -local function safecall(func, ...) - if func then - func(...) - end -end - -function jin.core.run() - safecall(jin.core.onLoad) - local previous = jin.time.second() - local SEC_PER_UPDATE = 1 / conf.fps - local dt = SEC_PER_UPDATE - local running = true - while(jin.core.running()) do - for _, e in pairs(jin.event.poll()) do - if e.type == "keydown" then - jin.keyboard.set(e.key, true) - elseif e.type == "keyup" then - jin.keyboard.set(e.key, false) - end - safecall(jin.core.onEvent, e) - running = jin.core.running() - if not running then break end - end - if not running then break end - - safecall(jin.core.onUpdate, dt) - - jin.graphics.unbind() - jin.graphics.clear() - jin.graphics.color() - jin.graphics.study() - safecall(jin.core.onDraw) - if jin.debug.status() then - jin.debug.render() - end - jin.graphics.present() - - local current = jin.time.second() - dt = current - previous - local wait = SEC_PER_UPDATE - dt - previous = previous + SEC_PER_UPDATE - if wait > 0 then - dt = SEC_PER_UPDATE - jin.time.sleep(wait) - else - previous = current - end - end -end - -local function onError(msg) - local tab = ' ' - print("Error:\n" .. msg) - function jin.core.onEvent(e) - if e.type == 'quit' then - jin.core.stop() - end - end - local ww, wh = jin.graphics.size() - function jin.core.onDraw() - jin.graphics.write("Error: ", 10, 10, 30, 3, 30) - jin.graphics.write(msg, 10, 50) - end -end - -local function boot() - if jin.filesystem.exist("main.lua") then - -- require main game script - xpcall(function() require"main" end, onError) - jin.core.run() - else - -- no game - function jin.core.onEvent(e) - if e.type == 'quit' then - jin.core.stop() - end - end - function jin.core.onDraw() - jin.graphics.clear(111, 134, 125, 255) - local ww, wh = jin.graphics.size() - local fw, fh = jin.graphics.box("no game", 20, 1, 20) - jin.graphics.write("no game", ww /2 - fw / 2, wh * 2/3, 16, 1, 18) - end - jin.core.run() - end - -- quit subsystems - jin.graphics.destroy() - jin.audio.destroy() - -- exit whole game - jin.core.quit() -end - -boot() diff --git a/src/lua/embed/debug.lua b/src/lua/embed/debug.lua deleted file mode 100644 index 76f59ed..0000000 --- a/src/lua/embed/debug.lua +++ /dev/null @@ -1,128 +0,0 @@ ---[[ - for debug purpose - +-------------------+ - |debug msg old | - |... | - |... | - |... | - |debug msg new | - +-------------------+ -]] - -jin.debug = jin.debug or {} - --- render panel -local panel = nil - -local debug = false - --- debug msg buffer -local buffer = {} - --- configure -local bsize = 10 -local fsize = 15 -local lheight = 18 -local alpha = 220 -local margin = 10 - --- refresh buffer or not -local refresh = true - -function jin.debug.init() - debug = true - panel = jin.graphics.Canvas(jin.graphics.size()) -end - --- set buffer size -function jin.debug.size(c) - bsize = c -end - -function jin.debug.print(msg) - if not debug then return end - - msg = tostring(msg) - local tp = type(msg) - if tp ~= "string" and tp ~= "number" then - msg = string.format("print failed, expect string or number but get a %s", tp) - end - - -- remove the first one (old msg) - if #buffer >= bsize then - table.remove(buffer, 1) - end - - buffer[#buffer + 1] = msg - refresh = true -end - --- clear debug buffer -function jin.debug.clear() - buffer = {} -end - -local function getStrHeight(str, lheight) - local h = lheight - if #str == 0 then - h = 0 - end - for i = 1, #str do - local c = string.sub(str, i, i) - if c == '\n' then - h = h + lheight - end - end - return h -end - -local function getBgQuad() - local width, height = 0, 0 - for i = 1, #buffer do - local w, h = jin.graphics.box( buffer[i], fsize, 1, lheight) - height = height + h - if width < w then - width = w - end - end - return width, height -end - --- render to screen -function jin.debug.render() - if not debug then return end - - if refresh then - - jin.graphics.bind(panel) - - jin.graphics.clear(0, 0, 0, 0) - - jin.graphics.study() - - local ww, wh = jin.graphics.size() - local bgw, bgh = getBgQuad() - jin.graphics.color(0, 0, 0, alpha) - jin.graphics.rect("fill", 0, wh - bgh - margin, bgw + margin, bgh + margin) - - jin.graphics.color() - local y = wh - for i = #buffer, 1, -1 do - local msg = buffer[i] - local h = getStrHeight(msg, lheight) - y = y - h - jin.graphics.write(msg, margin / 2, y - margin/ 2, fsize, 1, lheight) - end - - jin.graphics.bind() - - refresh = false - end - - jin.graphics.color() - jin.graphics.draw(panel, 0, 0) -end - -function jin.debug.status() - return debug -end diff --git a/src/lua/embed/graphics.lua b/src/lua/embed/graphics.lua deleted file mode 100644 index 03a891d..0000000 --- a/src/lua/embed/graphics.lua +++ /dev/null @@ -1,6 +0,0 @@ ------------------ --- jin.graphics ------------------ - -jin.graphics = jin.graphics or {} - diff --git a/src/lua/embed/graphics.lua.h b/src/lua/embed/graphics.lua.h index 85cf979..c27baaf 100644 --- a/src/lua/embed/graphics.lua.h +++ b/src/lua/embed/graphics.lua.h @@ -5,4 +5,5 @@ static const char* graphics_lua = R"( ----------------- jin.graphics = jin.graphics or {} + )"; diff --git a/src/lua/embed/keyboard.lua b/src/lua/embed/keyboard.lua deleted file mode 100644 index 08214f8..0000000 --- a/src/lua/embed/keyboard.lua +++ /dev/null @@ -1,16 +0,0 @@ ---[[ - jin.keyboard extension -]] - -jin.keyboard = jin.keyboard or {} - -local keys = {} - -function jin.keyboard.isDown(k) - return keys[k] -end - -function jin.keyboard.set(k, status) - keys[k] = status -end - diff --git a/src/lua/embed/keyboard.lua.h b/src/lua/embed/keyboard.lua.h index 66e3c2a..e892b66 100644 --- a/src/lua/embed/keyboard.lua.h +++ b/src/lua/embed/keyboard.lua.h @@ -16,5 +16,4 @@ function jin.keyboard.set(k, status) keys[k] = status end - )"; diff --git a/src/lua/embed/mouse.lua b/src/lua/embed/mouse.lua deleted file mode 100644 index 9dcd472..0000000 --- a/src/lua/embed/mouse.lua +++ /dev/null @@ -1,15 +0,0 @@ ---[[ - jin.mouse extension -]] - -jin.mouse = jin.mouse or {} - -local button = {} - -function jin.mouse.isDown(btn) - return button[btn] -end - -function jin.mouse.set(btn, status) - button[btn] = status -end diff --git a/src/lua/embed/path.lua b/src/lua/embed/path.lua deleted file mode 100644 index 5b99dd2..0000000 --- a/src/lua/embed/path.lua +++ /dev/null @@ -1,15 +0,0 @@ ---[[ - jin.path extension -]] - -jin.path = jin.path or {} - --- game root directory -jin._root = nil - --- return full path of a given path -function jin.path.full(path) - local root = jin._dir .. '/' .. jin._argv[2] - return root .. '/' .. path -end - diff --git a/src/lua/graphics/Canvas.cpp b/src/lua/graphics/Canvas.cpp deleted file mode 100644 index 1714e77..0000000 --- a/src/lua/graphics/Canvas.cpp +++ /dev/null @@ -1,24 +0,0 @@ -#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(); - } - -} // graphics -} // lua -} // jin
\ No newline at end of file diff --git a/src/lua/graphics/Canvas.h b/src/lua/graphics/Canvas.h deleted file mode 100644 index 4494db4..0000000 --- a/src/lua/graphics/Canvas.h +++ /dev/null @@ -1,60 +0,0 @@ -#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() {} - ~Canvas() - { - delete canvas; - } - jin::graphics::Canvas* canvas; - - }; - -} // graphics -} // lua -} // jin - -#endif // __JIN_LUA_GRAPHICS_CANVAS_H
\ No newline at end of file diff --git a/src/lua/graphics/Color.h b/src/lua/graphics/Color.h deleted file mode 100644 index 4110bc4..0000000 --- a/src/lua/graphics/Color.h +++ /dev/null @@ -1,19 +0,0 @@ -#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 // __JIN_LUA_GRAPHICS_COLOR_H
\ No newline at end of file diff --git a/src/lua/graphics/Font.h b/src/lua/graphics/Font.h deleted file mode 100644 index 3343690..0000000 --- a/src/lua/graphics/Font.h +++ /dev/null @@ -1,52 +0,0 @@ -#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 // __JIN_LUA_GRAPHICS_FONT_H
\ No newline at end of file diff --git a/src/lua/graphics/Image.cpp b/src/lua/graphics/Image.cpp deleted file mode 100644 index 15c576a..0000000 --- a/src/lua/graphics/Image.cpp +++ /dev/null @@ -1,26 +0,0 @@ -#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; - } - -} // graphics -} // lua -} // jin
\ No newline at end of file diff --git a/src/lua/graphics/Image.h b/src/lua/graphics/Image.h deleted file mode 100644 index 0d1d010..0000000 --- a/src/lua/graphics/Image.h +++ /dev/null @@ -1,65 +0,0 @@ -#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() {}; - - ~Image() - { - delete image; - } - - jin::graphics::Texture* image; - - }; - -} // graphics -} // lua -} // jin - -#endif // __JIN_LUA_GRAPHICS_IMAGE_H
\ No newline at end of file diff --git a/src/lua/graphics/JSL.cpp b/src/lua/graphics/JSL.cpp deleted file mode 100644 index 42ec2c8..0000000 --- a/src/lua/graphics/JSL.cpp +++ /dev/null @@ -1,20 +0,0 @@ -#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; - } - -} // graphics -} // lua -} // jin
\ No newline at end of file diff --git a/src/lua/graphics/JSL.h b/src/lua/graphics/JSL.h deleted file mode 100644 index af6c54d..0000000 --- a/src/lua/graphics/JSL.h +++ /dev/null @@ -1,92 +0,0 @@ -#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() {} - - ~JSLProgram() - { - delete jslprogram; - } - - jin::graphics::JSLProgram * jslprogram; - - }; - -} // graphics -} // lua -} // jin - -#endif // __JIN_LUA_GRAPHICS_JSL_H
\ No newline at end of file diff --git a/src/lua/graphics/graphics.h b/src/lua/graphics/graphics.h deleted file mode 100644 index bd9bec1..0000000 --- a/src/lua/graphics/graphics.h +++ /dev/null @@ -1,18 +0,0 @@ -#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 d08b181..7b694e9 100644 --- a/src/lua/graphics/luaopen_Canvas.cpp +++ b/src/lua/graphics/luaopen_Canvas.cpp @@ -1,50 +1,49 @@ #include "lua/luax.h" #include "lua/luaopen_types.h" -#include "Canvas.h" +#include "libjin/jin.h" namespace jin { namespace lua { - using namespace lua::graphics; + using namespace jin::graphics; - static inline Canvas* checkCanvas(lua_State* L) + static inline Ref<Canvas>& checkCanvas(lua_State* L) { Proxy* proxy = (Proxy*)luax_checktype(L, 1, JIN_GRAPHICS_CANVAS); - if (proxy != nullptr) - return (Canvas*)proxy->object; - return nullptr; - } + Ref<Canvas>* ref = (Ref<Canvas>*)proxy->reference; + return *ref; + } static int l_getWidth(lua_State* L) { - Canvas* c = checkCanvas(L); - luax_pushnumber(L, c->getWidth()); + Ref<Canvas>& ref = checkCanvas(L); + luax_pushnumber(L, (*ref).getWidth()); return 1; } static int l_getHeight(lua_State* L) { - Canvas* c = checkCanvas(L); - luax_pushnumber(L, c->getHeight()); + Ref<Canvas>& ref = checkCanvas(L); + luax_pushnumber(L, (*ref).getHeight()); return 1; } static int l_getSize(lua_State* L) { - Canvas* c = checkCanvas(L); - luax_pushnumber(L, c->getWidth()); - luax_pushnumber(L, c->getHeight()); + Ref<Canvas>& ref = checkCanvas(L); + luax_pushnumber(L, (*ref).getWidth()); + luax_pushnumber(L, (*ref).getHeight()); return 2; } static int l_setAnchor(lua_State* L) { - Canvas* c = checkCanvas(L); + Ref<Canvas>& ref = checkCanvas(L); int x = luax_checknumber(L, 1); int y = luax_checknumber(L, 2); - c->setAnchor(x, y); + (*ref).setAnchor(x, y); return 0; } diff --git a/src/lua/graphics/luaopen_Font.cpp b/src/lua/graphics/luaopen_Font.cpp index 8585d46..341af87 100644 --- a/src/lua/graphics/luaopen_Font.cpp +++ b/src/lua/graphics/luaopen_Font.cpp @@ -19,13 +19,13 @@ namespace lua static int l_box(lua_State* L) { Proxy* proxy = (Proxy*)luax_newinstance(L, JIN_GRAPHICS_FONT, sizeof(Proxy)); - Font* font = (Font*)proxy->object; + Ref<Font>& ref = proxy->getRef<Font>(); const char* text = luax_checkstring(L, 2); int fheight = luax_checknumber(L, 3); int spacing = luax_checknumber(L, 4); int lheight = luax_checknumber(L, 5); int w, h; - font->box(text, fheight, lheight, spacing, &w, &h); + (*ref).box(text, fheight, lheight, spacing, &w, &h); luax_pushnumber(L, w); luax_pushnumber(L, h); return 2; diff --git a/src/lua/graphics/luaopen_Image.cpp b/src/lua/graphics/luaopen_Image.cpp index 8d89a80..b1672d5 100644 --- a/src/lua/graphics/luaopen_Image.cpp +++ b/src/lua/graphics/luaopen_Image.cpp @@ -1,43 +1,42 @@ #include "lua/luax.h" #include "lua/luaopen_types.h" -#include "Image.h" -#include "Color.h" +#include "libjin/jin.h" namespace jin { namespace lua { - using namespace lua::graphics; + using namespace jin::graphics; - static inline Image* checkImage(lua_State* L) + typedef Texture Image; + + static inline Ref<Image>& checkImage(lua_State* L) { Proxy* proxy = (Proxy*)luax_checktype(L, 1, JIN_GRAPHICS_IMAGE); - if (proxy != nullptr) - return (Image*)proxy->object; - return nullptr; + return proxy->getRef<Image>(); } static int l_getWidth(lua_State* L) { - Image* i = checkImage(L); - luax_pushnumber(L, i->getWidth()); + Ref<Image>& ref = checkImage(L); + luax_pushnumber(L, (*ref).getWidth()); return 1; } static int l_getHeight(lua_State *L) { - Image* i = checkImage(L); - luax_pushnumber(L, i->getHeight()); + Ref<Image>& ref = checkImage(L); + luax_pushnumber(L, (*ref).getHeight()); return 1; } static int l_getPixel(lua_State* L) { - Image* i = checkImage(L); + Ref<Image>& ref = checkImage(L); int x = luax_checknumber(L, 2); int y = luax_checknumber(L, 3); - color c = i->getPixel(x, y); + color c = (*ref).getPixel(x, y); luax_pushnumber(L, c.rgba.r); luax_pushnumber(L, c.rgba.g); luax_pushnumber(L, c.rgba.b); @@ -47,18 +46,18 @@ namespace lua static int l_setAnchor(lua_State* L) { - Image* i = checkImage(L); + Ref<Image>& ref = checkImage(L); int x = luax_checknumber(L, 2); int y = luax_checknumber(L, 3); - i->setAnchor(x, y); + (*ref).setAnchor(x, y); return 0; } static int l_getSize(lua_State* L) { - Image* i = checkImage(L); - luax_pushnumber(L, i->getWidth()); - luax_pushnumber(L, i->getHeight()); + Ref<Image>& ref = checkImage(L); + luax_pushnumber(L, (*ref).getWidth()); + luax_pushnumber(L, (*ref).getHeight()); return 2; } diff --git a/src/lua/graphics/luaopen_JSL.cpp b/src/lua/graphics/luaopen_JSL.cpp index 774f2b6..e644c35 100644 --- a/src/lua/graphics/luaopen_JSL.cpp +++ b/src/lua/graphics/luaopen_JSL.cpp @@ -1,23 +1,20 @@ #include "lua/luax.h" #include "lua/luaopen_types.h" -#include "Image.h" -#include "JSL.h" -#include "Canvas.h" -#include "Color.h" +#include "libjin/jin.h" namespace jin { namespace lua { - using namespace lua::graphics; + using namespace jin::graphics; - static inline JSLProgram* checkJSLProgram(lua_State* L) + typedef Texture Image; + + static inline Ref<JSLProgram>& checkJSLProgram(lua_State* L) { Proxy* proxy = (Proxy*)luax_checktype(L, 1, JIN_GRAPHICS_SHADER); - if(proxy != nullptr) - return (JSLProgram*)proxy->object; - return nullptr; + return proxy->getRef<JSLProgram>(); } static enum VARIABLE_TYPE @@ -51,7 +48,7 @@ namespace lua */ static int l_send(lua_State* L) { - JSLProgram* jsl = checkJSLProgram(L); + Ref<JSLProgram>& ref = checkJSLProgram(L); // number Image Texel const char* typestr = luax_checkstring(L, 2); // variable name @@ -64,28 +61,28 @@ namespace lua case NUMBER: { float number = luax_checknumber(L, 4); - jsl->sendFloat(variable, number); + (*ref).sendFloat(variable, number); break; } case IMAGE: { Proxy* proxy = (Proxy*)luax_checktype(L, 4, JIN_GRAPHICS_IMAGE); - Image* tex = (Image*)proxy->object; - jsl->sendImage(variable, tex); + Ref<Image>& tex = proxy->getRef<Image>(); + (*ref).sendTexture(variable, &(*tex)); break; } case CANVAS: { Proxy* proxy = (Proxy*)luax_checktype(L, 4, JIN_GRAPHICS_CANVAS); - Canvas* canvas = (Canvas*)proxy->object; - jsl->sendCanvas(variable, canvas); + Ref<Canvas>& canvas = proxy->getRef<Canvas>(); + (*ref).sendCanvas(variable, &(*canvas)); break; } case VEC2: { float x = luax_checknumber(L, 4); float y = luax_checknumber(L, 5); - jsl->sendVec2(variable, x, y); + (*ref).sendVec2(variable, x, y); break; } case VEC3: @@ -93,7 +90,7 @@ namespace lua float x = luax_checknumber(L, 4); float y = luax_checknumber(L, 5); float z = luax_checknumber(L, 6); - jsl->sendVec3(variable, x, y, z); + (*ref).sendVec3(variable, x, y, z); break; } case VEC4: @@ -102,7 +99,7 @@ namespace lua float y = luax_checknumber(L, 5); float z = luax_checknumber(L, 6); float w = luax_checknumber(L, 7); - jsl->sendVec4(variable, x, y, z, w); + (*ref).sendVec4(variable, x, y, z, w); break; } case COLOR: @@ -112,7 +109,7 @@ namespace lua col.rgba.g = luax_checkinteger(L, 5); col.rgba.b = luax_checkinteger(L, 6); col.rgba.a = luax_checkinteger(L, 7); - jsl->sendColor(variable, &col); + (*ref).sendColor(variable, &col); break; } default: diff --git a/src/lua/graphics/luaopen_graphics.cpp b/src/lua/graphics/luaopen_graphics.cpp index 5b7f848..dc8c9a1 100644 --- a/src/lua/graphics/luaopen_graphics.cpp +++ b/src/lua/graphics/luaopen_graphics.cpp @@ -2,23 +2,17 @@ #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 lua::graphics; - using jin::graphics::Window; + using namespace jin::graphics; using jin::filesystem::Filesystem; using jin::filesystem::Buffer; + typedef Texture Image; + /** * jin.graphics context, storge some module * shared variables. @@ -87,8 +81,8 @@ namespace lua fs->read(f, &b); Proxy* proxy = (Proxy*)luax_newinstance(L, JIN_GRAPHICS_IMAGE, sizeof(Proxy)); - Image* img = Image::createImage(b.data, b.size); - proxy->bind(img, JIN_GRAPHICS_IMAGE); + Image* img = Image::createTexture(b.data, b.size); + proxy->bind(new Ref<Image>(img), JIN_GRAPHICS_IMAGE); return 1; } @@ -101,7 +95,7 @@ namespace lua 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, JIN_GRAPHICS_SHADER); + proxy->bind(new Ref<JSLProgram>(jsl), JIN_GRAPHICS_SHADER); return 1; } @@ -115,7 +109,8 @@ namespace lua int h = luax_checknumber(L, 2); Proxy* proxy = (Proxy*)luax_newinstance(L, JIN_GRAPHICS_CANVAS, sizeof(Proxy)); Canvas* cvs = Canvas::createCanvas(w, h); - proxy->bind(cvs, JIN_GRAPHICS_CANVAS); + Ref<Canvas>* ref = new Ref<Canvas>(cvs); + proxy->bind(ref, JIN_GRAPHICS_CANVAS); return 1; } @@ -157,13 +152,13 @@ namespace lua if (luax_istype(L, 1, JIN_GRAPHICS_IMAGE)) { Proxy* proxy = (Proxy*)luax_toudata(L, 1); - Image* tex = (Image*)proxy->object; + Image* tex = &*proxy->getRef<Image>(); tex->draw(x, y, sx, sy, r); } else if (luax_istype(L, 1, JIN_GRAPHICS_CANVAS)) { Proxy* proxy = (Proxy*)luax_toudata(L, 1); - Canvas* p = (Canvas*)proxy->object; + Canvas* p = &*proxy->getRef<Canvas>(); p->draw(x, y, sx, sy, r); } else @@ -214,8 +209,8 @@ namespace lua return 0; } Proxy* proxy = (Proxy*)luax_checktype(L, 1, JIN_GRAPHICS_CANVAS); - Canvas* c = (Canvas*)proxy->object; - c->bind(); + Ref<Canvas>& ref = proxy->getRef<Canvas>(); + (*ref).bind(); return 0; } @@ -235,7 +230,7 @@ namespace lua if (luax_istype(L, 1, JIN_GRAPHICS_SHADER)) { Proxy* proxy = (Proxy*)luax_toudata(L, 1); - JSLProgram* jsl = (JSLProgram*)proxy->object; + JSLProgram* jsl = &*proxy->getRef<JSLProgram>(); jsl->use(); } else @@ -412,7 +407,8 @@ namespace lua fs->read(path, &b); font->loadb((const unsigned char*)b.data); } - proxy->bind(font, JIN_GRAPHICS_FONT); + Ref<Font>* ref = new Ref<Font>(font); + proxy->bind(ref, JIN_GRAPHICS_FONT); return 1; } diff --git a/src/lua/luaopen_types.h b/src/lua/luaopen_types.h index a685407..b328d44 100644 --- a/src/lua/luaopen_types.h +++ b/src/lua/luaopen_types.h @@ -21,59 +21,87 @@ namespace jin { namespace lua { - - class Object - { - public: - Object() - : count(1) - { - } - int getReferenceCount() const - { - return count; - } - void retain() - { - ++count; - } - void release() - { - if (--count <= 0) - delete this; - } - - protected: - virtual ~Object() = 0 {} + class Reference + { + public: - private: - // reference count - int count; - }; + Reference(void* obj) + : count(1) + , object(obj) + { + } + void retain() + { + ++count; + } + void release() + { + if (--count <= 0) + delete this; + } + + protected: + Reference(const Reference&); + virtual ~Reference() + { + } + void* object; + int count; + + }; + + template<class T> + class Ref : public Reference + { + public: + Ref(T* obj) + : Reference(obj) + { + } + ~Ref() + { + T* obj = (T*)object; + delete obj; + } + T& operator *() + { + T* obj = (T*)object; + return *obj; + } + private: + Ref(const Ref<T>& ref); + }; class Proxy { public: - void bind(Object* obj, const char* t) + void bind(Reference* ref, const char* t) { - if (obj == nullptr) + if (ref == nullptr) return; - object = obj; + reference = ref; type = t; } void release() { - if (object != nullptr) + if (reference != nullptr) { - object->release(); - object = nullptr; + reference->release(); + reference = nullptr; type = nullptr; } } - Object* object; // acctual object binded + template<class T> + Ref<T>& getRef() + { + Ref<T>* ref = (Ref<T>*) reference; + return *ref; + } + + Reference* reference; // acctual object binded const char* type; // type name and metatable name }; diff --git a/src/lua/net/Buffer.h b/src/lua/net/Buffer.h index 035da5c..894de8c 100644 --- a/src/lua/net/Buffer.h +++ b/src/lua/net/Buffer.h @@ -12,7 +12,7 @@ namespace lua namespace net { - class Buffer : public Object + class Buffer { public: Buffer(size_t s = 0) diff --git a/src/lua/net/Socket.h b/src/lua/net/Socket.h deleted file mode 100644 index f679bd1..0000000 --- a/src/lua/net/Socket.h +++ /dev/null @@ -1,77 +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 -{ - - typedef jin::net::SocketInformation SocketInformation; - typedef jin::net::SocketType SocketType; - - class Socket : public Object - { - public: - - Socket(const 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() {} - ~Socket() - { - delete socket; - } - - }; - -} // 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 6d42c86..e7100cc 100644 --- a/src/lua/net/luaopen_Buffer.cpp +++ b/src/lua/net/luaopen_Buffer.cpp @@ -10,24 +10,22 @@ namespace lua namespace net { - static inline Buffer* checkNetBuffer(lua_State* L) + static inline Ref<Buffer>& checkNetBuffer(lua_State* L) { Proxy* proxy = (Proxy*)luax_checktype(L, 1, JIN_NETWORK_BUFFER); - if (proxy != 0 && proxy != nullptr) - return (Buffer*)proxy->object; - return nullptr; + return proxy->getRef<Buffer>(); } // net.Buffer:append(value) -> value_length static int l_append(lua_State* L) { - Buffer* buffer = checkNetBuffer(L); + Ref<Buffer>& ref = checkNetBuffer(L); const int vp = 2; if (luax_isintegerstrict(L, vp)) { int n = luax_checkinteger(L, vp); int size = sizeof(n); - buffer->append(&n, size); + (*ref).append(&n, size); luax_pushinteger(L, size); return 1; } @@ -35,7 +33,7 @@ namespace net { float n = luax_checknumber(L, vp); int size = sizeof(n); - buffer->append(&n, size); + (*ref).append(&n, size); luax_pushinteger(L, size); return 1; } @@ -43,7 +41,7 @@ namespace net { bool n = luax_checkbool(L, vp); int size = sizeof(n); - buffer->append(&n, size); + (*ref).append(&n, size); luax_pushinteger(L, size); return 1; } @@ -51,7 +49,7 @@ namespace net { const char* str = luax_checkstring(L, vp); int size = strlen(str) + 1; - buffer->append(str, size); + (*ref).append(str, size); luax_pushinteger(L, size); return 1; } @@ -65,10 +63,10 @@ namespace net // net.Buffer:grabString(offset) -> string, length static int l_grabString(lua_State* L) { - Buffer* buffer = checkNetBuffer(L); + Ref<Buffer>& ref = checkNetBuffer(L); int offset = luax_checkinteger(L, 2); int len; - const char* str = buffer->grabString(&len, offset); + const char* str = (*ref).grabString(&len, offset); luax_pushstring(L, str); luax_pushinteger(L, len); return 2; @@ -77,10 +75,10 @@ namespace net // net.Buffer:grabInteger(offset) -> integer, length static int l_grabInteger(lua_State* L) { - Buffer* buffer = checkNetBuffer(L); + Ref<Buffer>& ref = checkNetBuffer(L); int offset = luax_checkinteger(L, 2); int len; - int integer = buffer->grabInteger(&len, offset); + int integer = (*ref).grabInteger(&len, offset); luax_pushinteger(L, integer); luax_pushinteger(L, len); return 2; @@ -88,10 +86,10 @@ namespace net static int l_grabFloat(lua_State* L) { - Buffer* buffer = checkNetBuffer(L); + Ref<Buffer>& ref = checkNetBuffer(L); int offset = luax_checkinteger(L, 2); int len; - float floatv = buffer->grabFloat(&len, offset); + float floatv = (*ref).grabFloat(&len, offset); luax_pushnumber(L, floatv); luax_pushinteger(L, len); return 2; @@ -99,10 +97,10 @@ namespace net static int l_grabBoolean(lua_State* L) { - Buffer* buffer = checkNetBuffer(L); + Ref<Buffer>& ref = checkNetBuffer(L); int offset = luax_checkinteger(L, 2); int len; - bool boolean = buffer->grabBoolean(&len, offset); + bool boolean = (*ref).grabBoolean(&len, offset); luax_pushboolean(L, boolean); luax_pushinteger(L, len); return 2; diff --git a/src/lua/net/luaopen_Socket.cpp b/src/lua/net/luaopen_Socket.cpp index 5ccdaa0..40a62b6 100644 --- a/src/lua/net/luaopen_Socket.cpp +++ b/src/lua/net/luaopen_Socket.cpp @@ -2,75 +2,75 @@ #include "../luaopen_types.h" #include "libjin/jin.h" #include "Buffer.h" -#include "Socket.h" namespace jin { namespace lua { - using namespace jin::lua::net; + using namespace jin::net; + using namespace lua::net; const int BUFFER_SIZE = 1024; - static inline Socket* checkSocket(lua_State* L, int pos = 1) + static inline Ref<Socket>& checkSocket(lua_State* L, int pos = 1) { Proxy* proxy = (Proxy*)luax_checktype(L, pos, JIN_NETWORK_SOCKET); - if (proxy != 0 && proxy != nullptr) - return (Socket*)proxy->object; - return nullptr; + return proxy->getRef<Socket>(); } - static inline net::Buffer* checkNetBuffer(lua_State* L, int pos = 1) + static inline Ref<Buffer>& checkNetBuffer(lua_State* L, int pos = 1) { Proxy* proxy = (Proxy*)luax_checktype(L, pos, JIN_NETWORK_BUFFER); - if (proxy != 0 && proxy != nullptr) - return (net::Buffer*)proxy->object; - return nullptr; + return proxy->getRef<Buffer>(); } - // return net.Buffer + // return net.Socket static int l_accept(lua_State* L) { - Socket* socket = checkSocket(L); - Socket* client = socket->accept(); + Ref<Socket>& socket = checkSocket(L); + Socket* client = (*socket).accept(); + Ref<Socket>* ref = new Ref<Socket>(client); Proxy* proxy = (Proxy*)luax_newinstance(L, JIN_NETWORK_SOCKET, sizeof(Proxy)); - proxy->bind(client, JIN_NETWORK_SOCKET); + proxy->bind(ref, JIN_NETWORK_SOCKET); return 1; } // return net.Buffer static int l_receive(lua_State* L) { - Socket* socket = checkSocket(L); + Ref<Socket>& socket = checkSocket(L); char buffer[BUFFER_SIZE] = {0}; - int size = socket->receive(buffer, BUFFER_SIZE); + int size = (*socket).receive(buffer, BUFFER_SIZE); Proxy* proxy = (Proxy*)luax_newinstance(L, JIN_NETWORK_BUFFER, sizeof(Proxy)); net::Buffer* netBuffer = new net::Buffer(buffer, size); - proxy->bind(netBuffer, JIN_NETWORK_BUFFER); + Ref<Buffer>* ref = new Ref<Buffer>(netBuffer); + proxy->bind(ref, JIN_NETWORK_BUFFER); return 1; } // Socket:receiveFrom(address, port) static int l_receiveFrom(lua_State* L) { - Socket* socket = checkSocket(L); + Ref<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); + int size = (*socket).receiveFrom(buffer, BUFFER_SIZE, address, port); net::Buffer* netBuffer = new net::Buffer(buffer, size); + Ref<Buffer>* ref = new Ref<Buffer>(netBuffer); Proxy* proxy = (Proxy*)luax_newinstance(L, JIN_NETWORK_BUFFER, sizeof(Proxy)); - proxy->bind(netBuffer, JIN_NETWORK_BUFFER); + proxy->bind(ref, JIN_NETWORK_BUFFER); return 1; } // Socket:send(net.Buffer) -> data_length static int l_send(lua_State* L) { - Socket* socket = checkSocket(L); - net::Buffer* buffer = checkNetBuffer(L, 2); - int len = socket->send(buffer->buffer, buffer->size); + Ref<Socket>& socket = checkSocket(L); + Ref<Buffer>& ref = checkNetBuffer(L, 2); + net::Buffer* buffer = &*ref; + int len = (*socket).send(buffer->buffer, buffer->size); luax_pushinteger(L, len); return 1; } @@ -78,26 +78,27 @@ namespace lua // Socket:sendTo(address, port, net.Buffer) static int l_sendTo(lua_State* L) { - Socket* socket = checkSocket(L); + Ref<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); + Ref<Buffer>& ref = checkNetBuffer(L, 4); + net::Buffer* buffer = &*ref; + (*socket).sendTo(buffer->buffer, buffer->size, address, port); return 0; } static int l_close(lua_State* L) { - Socket* socket = checkSocket(L); - socket->close(); + Ref<Socket>& socket = checkSocket(L); + (*socket).close(); return 0; } static int l_configBlocking(lua_State* L) { - Socket* socket = checkSocket(L); + Ref<Socket>& socket = checkSocket(L); bool blocking = luax_checkbool(L, 2); - socket->configureBlocking(blocking); + (*socket).configureBlocking(blocking); return 0; } diff --git a/src/lua/net/luaopen_net.cpp b/src/lua/net/luaopen_net.cpp index 7ec9cc7..0fce20f 100644 --- a/src/lua/net/luaopen_net.cpp +++ b/src/lua/net/luaopen_net.cpp @@ -2,13 +2,13 @@ #include "libjin/jin.h" #include "../luaopen_types.h" #include "Buffer.h" -#include "Socket.h" namespace jin { namespace lua { using namespace jin::lua::net; + using namespace jin::net; static int l_initNetwork(lua_State* L) { @@ -47,8 +47,9 @@ namespace lua } } Socket* socket = new Socket(info); + Ref<Socket>* ref = new Ref<Socket>(socket); Proxy* proxy = (Proxy*)luax_newinstance(L, JIN_NETWORK_SOCKET, sizeof(Proxy)); - proxy->bind(socket, JIN_NETWORK_SOCKET); + proxy->bind(ref, JIN_NETWORK_SOCKET); return 1; } @@ -58,7 +59,8 @@ namespace lua int size = luax_checkinteger(L, 1); Proxy* proxy = (Proxy*)luax_newinstance(L, JIN_NETWORK_BUFFER, sizeof(Proxy)); net::Buffer* buffer = new net::Buffer(size); - proxy->bind(buffer, JIN_NETWORK_BUFFER); + Ref<Buffer>* ref = new Ref<Buffer>(buffer); + proxy->bind(ref, JIN_NETWORK_BUFFER); return 1; } diff --git a/src/lua/thread/Thread.h b/src/lua/thread/Thread.h index 6969fd8..4e39c34 100644 --- a/src/lua/thread/Thread.h +++ b/src/lua/thread/Thread.h @@ -8,7 +8,7 @@ namespace lua namespace thread { - class Thread : public Object + class Thread { public: typedef jin::thread::Thread::Variant Variant; @@ -21,6 +21,11 @@ namespace thread thread = new jin::thread::Thread(_name, runner); } + ~Thread() + { + delete thread; + } + bool start(void* p) { return thread->start(p); @@ -76,17 +81,10 @@ namespace thread thread->unlock(); } - static void threadRunner(Thread* t); - const std::string name; const std::string code; - private: - ~Thread() - { - delete thread; - } - + private: jin::thread::Thread* thread; }; diff --git a/src/lua/thread/luaopen_Thread.cpp b/src/lua/thread/luaopen_Thread.cpp index 30eb343..2b4d01d 100644 --- a/src/lua/thread/luaopen_Thread.cpp +++ b/src/lua/thread/luaopen_Thread.cpp @@ -13,30 +13,28 @@ namespace lua int luaopen_thread(lua_State* L); - static inline Thread* checkThread(lua_State* L) + static inline Ref<Thread>& checkThread(lua_State* L) { Proxy* proxy = (Proxy*)luax_checktype(L, 1, JIN_THREAD_THREAD); - if (proxy != nullptr) - return (Thread*)proxy->object; - return nullptr; + return proxy->getRef<Thread>(); } static int threadRunner(void* t) { - Thread* thread = (Thread*)t; + Ref<Thread>& ref = *(Ref<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); + ref.retain(); + proxy->bind(&ref, JIN_THREAD_THREAD); luax_setfield(L, -2, "_curThread"); - luax_dostring(L, thread->code.c_str()); + luax_dostring(L, (*ref).code.c_str()); luax_close(L); return 0; } - + static int l_thread_gc(lua_State* L) { Proxy* proxy = (Proxy*)luax_checktype(L, 1, JIN_THREAD_THREAD); @@ -46,66 +44,66 @@ namespace lua static int l_start(lua_State* L) { - Thread* t = checkThread(L); - bool result = t->start(t); + Ref<Thread>& ref = checkThread(L); + bool result = (*ref).start(&ref); luax_pushboolean(L, result); return 1; } static int l_wait(lua_State* L) { - Thread* t = checkThread(L); - t->wait(); + Ref<Thread>& ref = checkThread(L); + (*ref).wait(); return 0; } static int l_send(lua_State* L) { - Thread* t = checkThread(L); + Ref<Thread>& ref = 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); + (*ref).send(slot, real); } else if (luax_isbooleanstrict(L, vp)) { bool bol = luax_checkbool(L, vp); - t->send(slot, bol); + (*ref).send(slot, bol); } else if (luax_isstringstrict(L, vp)) { const char* str = luax_checkstring(L, vp); - t->send(slot, str); + (*ref).send(slot, str); } else if (luax_isuserdata(L, vp)) { void* p = luax_touserdata(L, vp); - t->send(slot, p); + (*ref).send(slot, p); } else if (luax_islightuserdata(L, vp)) { void* p = luax_tolightuserdata(L, vp); - t->send(slot, p); + (*ref).send(slot, p); } return 0; } static int l_receive(lua_State* L) { - Thread* t = checkThread(L); + Ref<Thread>& ref = checkThread(L); int slot = luax_checkinteger(L, 2); - bool result = t->receive(slot); + bool result = (*ref).receive(slot); luax_pushboolean(L, result); return 1; } static int l_fetch(lua_State* L) { - Thread* t = checkThread(L); + Ref<Thread>& ref = checkThread(L); int slot = luax_checkinteger(L, 2); - Thread::Variant v = t->fetch(slot); + Thread::Variant v = (*ref).fetch(slot); switch (v.type) { case thread::Thread::Variant::INTERGER: @@ -127,8 +125,8 @@ namespace lua case thread::Thread::Variant::POINTER: Proxy* p = (Proxy*)v.pointer; Proxy* proxy = (Proxy*)luax_newinstance(L, p->type, sizeof(Proxy)); - p->object->retain(); - proxy->bind(p->object, p->type); + p->reference->retain(); + proxy->bind(p->reference, p->type); break; } @@ -137,9 +135,9 @@ namespace lua static int l_demand(lua_State* L) { - Thread* t = checkThread(L); + Ref<Thread>& ref = checkThread(L); int slot = luax_checkinteger(L, 2); - Thread::Variant v = t->demand(slot); + Thread::Variant v = (*ref).demand(slot); switch (v.type) { case thread::Thread::Variant::INTERGER: @@ -161,8 +159,8 @@ namespace lua case thread::Thread::Variant::POINTER: Proxy* p = (Proxy*)v.pointer; Proxy* proxy = (Proxy*)luax_newinstance(L, p->type, sizeof(Proxy)); - p->object->retain(); - proxy->bind(p->object, p->type); + p->reference->retain(); + proxy->bind(p->reference, p->type); break; } @@ -171,24 +169,24 @@ namespace lua static int l_remove(lua_State* L) { - Thread* t = checkThread(L); + Ref<Thread>& ref = checkThread(L); int slot = luax_checkinteger(L, 2); - t->remove(slot); + (*ref).remove(slot); return 0; } static int l_getName(lua_State* L) { - Thread* t = checkThread(L); - const char* name = t->getName(); + Ref<Thread>& ref = checkThread(L); + const char* name = (*ref).getName(); luax_pushstring(L, name); return 1; } static int l_isRunning(lua_State* L) { - Thread* t = checkThread(L); - bool running = t->isRunning(); + Ref<Thread>& ref = checkThread(L); + bool running = (*ref).isRunning(); luax_pushboolean(L, running); return 1; } @@ -221,7 +219,8 @@ namespace lua const char* code = luax_checkstring(L, 2); Proxy* proxy = (Proxy*)luax_newinstance(L, JIN_THREAD_THREAD, sizeof(Proxy)); Thread* thread = new Thread(name, code, threadRunner); - proxy->bind(thread, JIN_THREAD_THREAD); + Ref<Thread>* ref = new Ref<Thread>(thread); + proxy->bind(ref, JIN_THREAD_THREAD); return 1; } diff --git a/src/lua/thread/luaopen_thread.cpp b/src/lua/thread/luaopen_thread.cpp index 30eb343..2b4d01d 100644 --- a/src/lua/thread/luaopen_thread.cpp +++ b/src/lua/thread/luaopen_thread.cpp @@ -13,30 +13,28 @@ namespace lua int luaopen_thread(lua_State* L); - static inline Thread* checkThread(lua_State* L) + static inline Ref<Thread>& checkThread(lua_State* L) { Proxy* proxy = (Proxy*)luax_checktype(L, 1, JIN_THREAD_THREAD); - if (proxy != nullptr) - return (Thread*)proxy->object; - return nullptr; + return proxy->getRef<Thread>(); } static int threadRunner(void* t) { - Thread* thread = (Thread*)t; + Ref<Thread>& ref = *(Ref<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); + ref.retain(); + proxy->bind(&ref, JIN_THREAD_THREAD); luax_setfield(L, -2, "_curThread"); - luax_dostring(L, thread->code.c_str()); + luax_dostring(L, (*ref).code.c_str()); luax_close(L); return 0; } - + static int l_thread_gc(lua_State* L) { Proxy* proxy = (Proxy*)luax_checktype(L, 1, JIN_THREAD_THREAD); @@ -46,66 +44,66 @@ namespace lua static int l_start(lua_State* L) { - Thread* t = checkThread(L); - bool result = t->start(t); + Ref<Thread>& ref = checkThread(L); + bool result = (*ref).start(&ref); luax_pushboolean(L, result); return 1; } static int l_wait(lua_State* L) { - Thread* t = checkThread(L); - t->wait(); + Ref<Thread>& ref = checkThread(L); + (*ref).wait(); return 0; } static int l_send(lua_State* L) { - Thread* t = checkThread(L); + Ref<Thread>& ref = 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); + (*ref).send(slot, real); } else if (luax_isbooleanstrict(L, vp)) { bool bol = luax_checkbool(L, vp); - t->send(slot, bol); + (*ref).send(slot, bol); } else if (luax_isstringstrict(L, vp)) { const char* str = luax_checkstring(L, vp); - t->send(slot, str); + (*ref).send(slot, str); } else if (luax_isuserdata(L, vp)) { void* p = luax_touserdata(L, vp); - t->send(slot, p); + (*ref).send(slot, p); } else if (luax_islightuserdata(L, vp)) { void* p = luax_tolightuserdata(L, vp); - t->send(slot, p); + (*ref).send(slot, p); } return 0; } static int l_receive(lua_State* L) { - Thread* t = checkThread(L); + Ref<Thread>& ref = checkThread(L); int slot = luax_checkinteger(L, 2); - bool result = t->receive(slot); + bool result = (*ref).receive(slot); luax_pushboolean(L, result); return 1; } static int l_fetch(lua_State* L) { - Thread* t = checkThread(L); + Ref<Thread>& ref = checkThread(L); int slot = luax_checkinteger(L, 2); - Thread::Variant v = t->fetch(slot); + Thread::Variant v = (*ref).fetch(slot); switch (v.type) { case thread::Thread::Variant::INTERGER: @@ -127,8 +125,8 @@ namespace lua case thread::Thread::Variant::POINTER: Proxy* p = (Proxy*)v.pointer; Proxy* proxy = (Proxy*)luax_newinstance(L, p->type, sizeof(Proxy)); - p->object->retain(); - proxy->bind(p->object, p->type); + p->reference->retain(); + proxy->bind(p->reference, p->type); break; } @@ -137,9 +135,9 @@ namespace lua static int l_demand(lua_State* L) { - Thread* t = checkThread(L); + Ref<Thread>& ref = checkThread(L); int slot = luax_checkinteger(L, 2); - Thread::Variant v = t->demand(slot); + Thread::Variant v = (*ref).demand(slot); switch (v.type) { case thread::Thread::Variant::INTERGER: @@ -161,8 +159,8 @@ namespace lua case thread::Thread::Variant::POINTER: Proxy* p = (Proxy*)v.pointer; Proxy* proxy = (Proxy*)luax_newinstance(L, p->type, sizeof(Proxy)); - p->object->retain(); - proxy->bind(p->object, p->type); + p->reference->retain(); + proxy->bind(p->reference, p->type); break; } @@ -171,24 +169,24 @@ namespace lua static int l_remove(lua_State* L) { - Thread* t = checkThread(L); + Ref<Thread>& ref = checkThread(L); int slot = luax_checkinteger(L, 2); - t->remove(slot); + (*ref).remove(slot); return 0; } static int l_getName(lua_State* L) { - Thread* t = checkThread(L); - const char* name = t->getName(); + Ref<Thread>& ref = checkThread(L); + const char* name = (*ref).getName(); luax_pushstring(L, name); return 1; } static int l_isRunning(lua_State* L) { - Thread* t = checkThread(L); - bool running = t->isRunning(); + Ref<Thread>& ref = checkThread(L); + bool running = (*ref).isRunning(); luax_pushboolean(L, running); return 1; } @@ -221,7 +219,8 @@ namespace lua const char* code = luax_checkstring(L, 2); Proxy* proxy = (Proxy*)luax_newinstance(L, JIN_THREAD_THREAD, sizeof(Proxy)); Thread* thread = new Thread(name, code, threadRunner); - proxy->bind(thread, JIN_THREAD_THREAD); + Ref<Thread>* ref = new Ref<Thread>(thread); + proxy->bind(ref, JIN_THREAD_THREAD); return 1; } |