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

查看:89
本文介绍了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);
    }

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

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 等效于 try and 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天全站免登陆