aboutsummaryrefslogtreecommitdiff
path: root/src/lua/common
diff options
context:
space:
mode:
Diffstat (limited to 'src/lua/common')
-rw-r--r--src/lua/common/je_lua_function.cpp45
-rw-r--r--src/lua/common/je_lua_function.h46
-rw-r--r--src/lua/common/je_lua_reference.cpp31
-rw-r--r--src/lua/common/je_lua_reference.h27
4 files changed, 128 insertions, 21 deletions
diff --git a/src/lua/common/je_lua_function.cpp b/src/lua/common/je_lua_function.cpp
new file mode 100644
index 0000000..e202d99
--- /dev/null
+++ b/src/lua/common/je_lua_function.cpp
@@ -0,0 +1,45 @@
+#include "je_lua_function.h"
+
+namespace JinEngine
+{
+ namespace Lua
+ {
+
+ LuaFunc::LuaFunc(lua_State* L)
+ : mLuaFunc(nullptr)
+ , mParams(0)
+ , mL(L)
+ {
+ }
+
+ LuaFunc::~LuaFunc()
+ {
+ delete mLuaFunc;
+ for (auto p : mParams)
+ delete p;
+ }
+
+ void LuaFunc::setFunc(int i, uint nresults )
+ {
+ if (mLuaFunc != nullptr)
+ delete mLuaFunc;
+ mLuaFunc = new LuaRef(mL, i);
+ mNResults = nresults;
+ }
+
+ void LuaFunc::pushParam(int i)
+ {
+ mParams.push_back(new LuaRef(mL, i));
+ }
+
+ uint LuaFunc::call()
+ {
+ mLuaFunc->push();
+ for (auto p : mParams)
+ p->push();
+ luax_call(mL, mParams.size(), mNResults);
+ return mNResults;
+ }
+
+ }
+} \ No newline at end of file
diff --git a/src/lua/common/je_lua_function.h b/src/lua/common/je_lua_function.h
new file mode 100644
index 0000000..a2d5ccc
--- /dev/null
+++ b/src/lua/common/je_lua_function.h
@@ -0,0 +1,46 @@
+#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 LuaFunc
+ {
+ public:
+ LuaFunc(lua_State* L);
+ ~LuaFunc();
+
+ ///
+ ///
+ ///
+ void setFunc(int i, uint nresults);
+
+ ///
+ ///
+ ///
+ void pushParam(int i);
+
+ ///
+ ///
+ ///
+ uint call();
+
+ private:
+ LuaRef* mLuaFunc;
+ std::vector<LuaRef*> mParams;
+ lua_State* mL;
+ uint mNResults;
+ };
+
+ } // namespace Lua
+} // namespace JinEngine
+
+#endif // __JIN_COMMON_REFERENCE_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
index 28c3bc3..bde61b6 100644
--- a/src/lua/common/je_lua_reference.h
+++ b/src/lua/common/je_lua_reference.h
@@ -12,33 +12,18 @@ namespace JinEngine
/// This class wraps the reference functionality built into Lua, which allows C++ code to refer to Lua
/// variables.
///
- class Reference
+ class LuaRef
{
public:
- Reference(lua_State* L, unsigned int i)
- : mL(L)
- {
- luax_pushvalue(mL, i);
- mIndex = luax_ref(mL, LUA_REGISTRYINDEX);
- }
-
- ~Reference()
- {
- unref();
- }
-
- void unref()
- {
- luax_unref(mL, LUA_REGISTRYINDEX, mIndex);
- }
+ LuaRef(lua_State* L, int i);
+ ~LuaRef();
+
+ void unref();
///
/// Push value onto the stack.
///
- void push()
- {
- luax_rawgeti(mL, LUA_REGISTRYINDEX, mIndex);
- }
+ void push();
private:
lua_State* const mL;