Getting source code of a JsFunction object

Nov 7, 2011 at 4:09 PM
Edited Nov 15, 2011 at 1:09 PM

Hello,

i`ve another question regarding your awesome Jint library.

I need to determine the full source code of a JsFunction object passed as parameter to my C# function. I have already tried some things (ToString, GetBody() etc.) but none of them worked as I expected.

My current solution is to iterate through all block statements within the function and to concatenate the source code as follows:

 

//Js code of JsFunction  
function(a){
   var b = a;
   alert(b);
}

C#

 

 

            JsFunction fctJs = fct as JsFunction;

            if (fctJs != null)
            {
                LinkedList<Statement> statements = ((BlockStatement)fctJs.Statement).Statements;

                if(statements.Count == 0)
                    return;
                
                StringBuilder code = new StringBuilder();
                code.Append(fctJs.ToString().Substring(0, fctJs.ToString().IndexOf('{') + 1));

                foreach (Statement statement in statements)
                    code.Append(statement.Source.Code); // statement.Source is NULL if statement is variable declaration for example

                code.Append("}");

                // pass code to another function
            }

 

This works for block statements but not for variable declarations or something similar (because Statement.Source is NULL in this case).

So my question is: How can I get the full source code (if possible also with function header) of a JsFunction in a simple and fully working way ? I do NOT want to execute it I just want to retreive it...

Thank you very much !

Greets,
Thomas

Nov 15, 2011 at 1:05 PM
Edited Nov 15, 2011 at 1:05 PM

By the way the function GetBody() of an JsInstance object just returns "/* js code */" like ToString() also does even if the method body is not empty (like above).

Is this behaviour intended or maybe a bug ?

I`m using Jint 0.9.2 not the latest trunk.

Thanks !

Greets,
Thomas