xLua icon indicating copy to clipboard operation
xLua copied to clipboard

实现当lua访问C#类型不存在的字段时,不返回nil,而抛出异常,有相关解决方案吗

Open oOtroyOo opened this issue 2 years ago • 3 comments

由于lua是弱语言。有些时候写的不规范,可能会排查很久原因。 例如:当使用 if go.activeInHierarchy then,而activeInHierarchy 不小心写错,那么这个在if条件会始终判定为false,而程序逻辑不会有强异常报错

我希望在可以自定义做一些调整,比如重写__index,从而抛出异常,但是CodeEmit和Wrap文件,不是那么容易看懂。 如果有类似的FAQ方案,可以参考一下

oOtroyOo avatar Sep 20 '23 14:09 oOtroyOo

统一都在这里:https://github.com/Tencent/xLua/blob/master/build/xlua.c 静态和成员的set都有判断,get你试试参考set去改改

chexiongsheng avatar Sep 27 '23 07:09 chexiongsheng

我研究了一下,这样改可以初步满足需求。

https://github.com/oOtroyOo/xLua/commit/2bf0fdb52f32f888e2ca80fa52fbc13ad5dd214e

有个问题,就是这样改制后无法正常Generate,因为 Template 里有源码这样写的:

    for i = 0, (list.Count or list.Length) - 1 do 
        if predicate(list[i], i) then count = count + 1 end
    end

导致当不确定list的类型时,要么Count会异常,要么Length会异常。 诸如此类问题在Generate过程中还有几处,就不列举了。

我想到在lua侧,在游戏运行时自行设置全局变量 _G._CS_FIELD_CHECK 去决定要不要走这条判断。 由于不清楚这个如何测试性能,每一次进入都会去拿一个lua变量会不会产生过大的性能消耗。 希望可以指点一下

oOtroyOo avatar Oct 16 '23 03:10 oOtroyOo

你这样改会很慢

chexiongsheng avatar Oct 16 '23 09:10 chexiongsheng