Stacktraces from dynamically-constructed functions not as expected

Vivin Suresh Paliath vivin.paliath at gmail.com
Fri May 6 22:39:57 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)*


Is there a reason for this discrepancy? Isn't the second version
effectively the same as the first? Also, why is it *f$foo* instead of *foo* in
the first case?

I am running jdk8u92.

Thanks!

-- 
Ruin untold;
And thine own sadness,
Sing in the grass,
When eve has forgot, that no more hear common things that gleam and pass;
But seek alone to lip, sad Rose of love and ruin untold;
And thine own mother
Can know it as I know
More than another
What makes your own sadness,
Set in her eyes.

map{@n=split//;$j.=$n[0]x$n[1]}split/:/,"01:11:02".
":11:01:11:02:13:01:11:01:11:01:13:02:12:01:13:01".
":11:04:11:06:12:04:11:01:12:01:13:02:12:01:14:01".
":13:01:11:03:12:01:11:04:12:02:11:01:11:01:13:02".
":11:03:11:06:11:01:11:05:12:02:11:01:11:01:13:02".
":11:02:12:01:12:04:11:06:12:01:11:04:12:04:11:01".
":12:03:12:01:12:01:11:01:12:01:12:02:11:01:11:01".
":13:02:11:01:02:11:01:12:02";map{print chr unpack"
i",pack"B32",$_}$j=~m/.{8}/g


More information about the nashorn-dev mailing list