Meta Info on Hollywood vars at runtime ?

Discuss any general programming issues here

Meta Info on Hollywood vars at runtime ?

Postby p-OS » Mon Apr 24, 2017 6:02 pm

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 ?
p-OS
 
Posts: 72
Joined: Tue Nov 02, 2010 12:56 am

Re: Meta Info on Hollywood vars at runtime ?

Postby airsoftsoftwair » Wed Apr 26, 2017 9:45 pm

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.:

Code: Select all
Function test()
EndFunction

For i,v In Pairs(_G)
   If GetType(v) = #FUNCTION
      If v = test Then DebugPrint(i)
   EndIf
Next


This will print "test". You can easily adapt this code to resolve global table names from references as well (simply change GetType() to #TABLE).
User avatar
airsoftsoftwair
 
Posts: 2198
Joined: Fri Feb 12, 2010 3:33 pm
Location: Germany

Re: Meta Info on Hollywood vars at runtime ?

Postby p-OS » Thu Apr 27, 2017 1:33 am

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:

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)




outputs:
The function given as parameter has as real name: sum


That's exactly what I need :D
p-OS
 
Posts: 72
Joined: Tue Nov 02, 2010 12:56 am

Re: Meta Info on Hollywood vars at runtime ?

Postby airsoftsoftwair » Thu Apr 27, 2017 5:32 pm

seems, this here is working:


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:

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)


In that case your doAction() function will report sum, sum2, and sum3 because they're all globals, even though they reference the very same function.

Relying on undocumented Features in general might not be the best coding practise


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...
User avatar
airsoftsoftwair
 
Posts: 2198
Joined: Fri Feb 12, 2010 3:33 pm
Location: Germany

Re: Meta Info on Hollywood vars at runtime ?

Postby p-OS » Thu Apr 27, 2017 7:14 pm

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):
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
*/


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 ..
p-OS
 
Posts: 72
Joined: Tue Nov 02, 2010 12:56 am


Return to General programming

Who is online

Users browsing this forum: No registered users and 3 guests