Getting source code of a JsFunction object

Nov 7, 2011 at 5:09 PM
Edited Nov 15, 2011 at 2:09 PM


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  
   var b = a;




            JsFunction fctJs = fct as JsFunction;

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

                if(statements.Count == 0)
                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


                // 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 !


Nov 15, 2011 at 2:05 PM
Edited Nov 15, 2011 at 2: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 !