Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if a script is included via dofile() or run directly in Lua? [duplicate]

Tags:

lua

Possible Duplicate:
“main” function in Lua?

In Python, you can check if a script is being called direcly (and call some functions if it is, usually for testing) pretty easily:

if __name__ == "__main__":
    main()

Is there a way to do the same in Lua, to detect if it is run directly (lua foo.lua) or included in another script (dofile('foo.lua')).

There is always the trivial (and ugly) way of defining some dummy global variable in the main script file before dofile('foo.lua'), and checking in foo.lua if it is defined or not, but it would be great if there was a better way to do this.

like image 915
Wookai Avatar asked Jan 30 '12 10:01

Wookai


People also ask

What does Dofile do in Lua?

The dofile builtin function can be used to execute a chunk of code stored in a file.

What is Loadstring Lua?

The loadstring function is similar to loadfile , except that it reads its chunk from a string, not from a file. For instance, after the code f = loadstring("i = i + 1") f will be a function that, when invoked, executes i = i + 1 : i = 0 f(); print(i) --> 1 f(); print(i) --> 2.

What is a Lua state?

The lua_State is basically a way to access what's going on in the Lua "box" during execution of your program and allows you to glue the two languages together.


2 Answers

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

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

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.

like image 78
daurnimator Avatar answered Sep 19 '22 12:09

daurnimator


No. Lua has no way to tell if a script is being invoked "directly".

Remember: Lua and Python exist for different purposes. Python is designed for command-line scripting. You write a script, which calls modules that may be written in Python, C++, or whatever. The Python script is ultimately what is in charge of which code gets called when.

Lua is an embedded scripting language, first and foremost. While you can certainly use the standalone lua.exe interpreter to create command-line scripts, that is something of a kludge. The language's primary purpose is to be embedded in some application written in C, C++, or whatever.

Because of this, there is no concept of a "main" script. How would you define what is "main" and what is not? If C code can load any script at any time, for any purpose, which one of them is "main"?

If you want to define this concept, it's fairly simple. Just stick this at the top of your "main" script:

do
  local old_dofile = dofile
  function new_dofile(...)
    if(__is_main) then
      __is_main = __is_main + 1
    else
      __is_main = 1
    end
    old_dofile(...)
    __is_main = __is_main - 1
    if(__is_main == 0) then __is_main = nil end
  end
  dofile = new_dofile
end

Granted, this won't work for anything loaded with require; you'd need to write a version of this for require as well. And it certainly won't work if external code uses the C-API loading functions (which is why require probably won't work).

like image 45
Nicol Bolas Avatar answered Sep 22 '22 12:09

Nicol Bolas