You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pivot.apache.org by rw...@apache.org on 2017/05/10 22:38:41 UTC

svn commit: r1794769 - /pivot/trunk/core/src/org/apache/pivot/beans/BXMLSerializer.java

Author: rwhitcomb
Date: Wed May 10 22:38:41 2017
New Revision: 1794769

URL: http://svn.apache.org/viewvc?rev=1794769&view=rev
Log:
PIVOT-965:  Further changes needed to resolve user script functions
that used to work with Rhino, but need more processing to work with
Nashorn.  This time it is to take the code that works with
ElementInvocationHandler and apply it to ScriptBindMapping as well.
In addition, there is another place that needs the Nashorn compat
script run before doing an "eval" of the Javascript.  Not sure why
this is, but maybe has to do with the wrong scope being used.  At
any rate, adding the compat script here makes things work.

Modified:
    pivot/trunk/core/src/org/apache/pivot/beans/BXMLSerializer.java

Modified: pivot/trunk/core/src/org/apache/pivot/beans/BXMLSerializer.java
URL: http://svn.apache.org/viewvc/pivot/trunk/core/src/org/apache/pivot/beans/BXMLSerializer.java?rev=1794769&r1=1794768&r2=1794769&view=diff
==============================================================================
--- pivot/trunk/core/src/org/apache/pivot/beans/BXMLSerializer.java (original)
+++ pivot/trunk/core/src/org/apache/pivot/beans/BXMLSerializer.java Wed May 10 22:38:41 2017
@@ -147,7 +147,7 @@ public class BXMLSerializer implements S
                     Bindings bindings = scriptEngine.getBindings(ScriptContext.ENGINE_SCOPE);
                     bindings.put(ARGUMENTS_KEY, args);
                     result = scriptEngine.eval(script);
-		    bindings.remove(ARGUMENTS_KEY);
+                    bindings.remove(ARGUMENTS_KEY);
                 } catch (ScriptException exception) {
                     reportException(exception, script);
                 }
@@ -183,7 +183,7 @@ public class BXMLSerializer implements S
             }
 
             return invocable.invokeFunction(methodName, args);
-	}
+        }
 
         @Override
         public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
@@ -233,28 +233,52 @@ public class BXMLSerializer implements S
             this.functionName = functionName;
         }
 
+        private Object invokeFunction(String functionName, Object value) {
+            Invocable invocable;
+            try {
+                invocable = (Invocable) scriptEngine;
+            } catch (ClassCastException exception) {
+                throw new RuntimeException(exception);
+            }
+
+            Object result = value;
+            try {
+               result = invocable.invokeFunction(functionName, value);
+            } catch (NoSuchMethodException exception) {
+                throw new RuntimeException(exception);
+            } catch (ScriptException exception) {
+                throw new RuntimeException(exception);
+            }
+            return result;
+        }
+
         @Override
         public Object evaluate(final Object value) {
             Object result = value;
-            Bindings bindings = scriptEngine.getBindings(ScriptContext.GLOBAL_SCOPE);
+            Bindings bindings = scriptEngine.getBindings(ScriptContext.ENGINE_SCOPE);
             if (bindings.containsKey(functionName)) {
-                Invocable invocable;
-                try {
-                    invocable = (Invocable) scriptEngine;
-                } catch (ClassCastException exception) {
-                    throw new RuntimeException(exception);
-                }
-
-                try {
-                    result = invocable.invokeFunction(functionName, result);
-                } catch (NoSuchMethodException exception) {
-                    throw new RuntimeException(exception);
-                } catch (ScriptException exception) {
-                    throw new RuntimeException(exception);
+                result = invokeFunction(functionName, result);
+            } else if (bindings.containsKey(NASHORN_GLOBAL)) {
+                Bindings globalBindings = (Bindings)bindings.get(NASHORN_GLOBAL);
+                if (globalBindings.containsKey(functionName)) {
+                    result = invokeFunction(functionName, result);
+                } else {
+                    bindings = scriptEngine.getBindings(ScriptContext.GLOBAL_SCOPE);
+                    if (bindings.containsKey(functionName)) {
+                        result = invokeFunction(functionName, result);
+                    } else {
+                        throw new RuntimeException("Mapping function \"" + functionName
+                            + "\" is not defined.");
+                    }
                 }
             } else {
-                throw new RuntimeException("Mapping function \"" + functionName
-                    + "\" is not defined.");
+                bindings = scriptEngine.getBindings(ScriptContext.GLOBAL_SCOPE);
+                if (bindings.containsKey(functionName)) {
+                    result = invokeFunction(functionName, result);
+                } else {
+                    throw new RuntimeException("Mapping function \"" + functionName
+                        + "\" is not defined.");
+                }
             }
 
             return result;
@@ -1448,6 +1472,7 @@ public class BXMLSerializer implements S
                     scriptEngine.setBindings(scriptEngineManager.getBindings(), ScriptContext.ENGINE_SCOPE);
 
                     try {
+                        scriptEngine.eval(NASHORN_COMPAT_SCRIPT);
                         scriptEngine.eval(script);
                     } catch (ScriptException exception) {
                         reportException(exception, script);