lua-users home
lua-l archive

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


I think that if you use loadstring then source will actually contain the source code.  See http://www.lua.org/pil/23.1.html 

Not sure though whether it is only that function or the full block passed to loadstring.

Thijs



-------- Oorspronkelijk bericht --------
Van: Marc Lepage <mlepage@antimeta.com>
Datum:
Aan: Lua mailing list <lua-l@lists.lua.org>
Onderwerp: Re: Lua round-trip?


OK I looked more closely at the debug API and ran an experiment.

If I have this:

return {

    {
        name='foo',
        val=42,
        _onA_ = function(x)
            return doSomething(x)
        end,
    },

    {
        name='bar',
        val=123,
        _onA_ = function(x)
            return doSomethingElse(x)
        end,
        _onB_ = function(x)
            return doAnotherThing(x)
        end,
    },

}

Then if I store the result in t, and I use debug.getinfo(t[1].onA, 'L'), I get info.activelines containing keys which are the "valid" lines of the function. In this case, they are the two lines containing "return doSomething(x)" and "end,".

If I make the assumption that the first line of the function immediately precedes, then I can tell which are the original source file lines containing the function. I can use those to recover the source code for each function.

I think this would be good enough for my needs.

However, if I provide flag 'S' I get fields 'lastlinedefined' ("end,") and 'linedefined' ("_onA_ = function(x)") which is perfect.

'S' also has fields 'source' and 'short_src' which I thought might contain the actual source code of the function, but they just have the name of the file. Otherwise that would be even more convenient.

So the solution is to use 'S' to get the first and last defined line number for each handler function, then extract them from the original source file when round-tripping.

Thanks!


On Fri, Nov 8, 2013 at 6:39 PM, Paul Merrell <marbux@gmail.com> wrote:
On Fri, Nov 8, 2013 at 11:13 AM, Marc Lepage <mlepage@antimeta.com> wrote:

> I'm going to take a crack at some kind of solution this weekend, so keep the
> ideas flowing.

I misunderstood and thought you wanted to create a new file each time.
Given that you're writing back to the same file, you could park the
functions in a library file and load the functions using dofile.

Paul


--
[Notice not included in the above original message:  The U.S. National
Security Agency neither confirms nor denies that it intercepted this
message.]