且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何检查脚本是否通过dofile()包含或直接在Lua中运行?

更新时间:2023-12-04 22:33:16

在顶层,您可以检查debug.getinfo(2)是否为零

At the top level you can check if debug.getinfo(2) is nil

来自 http://www.lua.org/manual /5.1/manual.html#pdf-debug.getinfo

您可以给数字作为function的值,这意味着该函数在给定线程的调用堆栈的level函数上运行:level 0是当前函数(getinfo本身); 1级是调用getinfo的函数;等等.如果function的数量大于活动函数的数量,则getinfo返回nil.

you can give a number as the value of function, which means the function running at level function of the call stack of the given thread: level 0 is the current function (getinfo itself); level 1 is the function that called getinfo; and so on. If function is a number larger than the number of active functions, then getinfo returns nil.