Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function scope(body)
- local list,res={}
- local function auto(close,msg)
- return function(t)
- if t[1] then table.insert(list,{ arg=t[1], fn=close or io.close })
- else error(t[2] or "no resource",2) end
- return table.unpack(t)
- end
- end
- local ok,err=pcall(function() res=table.pack(body(auto)) end)
- for i=#list,1,-1 do list[i].fn(list[i].arg) end
- if not ok then
- if type(err)~='string' then error(err,2)
- else error("scope error\nlua: "..err,2) end
- end
- return table.unpack(res)
- end
- local f1,f2,f3
- scope(function(auto)
- f1=auto(io.close){ io.open("test1.txt","w") }
- f2=auto(io.close){ io.open("test2.txt","w") }
- f3=auto(io.close){ io.open("test8.txt","r") }
- f1:write "test"
- --error"shit happens"
- print "done"
- end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement