From cd5b0aa4bdbc31d85d0487e22862151efbba65e6 Mon Sep 17 00:00:00 2001 From: Pang Weiwei Date: Tue, 26 Sep 2017 11:41:53 +0800 Subject: [PATCH] fix interface export --- .../Plugins/Slua_Managed/LuaObject_basetype.cs | 16 ++++++++++++---- Assets/Plugins/Slua_Managed/LuaState.cs | 1 + Assets/Plugins/Slua_Managed/ObjectCache.cs | 11 +++++++++++ Assets/Slua/Editor/LuaCodeGen.cs | 7 +++++-- Assets/Slua/Resources/custom.txt | 1 + Assets/Slua/example/Custom.cs | 12 +++++++++++- 6 files changed, 41 insertions(+), 7 deletions(-) diff --git a/Assets/Plugins/Slua_Managed/LuaObject_basetype.cs b/Assets/Plugins/Slua_Managed/LuaObject_basetype.cs index 5f889f57..c3b7af72 100644 --- a/Assets/Plugins/Slua_Managed/LuaObject_basetype.cs +++ b/Assets/Plugins/Slua_Managed/LuaObject_basetype.cs @@ -157,11 +157,19 @@ public static void pushValue(IntPtr l, ushort v) { LuaDLL.lua_pushinteger(l, v); } - - #endregion - #region int - static public bool checkType(IntPtr l, int p, out int v) + #endregion + + + #region interface + static public void pushInterface(IntPtr l,object i,Type t) { + ObjectCache oc = ObjectCache.get(l); + oc.pushInterface(l, i, t); + } + #endregion + + #region int + static public bool checkType(IntPtr l, int p, out int v) { v = (int)LuaDLL.luaL_checkinteger(l, p); return true; diff --git a/Assets/Plugins/Slua_Managed/LuaState.cs b/Assets/Plugins/Slua_Managed/LuaState.cs index 86fe3705..2fbaf4b9 100644 --- a/Assets/Plugins/Slua_Managed/LuaState.cs +++ b/Assets/Plugins/Slua_Managed/LuaState.cs @@ -897,6 +897,7 @@ public virtual void Dispose(bool dispose) public static int errorReport(IntPtr L) { s.Length = 0; + s.Append(LuaDLL.lua_tostring(L,1)); LuaDLL.lua_getglobal(L, "debug"); LuaDLL.lua_getfield(L, -1, "traceback"); diff --git a/Assets/Plugins/Slua_Managed/ObjectCache.cs b/Assets/Plugins/Slua_Managed/ObjectCache.cs index 68cada7f..7ce1bb38 100644 --- a/Assets/Plugins/Slua_Managed/ObjectCache.cs +++ b/Assets/Plugins/Slua_Managed/ObjectCache.cs @@ -251,6 +251,17 @@ internal int allocID(IntPtr l,object o) { return index; } + internal void pushInterface(IntPtr l, object o, Type t) + { + + int index = allocID(l, o); + if (index < 0) + return; + + LuaDLL.luaS_pushobject(l, index, getAQName(t), true, udCacheRef); + } + + internal void push(IntPtr l, object o, bool checkReflect) { diff --git a/Assets/Slua/Editor/LuaCodeGen.cs b/Assets/Slua/Editor/LuaCodeGen.cs index e5acea25..55ed84e4 100644 --- a/Assets/Slua/Editor/LuaCodeGen.cs +++ b/Assets/Slua/Editor/LuaCodeGen.cs @@ -2357,6 +2357,8 @@ void WritePushValue(Type t, StreamWriter file) { if (t.IsEnum) Write(file, "pushEnum(l,(int)ret);"); + else if (t.IsInterface && t.IsDefined(typeof(CustomLuaClassAttribute), false)) + Write(file, "pushInterface(l,ret, typeof({0}));", TypeDecl(t)); else Write(file, "pushValue(l,ret);"); } @@ -2365,10 +2367,11 @@ void WritePushValue(Type t, StreamWriter file, string ret) { if (t.IsEnum) Write(file, "pushEnum(l,(int){0});", ret); + else if (t.IsInterface && t.IsDefined(typeof(CustomLuaClassAttribute),false)) + Write(file, "pushInterface(l,{0}, typeof({1}));", ret,TypeDecl(t)); else Write(file, "pushValue(l,{0});", ret); - } - + } void Write(StreamWriter file, string fmt, params object[] args) { diff --git a/Assets/Slua/Resources/custom.txt b/Assets/Slua/Resources/custom.txt index e8d16de8..c56b6cf9 100644 --- a/Assets/Slua/Resources/custom.txt +++ b/Assets/Slua/Resources/custom.txt @@ -25,5 +25,6 @@ function main() print("-->"..c:getItem("test")) assert(c:getInterface():getInt()==10) + c:getInterface():setInt(11) print("assert interface ok") end \ No newline at end of file diff --git a/Assets/Slua/example/Custom.cs b/Assets/Slua/example/Custom.cs index 1346790f..e88e3094 100644 --- a/Assets/Slua/example/Custom.cs +++ b/Assets/Slua/example/Custom.cs @@ -75,20 +75,30 @@ public string getTypeName(Type t) public interface IFoo { int getInt(); + void setInt(int i, bool ok); } class Foo : IFoo { public int getInt() { return 10; } + public void setInt(int i,bool ok) { + + } } public IFoo getInterface() { return new Foo(); } - } +public static class IFooExt +{ + public static void setInt(this Custom.IFoo f, int i) + { + + } +} namespace SLua {