Re: Introspection?

new topic     » goto parent     » topic index » view thread      » older message » newer message

In Phix:

function me(integer rid) 
    return get_routine_info(rid)[4] 
end function 
 
procedure Hello(sequence place)  
    puts(1, me(Hello) & ", " & place & "!\n")  
end procedure  
  
Hello("World")  

or

function me() 
    integer rid 
    #ilASM{ 
        [32] 
            mov eax,[ebp+20]    -- prev_ebp 
            mov eax,[eax+8]     -- rtn_id 
            mov [rid],eax 
        [64] 
            mov rax,[rbp+40]    -- prev_rbp 
            mov rax,[rax+16]    -- rtn_id 
            mov [rid],rax 
          } 
    return get_routine_info(rid)[4] 
end function 
 
procedure Hello(sequence place)  
    puts(1, me() & ", " & place & "!\n")  
end procedure  
  
Hello("World")  

If there is a real use for it, I could easily make get_routine_info(0) yield {...,"me"}, and get_routine_info(-1) yield {...,"Hello"}, ie get a rid<=0 from the call stack.
Heck, why not just do that anyway:

requires("1.0.3") 
function me() 
    return get_routine_info(-1)[4] 
end function 
 
procedure Hello(sequence place)  
    puts(1, me() & ", " & place & "!\n")  
end procedure  
  
Hello("World")  

PS: My JavaScript implementation of get_routine_info() was this:

function get_routine_info(fn) { 
    return ["sequence",fn.length,0,"",fn.name]; 
} 

Which means:
"sequence": ignore this / 1-based indexes,
fn.length: the max number of arguments,
0: there is no min "" in js,
"": there is no function signature in a typeless language,
fn.name is probably a bit better than what you were using?

new topic     » goto parent     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu