lua-users home
lua-l archive

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


>So if I understand correctly, the solution is to call your C code
>pretending it is lua

Yes.

>in which case the library will handle the stack
>and errors differently and more tolerantly

No, it'll just have something to longjmp to, that is, it'll be able to recover
from errors and return to the host program without having to exit().

>In the specific case that is
>causing me trouble, I'll just rewrite the function in lua instead of C
>(which might have been a better decision in the first place).

This is easier, yes.

>        handled = Function (arg1, arg2)
>
>        // calls Function
>        lua_getglobal(s_lua, "Function");
>        lua_pushstring (s_lua, arg1);
>        lua_pushstring (s_lua, arg2);
>        if (lua_call (s_lua, 2, 1) == 0)
>        {
>            handled = (bool) lua_tonumber (s_lua, -1);
>            lua_pop (s_lua, 1);
>        }
>
>This must work (i.e., leave a clean stack) regardless of what "Function"
>evaluates to and whether the execution of "Function" succeeded or failed
>for whatever reasons.

If you need this, I think the simplest solution is use lua_rawcall instead
of lua_call (same arguments, but lua_rawcall is void). In this case, if
"Function" fails, then you'll get an ordinary error through _ERRORMESSAGE.

>Is it guaranteed to push a value if it succeeds

Only if "Function" returns something.

>Is it guaranteed to not leave anything on the stack if it fails?

No, if "Function" is written in C, then it might fail after it had already
pushed some of the results. If "Function" is in Lua, then this cannot happen.

>do I need to set the stack to a saved absolute value on return?

If "Function" is in C, then this is the only safe way.
--lhf