[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: help to (slightly) modify Lua interpreter
- From: Leo Razoumov <slonik.az@...>
- Date: Fri, 6 Nov 2009 17:30:00 -0500
On 2009-11-06, Roberto Ierusalimschy <roberto@inf.puc-rio.br> wrote:
> > I'm planning to translate the code like that:
> >
> > m[i, j] => __index(m, i, j)
> > m[i, j] = v => __newindex(m, i, j, v)
> >
> > So all the indexes will be feeded to the __index or __newindex
> > metamethods in the order. I guess that the variable number of
> > arguments is not a problem: the metamethods are supposed to check how
> > many arguments and act accordingly.
>
>
> I guess the next code gives a reasonable solution for 2D arrays.
> (It should not be too dificult to generalize it to n dimensions.)
>
> -------------------------------------
> local mt1 = { __index = function (t,k)
> local nt = setmetatable({}, {
> __index = function (t1, k1)
> return t:index(k,k1)
> end,
> __newindex = function (t1, k1, v)
> t:newindex(k,k1,v)
> end,
> })
> t[k] = nt
> return nt
> end,
> mode = 'v',
> }
>
>
> function newmatrix (index, newindex)
> return setmetatable({index = index, newindex = newindex}, mt1)
> end
> -------------------------------------
Every time m[i][j] is called two new closures are created to be used
only once. Would it drive GC crazy if m[i][j] is used withing loops
millions of times??
--Leo--