Stacktraces from dynamically-constructed functions

Vivin Suresh Paliath vivin.paliath at gmail.com
Thu May 12 20:49:58 UTC 2016


I have the following code:

*var f = (function() {*
*    return function foo() {*
*        try {*
*            throw new Error();*
*        } catch(e) {*
*            print(e.stack);*
*        }*
*    }*
*})();*


When I call the function, I get the following stacktrace as expected
(mostly; I was expecting *foo* instead of *f$foo*).

*Error*
*        at f$foo (<shell>:1)*
*        at <program> (<shell>:1)*


However, if I dynamically construct the function as follows:

*var f = new Function([], "return function foo() { try { throw new Error();
} catch(e) { print(e.stack); } }")()*


I get:


*Error*
*        at <anonymous> (<function>:2)*
*        at <program> (<shell>:1)*


In this particular situation, isn't the second version effectively the same
as the first? Also, shouldn't it just be *foo* instead of *f$foo*?

I am running jdk8u92.

Thanks!

-- 
*[vivin.net <http://vivin.net> :: github <http://github.com/vivin> ::
linkedin <https://www.linkedin.com/in/vivin>]*


More information about the nashorn-dev mailing list