SitePoint Sponsor

User Tag List

Results 1 to 11 of 11
  1. #1
    SitePoint Enthusiast
    Join Date
    Feb 2004
    Location
    Third Stone From The Sun
    Posts
    82
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Error.prototype.stack, other browsers

    Does anyone know of a hacked up JS patch for Error.prototype.stack or a getStack functionality for IE and Safari?

    Does Opera have Error.prototype.stack?

    Maybe something like:
    Code:
    Error.getStack = function getStack( error ) {
      if( error.stack ) return error.stack;
     
     // look up the call chain...
    var fakeStack = [ ];
    for( var caller = arguments.callee.caller; 
            caller != null; caller = callee.caller ) {
       fakeStack.push( caller );
    }
    return fakeStack;
    };
    This is just typed in. I need tested library code that I can rely on.

    Thank you,

    Garrett
    Last edited by DHTML Kitchen; May 1, 2007 at 09:13. Reason: missing [/code]
    My outdated site is down for a while now.

  2. #2
    I'll take mine raw silver trophy MikeFoster's Avatar
    Join Date
    Dec 2002
    Location
    Alabama, USA
    Posts
    2,560
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Trying the following on Opera9.10 and FF2 on WinXP:
    Code:
    window.onload = function()
    {
      test();
    };
    function test()
    {
      alert(Error.stack + ', ' + Error.prototype.stack + '\n' + arguments.callee.caller);
    }
    Opera gives:

    "undefined, undefined
    undefined"

    FF gives:

    "undefined, undefined
    function() {
    test();
    }"

    Not what I expected.

  3. #3
    SitePoint Enthusiast
    Join Date
    Feb 2004
    Location
    Third Stone From The Sun
    Posts
    82
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok, so "stack" in Error.prototype is false. That should not be false, I think. Just like we can test for "concat" in Array.prototype;

    But we can still inspect an error instance to see if it has a stack property.

    you can test this using a try/catch.


    Code:
    Error.getStack = function getStack( error ) {
      if( error.stack ) return error.stack; // native support.
    If opera doesn't support arguments.callee, how can we get a reference to the function of context?

    Does Opera support arguments.caller? How about Function.prototype.caller?

    Code:
    // this was ecma-deprecated. Does opera support?
    var f = someFunction; 
    caller = someFunction.caller; 
    callerCaller = caller.caller;
    My outdated site is down for a while now.

  4. #4
    I'll take mine raw silver trophy MikeFoster's Avatar
    Join Date
    Dec 2002
    Location
    Alabama, USA
    Posts
    2,560
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    My test follows. It is also online.
    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
    "http://www.w3.org/TR/html4/strict.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>Test</title>
    <script type='text/javascript'>
    window.onload = function()
    {
      test1();
      test2();
    };
    function test1()
    {
      try {
        var x = window.object_that_doesnt_exist.value;
      }
      catch (e) {
        msg('Test 1');
        msg('e.stack = ' + e.stack);
        msg('e.prototype = ' + e.prototype);
      }
    }
    function test2()
    {
      msg('Test 2');
      msg('arguments.caller = ' + arguments.caller);
      msg('test2.caller = ' + test2.caller);
      msg('arguments.callee = ' + arguments.callee);
      msg('test2.callee = ' + test2.callee);
    }
    function msg(s)
    {
      document.getElementById('msg').innerHTML += '<p>' + s + '<\/p>';
    }
    </script>
    </head>
    <body>
    <h1>Test</h1>
    <h2>Results for Opera 9.10 on WinXP</h2>
    <pre>
    Test 1
    
    e.stack = undefined
    
    e.prototype = undefined
    
    Test 2
    
    arguments.caller = undefined
    
    test2.caller = undefined
    
    arguments.callee = function test2() { msg("Test 2"); msg("arguments.caller = " + arguments.caller); msg("test2.caller = " + test2.caller); msg("arguments.callee = " + arguments.callee); msg("test2.callee = " + test2.callee); } 
    
    test2.callee = undefined
    </pre>
    <h2>Results for FF 2.0 on WinXP</h2>
    <pre>
    Test 1
    
    e.stack = test1()@file://localhost/C:/Documents&#37;20and%20Settings/mfoster_local/My%20Documents/www/Support/test2.html:16 ([object Event])@file://localhost/C:/Documents%20and%20Settings/mfoster_local/My%20Documents/www/Support/test2.html:10 @:0
    
    e.prototype = undefined
    
    Test 2
    
    arguments.caller = undefined
    
    test2.caller = function () { test1(); test2(); }
    
    arguments.callee = function test2() { msg("Test 2"); msg("arguments.caller = " + arguments.caller); msg("test2.caller = " + test2.caller); msg("arguments.callee = " + arguments.callee); msg("test2.callee = " + test2.callee); }
    
    test2.callee = undefined
    </pre>
    <h2>Results for IE 7 on WinXP</h2>
    <pre>
    Test 1
    
    e.stack = undefined
    
    e.prototype = undefined
    
    Test 2
    
    arguments.caller = [object Object]
    
    test2.caller = function() { test1(); test2(); }
    
    arguments.callee = function test2() { msg('Test 2'); msg('arguments.caller = ' + arguments.caller); msg('test2.caller = ' + test2.caller); msg('arguments.callee = ' + arguments.callee); msg('test2.callee = ' + test2.callee); }
    
    test2.callee = undefined
    </pre>
    <h2>Results for IE 6 on WinNT</h2>
    <pre>
    Test 1
    
    e.stack = undefined
    
    e.prototype = undefined
    
    Test 2
    
    arguments.caller = [object Object]
    
    test2.caller = function() { test1(); test2(); }
    
    arguments.callee = function test2() { msg('Test 2'); msg('arguments.caller = ' + arguments.caller); msg('test2.caller = ' + test2.caller); msg('arguments.callee = ' + arguments.callee); msg('test2.callee = ' + test2.callee); }
    
    test2.callee = undefined
    </pre>
    <h2>Test Messages</h2>
    <div id='msg'></div>
    </body>
    </html>
    I forgot to add "test2.prototype.caller" to the test, but it is undefined in Opera.

  5. #5
    SitePoint Enthusiast
    Join Date
    Feb 2004
    Location
    Third Stone From The Sun
    Posts
    82
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    added test2.caller to the test code.
    Code:
    function test2()
    {
      msg('Test 2');
      msg('arguments.caller = ' + arguments.caller);
      msg('test2.caller = ' + test2.caller);
      msg('arguments.callee = ' + arguments.callee);
      msg('test2.callee = ' + test2.callee);
      msg('test2.caller = ' + test2.caller);
    }
    Looks like Opera9 supports neither Function.prototype.caller nor arguments.caller. Although arguments.caller was deprecated, Function.prototype.caller was not.

    I don't know of any another way to reference the caller.

    Safari: Result is the same as Opera.
    Code:
    Test 2
    
    arguments.caller = undefined
    
    test2.caller = undefined
    
    arguments.callee = function test2() { msg("Test 2"); msg("arguments.caller = "+arguments.caller); msg("test2.caller = "+test2.caller); msg("arguments.callee = "+arguments.callee); msg("test2.callee = "+test2.callee); msg("test2.caller = "+test2.caller); }
    
    test2.callee = undefined
    
    test2.caller = undefined
    My outdated site is down for a while now.

  6. #6
    SitePoint Enthusiast
    Join Date
    Feb 2004
    Location
    Third Stone From The Sun
    Posts
    82
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It should be possible to get a stack trace from IE, at least.
    My outdated site is down for a while now.

  7. #7
    SitePoint Enthusiast
    Join Date
    Feb 2004
    Location
    Third Stone From The Sun
    Posts
    82
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Filed a bug for Opera. Function.prototype.caller should return function's caller.
    My outdated site is down for a while now.

  8. #8
    SitePoint Guru
    Join Date
    Apr 2006
    Posts
    802
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    caller has been deprecated in ecmaScript for years- implementations are encouraged NOT to use it.

    I always check for it in a new browser version. So far, IE and firefox still support it, and Opera doesn't. I don't know about Konquerer and Safari-

  9. #9
    SitePoint Enthusiast
    Join Date
    Feb 2004
    Location
    Third Stone From The Sun
    Posts
    82
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    arguments.caller was deprecated.

    Function.prototype.caller is a non-standard feature, but is essential for practical debugging.

    http://developer.mozilla.org/en/docs...unction:caller
    https://bugzilla.mozilla.org/show_bug.cgi?id=65683
    My outdated site is down for a while now.

  10. #10
    SitePoint Enthusiast
    Join Date
    Feb 2004
    Location
    Third Stone From The Sun
    Posts
    82
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This feature is also essential for enforcing entryPoints, or "access modifiers" as their called in java. private, protected.

    It is necessary for lazy-load singleton.
    My outdated site is down for a while now.

  11. #11
    I'll take mine raw silver trophy MikeFoster's Avatar
    Join Date
    Dec 2002
    Location
    Alabama, USA
    Posts
    2,560
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Interesting stuff!

    Thanks mrhoo and Garrett!



Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •