Shannon Max
Shannon Max

Reputation: 17

Functions in lua tables

I have someTabe = {}

someTabe.foo = function (x,y) return x + y end

How can I get pint(function"(function (x,y) return x + y end)??? Not return result.

Upvotes: 1

Views: 97

Answers (3)

val - disappointed in SE
val - disappointed in SE

Reputation: 1543

You can't get code itself, but you can get bytecode using string.dump():

local f = function(x,y) print('AAA') end)
local bytecode = string.dump(f) -- Get bytecode of function
local f2 = load(f) -- It is copy of f, but (f ~= f2)
f2() -- prints AAA

Upvotes: 0

A.J. Steinhauser
A.J. Steinhauser

Reputation: 40

You could could look for a decompiler online if you are just trying to see the code. However in your own code it is impossible.

Upvotes: 0

lhf
lhf

Reputation: 72402

You cannot recover the source code of a function from inside Lua.

> print(someTabe.foo)
function: 0x7fed0bc091f0

This is telling you that someTabe.foo contains a function, which has been converted to internal representation stored at the address shown.

If you need to recover the source code of a function from inside Lua, you need to compile it manually with load and then use the debug library to get the source code.

Upvotes: 1

Related Questions