[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Is Lua used as a data representation language?
- From: Daurnimator <quae@...>
- Date: Wed, 9 Jan 2013 05:16:39 +1100
On 9 January 2013 04:10, Roberto Ierusalimschy <roberto@inf.puc-rio.br> wrote:
>> On Tue, Jan 8, 2013 at 5:31 PM, Ashwin Hirschi <lua-l@reflexis.com> wrote:
>> > It doesn't seem to handle a simple case like:>
>> > while 1 do end
>>
>> OK, thanks - I'll try to close some more gaps. The approach here is
>> static checking, unlike the runtime limits Roberto suggests.
>
> Another runtime aproach is this:
>
> function loaddata (data)
> local f = assert(loadstring("return (" .. data .. ")"))
> local count = 0
> debug.sethook(function ()
> count = count + 1
> if count >= 2 then error"cannot call functions" end
> end, "c")
> local res = f()
> count = 0
> debug.sethook()
> return res
> end
>
> It only allows expressions (no variables, no assignments) and disallows
> any kind of function calls.
>
> -- Roberto
>
Don't forget to use a pcall (and the pcall counts as a call)!
Even if you wanted to through an error, you'd need to unset the hook.
function loaddata (data)
local f = assert(loadstring("return (" .. data .. ")"))
local count = 0
debug.sethook(function ()
count = count + 1
if count >= 3 then
error"cannot call functions"
end
end, "c")
local ok, res = pcall(f)
count = 0
debug.sethook()
return ok, res
end