Function call by String name


#1

How can I call function by String name??


#2

Can you say a little bit more about what you want to do? Maybe something like this:

http://play.integer32.com/?gist=574df59aef4448bd1fac21e3d9bc8509&version=undefined


#3

Most likely by figuring out a different way to call your functions without involving string names. But how you do this depends a lot on the underlying problem you want to solve. Can you tell us more about that? Why do you want to call functions by their name?


#4

if i have json function name and i want to call function by string name


#5

The suggestion by @gregwtmtno should get the job done quickly.

Personally, I would introduce an intermediate enum type where each possible function is listed. This is especially nice if parsing/validation and execution take place at different times. But it does create some boilerplate.


#6

maybe hash map . just thought ?


#7

How many different functions do you have? If it is only a dozen, then I wouldn’t bother with a hashmap. If you really want to use one, you should be able to store boxed closures. (Maybe even unboxed ones, I don’t know about that)

http://play.integer32.com/?gist=4040b4c157a192d3c45bcfae99784289&version=undefined


#8

the code build map every time we call it.
if function have variables.

http://play.integer32.com/?version=stable&mode=debug


#9

Your link doesn’t point to any example. Did you forget saving it as a gist?

And the map is only build one if you store it. I updated the code to show that: http://play.integer32.com/?gist=c46e0dfa84fd293b3aca44d31e710262&version=undefined

If you want your functions to handle variables, I expect them to take different variables. At this point I believe that the suggestion of @gregwtmtno gives you the right amount of power.

You can extend the Hashmap example to functions which take a JSON parameter, but really how many different functions do you have? The way you are trying to do this doesn’t seem to fit a language like rust very well. I believe you should take a step back, look at your original problem again. You might come up with a different solution which is more suited to Rust. Maybe describe your problem here so other people can help you. Post some example data and what you want to happen. This looks a lot like the XY problem.


#10

I haven’t tried it but I suppose it might be possible to load up the DWARF information for the executable and then lookup the function name that way. It might be a bit naughty to do it that way. :stuck_out_tongue:


#11
  • Ehiggs:

I haven’t tried it but I suppose it might be possible to load up the
DWARF information for the executable and then lookup the function name
that way. It might be a bit naughty to do it that way. :stuck_out_tongue:

While that will work (unless the executable has been stripped), it’s easier on ELF systems (such as GNU/Linux) to link with -Bdynamic and use the dlsym function (with an RTLD_DEFAULT argument).


#12

I want to get function name in string and call function with variables
maybe if i can debugging function name in some how


#13

@miko_aab: It looks like you want Remote Procedure Calls.
How about json_rpc ?


#14

@fweimer, even better.

FWIW, I think OP’s question may come from a scripting language bent where you might have a DSL which calls functions. Instead of having to register every gosh darn function and making a mapping up front, it might be preferable to try to look up the symbols in the ELF and if the symbol exists, call it.

If that’s the case, if you’re working with Rust symbols, is it possible to parse the mangled name using a wrapper that yields something like fn get_func(name &str) -> Result<Fn(i32, i32) -> i32>? Using C apis you only get the symbol name and no information about the arguments or return values. And if I understand correctly there is some type erasure that takes place (maybe the terminology is incorrect but I think it’s dune by “coalescing generics”) which make this challenging.


#15

That sound interesting.

I just want to add, that this might pose a security risk - depending on the kind of input. Maybe you don’t want to expose every function of your crate, right? In that case I would suggest some kind of explicit mapping, or at least whitelisting. :slight_smile:

If no arguments are needed, then a simple macro could help to define that mapping
http://play.integer32.com/?gist=4d52c229918537b01dcdd84509c02b63&version=undefined

That sounds really interesting. Thx.


#16

If you are looking for something like Ruby’s .send method, you’re unlikely to find it as a built-in feature in compiled languages.

When source code is transformed into executable code, function calls become, essentially, jump statements, which require something like a memory address or a relative memory index.

In interpreted languages like Ruby, this transformation from scoped method name to memory index is (like everything else) done at runtime, so it’s easy to use some kind of string-like data (in Ruby’s case, a symbol) to perform the memory look-up for the destination address corresponding to the start of the desired function.

However, in compiled languages, the runtime typically doesn’t have the infrastructure necessary to perform this lookup on arbitrary strings. There would need to be some kind of lookup table in memory, and a mechanism for actually doing the search and invoking the function. There’s no reason this couldn’t exist, but there would definitely be a speed/memory cost.