This week's book giveaway is in the OO, Patterns, UML and Refactoring forum.
We're giving away four copies of Five Lines of Code and have Christian Clausen on-line!
See this thread for details.
Win a copy of Five Lines of Code this week in the OO, Patterns, UML and Refactoring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Nashorn es6 scoping issue

Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I am currently investigating following issue in Nashorn js scripting engine and I'd appreciate your input.
Following code snippet fails execution (code in Kotlin, sorry about that...):


Exception in thread "main" javax.script.ScriptException: ReferenceError: "descriptor1" is not defined in <eval> at line number 8

  • issue is influenced by eval function - ommiting it from script helps - also need to be nested in function - works fine at top level
  • issue is influenced by Object.getOwnPropertyDescriptor function - ommiting it from script helps
  • issue is influenced by scoping - changing variable declaration from scoped (const/let) to unscoped var helps
  • output log contains caught RewriteException [pp=3, type=object, value='Pepa')]

  • 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)
    Saloon Keeper
    Posts: 12127
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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:

    For this to work, you need runtime dependencies on the GraalVM JavaScript implementation and the GraalJS script engine factory. I'm not sure how you manage your dependencies, but this example POM might help you out:
    Jiří Linhart
    Posts: 2
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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:
    Thread Boost feature
      Bookmark Topic Watch Topic
    • New Topic