Forum Discussion

user_j_e's avatar
user_j_e
Occasional Contributor
14 years ago

function.caller and USEUNIT

I am running tests on a remote machine using TestExecute.



I was thinking I could do a Log.SaveResultsAs() and save the results locally so management can view the errors in a browser.



I wanted to save the file name of results as the function.caller + ".mht".



However the function.caller does not seem to span USEUNIT



For example when these two functions are in the same USEUNIT  and I run test();



function test()

{

  called();

}



function called()

{

  Log.Message(called.caller);

}



It prints the "function test() .... ";



If I call the "Called" function from a different USEUNIT it does not print the function.



Is there another way I can get the original caller name ?   I only plan on doing the Log.SaveresultsAs() for errors.



Thanks.

1 Reply

  • Hi,



    Information about the call stack is not passed across units (JScript doesn't have native support for multiple units).


    You can try using the following function to call routines across units and get the caller name:




    function callFunction(name)


    {


      var args = arguments;


      var callStr = name + "(";


      


      for(var i = 1; i < args.length; i++)


        callStr += ((VarType(args) == varOleStr)?"\"" + args + "\"":args) + ", ";


        


      callStr += "\"" + arguments.callee.caller.toString().match(/function (\w*)/)[1] + "\")";


      return eval(callStr);


    }






    Here's how to use it:




    //Unit1


    function callFunction(name)


    {


      //


    }


    ...


    var result = callFunction("Unit2.myFunction", "some parameter", "some other parameter");


    ...






    //Unit2


    function myFunction(p1, p2)


    {


      // Obtain the caller name:


      var callerName = arguments[arguments.length - 1];






      // Some code


      return "something";


    }






    In the code above, the caller name is passed to the target function as an additional parameter (it is passed after 'p1' and 'p2' in the example).