summaryrefslogtreecommitdiff
path: root/Runtime/Lua
diff options
context:
space:
mode:
authorchai <chaifix@163.com>2021-11-09 19:22:13 +0800
committerchai <chaifix@163.com>2021-11-09 19:22:13 +0800
commitd8417b03b9c2a820d3d3be0dfa80841b4d1f4c04 (patch)
tree7d036d283bd7a626d56c5c5a725733df439c8368 /Runtime/Lua
parent13f477664c07826c92eac774f0035994c460c057 (diff)
*misc
Diffstat (limited to 'Runtime/Lua')
-rw-r--r--Runtime/Lua/LuaBind/LuaBind.h1
-rw-r--r--Runtime/Lua/LuaBind/LuaBindInvoker.cpp8
-rw-r--r--Runtime/Lua/LuaBind/LuaBindInvoker.h3
-rw-r--r--Runtime/Lua/LuaBind/LuaBindLClass.h16
-rw-r--r--Runtime/Lua/LuaBind/LuaBindState.cpp7
-rw-r--r--Runtime/Lua/LuaBind/LuaBindState.h4
-rw-r--r--Runtime/Lua/LuaBind/LuaBindTable.h2
-rw-r--r--Runtime/Lua/LuaHelper.cpp29
-rw-r--r--Runtime/Lua/LuaHelper.h7
9 files changed, 69 insertions, 8 deletions
diff --git a/Runtime/Lua/LuaBind/LuaBind.h b/Runtime/Lua/LuaBind/LuaBind.h
index b6c5870..2a73c98 100644
--- a/Runtime/Lua/LuaBind/LuaBind.h
+++ b/Runtime/Lua/LuaBind/LuaBind.h
@@ -12,5 +12,6 @@
#include "LuaBindState.inc"
#include "LuaBindInvoker.h"
#include "LuaBindTable.h"
+#include "LuaBindLClass.h"
#endif \ No newline at end of file
diff --git a/Runtime/Lua/LuaBind/LuaBindInvoker.cpp b/Runtime/Lua/LuaBind/LuaBindInvoker.cpp
index debebf8..a95ef5c 100644
--- a/Runtime/Lua/LuaBind/LuaBindInvoker.cpp
+++ b/Runtime/Lua/LuaBind/LuaBindInvoker.cpp
@@ -76,12 +76,18 @@ namespace LuaBind
++argc;
}
- void MemberInvoker::AddTable(INativeTable& tb)
+ void MemberInvoker::AddTable(const INativeTable& tb)
{
tb.CastToTable(state);
++argc;
}
+ void MemberInvoker::AddLuaObject(const ILuaClass& obj)
+ {
+ obj.CastToLuaObject(state);
+ ++argc;
+ }
+
void MemberInvoker::AddMember(MemberRef member)
{
owner->PushMemberRef(state, member);
diff --git a/Runtime/Lua/LuaBind/LuaBindInvoker.h b/Runtime/Lua/LuaBind/LuaBindInvoker.h
index 8ea57a2..edb0725 100644
--- a/Runtime/Lua/LuaBind/LuaBindInvoker.h
+++ b/Runtime/Lua/LuaBind/LuaBindInvoker.h
@@ -56,7 +56,8 @@ namespace LuaBind
void AddNil();
void AddBool(bool b);
void AddString(const char* str);
- void AddTable(INativeTable& tb);
+ void AddTable(const INativeTable& tb);
+ void AddLuaObject(const ILuaClass& obj);
template<class T>
void AddUserdata(NativeClass<T>& udata) {
udata.PushUserdata(state);
diff --git a/Runtime/Lua/LuaBind/LuaBindLClass.h b/Runtime/Lua/LuaBind/LuaBindLClass.h
new file mode 100644
index 0000000..9f4c959
--- /dev/null
+++ b/Runtime/Lua/LuaBind/LuaBindLClass.h
@@ -0,0 +1,16 @@
+#pragma once
+
+namespace LuaBind
+{
+
+ class State;
+
+ // 需要转成lua class对象的实现这个接口,创建一个lua的对象,放在栈顶
+ class ILuaClass
+ {
+ public:
+ virtual void CastToLuaObject(State&) const = 0 ;
+
+ };
+
+}
diff --git a/Runtime/Lua/LuaBind/LuaBindState.cpp b/Runtime/Lua/LuaBind/LuaBindState.cpp
index 676c19c..92e46cd 100644
--- a/Runtime/Lua/LuaBind/LuaBindState.cpp
+++ b/Runtime/Lua/LuaBind/LuaBindState.cpp
@@ -196,7 +196,12 @@ namespace LuaBind
}
}
- void State::PushTable(INativeTable& tb)
+ void State::PushLuaObject(const ILuaClass& lc)
+ {
+ lc.CastToLuaObject(*this);
+ }
+
+ void State::PushTable(const INativeTable& tb)
{
tb.CastToTable(*this);
}
diff --git a/Runtime/Lua/LuaBind/LuaBindState.h b/Runtime/Lua/LuaBind/LuaBindState.h
index 24bd092..aaad952 100644
--- a/Runtime/Lua/LuaBind/LuaBindState.h
+++ b/Runtime/Lua/LuaBind/LuaBindState.h
@@ -7,6 +7,7 @@
#include "LuaBindRefTable.h"
#include "LuaBindGlobalState.h"
#include "LuaBindTable.h"
+#include "LuaBindLClass.h"
namespace LuaBind
{
@@ -101,7 +102,8 @@ namespace LuaBind
bool HasField(int idx, int name, int type);
bool HasKeys(int idx);
- void PushTable(INativeTable& tb);
+ void PushLuaObject(const ILuaClass& lc);
+ void PushTable(const INativeTable& tb);
void PushNil();
void Push(bool value);
void Push(cc8* value);
diff --git a/Runtime/Lua/LuaBind/LuaBindTable.h b/Runtime/Lua/LuaBind/LuaBindTable.h
index 3a6ec6c..a3822d3 100644
--- a/Runtime/Lua/LuaBind/LuaBindTable.h
+++ b/Runtime/Lua/LuaBind/LuaBindTable.h
@@ -9,7 +9,7 @@ namespace LuaBind
struct INativeTable
{
// 将结构转换为table并留在栈顶
- virtual void CastToTable(State& state) = 0;
+ virtual void CastToTable(State& state) const = 0;
};
} \ No newline at end of file
diff --git a/Runtime/Lua/LuaHelper.cpp b/Runtime/Lua/LuaHelper.cpp
index f1dc349..71fe782 100644
--- a/Runtime/Lua/LuaHelper.cpp
+++ b/Runtime/Lua/LuaHelper.cpp
@@ -5,6 +5,7 @@
#include "Runtime/Graphics/Color.h"
using namespace LuaBind;
+using namespace std;
template <>
Rect State::GetValue < Rect >(int idx, const Rect value)
@@ -207,4 +208,32 @@ bool LuaHelper::IsType(LuaBind::State& state, const char* typeName, int idx)
bool LuaHelper::InstantiateClass(LuaBind::State& state, const char* classFullName)
{
return false;
+}
+
+bool LuaHelper::GetLuaClass(LuaBind::State& state, const char* fullName)
+{
+ if (fullName == NULL || strlen(fullName) == 0)
+ return false;
+ int top = state.GetTop();
+ lua_pushvalue(state, LUA_GLOBALSINDEX);
+ string name = fullName;
+ while (name.size() > 0)
+ {
+ int dot = name.find('.');
+ dot = dot != string::npos ? dot : name.size();
+ string pkg = name.substr(0, dot);
+ if (dot < name.size())
+ name = name.substr(dot + 1, name.size() - dot - 1);
+ else
+ name = "";
+ lua_getfield(state, -1, pkg.c_str());
+ if (lua_isnil(state, -1))
+ {
+ lua_settop(state, top);
+ return false;
+ }
+ }
+ lua_replace(state, top + 1);
+ lua_settop(state, top + 1);
+ return true;
} \ No newline at end of file
diff --git a/Runtime/Lua/LuaHelper.h b/Runtime/Lua/LuaHelper.h
index c3cd70e..79cab29 100644
--- a/Runtime/Lua/LuaHelper.h
+++ b/Runtime/Lua/LuaHelper.h
@@ -1,6 +1,4 @@
#pragma once
-#include "Runtime/Math/Rect.h"
-#include "Runtime/Math/Vector2.h"
#include "./LuaBind/LuaBind.h"
// lua 5.1 doc: https://www.lua.org/manual/5.1/
@@ -14,5 +12,8 @@ public:
static void OnRegisterNativeClass(LuaBind::State& state, int cls, std::string clsName, std::string pkgName);
// 创建lua类的实例并留在栈顶,如果失败返回false且不压栈
static bool InstantiateClass(LuaBind::State& state, const char* classFullName);
-
+
+ // 找到类table,并留在栈顶,如果返回false表示没有找到,栈顶无值
+ static bool GetLuaClass(LuaBind::State& state, const char* fullName);
+
}; \ No newline at end of file