lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Andrew:
Then don't use luaL_dofile, instead use luaL_loadfilex, which has a
"mode" parameter. Do _NOT_ attempt to assume that you know what a
compiled file looks like.


Yes, I don't assume anything. I read the source code of luaL_loadfilex
to see what kind of input (files) are read as binaries and wich other as source code.

Thanks to point me out the *mode* parameter.


About the return code of luaL_loadfile / luaL_loadfilex...
Andrew, you wrote
   That's "false" and "true" in the C sense, where 0 is false and anything that's not 0 is true.
and
   You can check the return value against the LUA_ERR* constants if the code needs to distinguish between file, syntax and runtime errors.


I stand my observations, this is a fallacy: a value is boolean or in a set of values.

Reading the source code, luaL_loadfilex can return with
LUA_OK
LUA_ERRFILE
other values, I cannot see (there are too many calls to other functions).

I think it is good to keep as much information as possible, when available, it is very useful for troubleshooting.

If the authors can confirm all the other possible values are part of LUA_ERR*
I suggest them to correct the manual: luaL_loadfilex returns LUA_OK or LUA_ERR*

M


On Mon, 11 May 2020 at 18:36, Andrew Gierth <andrew@tao11.riddles.org.uk> wrote:
>>>>> "Massimo" == Massimo Sala <massimo.sala.71@gmail.com> writes:

 [luaL_dofile]

 Massimo> 1) return code

 Massimo> The manual says " It returns false if there are no errors or
 Massimo> true in case of errors."

That's "false" and "true" in the C sense, where 0 is false and anything
that's not 0 is true.

i.e. the simplest usage is just

  if (luaL_dofile(L, fn))
  {
    /* failed; do something with the error message that's at the top of
       the Lua stack */
  }

You can check the return value against the LUA_ERR* constants if the
code needs to distinguish between file, syntax and runtime errors.

 Massimo> So it seems to me the return values can - at least - be 0, 1,
 Massimo> 7. Am I right ?

It can be any of the error values.

 Massimo> 2) loading compiled files

 Massimo> luaL_dofile can load plain source code and compiled scripts
 Massimo> (bytecode).

 Massimo> I have to check users don't pass filenames of compiled
 Massimo> scripts.

Then don't use luaL_dofile, instead use luaL_loadfilex, which has a
"mode" parameter. Do _NOT_ attempt to assume that you know what a
compiled file looks like.

--
Andrew.
_______________________________________________
lua-l mailing list -- lua-l@lists.lua.org
To unsubscribe send an email to lua-l-leave@lists.lua.org