Netrus
Netrus

Reputation: 79

Is it possible to use string as AS3 statement?

for example i have a string

    var string:String = new String("myFunc(para1, para2);");

is there any way i could use this value of string as an AS3 statement?

note: the amount of parameters may vary, as the value of string will be decided dynamically, so if you are suggesting me to use string properties to seperate parameters and function name and then use it like in many tutorials :-

    this[string](para1, para2);

that wont solve my problem.

Upvotes: 2

Views: 438

Answers (2)

mitim
mitim

Reputation: 3201

I believe you could still solve your problem by breaking your string up. You can write your method/function to take in a variable amount of parameters using the 'rest' parameter or the arguments object. Both of which are mentioned here with examples:http://help.adobe.com/en_US/ActionScript/3.0_ProgrammingAS3/WS5b3ccc516d4fbf351e63e3d118a9b90204-7f56.html

Based off my comment, some sample code:

// this just takes in a bunch of numbers and returns the sum
function myFunc(...args:Array):Number {
    var result:Number = 0;
    for(var counter:int = 0; counter < args.length; counter++){
        result += args[counter];
    }
    return result;
}

// can manually call it and pass in as many parameters as you want
myFunc(1,2,3); // will return 6
myFunc(1,2,3,4,5); // will return 15
myFunc(1,2,3,4,5,6,7,8,9,10); // will return 55


// and based on your comment:
var functionName:String = "myFunc";
var parameters:Array = new Array(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20);
// since you've mentioned your parameters in an array already, can just use the apply() call as Pan suggested
this[functionName].apply(this, parameters); // will return 210

Upvotes: 1

Jason Sturges
Jason Sturges

Reputation: 15955

If you're referring to dynamically evaluating and executing expressions by interpreting code from strings, ActionScript 3 does not support eval() as some languages do.

There are some bytecode generators such as the ActionScript 3 Eval Library and as3scriptinglib which accomplish similar real time interpretation of code.

As noted, you could leverage a function rest parameter:

public function myFunc(... args) {
    for each (var arg:String in args) {
        trace(arg);
    }
}

Called as:

myFunc("para1");
myFunc("para1", "para2");

Or, use a Function object passing an array of parameters using apply(), called as:

var f:Function = myFunc;
f.apply(this, ["para1", "para2"]);

Or, pass parameters using call(), called as:

var f:Function = myFunc;
f.call(this, "para1", "para2");

Another approach would be creating a Domain Specific Language to be parsed; or, simply parsing the string value as ActionScript such as:

package {

    public class DSL {

        public static function parse(thisArg:*, statement:String) {
            // get the function name
            var fn:String = statement.substr(0, statement.indexOf('('));

            // get parameters (between parenthesis)
            var params:Array = statement.substring(statement.indexOf('(') + 1, statement.lastIndexOf(')')).split(',');
            params.forEach(function(element:*, index:int, arr:Array):void {
                arr[index] = this[element.replace(/^\s+|\s+$/g, '')];
            }, thisArg);

            // execute
            var f:Function = thisArg[fn];
            f.apply(thisArg, params);
        }
    }
}

As a demo, call DSL.parse() and pass a string referencing a function, and properties:

package {

    public class Demo {

        public var para1:String = "hello";
        public var para2:String = "world";

        public function myFunc(... args) {
            for each (var arg:String in args) {
                trace(arg);
            }
        }

        public function Demo() {
            DSL.parse(this, "myFunc(para1);");
            DSL.parse(this, "myFunc(para1, para2);");
        }
    }
}

This would output:

hello

hello
world

Upvotes: 0

Related Questions