Tuesday, October 13, 2009

JavaScript Call Stack

This post explains how to get JavaScript Calls stack in Mozilla extension. You can also use jsdI interface hooks to get JS call stack. Pls refer jsdIDebuggerService on MDC.

You need to call the function callstack( ) to get the JSCall stack. From where to call this function is depend on need of user.  You can call this function wherever you want to get JS call stack.

function callstack( ){
/* stackFrameStrings holds entire JS calls stack in string format */

var stackFrameStrings = new Error().stack.split('\n');

/* remove first two stack frames because they are frames of callstack() and error() functions */
stackFrameStrings.splice(0,2);

  for (var i in stackFrameStrings) {
      /*
        A stack frame string split into parts. (example of stack frame string is:
        href("http://www.comp.nus.edu.sg")@:0)   Note 0 indicates native function call in Mozilla firefox, and 1 is for onclick, onmouseover functions, ie. JS event functions, otherwise the number is line number in source file and contains URL of source file. Anothe rexample of stack frame string is :
 onclick([object MouseEvent])@file:///home/patilkr/Desktop/Html%20Test/js_http_test.html:1
      */
      var stackFrame = stackFrameStrings[i].split('@');

    if (stackFrame && stackFrame.length == 2) {
                dump("\n stackFrame[0]=" + stackFrame[0] + " stackFrame[1]=" + stackFrame[1] );
   } // end of if loop

 }//end of for loop
} //end of callstack function
 

No comments:

Post a Comment