Win a copy of Transfer Learning for Natural Language Processing (MEAP) this week in the Artificial Intelligence and Machine Learning 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 ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Paul Clapham
  • Devaka Cooray
  • Bear Bibeault
Sheriffs:
  • Junilu Lacar
  • Knute Snortum
  • Liutauras Vilda
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Piet Souris
Bartenders:
  • salvin francis
  • Carey Brown
  • Frits Walraven

Nashorn es6 scoping issue

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

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...):

Code


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

Observations
  • 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: 11881
    253
    • 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
    Greenhorn
    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.
     
    There’s no place like 127.0.0.1. But I'll always remember this tiny ad:
    Two software engineers solve most of the world's problems in one K&R sized book
    https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
      Bookmark Topic Watch Topic
    • New Topic