lua-users home
lua-l archive

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


On Sun, Jun 21, 1998 at 12:14:25PM -0300, Dan Marks wrote:
> Is there any workaround for it, and perhaps maybe in C++ this
> should be handled using the exception mechanism?  Perhaps the
> lua_call should be encapsulated in a try {} catch block?  The
> only strategy I have been able to devise so far is:
> 
> void function_from_lua()
> {
>   int parm = luaL_check_number(1);
>   int iserror = 0;
>   {
>     object o(myconstructordata);
>  
>     if (do_the_work_fails()) {
>        iserror = 1;
>     } 
>   }  /* end of block ensures destructor executes */
>   if (iserror)
>     lua_error("Something bad happened");
> }
> 
> It is doable, but seems kind of clunky.  Perhaps something
> better exists?

Interesting. if I were you, I would just stop allocating objects on the
stack inside functions called from lua, and then you can explicitly
deallocate them before you call lua_error(). Or use a goto to work like a
finally block:

void function_from_lua()
{
  int param = luaL_check_number(1);
  {  
    object o(construction args)
    
    if (do_the_work_fails()) {
       goto error;
    }

    if (we_find_another_error) {
       goto error;
    }

    // continue doing useful work
   
  }

  return; // success
error:
  lua_error("Something bad happened");
}

However, I agree this stuff is pretty ugly. However, C++ is pretty ugly,
especially when you allocate objects on the stack. :)

-- 
David Jeske (N9LCA) + http://www.chat.net/~jeske/ + jeske@chat.net