lua-users home
lua-l archive

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


On Tue, Mar 2, 2010 at 5:01 PM, Nicolas <nicolas@net-core.org> wrote:
> My bad :)
> Yet extracting from line to line would not get me the code of the function
> it would give me:
>        foo = function()
>                ....
>        end,
>
> which I cant extract and pass to loadstring (and it could be much worse
> and be embeded in a line with many other things).
You can scan the starting line for where "function" occurs, then trim
off everything prior, and also trim off the function name if it occurs
between "function" and "(". If "function" occurs more than once, then
iteratively try each position where it occurs. For the final line,
scan for "end" and trim off everything afterwards. If "end" occurs
more than once, then iteratively try each position. As long as you
don't define two functions which start and end on the same line (and
no sane programmer would), then loadstring("return function (etc.)
etc. end") will succeed for precisely one occurrence of "function" and
one occurrence of "end" (except for perhaps some strange and obscure
edge-cases which don't occur in the real world anyway).

Yes, it gives you a dependence on the debug library, and yes it is a
lot more work than dumping bytecode, but it does give you two
important advantages:
1) Works with Lua implementations with no bytecode format (e.g. LuaJIT2)
2) Serialised functions can be loaded by a Lua implementation which
has a different bytecode format to the Lua implementation which
serialised them (so you could move between 5.1 and 5.2, between
little-endian and big-endian, or between x86 and x64, etc.)