Current hypothesis Object.getOwnPropertyDescriptor function raises UnwarrantedOptimismException that forces the function to recompile.
Scope of recompiled script is corrupted. Usually the scope is manipulated by eval function usage so it can declare variables - maybe this is resolved incorrectly during the runtime recompilation.
Accessing scoped variables fails.
What outcome I hope for? (in order of preference)
it works just as when executed in node - obviously
issue is identified at compile time without turning off es6 features
disabling eval function prevents the issue - I tried this by removing eval from Bindings object but script is corrupted at compilation phase and modified Bindings are passed later to script.eval(bindings) - so the script still fails before reaching eval line
bypassing issue by turning off es6 features (removing --language=es6 - this forces the script to fail at compile time due to usage of scoped variables (const/let)
I managed to reproduce your problem in Java. Looks like a bug in Nashorn. I'm not sure if they will fix it because Nashorn is deprecated and I don't think it will be supported for much longer.
I managed to solve the problem by using GraalJS instead of Nashorn. Here is the Java version, I'm sure you can port it to Kotlin:
posted 2 months ago
Thank you for your input Stephan.
I am aware that Nashorn is deprecated, unfortunately there are some obstacles to overcome in order to transition to another scripting engine.
Graal based solution is considered as possible successor, so thank you for your example.
Time flies like an arrow. Fruit flies like a banana. Steve flies like a tiny ad: