Skip to content

Commit

Permalink
fix pushVar/checkVar for color
Browse files Browse the repository at this point in the history
  • Loading branch information
pangweiwei committed Mar 31, 2015
1 parent 698610a commit 20fc806
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 35 deletions.
20 changes: 12 additions & 8 deletions Assets/Slua/Script/LuaObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,11 @@ static void setupPushVar()
{
pushValue(L, (Quaternion)o);
};

typePushMap[typeof(Color)] = (IntPtr L, object o) =>
{
pushValue(L, (Color)o);
};
}

static int luaOp(IntPtr l, string f, string tip)
Expand Down Expand Up @@ -514,20 +519,12 @@ static void throwLuaError(IntPtr l)
[MonoPInvokeCallbackAttribute(typeof(LuaCSFunction))]
static public int luaGC(IntPtr l)
{
#if !PUSH_PTR
int index = LuaDLL.luaS_rawnetobj(l, 1);
if (index > 0)
{
ObjectCache t = ObjectCache.get(l);
t.gc(index);
}
#else
IntPtr ptr = LuaDLL.luaS_objectptr(l,1);
if(ptr!=IntPtr.Zero){
GCHandle g = GCHandle.FromIntPtr(ptr);
g.Free();
}
#endif
return 0;
}

Expand Down Expand Up @@ -888,6 +885,7 @@ static internal bool checkEnum<T>(IntPtr l, int p, out T o) where T : struct
LuaDLL.luaL_checktype(l, p, LuaTypes.LUA_TNUMBER);
int i = LuaDLL.lua_tointeger(l, p);
o = (T)Enum.ToObject(typeof(T), i);

return true;
}

Expand Down Expand Up @@ -1007,6 +1005,12 @@ static public object checkVar(IntPtr l, int p)
checkType(l, p, out v);
return v;
}
else if (luaTypeCheck(l, p, "Color"))
{
Color c;
checkType(l, p, out c);
return c;
}
else
{
LuaDLL.lua_pushvalue(l, p);
Expand Down
32 changes: 5 additions & 27 deletions Assets/Slua/Script/ObjectCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -184,45 +184,26 @@ internal int add(object o)

internal object get(IntPtr l, int p)
{
#if !PUSH_PTR

int index = LuaDLL.luaS_rawnetobj(l, p);
object o;
if (index != -1 && cache.get(index, out o))
{
return o;
}
return null;
#else
IntPtr ptr = LuaDLL.luaS_objectptr(l, p);
GCHandle g = GCHandle.FromIntPtr(ptr);
if(g.IsAllocated)
return g.Target;
return null;
#endif

}

internal void setBack(IntPtr l, int p, object o)
{
#if !PUSH_PTR

int index = LuaDLL.luaS_rawnetobj(l, p);
if (index != -1)
{
cache.set(index, o);
}
#else
IntPtr ptr = LuaDLL.luaS_objectptr(l, p);
GCHandle g = GCHandle.FromIntPtr(ptr);
if (g.IsAllocated && o.GetType()==g.Target.GetType())
{
g.Target = o;
}
#endif
}

IntPtr addressOf(object o)
{
GCHandle g = GCHandle.Alloc(o);
return GCHandle.ToIntPtr(g);
}

internal void push(IntPtr l, object o)
Expand All @@ -232,7 +213,7 @@ internal void push(IntPtr l, object o)
LuaDLL.lua_pushnil(l);
return;
}
#if !PUSH_PTR

int index = -1;

bool gco = isGcObject(o);
Expand All @@ -245,10 +226,7 @@ internal void push(IntPtr l, object o)

index = add(o);
LuaDLL.luaS_pushobject(l, index, getAQName(o), gco, udCacheRef);
#else
IntPtr p = addressOf(o);
LuaDLL.luaS_pushobjectptr(l, p, getAQName(o), udCacheRef);
#endif

}

static Dictionary<Type, string> aqnameMap = new Dictionary<Type, string>();
Expand Down

0 comments on commit 20fc806

Please sign in to comment.