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, in which case the library will handle the stack
and errors differently and more tolerantly. 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).

I have a newbie question then: how should I call this function in an
*error-proof* way?

        -- takes 2 strings, returns a boolean
        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. Is it guaranteed to push a value if it succeeds (I
believe the docs say so)? Is it guaranteed to not leave anything on the
stack if it fails (couldn't find this in the docs)? The sample code in
the manual omits the lua_call return test for clarity, I assume. Is that
all I need, or do I need to set the stack to a saved absolute value on
return?

Bruno


-----Original Message-----
From: Luiz Henrique de Figueiredo [mailto:lhf@tecgraf.puc-rio.br] 
Sent: Monday, August 20, 2001 6:54 PM
To: Multiple recipients of list
Subject: Re: lua calling exit()

>(entire app exiting with no warning).

exit() is only called in luaD_breakrun. If there's no warning it's
because
_ERRORMESSAGE is not displaying it. (You said you've redefined it.)
But you'd only get "unable to recover; exiting", which is hardly
helpful...

>- What are some best practices for stack control?

One simple rule: never leave garbage on the stack. In other words,
unless
you're returning values to Lua, leave the stack as you found it.

>- Do I have to extensively and manually check before every stack
>operation to make sure I never pop an empty stack (causes protection
>faults later on) or allow the stack to leak (eventually causes exit()
on
>the entire app)?

You could try setting up call hooks and testing the stack only in these.

>- Is there a recommended way to completely prevent lua from ever
calling
>exit, or at least call my exit handler so I can let people know what's
>going on?

Simply define a Lua C function that does your main work and then
lua_call it,
instead of calling it as you'd normally do in C:
	lua_pushcclosure(L,f,0);
	lua_call(L,0,0);
If something goes wrong, error will be called normally.
--lhf