Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nashorn and script binding scopes

Tags:

nashorn

Somewhat confused about ENGINE_SCOPE and GLOBAL_SCOPE binding in Nashorn, trying to follow the discussion here.

Before reading this my understanding of scopes (at least in rhino) was that there's a single, shared Bindings in the GLOBAL_SCOPE and individual bindings in ENGINE_SCOPE for each individual engine. However this page seems to be saying that each individual engine stores the basic javascript constructs in bindings that exist in the engines ENGINE_SCOPE (confusingly called the "Nashorn Global Scope"). This sounds like it makes the GLOBAL_SCOPE bindings effectively useless (because they won't have access to any of those basic constructs).

What I'm trying to do is create a context that I can inject a few scripts into, and then repeatedly eval different bindings in the context of those scripts. However if the only context I can access is the individual engines ENGINE_SCOPE (because anything above that won't have access to basic javascript constructs) then it seems that any local invocation has to add to those same bindings. Does anyone know how to manage multiple levels of bindings in Nashorn?

like image 727
Steve B. Avatar asked May 30 '14 16:05

Steve B.


People also ask

How does Nashorn work with Jython?

Nashorn running along with Jython on the JVM. In the example in Listing 1, along with the ScriptEngine code, Nashorn is directly using the Java 8 Stream interface and expression closure supplied to forEach —if the function has a simple return type, the braces and return keyword may be omitted.

Is Nashorn compatible with GraalVM JavaScript?

To learn more, please read Migration Guide from Nashorn to GraalVM JavaScript. This series of articles introduces Nashorn, a blazing fast JavaScript runtime engine that shipped with Java SE 8 to provide a lightweight environment for extending, supplementing, and often even replacing Java code.

What is Nashorn script engine factory?

Nashorn script engine factory used to create an engine with extra attributes. Not everything has to be pulled back to Java for processing. JavaScript's specification contains a built-in JSON Object that is used for encoding and decoding JSON format. Nashorn can effectively work around a lack of this functionality in JDK 8 and earlier versions.

What is loadwithnewglobal in Nashorn?

Because new code loaded into the Nashorn interpreter could clutter global scope, scripts that are intended for execution in isolation can be launched through the loadWithNewGlobal function. The global scope ENGINE_SCOPE is equivalent to ECMAScript's global, which is accessed through top-level use of the this keyword.


1 Answers

If a variable is not found in ENGINE_SCOPE, GLOBAL_SCOPE bindings is searched. Nashorn's global object (the one that has JS Object, Number, RegExp, parseInt etc.) is wrapped as Bindings - and that is your ENGINE_SCOPE. For eg. if you put "foo"->"hello" map entry into GLOBAL_SCOPE, that will be visible in scripts - if ENGINE_SCOPE does not have map entry by the name "foo".

like image 168
A. Sundararajan Avatar answered Sep 27 '22 23:09

A. Sundararajan