Hello, as HW compiles to kind of Byte Code I wonder if it preserves meta information like table and function names in the compiled code and if it is possible to access these informations ?
If I have a reference/pointer to a function, can I find out the name of this function ?
Meta Info on Hollywood vars at runtime ?
- airsoftsoftwair
- Posts: 5443
- Joined: Fri Feb 12, 2010 2:33 pm
- Location: Germany
- Contact:
Re: Meta Info on Hollywood vars at runtime ?
Yes, but only for global variables. Local variables are stored in VM registers and their names aren't preserved. That's also why local variables are much faster and should always be preferred to global variables.
You can get the name of a global function from its reference by simply iterating the global variable table _G, e.g.:
This will print "test". You can easily adapt this code to resolve global table names from references as well (simply change GetType() to #TABLE).
You can get the name of a global function from its reference by simply iterating the global variable table _G, e.g.:
Code: Select all
Function test()
EndFunction
For i,v In Pairs(_G)
If GetType(v) = #FUNCTION
If v = test Then DebugPrint(i)
EndIf
Next
Re: Meta Info on Hollywood vars at runtime ?
So there is a hidden feature
Relying on undocumented Features in general might not be the best coding practise, but good to know, migth be useful for debugging purposes.
If I understood right, when it comes to tables or functions (that are normally not copied but only passed as reference/pointer), it is not important that the variable (e.g. a named parameter defined in another function definition) is global ,but the function/table that it points to ?
seems, this here is working:
outputs:
Relying on undocumented Features in general might not be the best coding practise, but good to know, migth be useful for debugging purposes.
If I understood right, when it comes to tables or functions (that are normally not copied but only passed as reference/pointer), it is not important that the variable (e.g. a named parameter defined in another function definition) is global ,but the function/table that it points to ?
seems, this here is working:
Code: Select all
Function sum(a,b)
Return(a+b)
EndFunction
Function doAction(p_func)
For i,v In Pairs(_G)
If GetType(v) = #FUNCTION
If v = p_func Then DebugPrint("The function given as parameter has as real name: " .. i)
EndIf
Next
EndFunction
doAction(sum)
That's exactly what I needThe function given as parameter has as real name: sum
- airsoftsoftwair
- Posts: 5443
- Joined: Fri Feb 12, 2010 2:33 pm
- Location: Germany
- Contact:
Re: Meta Info on Hollywood vars at runtime ?
Yes, but you won't be able to get the name "p_func" from the global symbol table because it is a local variable. But you will be able to retrieve the name "sum" because that is a global variable. Be careful, though, because you can also end up with multiple names in case there are multiple global variables which point to the table or function, e.g. consider this code:seems, this here is working:
Code: Select all
Function sum(a,b)
Return(a+b)
EndFunction
sum2 = sum
sum3 = sum
Function doAction(p_func)
For i,v In Pairs(_G)
If GetType(v) = #FUNCTION
If v = p_func Then DebugPrint("The function given as parameter has as real name: " .. i)
EndIf
Next
EndFunction
doAction(sum)
Well, the _G symbol table isn't very likely to go away in future versions but still your whole endeavour looks a little ugly... I'm not sure why anybody would want to resolve variable references to strings but if you really feel like you have to, it's possible at least...Relying on undocumented Features in general might not be the best coding practise
Re: Meta Info on Hollywood vars at runtime ?
Hmm thanx for the hint, might indeed be dangerous.
You wonder why I wanted to do this. It has a background:
I have a lib (actually a table).
It offers some methods to the user (=developer using my lib).
I want to give the user the possibility to extend the lib by adding new methods.
He shall only provide his code snippet, the rest of the code is added by my function, which creates a new function and adds this as method to the lib.
sth like this (not tested):
For the case, that provided function is a named function and not an anonymous (defined on the fly) one, I wanted to make the function_name parameter optional, using the original's function name as default value ..
You wonder why I wanted to do this. It has a background:
I have a lib (actually a table).
It offers some methods to the user (=developer using my lib).
I want to give the user the possibility to extend the lib by adding new methods.
He shall only provide his code snippet, the rest of the code is added by my function, which creates a new function and adds this as method to the lib.
sth like this (not tested):
Code: Select all
mylib={..some data..}
function mylib:method1(..some params...)=...some code... endfunction
function mylib:method2(..some params...)=...some code... endfunction
function mylib:method3(..some params...)=...some code... endfunction
function mylib:addmethod(p_func,function_name)=...do some magic...endfunction
; now user adds a function:
function square(a)
return (a*a)
endfunction
mylib:addmethod(square,"squarify")
result=mylib:squarify()
/*
mylib:squarify is more complex than the function provided (square)
user just provides simple function, does not need to know internas of the lib and its data
*/