Author: rwhitcomb Date: Wed Feb 3 02:31:39 2016 New Revision: 1728247 URL: http://svn.apache.org/viewvc?rev=1728247&view=rev Log: PIVOT-965: Fix an issue with Java 8 (Nashorn script engine) and the way we deal with included scripts. The problem is that in creating the new serializer for the included script, the script engine ends up creating a new "nashorn.global" context, which does not include any previous defined variables or functions. This new context persists as the current "global" context after the included script has been read. Then when attempting to look up any variables/functions defined before the "include" they won't be found.
The "solution" (or workaround) is to detect when the new global context is being established and copy the old definitions into it. Now, that sort of begs the question as to: * Is this the right place to do this? * What about the situation when "inline" is false for the included script? * What about multiple includes? * What about nested includes? These are still unanswered questions needing more research. So, for now, just add the workaround (and some of the debug code still there, but commented out). Added: pivot/trunk/tests/src/org/apache/pivot/tests/issues/pivot965/ pivot/trunk/tests/src/org/apache/pivot/tests/issues/pivot965/IncludedSection965.bxml pivot/trunk/tests/src/org/apache/pivot/tests/issues/pivot965/Pivot965Main.java pivot/trunk/tests/src/org/apache/pivot/tests/issues/pivot965/Window965.bxml 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=1728247&r1=1728246&r2=1728247&view=diff ============================================================================== --- pivot/trunk/core/src/org/apache/pivot/beans/BXMLSerializer.java (original) +++ pivot/trunk/core/src/org/apache/pivot/beans/BXMLSerializer.java Wed Feb 3 02:31:39 2016 @@ -105,6 +105,22 @@ public class BXMLSerializer implements S } } +/* private static void printBindings(java.util.Map<String,Object> bindings) { + System.out.format("=== Bindings %1$s ===%n", bindings); + for (String key : bindings.keySet()) { + Object value = bindings.get(key); + System.out.format("key: %1$s, value: %2$s [%3$s]%n", key, value, Integer.toHexString(System.identityHashCode(value))); + if (key.equals(NASHORN_GLOBAL)) { + Bindings globalBindings = (Bindings)value; + for (String globalKey : globalBindings.keySet()) { + Object globalValue = globalBindings.get(globalKey); + System.out.format(" global key: %1$s, value: %2$s [%3$s]%n", globalKey, globalValue, Integer.toHexString(System.identityHashCode(globalValue))); + } + } + } + System.out.println("====================="); + } +*/ private class AttributeInvocationHandler implements InvocationHandler { private ScriptEngine scriptEngine; private String event; @@ -310,6 +326,21 @@ public class BXMLSerializer implements S @Override public Object put(String key, Object value) { + // Okay, this is a hack that seems to fix problems with included scripts + // under Java 8 (Nashorn) where a new global object gets setup that + // doesn't include any of the old values. + // TODO: do we need to do this if "inline" is not true on the INCLUDE? + if (key.equals(NASHORN_GLOBAL)) { + Object oldGlobal = namespace.put(key, value); + // We have to copy the old global values into the new one + if (oldGlobal != null && oldGlobal instanceof Bindings && + value != null && value instanceof Bindings) { + Bindings oldGlobalBindings = (Bindings)oldGlobal; + Bindings newBindings = (Bindings)value; + newBindings.putAll(oldGlobalBindings); + } + return oldGlobal; + } return namespace.put(key, value); } @@ -977,7 +1008,8 @@ public class BXMLSerializer implements S case INCLUDE: { property = (localName.equals(INCLUDE_SRC_ATTRIBUTE) || localName.equals(INCLUDE_RESOURCES_ATTRIBUTE) - || localName.equals(INCLUDE_MIME_TYPE_ATTRIBUTE) || localName.equals(INCLUDE_INLINE_ATTRIBUTE)); + || localName.equals(INCLUDE_MIME_TYPE_ATTRIBUTE) + || localName.equals(INCLUDE_INLINE_ATTRIBUTE)); break; } @@ -1304,8 +1336,7 @@ public class BXMLSerializer implements S + "\" not found."); } - // Don't pollute the engine namespace with the listener - // functions + // Don't pollute the engine namespace with the listener functions scriptEngine.setBindings(new SimpleBindings(), ScriptContext.ENGINE_SCOPE); try { Added: pivot/trunk/tests/src/org/apache/pivot/tests/issues/pivot965/IncludedSection965.bxml URL: http://svn.apache.org/viewvc/pivot/trunk/tests/src/org/apache/pivot/tests/issues/pivot965/IncludedSection965.bxml?rev=1728247&view=auto ============================================================================== --- pivot/trunk/tests/src/org/apache/pivot/tests/issues/pivot965/IncludedSection965.bxml (added) +++ pivot/trunk/tests/src/org/apache/pivot/tests/issues/pivot965/IncludedSection965.bxml Wed Feb 3 02:31:39 2016 @@ -0,0 +1,30 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- +Licensed to the Apache Software Foundation (ASF) under one or more +contributor license agreements. See the NOTICE file distributed with +this work for additional information regarding copyright ownership. +The ASF licenses this file to you under the Apache License, +Version 2.0 (the "License"); you may not use this file except in +compliance with the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +--> + +<Form.Section + xmlns="org.apache.pivot.wtk" + xmlns:bxml="http://pivot.apache.org/bxml" + heading="Included Section"> + + <bxml:script> + function concateText(value) { + return value+value; + } + </bxml:script> + <Label Form.label="Concatenated Text" text="${concateText:textInput.text}"/> +</Form.Section> Added: pivot/trunk/tests/src/org/apache/pivot/tests/issues/pivot965/Pivot965Main.java URL: http://svn.apache.org/viewvc/pivot/trunk/tests/src/org/apache/pivot/tests/issues/pivot965/Pivot965Main.java?rev=1728247&view=auto ============================================================================== --- pivot/trunk/tests/src/org/apache/pivot/tests/issues/pivot965/Pivot965Main.java (added) +++ pivot/trunk/tests/src/org/apache/pivot/tests/issues/pivot965/Pivot965Main.java Wed Feb 3 02:31:39 2016 @@ -0,0 +1,59 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except in + * compliance with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.pivot.tests.issues.pivot965; + +import org.apache.pivot.beans.BXMLSerializer; +import org.apache.pivot.collections.Map; +import org.apache.pivot.wtk.Application; +import org.apache.pivot.wtk.DesktopApplicationContext; +import org.apache.pivot.wtk.Display; +import org.apache.pivot.wtk.TextInput; +import org.apache.pivot.wtk.Window; + + +public class Pivot965Main extends Application.Adapter +{ + private Window window = null; + + @Override + public void startup(Display display, Map<String, String> properties) throws Exception { + BXMLSerializer bxmlSerializer = new BXMLSerializer(); + window = (Window) bxmlSerializer.readObject(Window.class,"/org/apache/pivot/tests/issues/pivot965/Window965.bxml"); + window.open(display); + TextInput textInput = (TextInput)bxmlSerializer.getNamespace().get("textInput"); + textInput.requestFocus(); + } + + public static String myMappingFunction(String value) { + return value.toUpperCase(); + } + + @Override + public boolean shutdown(boolean optional) { + if (this.window != null) { + this.window.close(); + } + + return false; + } + + // useful to run this as a Java Application in a simpler way (directly) + public static void main(String[] args) { + DesktopApplicationContext.main(Pivot965Main.class, args); + } + +} Added: pivot/trunk/tests/src/org/apache/pivot/tests/issues/pivot965/Window965.bxml URL: http://svn.apache.org/viewvc/pivot/trunk/tests/src/org/apache/pivot/tests/issues/pivot965/Window965.bxml?rev=1728247&view=auto ============================================================================== --- pivot/trunk/tests/src/org/apache/pivot/tests/issues/pivot965/Window965.bxml (added) +++ pivot/trunk/tests/src/org/apache/pivot/tests/issues/pivot965/Window965.bxml Wed Feb 3 02:31:39 2016 @@ -0,0 +1,49 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- +Licensed to the Apache Software Foundation (ASF) under one or more +contributor license agreements. See the NOTICE file distributed with +this work for additional information regarding copyright ownership. +The ASF licenses this file to you under the Apache License, +Version 2.0 (the "License"); you may not use this file except in +compliance with the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +--> + +<Window title="Privileged Actions" maximized="true" + xmlns:bxml="http://pivot.apache.org/bxml" + xmlns="org.apache.pivot.wtk"> + + <bxml:script type="text/javascript"> + function isNull(value) { + return value == null; + } + + function isNotNull(value) { + return value != null; + } + + function toUpperCase(value) { + return value.toUpperCase(); + } + </bxml:script> + <Border> + <Form> + <Form.Section heading="One-Way Binding"> + <TextInput bxml:id="textInput" Form.label="Text Input"/> + <Label Form.label="Text" text="${textInput.text}"/> + <Label Form.label="Uppercase Text" text="${toUpperCase:textInput.text}"/> + </Form.Section> + + <bxml:include src="IncludedSection965.bxml" inline="true"/> + + </Form> + </Border> + +</Window>