提交 52b06873 编写于 作者: J johnche

解决如果枚举定义了很多个值(几千个),会触发unity在android下的一个bug:函数体很大而且有很多分支,执行该函数会crash

上级 cab4d1e4
...@@ -20,6 +20,12 @@ namespace XLua.CSObjectWrap ...@@ -20,6 +20,12 @@ namespace XLua.CSObjectWrap
using Utils = XLua.Utils; using Utils = XLua.Utils;
<%ForEachCsList(types, function(type) <%ForEachCsList(types, function(type)
local fields = type2fields and type2fields[type] or type:GetFields(enum_or_op(CS.System.Reflection.BindingFlags.Public, CS.System.Reflection.BindingFlags.Static)) local fields = type2fields and type2fields[type] or type:GetFields(enum_or_op(CS.System.Reflection.BindingFlags.Public, CS.System.Reflection.BindingFlags.Static))
local fields_to_gen = {}
ForEachCsList(fields, function(field)
if field.Name ~= "value__" and not IsObsolute(field) then
table.insert(fields_to_gen, field)
end
end)
%> %>
public class <%=CSVariableName(type)%>Wrap public class <%=CSVariableName(type)%>Wrap
{ {
...@@ -30,11 +36,18 @@ namespace XLua.CSObjectWrap ...@@ -30,11 +36,18 @@ namespace XLua.CSObjectWrap
Utils.EndObjectRegister(typeof(<%=CsFullTypeName(type)%>), L, translator, null, null, null, null, null); Utils.EndObjectRegister(typeof(<%=CsFullTypeName(type)%>), L, translator, null, null, null, null, null);
Utils.BeginClassRegister(typeof(<%=CsFullTypeName(type)%>), L, null, <%=fields.Length + 1%>, 0, 0); Utils.BeginClassRegister(typeof(<%=CsFullTypeName(type)%>), L, null, <%=fields.Length + 1%>, 0, 0);
<%if #fields_to_gen <= 20 then%>
<% ForEachCsList(fields, function(field) <% ForEachCsList(fields, function(field)
if field.Name == "value__" or IsObsolute(field) then return end if field.Name == "value__" or IsObsolute(field) then return end
%> %>
Utils.RegisterObject(L, translator, Utils.CLS_IDX, "<%=field.Name%>", <%=CsFullTypeName(type)%>.<%=UnK(field.Name)%>); Utils.RegisterObject(L, translator, Utils.CLS_IDX, "<%=field.Name%>", <%=CsFullTypeName(type)%>.<%=UnK(field.Name)%>);
<%end)%> <%end)%>
<%else%>
foreach(var e in System.Enum.GetValues(typeof(<%=CsFullTypeName(type)%>)))
{
Utils.RegisterObject(L, translator, Utils.CLS_IDX, e.ToString(), e);
}
<%end%>
Utils.RegisterFunc(L, Utils.CLS_IDX, "__CastFrom", __CastFrom); Utils.RegisterFunc(L, Utils.CLS_IDX, "__CastFrom", __CastFrom);
Utils.EndClassRegister(typeof(<%=CsFullTypeName(type)%>), L, translator); Utils.EndClassRegister(typeof(<%=CsFullTypeName(type)%>), L, translator);
...@@ -49,9 +62,10 @@ namespace XLua.CSObjectWrap ...@@ -49,9 +62,10 @@ namespace XLua.CSObjectWrap
{ {
translator.Push<%=CSVariableName(type)%>(L, (<%=CsFullTypeName(type)%>)LuaAPI.xlua_tointeger(L, 1)); translator.Push<%=CSVariableName(type)%>(L, (<%=CsFullTypeName(type)%>)LuaAPI.xlua_tointeger(L, 1));
} }
<%if fields.Length > 0 then%> <%if #fields_to_gen > 0 then%>
else if(lua_type == LuaTypes.LUA_TSTRING) else if(lua_type == LuaTypes.LUA_TSTRING)
{ {
<%if #fields_to_gen <= 20 then%>
<% <%
local is_first = true local is_first = true
ForEachCsList(fields, function(field, i) ForEachCsList(fields, function(field, i)
...@@ -67,6 +81,16 @@ namespace XLua.CSObjectWrap ...@@ -67,6 +81,16 @@ namespace XLua.CSObjectWrap
{ {
return LuaAPI.luaL_error(L, "invalid string for <%=CsFullTypeName(type)%>!"); return LuaAPI.luaL_error(L, "invalid string for <%=CsFullTypeName(type)%>!");
} }
<%else%>
try
{
translator.TranslateToEnumToTop(L, typeof(<%=CsFullTypeName(type)%>), 1);
}
catch (System.Exception e)
{
return LuaAPI.luaL_error(L, "cast to " + typeof(<%=CsFullTypeName(type)%>) + " exception:" + e);
}
<%end%>
} }
<%end%> <%end%>
else else
......
...@@ -21,6 +21,12 @@ namespace XLua ...@@ -21,6 +21,12 @@ namespace XLua
{ {
<%ForEachCsList(types, function(type) <%ForEachCsList(types, function(type)
local fields = type2fields and type2fields[type] or type:GetFields(enum_or_op(CS.System.Reflection.BindingFlags.Public, CS.System.Reflection.BindingFlags.Static)) local fields = type2fields and type2fields[type] or type:GetFields(enum_or_op(CS.System.Reflection.BindingFlags.Public, CS.System.Reflection.BindingFlags.Static))
local fields_to_gen = {}
ForEachCsList(fields, function(field)
if field.Name ~= "value__" and not IsObsolute(field) then
table.insert(fields_to_gen, field)
end
end)
local v_type_name = CSVariableName(type) local v_type_name = CSVariableName(type)
%> %>
public void __Register<%=v_type_name%>(RealStatePtr L) public void __Register<%=v_type_name%>(RealStatePtr L)
...@@ -29,11 +35,18 @@ namespace XLua ...@@ -29,11 +35,18 @@ namespace XLua
Utils.EndObjectRegister(typeof(<%=CsFullTypeName(type)%>), L, this, null, null, null, null, null); Utils.EndObjectRegister(typeof(<%=CsFullTypeName(type)%>), L, this, null, null, null, null, null);
Utils.BeginClassRegister(typeof(<%=CsFullTypeName(type)%>), L, null, <%=fields.Length + 1%>, 0, 0); Utils.BeginClassRegister(typeof(<%=CsFullTypeName(type)%>), L, null, <%=fields.Length + 1%>, 0, 0);
<%if #fields_to_gen <= 20 then%>
<% ForEachCsList(fields, function(field) <% ForEachCsList(fields, function(field)
if field.Name == "value__" or IsObsolute(field) then return end if field.Name == "value__" or IsObsolute(field) then return end
%> %>
Utils.RegisterObject(L, this, Utils.CLS_IDX, "<%=field.Name%>", <%=CsFullTypeName(type)%>.<%=UnK(field.Name)%>); Utils.RegisterObject(L, this, Utils.CLS_IDX, "<%=field.Name%>", <%=CsFullTypeName(type)%>.<%=UnK(field.Name)%>);
<%end)%> <%end)%>
<%else%>
foreach(var e in System.Enum.GetValues(typeof(<%=CsFullTypeName(type)%>)))
{
Utils.RegisterObject(L, translator, Utils.CLS_IDX, e.ToString(), e);
}
<%end%>
Utils.RegisterFunc(L, Utils.CLS_IDX, "__CastFrom", __CastFrom<%=v_type_name%>); Utils.RegisterFunc(L, Utils.CLS_IDX, "__CastFrom", __CastFrom<%=v_type_name%>);
Utils.EndClassRegister(typeof(<%=CsFullTypeName(type)%>), L, this); Utils.EndClassRegister(typeof(<%=CsFullTypeName(type)%>), L, this);
...@@ -46,9 +59,10 @@ namespace XLua ...@@ -46,9 +59,10 @@ namespace XLua
{ {
Push<%=v_type_name%>(L, (<%=CsFullTypeName(type)%>)LuaAPI.xlua_tointeger(L, 1)); Push<%=v_type_name%>(L, (<%=CsFullTypeName(type)%>)LuaAPI.xlua_tointeger(L, 1));
} }
<%if fields.Length > 0 then%> <%if #fields_to_gen > 0 then%>
else if(lua_type == LuaTypes.LUA_TSTRING) else if(lua_type == LuaTypes.LUA_TSTRING)
{ {
<%if #fields_to_gen <= 20 then%>
<% <%
local is_first = true local is_first = true
ForEachCsList(fields, function(field, i) ForEachCsList(fields, function(field, i)
...@@ -64,6 +78,16 @@ namespace XLua ...@@ -64,6 +78,16 @@ namespace XLua
{ {
return LuaAPI.luaL_error(L, "invalid string for <%=CsFullTypeName(type)%>!"); return LuaAPI.luaL_error(L, "invalid string for <%=CsFullTypeName(type)%>!");
} }
<%else%>
try
{
translator.TranslateToEnumToTop(L, typeof(<%=CsFullTypeName(type)%>), 1);
}
catch (System.Exception e)
{
return LuaAPI.luaL_error(L, "cast to " + typeof(<%=CsFullTypeName(type)%>) + " exception:" + e);
}
<%end%>
} }
<%end%> <%end%>
else else
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册