Search code examples
javascriptjvmjitnashorn

Does Nashorn Javascript compile "eval" statements?


I understand that Nashorn compiles to JVM byte code on the fly. But, what does Nashorn do when it encounters the eval function with a String? Does it compile the string contents or interpret it?

For example:

function sayHi() {
  console.log("hi world");
}

for (var i=0;i<10;i++) {
   eval("sayHi()"); // what happens here?
}

A couple options could be: 1) it does not compile the string within an eval 2) it compiles it once, caches it, and then reuses the same byte code if it encounters the same string (as in the loop above) 3) it re-compiles the contents of an eval String a-fresh each time

Of course this is a small example in which the contents of an eval string is just a method call, but imagine it is more complex JS code being passed as a string into eval.


Solution

  • Nashorn always compiles javascript to bytecode for execution. There is no interpreter for JS. Yes, compiled/loaded Classes are unloaded if not reachable from live objects.