Author: rwhitcomb Date: Mon Jul 20 19:49:19 2015 New Revision: 1692012 URL: http://svn.apache.org/r1692012 Log: PIVOT-974: Fix BXMLSerializer to correctly deal with the new Java 8 Nashorn script engine by doing two things: 1) Accessing global variables needs to be done through the "nashorn.global" object. 2) The "nashorn:mozilla_compat.js" script needs to be loaded first before any other scripts are executed.
This change implements these two fixes. This is a merge of revision 1691618 from trunk to branches/2.0.x. Since we build 2.0.x with Java 6, strictly speaking this change wouldn't be necessary there, except that a lot of users will be running (not building) on Java 8 now, since Java 7 is EOL, so this change will actually be necessary for the 2.0.5 release. Modified: pivot/branches/2.0.x/ (props changed) pivot/branches/2.0.x/core/src/org/apache/pivot/beans/BXMLSerializer.java Propchange: pivot/branches/2.0.x/ ------------------------------------------------------------------------------ --- svn:mergeinfo (original) +++ svn:mergeinfo Mon Jul 20 19:49:19 2015 @@ -1 +1 @@ -/pivot/trunk:1346574,1347051,1394847,1394858,1398511,1399331,1401781,1405882,1407585,1409081,1410536,1410555,1417081,1417258,1428056,1428650,1435351,1436707,1438126,1438659,1444260,1444910,1502657,1510821,1516518,1519859,1522078,1523205,1523736,1523776,1525982,1526005,1536829,1537222,1604238,1610563,1611829,1614462,1624381,1675204,1675517,1678238,1678251,1688523 +/pivot/trunk:1346574,1347051,1394847,1394858,1398511,1399331,1401781,1405882,1407585,1409081,1410536,1410555,1417081,1417258,1428056,1428650,1435351,1436707,1438126,1438659,1444260,1444910,1502657,1510821,1516518,1519859,1522078,1523205,1523736,1523776,1525982,1526005,1536829,1537222,1604238,1610563,1611829,1614462,1624381,1675204,1675517,1678238,1678251,1688523,1691618 Modified: pivot/branches/2.0.x/core/src/org/apache/pivot/beans/BXMLSerializer.java URL: http://svn.apache.org/viewvc/pivot/branches/2.0.x/core/src/org/apache/pivot/beans/BXMLSerializer.java?rev=1692012&r1=1692011&r2=1692012&view=diff ============================================================================== --- pivot/branches/2.0.x/core/src/org/apache/pivot/beans/BXMLSerializer.java (original) +++ pivot/branches/2.0.x/core/src/org/apache/pivot/beans/BXMLSerializer.java Mon Jul 20 19:49:19 2015 @@ -136,6 +136,7 @@ public class BXMLSerializer implements S SimpleBindings bindings = new SimpleBindings(); bindings.put(ARGUMENTS_KEY, args); scriptEngine.setBindings(bindings, ScriptContext.ENGINE_SCOPE); + scriptEngine.eval(NASHORN_COMPAT_SCRIPT); scriptEngine.eval(script); } catch (ScriptException exception) { reportException(exception, script); @@ -168,6 +169,7 @@ public class BXMLSerializer implements S String methodName = method.getName(); Bindings bindings = scriptEngine.getBindings(ScriptContext.ENGINE_SCOPE); + bindings = substituteGlobals(scriptEngine, bindings); if (bindings.containsKey(methodName)) { Invocable invocable; try { @@ -206,6 +208,7 @@ public class BXMLSerializer implements S public Object evaluate(final Object value) { Object result = value; Bindings bindings = scriptEngine.getBindings(ScriptContext.GLOBAL_SCOPE); + bindings = substituteGlobals(scriptEngine, bindings); if (bindings.containsKey(functionName)) { Invocable invocable; try { @@ -261,6 +264,9 @@ public class BXMLSerializer implements S public static final String LANGUAGE_PROCESSING_INSTRUCTION = "language"; + public static final String NASHORN_GLOBAL = "nashorn.global"; + public static final String NASHORN_COMPAT_SCRIPT = "if (typeof importClass != \"function\") { load(\"nashorn:mozilla_compat.js\"); }"; + public static final String BXML_PREFIX = "bxml"; public static final String BXML_EXTENSION = "bxml"; public static final String ID_ATTRIBUTE = "id"; @@ -398,6 +404,16 @@ public class BXMLSerializer implements S throw new UnsupportedOperationException("https://issues.apache.org/jira/browse/PIVOT-742"); } + private static Bindings substituteGlobals(ScriptEngine scriptEngine, final Bindings bindings) { + Bindings newBindings = bindings; + Object nashornGlobals = newBindings.get(NASHORN_GLOBAL); + if (nashornGlobals != null && nashornGlobals instanceof Bindings) { + newBindings = (Bindings)nashornGlobals; + scriptEngine.setBindings(newBindings, ScriptContext.ENGINE_SCOPE); + } + return newBindings; + } + /** * Deserializes an object hierarchy from a BXML resource. * <p> @@ -1289,6 +1305,7 @@ public class BXMLSerializer implements S scriptEngine.setBindings(new SimpleBindings(), ScriptContext.ENGINE_SCOPE); try { + scriptEngine.eval(NASHORN_COMPAT_SCRIPT); scriptEngine.eval(script); } catch (ScriptException exception) { reportException(exception, script); @@ -1374,6 +1391,7 @@ public class BXMLSerializer implements S BufferedReader scriptReader = null; try { scriptReader = new BufferedReader(new InputStreamReader(scriptLocation.openStream())); + scriptEngine.eval(NASHORN_COMPAT_SCRIPT); scriptEngine.eval(scriptReader); } catch(ScriptException exception) { reportException(exception); @@ -1400,6 +1418,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);