Skip to content

Commit

Permalink
fix interface export
Browse files Browse the repository at this point in the history
  • Loading branch information
pangweiwei committed Sep 26, 2017
1 parent 937fad3 commit cd5b0aa
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 7 deletions.
16 changes: 12 additions & 4 deletions Assets/Plugins/Slua_Managed/LuaObject_basetype.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions Assets/Plugins/Slua_Managed/LuaState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
11 changes: 11 additions & 0 deletions Assets/Plugins/Slua_Managed/ObjectCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{

Expand Down
7 changes: 5 additions & 2 deletions Assets/Slua/Editor/LuaCodeGen.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);");
}
Expand All @@ -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)
{
Expand Down
1 change: 1 addition & 0 deletions Assets/Slua/Resources/custom.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,6 @@ function main()
print("-->"..c:getItem("test"))

assert(c:getInterface():getInt()==10)
c:getInterface():setInt(11)
print("assert interface ok")
end
12 changes: 11 additions & 1 deletion Assets/Slua/example/Custom.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down

0 comments on commit cd5b0aa

Please sign in to comment.