Extract function body
How can I extract function body (as a string)? For example I call C function, extract function 开发者_如何学JAVAfrom stack, check if type is LUA_TFUNCTION
and what do I need to do to get its body?
When the function is on the stack, it has already been compiled. The best you can try to do is a lua_dump
and then decode the bytecode.
You can call lua_getinfo
with a string parameter of "S", then check the "source" member of the lua_Debug structure. If that string starts with '@', it's a filename, and you'll need to re-read the file if you want the source (Lua only read the file incrementally to load the function and never saved it as a string). Otherwise, its contents will be the string loaded as the chunk the function was defined in.
Note that in either case the source
returned will be the entire chunk that defined the function in question. You can narrow the string down to only that function using the other fields defined in the structure: note, however, that this is not a guarantee that you will be able to load that string back in to get the same behavior (the function definition may refer to variables defined in an outer scope, for instance).
The Debug library can do this. The Lua C API doesn't have it, you'd want to call a Lua function for this purpose.
精彩评论