Skip to content

Commit

Permalink
优化__CastFrom实现
Browse files Browse the repository at this point in the history
  • Loading branch information
chexiongsheng committed Jun 27, 2017
1 parent 57d9eb2 commit 0cacaea
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 18 deletions.
20 changes: 4 additions & 16 deletions Assets/XLua/Src/ObjectTranslator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -971,25 +971,13 @@ public int TranslateToEnumToTop(RealStatePtr L, Type type, int idx)
int ival = (int)LuaAPI.lua_tonumber(L, idx);
res = Enum.ToObject(type, ival);
}
else
if (lt == LuaTypes.LUA_TSTRING)
else if (lt == LuaTypes.LUA_TSTRING)
{
string sflags = LuaAPI.lua_tostring(L, idx);
string err = null;
try
{
res = Enum.Parse(type, sflags);
}
catch (ArgumentException e)
{
err = e.Message;
}
if (err != null)
{
return LuaAPI.luaL_error(L, err);
}
res = Enum.Parse(type, sflags);
}
else {
else
{
return LuaAPI.luaL_error(L, "#1 argument must be a integer or a string");
}
PushAny(L, res);
Expand Down
11 changes: 9 additions & 2 deletions Assets/XLua/Src/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -376,8 +376,15 @@ static LuaCSFunction genEnumCastFrom(Type type)
{
return (RealStatePtr L) =>
{
ObjectTranslator translator = ObjectTranslatorPool.Instance.Find(L);
return translator.TranslateToEnumToTop(L, type, 1);
try
{
ObjectTranslator translator = ObjectTranslatorPool.Instance.Find(L);
return translator.TranslateToEnumToTop(L, type, 1);
}
catch(Exception e)
{
return LuaAPI.luaL_error(L, "cast to " + type + " exception:" + e);
}
};
}

Expand Down

0 comments on commit 0cacaea

Please sign in to comment.