3

I've been trying to create methods dynamically from strings using Dart to no avail. String example: "(String str) => return str.length;". The idea is to allow users to create their own functions to apply to a given string. The only thing I've found is NoSuchMethod which does not seem to apply to my case. I tried using new Function in JavaScript but when passing the function to Dart and executing it, I get the following error: Uncaught TypeError: J.$index$asx(...).call$0 is not a function.

Code examples:

Dart:

context["UpdateNames"] =

(JsObject pTag)
{
    print(pTag["function"]("text"));
};

JS:

function execute ()
{
    var func = {"function": new Function("str", "return str.length;")};
    UpdateNames(func);
}

EDIT:

Solution: Create an object in JavaScript such as this:

this.fun = function (name)
  {
    var text = "var funs = " + document.getElementById("personalFun").value;
    eval(text);
    return funs(name);
  };

Then create the object in Dart:

caller = new JsObject(context['Point'], []);

Finally call the method to dynamically create the function:

caller.callMethod('fun', [text]);

2 Answers 2

6

I'm not sure to completely understand what you want achieve so i will try to provide the best answers

You want to dynamically add method to a class with a specific string identifier

In this case it's completely possible but you need to use some mirror so be careful if you want to use this for the web

here an implementation example :

import "dart:mirrors";

class Test {
  Map<String, dynamic> _methods = {};

  void addMethod(String name, var cb) {
    _methods[name] = cb;
  }

  void noSuchMethod(Invocation inv) {
    if (inv.isMethod) {
      Function.apply(_methods[MirrorSystem.getName(inv.memberName)],     inv.positionalArguments);
    }
  }
}

void testFunction() {
  for (int i = 0; i < 5; i++) {
    print('hello ${i + 1}');
  }
}

void testFunctionWithParam(var n) {
  for (int i = 0; i < 5; i++) {
    print('hello ${i + n}');
  }
}

void main() {
  var t = new Test();

  t.addMethod("printHello", testFunction);
  t.addMethod("printHelloPlusN", testFunctionWithParam);
  t.printHello();
  t.printHelloPlusN(42);
}

You want to "execute" code within a string

Sorry but it's not possible. it's a big requested features but it's not planned by the dart team be cause it will involve to many change and trad off.

Maybe it's possible to trick by creating dart file and use isolate to run it.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for the answer. What I wanted to do was exactly the second point. I made it work by calling a JavaScript method, which saddens me since I would like to be able to complete it in Dart.
1

Solution: Create an object in JavaScript such as this:

var FunctionObject = function() {
  this.fun = function (name)
  {
    var text = "var funs = " + document.getElementById("personalFun").value;
    eval(text);
    return funs(name);
  };
};

Then create the object in Dart:

caller = new JsObject(context['FunctionObject'], []);

Finally call the method to dynamically create the function:

caller.callMethod('fun', [text]);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.