lua-users home
lua-l archive

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


David Manura wrote:
> In Lua 5.2.0-beta-rc4, gotos allow transactional error handling code
> to be written something like this:
> 
>   -- 5.2.0-beta-rc2
>   function perform_transaction()
>     if not f() then goto undone_f end  -- [*2]
>     if not g() then goto undone_g end  -- [*2]
>     local hval = h()
>     if not hval then goto undone_h end  -- [*2]
> 
>     local x
>     .....
> 
>     return true  -- success [*1]
> 
>     -- rollback
>     ::undone_h::
>     undo_g()  ::undone_g::
>     undo_f()  ::undone_f::
>     print(x)
> 
>     return false  -- failure
>   end
> 
> except for two issues.  First, the "return true" is required to be
> wrapped in do/end [*1].  Secondly, and more importantly, the gotos
> will be rejected as written because they jump into the scope of locals

You could catch both issues with a single do/end, the "do" directly
at the start of the function and the "end" after the "return true":

  function foo()
     do
        function body with gotos and locals
        return
     end
     ::labels::
     cleanup
  end

Though I would have no problems if jumping into the scope of new
locals would simply nil them (may even ease error handling).
Doesn't OP_JMP already include the target stack level anyway (for
closing upvalues)?

Ciao, ET.