Skip to content

Commit

Permalink
update LuaVarObject set
Browse files Browse the repository at this point in the history
  • Loading branch information
pangweiwei committed Jun 11, 2015
1 parent eebc5f2 commit d71f71e
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 5 deletions.
3 changes: 3 additions & 0 deletions Assets/Slua/Resources/varobj.txt
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ function main()
cc=cls.Normalize(cc)
print(cc.x,cc.y,cc.z)

local r=Slua.CreateClass("Ref")
r.depth=10
print(r,r.depth)
end


13 changes: 13 additions & 0 deletions Assets/Slua/Script/LuaObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1224,6 +1224,19 @@ static public bool checkParams(IntPtr l, int p, out char[] pars)
return true;
}

static public object checkVar(IntPtr l, int p, Type t)
{
object obj = checkVar(l, p);
try
{
return Convert.ChangeType(obj, t);
}
catch(Exception) {
LuaDLL.luaL_error(l, "parameter {0} expected {1}, got {2}", p, t.Name, obj==null?"null":obj.GetType().Name);
}
return null;
}

static public object checkVar(IntPtr l, int p)
{
LuaTypes type = LuaDLL.lua_type(l, p);
Expand Down
10 changes: 5 additions & 5 deletions Assets/Slua/Script/LuaVarObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -249,11 +249,11 @@ static int indexString(IntPtr l, object self, string key)

}

static void newindexString(IntPtr l, object self, string key, object v)
static void newindexString(IntPtr l, object self, string key)
{
if (self is IDictionary)
{
(self as IDictionary)[key] = v;
(self as IDictionary)[key] = checkVar(l, 3);
return;
}

Expand All @@ -271,14 +271,14 @@ static void newindexString(IntPtr l, object self, string key, object v)
{
PropertyInfo p = (PropertyInfo)mi;
MethodInfo set = p.GetSetMethod();
var value = Convert.ChangeType(v, p.PropertyType);
var value = checkVar(l, 3, p.PropertyType);
set.Invoke(self, new object[] { value });
break;
}
case MemberTypes.Field:
{
FieldInfo f = (FieldInfo)mi;
var value = Convert.ChangeType(v, f.FieldType);
var value = checkVar(l, 3, f.FieldType);
f.SetValue(self, value);
break;
}
Expand Down Expand Up @@ -364,7 +364,7 @@ static public int luaNewIndex(IntPtr l)
switch (t)
{
case LuaTypes.LUA_TSTRING:
newindexString(l, self, LuaDLL.lua_tostring(l, 2), checkVar(l, 3));
newindexString(l, self, LuaDLL.lua_tostring(l, 2));
return 0;
case LuaTypes.LUA_TNUMBER:
newindexInt(l, self, LuaDLL.lua_tointeger(l, 2));
Expand Down
16 changes: 16 additions & 0 deletions Assets/Slua/example/HelloWorld.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,22 @@ public class SLuaTest : MonoBehaviour { }
[CustomLuaClass]
public class XXList : List<int> { }


public class Ref
{
int mDepth;
public int depth
{
get
{
return mDepth;
}
set {
mDepth = value;
}
}
}

[CustomLuaClass]
public class HelloWorld
{
Expand Down

0 comments on commit d71f71e

Please sign in to comment.