5

I want to be able to take a string (with the proper Dart syntax) and convert it into a callable dart function. Is there a way to do that?

For example, I would receive the string,

void test() { print("testing!"); }

And then turn it into a callable function. The reason I want to do this is to be able to download dart files from other servers and call their functions.

Pixel Elephant
  • 20,649
  • 9
  • 66
  • 83
Austin Salgat
  • 416
  • 5
  • 13

3 Answers3

2

Per the dart FAQ, https://www.dartlang.org/support/faq.html#q-is-it-really-a-dynamic-language-if-it-doesnt-have-eval-or-adding-fields-to-a-value-at-run-time

Dart does not currently have an eval() function, nor support runtime compilation of arbitrary strings, though it may in the future.

So, you'll have to make your own VM within dart to do what you want to do.

Bandrami
  • 4,242
  • 1
  • 13
  • 12
  • Thank you. That's unfortunate but at least now I know. – Austin Salgat Dec 24 '13 at 05:22
  • 1
    I agree with the FAQ that I'm not sure in what sense dart is "dynamic", given that... And they stress the importance of javascript to the design, and eval is pretty crucial there. – Bandrami Dec 24 '13 at 05:34
  • 1
    One thing to keep in mind is that the lack of things like `eval` (which some argue aren't all that good to start with) allows the compiler to make more optimizations that would otherwise be possible. – Michael Fenwick Dec 24 '13 at 07:31
1

Depends on your target platform. If you are targeting the standalone Dart VM, it is already possible. The SDK tool Pub is doing it.

You can setup a application internal webserver that serves your function embedded in a gernated application. Than you can load the application into a additional isolate and use message passing to communicate with that isolate. This approach can also be used to create plugins for our application.

But it may take much time to implement this on your own. If you want to parse mathematic expression or other simplified sub parts of the language, you might look into the available Pub packages (parsers or math_exprerssions).

Fox32
  • 13,126
  • 9
  • 50
  • 71
0

What you can currently do is create JavaScript and use it's eval() and use JS-interop.

Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567