How to dump lua function chunk to string?

Flash picture Flash · Feb 6, 2013 · Viewed 17.7k times · Source

How to dump lua function chunk to string ?

function test(a, b)
  local c = a + b
  return c
end

print( type(test) )  --> function
print( test )         --> function: 0053B108
print( dumpToString(test) )

I wish dumpToString result is following:

function test(a, b)
  local c = a + b
  return c
end

How to do this ?

=== update1 ===
I want to automatically log and inject code.

Answer

Paul Kulchenko picture Paul Kulchenko · Feb 6, 2013

You don't say why you want to do this, which may be important. You can dump a function to a string, it just won't be a very readable string; you can store and transmit your function this way (between compatible Lua engines):

string.dump(function() print "Hello" end)