lua脚本错误检查 [英] lua script error checking

查看:1152
本文介绍了lua脚本错误检查的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在不执行lua脚本的情况下检查它是否包含错误?我有休闲代码:

Is it possible to check if a lua script contains errors without executing it? I have fallowing code:


    if(luaL_loadbuffer(L, data, size, name))
    {
        fprintf (stderr, "%s", lua_tostring (L, -1));
        lua_pop (L, 1);
    }

    if(lua_pcall(L, 0, 0, 0))
    {
        fprintf (stderr, "%s", lua_tostring (L, -1));
        lua_pop (L, 1);
    }

但是,如果脚本包含错误,它将首先通过并执行.我想知道它在加载时是否包含错误,而不是在执行时包含错误.这可能吗?

But if the script contains errors it passes first if and it is executed. I want to know if it contains errors when I load it, not when I execute it. Is this possible?

推荐答案

(原本是对对Krtek问题的第一条评论的答复,但我在那里用光了空间,说实话,它只是作为答案而已很好.

(This was originally meant as a reply to the first comment to Krtek's question, but I ran out of space there and to be honest it works as an answer just fine.)

函数本质上是值,因此,命名函数实际上是该名称的变量.根据变量的定义,变量可以在执行脚本时更改.地狱,有人可能不小心重新定义了其中一项功能.那不好吗?总结一下我的想法:根据脚本,传递的参数和/或您所说的那些预定义功能的实际实现(例如,一个可能未设置自身或其他功能),除非您愿意缩小您的一些需求.对于您要寻找的东西,Lua太动态了. :)

Functions are essentially values, and thus a named function is actually a variable of that name. Variables, by their very definition, can change as a script is executed. Hell, someone might accidentally redefine one of those functions. Is that bad? To sum my thoughts up: depending on the script, parameters passed and/or actual implementations of those pre-defined functions you speak of (one might unset itself or others, for example), it is not possible to guarantee things work unless you are willing to narrow down some of your demands. Lua is too dynamic for what you are looking for. :)

如果您想进行无瑕测试:创建一个虚拟环境,将所有铃声都放在适当的位置,然后查看它是否在崩溃过程中的任何地方崩溃(加载,执行等).这基本上是一种单元测试,因此非常繁重.

If you want a flawless test: create a dummy environment with all bells and whistles in place, and see if it crashes anywhere along the way (loading, executing, etc). This is basically a sort of unit test, and as such would be pretty heavy.

如果您要基本检查以查看脚本是否具有有效的语法:Krtek已经给出了答案.我非常确定(但不是100%),lua等效于loadfileloadstring,相应的C等效于尝试和lua_load()代码,每个代码都将可读脚本转换为字节码,在正常的万事俱备的用例中实际执行代码之前,已经需要做的事情了. (并且,如果其中包含函数定义,则稍后需要执行这些定义,以使其中的代码得以执行.)

If you want a basic check to see if a script has a valid syntax: Krtek gave an answer for that already. I am quite sure (but not 100%) that the lua equivalent is to loadfile or loadstring, and the respective C equivalent is to try and lua_load() the code, each of which convert readable script to bytecode which you would already need to do before you could actually execute the code in your normal all-is-well usecase. (And if that contained function definitions, those would need to be executed later on for the code inside those to execute.)

但是,这是您在实际发生错误之前可以选择的范围. Lua是一种非常动态的语言,当您想证明正确性时,强项就是弱点.完美的解决方案涉及太多的变量.

However, these are the extent of your options with regards to pre-empting errors before they actually happen. Lua is a very dynamic language, and what is a great strength also makes for a weakness when you want to prove correctness. There are simply too many variables involved for a perfect solution.

这篇关于lua脚本错误检查的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆