• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Is convert string to executable code possible?

Participant ,
Mar 07, 2018 Mar 07, 2018

Copy link to clipboard

Copied

Hi All,

Is it possible to convert string as executable code? like the below one.

For ex:

var fun1 = 'function myFun(arg){

     trace(arg);

}'

execute(fun1);

Thanks,

Guru

TOPICS
ActionScript

Views

1.2K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 07, 2018 Mar 07, 2018

Copy link to clipboard

Copied

Hi.

Try this:

var fun1:Function = function myFun(arg)

{

    trace(arg);

};

this["fun1"]("working");

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Mar 07, 2018 Mar 07, 2018

Copy link to clipboard

Copied

Hi JoãoCésar,

Thanks for your quick reply. Its very nice. But I'm trying something different. Let me explain more.

I have a xml called "action_scripts.xml" and in that xml I have some functions inside CDATA. I get that xml data in flash and try to execute that scripts in run time. I will get that functions as string from the xml and I want it to convert as executable functions inside as3.

action_scripts.xml

<?xml version="1.0" encoding="utf-8"?>

<action_scripts>

     <action_script><![CDATA[function fun1(){var a=5; var b=5; var c = a+b; trace('result = '+c)}]]></action_script>

     <action_script><![CDATA[function fun2(){var a=5; var b=5; var c = a+b; trace('result = '+c)}]]></action_script>

</action_scripts>

test.fla

var urlLoader:URLLoader;

var urlRequest:URLRequest;

var loader:Loader;

var myXMLData:XML;

urlRequest = new URLRequest("action_scripts.xml");

urlLoader = new URLLoader(urlRequest);

urlLoader.addEventListener(Event.COMPLETE, urlLoaderEventComplete);

function urlLoaderEventComplete(e:Event){

myXMLData = new XML(e.target.data);

executeFun(myXMLData.action_script[0]);

}

function executeFun(fun){

trace(fun) // result as string -> "function fun1(){var a=5; var b=5; var c = a+b; trace('result = '+c)}"

var myFun = fun as Function;

this["myFun"](); // Here the code should work as real function

}

When I published, I'm getting the below error

TypeError: Error #1006: value is not a function.

How can I convert a value/string as real executable function? Is it possible? Please advice me .

Thanks,

Guru

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 08, 2018 Mar 08, 2018

Copy link to clipboard

Copied

Oh I see.

I think this thread is what you need: How to interpret code from string in as3? .

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Mar 08, 2018 Mar 08, 2018

Copy link to clipboard

Copied

Hi JoãoCésar,

Thanks you again for the reply.

I already checked that code. But, it seems that one is related to AS2 and not AS3. The eval() is no more in AS3 I think. Any other options?

Thanks,

Guru

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 08, 2018 Mar 08, 2018

Copy link to clipboard

Copied

There are some links in that thread to third party libraries that are alternatives to the eval function:

http://eval.hurlant.com/

http://code.google.com/p/as3scriptinglib/

If those don't work, you will have to consider using JavaScript's eval function through the ExternalInterface class:

Convert a string to an operator

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Mar 08, 2018 Mar 08, 2018

Copy link to clipboard

Copied

LATEST

Thank you JoãoCésar. Let me try all.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines