detecting unregistered functions
I have recently started using Lua embedded within a C++ application. Obviously, if you try to call a C function that has not been registered, you get an error when you attempt to call that function. Is there an easy way to get a list of all C-functions that a Lua script references so that I can detect the errors at load-time instead of what may be a much later run-time?
Thanks
Well the simple way to handle something like this is just to check if the function is nil, which is equivilent in Lua to asking if it exists.
Say you have a function foo registered:
if not foo then
print "Uh oh. foo is not registered with Lua."
end
Or say you have several functions, foo1, foo2, and foo3, that all may or may not be registered, but you want the script to be able to adapt to them dynamically. You could bind the function with code like this:
foo = foo1 or foo2 or foo3
This just means foo will be assigned to foo1, and foo2 if foo1 is nil, and foo3 if both foo1 and foo2 are nil.
Say you have a function foo registered:
if not foo then
print "Uh oh. foo is not registered with Lua."
end
Or say you have several functions, foo1, foo2, and foo3, that all may or may not be registered, but you want the script to be able to adapt to them dynamically. You could bind the function with code like this:
foo = foo1 or foo2 or foo3
This just means foo will be assigned to foo1, and foo2 if foo1 is nil, and foo3 if both foo1 and foo2 are nil.
Thanks, but checking each function explicitly to see if it is nil is not quite what I need. What I would like to do is to load a lua file that contains multiple functions. Each of these functions contain multiple calls to C-functions that I have registered from my C application. I would like to check upon loading the lua script that all C-function calls contained in the lua functions have been registered, so that I do not need to wait until the lua function gets called to find out that a problem exists.
I don't want to have to manually check each function to see if it is nil, because I may forget to check one, or spell it wrong in the function definition, but spell it correctly in the check. I'm looking for an automatic or programmatic solution so that I can have many scripts and not have to worry about any missing functions which may be due to typos.
Thanks for the speedy response, though.
I don't want to have to manually check each function to see if it is nil, because I may forget to check one, or spell it wrong in the function definition, but spell it correctly in the check. I'm looking for an automatic or programmatic solution so that I can have many scripts and not have to worry about any missing functions which may be due to typos.
Thanks for the speedy response, though.
AFAIK the only way you could do that would be to parse the LUA script yourself and hence get a list of all the functions called and then check that against the list of registered functions.
Though really I don't see the point, if you do detect a function being called that isn't registered there's not much you can do about it apart from manually alter the script to fix it. You may as well just run your scripts normally without checking first and then if it fails, print out the LUA error message and then go fix it.
Though really I don't see the point, if you do detect a function being called that isn't registered there's not much you can do about it apart from manually alter the script to fix it. You may as well just run your scripts normally without checking first and then if it fails, print out the LUA error message and then go fix it.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement