[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: string.gsub accepting a callable userdata
- From: Jerome Vuarand <jerome.vuarand@...>
- Date: Thu, 13 Dec 2012 18:27:11 +0000
2012/12/13 Rena <hyperhacker@gmail.com>:
> You want __mode='v', not __weak.
Yes, of course :-)
> And I'm not sure why you're using a table
> to store that one item. It's not necessary to use a table to create local
> upvalues:
But then you get a hard reference to the userdata object, and that may
change its lifetime.
> local gsub,type = string.gsub,type
> local ref
> local function unref(...) return ref(...) end
>
> function string.gsub(s,pat,repl)
> if type(repl) == 'userdata' then
> ref = repl
>
> repl = unref
> end
> return gsub(s,pat,repl)
> end
>
> Although this function just looks like an elaborate noop... is it needed at
> all?
What function, unref or the new string.gsub? The point of this whole
topic is to be able to pass a callable userdata to string.gsub, which
these two function achieve, so I don't think it's a no-op.