diff options
Diffstat (limited to 'src/lua/common')
-rw-r--r-- | src/lua/common/Proxy.h | 67 | ||||
-rw-r--r-- | src/lua/common/Reference.hpp | 88 | ||||
-rw-r--r-- | src/lua/common/common.h | 8 | ||||
-rw-r--r-- | src/lua/common/error.h | 28 | ||||
-rw-r--r-- | src/lua/common/je_lua_callback.cpp | 43 | ||||
-rw-r--r-- | src/lua/common/je_lua_callback.h | 67 | ||||
-rw-r--r-- | src/lua/common/je_lua_common.h | 10 | ||||
-rw-r--r-- | src/lua/common/je_lua_constant.h (renamed from src/lua/common/constant.h) | 0 | ||||
-rw-r--r-- | src/lua/common/je_lua_error.h | 28 | ||||
-rw-r--r-- | src/lua/common/je_lua_function.cpp | 0 | ||||
-rw-r--r-- | src/lua/common/je_lua_function.h | 6 | ||||
-rw-r--r-- | src/lua/common/je_lua_port.h | 8 | ||||
-rw-r--r-- | src/lua/common/je_lua_proxy.h | 72 | ||||
-rw-r--r-- | src/lua/common/je_lua_reference.cpp | 31 | ||||
-rw-r--r-- | src/lua/common/je_lua_reference.h | 54 | ||||
-rw-r--r-- | src/lua/common/je_lua_shared.hpp | 151 |
16 files changed, 470 insertions, 191 deletions
diff --git a/src/lua/common/Proxy.h b/src/lua/common/Proxy.h deleted file mode 100644 index 5ebb5b2..0000000 --- a/src/lua/common/Proxy.h +++ /dev/null @@ -1,67 +0,0 @@ -#ifndef __JIN_COMMON_PROXY_H -#define __JIN_COMMON_PROXY_H - -#include "Reference.hpp" - -namespace JinEngine -{ - namespace Lua - { - - class Proxy - { - public: - void bind(RefBase* ref) - { - if (ref == nullptr) - return; - reference = ref; - } - - void release() - { - if (reference != nullptr) - { - reference->release(); - reference = nullptr; - } - } - - void retain() - { - if (reference != nullptr) - reference->retain(); - } - - void setUserdata(void* data) - { - if (reference != nullptr) - reference->setUserdata(data); - } - - template<class T> - Ref<T>& getRef() - { - return *(Ref<T>*) reference; - } - - template<class T> - T* getObject() - { - Ref<T>& ref = getRef<T>(); - return ref.getObject(); - } - - const char* getObjectType() - { - return reference->type; - } - - RefBase* reference; - - }; - - } // namespace Lua -} // namespace JinEngine - -#endif // __JIN_COMMON_PROXY_H
\ No newline at end of file diff --git a/src/lua/common/Reference.hpp b/src/lua/common/Reference.hpp deleted file mode 100644 index ba918bb..0000000 --- a/src/lua/common/Reference.hpp +++ /dev/null @@ -1,88 +0,0 @@ -#ifndef __JIN_COMMON_REFERENCE_H -#define __JIN_COMMON_REFERENCE_H - -namespace JinEngine -{ - namespace Lua - { - - /*abstract*/class RefBase - { - public: - void retain() - { - ++count; - } - - void release() - { - if (--count <= 0) - delete this; - } - - // object type string - const char* const type; - - void setUserdata(void* data) - { - userdata = data; - } - - void* getUserdata() - { - return userdata; - } - - protected: - RefBase(void* obj, const char* t) - : count(1) - , object(obj) - , type(t) - { - } - - RefBase(const RefBase&); - - virtual ~RefBase() - { - } - - void* object; - int count; - void* userdata; - }; - - template<class T> - class Ref : public RefBase - { - public: - Ref(T* obj, const char* type) - : RefBase(obj, type) - { - } - - ~Ref() - { - T* obj = static_cast<T*>(object); - delete obj; - } - - T* operator->() - { - return (T*)object; - } - - T* getObject() - { - return (T*)object; - } - - private: - Ref(const Ref<T>& ref); - - }; - - } -} - -#endif
\ No newline at end of file diff --git a/src/lua/common/common.h b/src/lua/common/common.h deleted file mode 100644 index 0ee72cc..0000000 --- a/src/lua/common/common.h +++ /dev/null @@ -1,8 +0,0 @@ -#ifndef __JIN_M_TYPES_H -#define __JIN_M_TYPES_H - -#include "Proxy.h" -#include "Reference.hpp" -#include "error.h" - -#endif
\ No newline at end of file diff --git a/src/lua/common/error.h b/src/lua/common/error.h deleted file mode 100644 index c254486..0000000 --- a/src/lua/common/error.h +++ /dev/null @@ -1,28 +0,0 @@ -#ifndef __JIN_ERROR_H -#define __JIN_ERROR_H -#include "../../luax.h" -#include "../jin.h" -#include <string.h> - -namespace JinEngine -{ -namespace Lua -{ - - static const int FORMAT_MSG_BUFFER_SIZE = 2048; - - inline void error(lua_State* L, const char* fmt, ...) - { - char err[FORMAT_MSG_BUFFER_SIZE + 1] = { 0 }; - va_list args; - va_start(args, fmt); - vsnprintf(err + strlen(err), FORMAT_MSG_BUFFER_SIZE, fmt, args); - va_end(args); - luax_getglobal(L, MODULE_NAME); - luax_setfieldstring(L, "error", err); - } - -} -} - -#endif
\ No newline at end of file diff --git a/src/lua/common/je_lua_callback.cpp b/src/lua/common/je_lua_callback.cpp new file mode 100644 index 0000000..392f919 --- /dev/null +++ b/src/lua/common/je_lua_callback.cpp @@ -0,0 +1,43 @@ +#include "je_lua_callback.h" + +namespace JinEngine +{ + namespace Lua + { + + LuaCallback::LuaCallback(lua_State* L) + : mLuaFunc(nullptr) + , mParams(0) + , mL(L) + { + } + + LuaCallback::~LuaCallback() + { + delete mLuaFunc; + for (auto p : mParams) + delete p; + } + + void LuaCallback::setFunc(int i) + { + if (mLuaFunc != nullptr) + delete mLuaFunc; + mLuaFunc = new LuaRef(mL, i); + } + + void LuaCallback::pushParam(int i) + { + mParams.push_back(new LuaRef(mL, i)); + } + + void LuaCallback::call() + { + mLuaFunc->push(); + for (auto p : mParams) + p->push(); + luax_call(mL, mParams.size(), 0); + } + + } +}
\ No newline at end of file diff --git a/src/lua/common/je_lua_callback.h b/src/lua/common/je_lua_callback.h new file mode 100644 index 0000000..f3301fc --- /dev/null +++ b/src/lua/common/je_lua_callback.h @@ -0,0 +1,67 @@ +#ifndef __JIN_COMMON_FUNCTION_H +#define __JIN_COMMON_FUNCTION_H + +#include <vector> + +#include "libjin/jin.h" +#include "../luax.h" +#include "je_lua_reference.h" + +namespace JinEngine +{ + namespace Lua + { + + /// + /// + /// + class LuaCallback + { + public: + /// + /// + /// + LuaCallback(lua_State* L); + + /// + /// + /// + ~LuaCallback(); + + /// + /// + /// + void setFunc(int i); + + /// + /// + /// + void pushParam(int i); + + /// + /// + /// + void call(); + + private: + /// + /// + /// + LuaRef* mLuaFunc; + + /// + /// + /// + std::vector<LuaRef*> mParams; + + /// + /// + /// + lua_State* const mL; + + }; + + } // namespace Lua +} // namespace JinEngine + +#endif // __JIN_COMMON_REFERENCE_H
\ No newline at end of file diff --git a/src/lua/common/je_lua_common.h b/src/lua/common/je_lua_common.h new file mode 100644 index 0000000..5b217a2 --- /dev/null +++ b/src/lua/common/je_lua_common.h @@ -0,0 +1,10 @@ +#ifndef __JIN_M_TYPES_H +#define __JIN_M_TYPES_H + +#include "je_lua_port.h" +#include "je_lua_proxy.h" +#include "je_lua_shared.hpp" +#include "je_lua_error.h" +#include "je_lua_reference.h" + +#endif
\ No newline at end of file diff --git a/src/lua/common/constant.h b/src/lua/common/je_lua_constant.h index 6f70f09..6f70f09 100644 --- a/src/lua/common/constant.h +++ b/src/lua/common/je_lua_constant.h diff --git a/src/lua/common/je_lua_error.h b/src/lua/common/je_lua_error.h new file mode 100644 index 0000000..3f7e76f --- /dev/null +++ b/src/lua/common/je_lua_error.h @@ -0,0 +1,28 @@ +#ifndef __JIN_ERROR_H +#define __JIN_ERROR_H +#include "../luax.h" +#include <string.h> + +namespace JinEngine +{ + namespace Lua + { + + static const int FORMAT_MSG_BUFFER_SIZE = 2048; + + inline void error(lua_State* L, const char* fmt, ...) + { + char err[FORMAT_MSG_BUFFER_SIZE + 1] = { 0 }; + va_list args; + va_start(args, fmt); + vsnprintf(err + strlen(err), FORMAT_MSG_BUFFER_SIZE, fmt, args); + va_end(args); + //luax_getglobal(L, "jin"); + //luax_setfieldstring(L, "error", err); + luax_error(L, err); + } + + } // namespace Lua +} // namespace JinEngine + +#endif
\ No newline at end of file diff --git a/src/lua/common/je_lua_function.cpp b/src/lua/common/je_lua_function.cpp new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/src/lua/common/je_lua_function.cpp diff --git a/src/lua/common/je_lua_function.h b/src/lua/common/je_lua_function.h new file mode 100644 index 0000000..49c1b31 --- /dev/null +++ b/src/lua/common/je_lua_function.h @@ -0,0 +1,6 @@ +#ifndef __JE_LUA_FUNCTION_H__ +#define __JE_LUA_FUNCTION_H__ + + + +#endif
\ No newline at end of file diff --git a/src/lua/common/je_lua_port.h b/src/lua/common/je_lua_port.h new file mode 100644 index 0000000..8e99ca4 --- /dev/null +++ b/src/lua/common/je_lua_port.h @@ -0,0 +1,8 @@ +#ifndef __JE_LUA_PORT_H +#define __JE_LUA_PORT_H + +#define LUA_PORT extern +#define LUA_IMPLEMENT static +#define LUA_EXPORT + +#endif
\ No newline at end of file diff --git a/src/lua/common/je_lua_proxy.h b/src/lua/common/je_lua_proxy.h new file mode 100644 index 0000000..ca4a56a --- /dev/null +++ b/src/lua/common/je_lua_proxy.h @@ -0,0 +1,72 @@ +#ifndef __JIN_COMMON_PROXY_H +#define __JIN_COMMON_PROXY_H + +#include "../luax.h" + +#include "je_lua_shared.hpp" + +namespace JinEngine +{ + namespace Lua + { + + class Proxy + { + public: + void bind(SharedBase* s) + { + if (s == nullptr) + return; + shared = s; + shared->retain(); + } + + void release() + { + if (shared != nullptr) + { + shared->release(); + shared = nullptr; + } + } +/* + void retain() + { + if (shared != nullptr) + shared->retain(); + } +*/ + template<class T> + Shared<T>& getShared() + { + return *(Shared<T>*)shared; + } + + /// + /// For convenience. + /// + template<class T> + T* getObject() + { + Shared<T>& shared = getShared<T>(); + return shared.getObject(); + } + + const char* getObjectType() + { + return shared->type; + } + + SharedBase* shared; + + }; + + inline Proxy* luax_newinstance(lua_State* L, const char* type) + { + return static_cast<Proxy*>(luax_newinstance(L, type, sizeof(Proxy))); + } + + } // namespace Lua +} // namespace JinEngine + +#endif // __JIN_COMMON_PROXY_H
\ No newline at end of file diff --git a/src/lua/common/je_lua_reference.cpp b/src/lua/common/je_lua_reference.cpp new file mode 100644 index 0000000..90223de --- /dev/null +++ b/src/lua/common/je_lua_reference.cpp @@ -0,0 +1,31 @@ +#include "je_lua_reference.h" + +namespace JinEngine +{ + namespace Lua + { + + LuaRef::LuaRef(lua_State* L, int i) + : mL(L) + { + luax_pushvalue(mL, i); + mIndex = luax_ref(mL, LUA_REGISTRYINDEX); + } + + LuaRef::~LuaRef() + { + unref(); + } + + void LuaRef::unref() + { + luax_unref(mL, LUA_REGISTRYINDEX, mIndex); + } + + void LuaRef::push() + { + luax_rawgeti(mL, LUA_REGISTRYINDEX, mIndex); + } + + } +}
\ No newline at end of file diff --git a/src/lua/common/je_lua_reference.h b/src/lua/common/je_lua_reference.h new file mode 100644 index 0000000..f338762 --- /dev/null +++ b/src/lua/common/je_lua_reference.h @@ -0,0 +1,54 @@ +#ifndef __JIN_COMMON_REFERENCE_H +#define __JIN_COMMON_REFERENCE_H + +#include "../luax.h" + +namespace JinEngine +{ + namespace Lua + { + + /// + /// This class wraps the reference functionality built into Lua, which allows C++ code to refer to Lua + /// variables. + /// + class LuaRef + { + public: + /// + /// + /// + LuaRef(lua_State* L, int i); + + /// + /// + /// + ~LuaRef(); + + /// + /// + /// + void unref(); + + /// + /// Push value onto the stack. + /// + void push(); + + private: + /// + /// + /// + lua_State* const mL; + + /// + /// + /// + int mIndex; + + }; + + } // namespace Lua +} // namespace JinEngine + +#endif // __JIN_COMMON_REFERENCE_H
\ No newline at end of file diff --git a/src/lua/common/je_lua_shared.hpp b/src/lua/common/je_lua_shared.hpp new file mode 100644 index 0000000..91705d1 --- /dev/null +++ b/src/lua/common/je_lua_shared.hpp @@ -0,0 +1,151 @@ +#ifndef __JIN_COMMON_SHARED_H__ +#define __JIN_COMMON_SHARED_H__ + +#include <map> +#include <vector> + +namespace JinEngine +{ + namespace Lua + { + + class SharedBase + { + public: + void retain() + { + ++mCount; + } + + void release() + { + if (--mCount <= 0) + delete this; + } + + // Object type. + const char* const type; + + void setDependency(int key, SharedBase* shared) + { + removeDependency(key); + shared->retain(); + mDependencies.insert(std::pair<int, SharedBase*>(key, shared)); + } + + void removeDependency(int key) + { + if (!isDependOn(key)) + return; + DepMap::iterator it = mDependencies.find(key); + it->second->release(); + mDependencies.erase(it); + } + + void removeDependency(SharedBase* dep) + { + for (DepMap::iterator it = mDependencies.begin(); it != mDependencies.end();) + { + if (it->second == dep) + { + it->second->release(); + mDependencies.erase(it); + } + else + ++it; + } + } + + bool isDependOn(int key) + { + return mDependencies.find(key) != mDependencies.end(); + } + + bool isDependOn(SharedBase* shared) + { + for (std::pair<int, SharedBase*> dep : mDependencies) + { + if (dep.second == shared) + return true; + } + return false; + } + + void clearDependencies() + { + for (std::pair<int, SharedBase*> dep : mDependencies) + dep.second->release(); + mDependencies.clear(); + } + + SharedBase* getDependency(int key) + { + if (!isDependOn(key)) + return nullptr; + return mDependencies.find(key)->second; + } + + bool isType(const char* t) + { + return strcmp(type, t) == 0; + } + + protected: + + using DepMap = std::map<int, SharedBase*>; + + SharedBase(void* obj, const char* t) + : mCount(0) + , mObject(obj) + , type(t) + { + } + + SharedBase(const SharedBase&); + + virtual ~SharedBase() + { + clearDependencies(); + } + + void* mObject; + int mCount; + DepMap mDependencies; + }; + + template<class T> + class Shared : public SharedBase + { + public: + Shared(T* obj, const char* type) + : SharedBase(obj, type) + { + } + + T* operator->() + { + return static_cast<T*>(mObject); + } + + T* getObject() + { + return static_cast<T*>(mObject); + } + + private: + // Disable copy constructor. + Shared(const Shared<T>& shared); + + // Make shared only be able created with new. + ~Shared() + { + T* obj = static_cast<T*>(mObject); + delete obj; + } + + }; + + } // namespace Lua +} // namespace JinEngine + +#endif
\ No newline at end of file |