jeudi 18 mars 2021

How do I get information about function calls from a Lua script?

I have a script written in Lua 5.1 that imports third-party module and calls some functions from it. I would like to get a list of function calls from a module with their arguments (when they are known before execution).

So, I need to write another script which takes the source code of my first script, parses it, and extracts information from its code.

Consider the minimal example.

I have the following module:

local mod = {}

function mod.foo(a, ...)
    print(a, ...)
end

return mod

And the following driver code:

local M = require "mod"
M.foo('a', 1)
M.foo('b')

What is the better way to retrieve the data with the "use" occurrences of the M.foo function?

Ideally, I would like to get the information with the name of the function being called and the values of its arguments. From the example code above, it would be enough to get the mapping like this: {'foo': [('a', 1), ('b')]}.

I'm not sure if Lua has functions for reflection to retrieve this information. So probably I'll need to use one of the existing parsers for Lua to get the complete AST and find the function calls I'm interested in.

Any other suggestions?





Aucun commentaire:

Enregistrer un commentaire