diff --git a/WHATSNEW b/WHATSNEW
index 263297433..2ff4e0eb8 100644
--- a/WHATSNEW
+++ b/WHATSNEW
@@ -39,7 +39,13 @@ Other changes:
* added some special code to support GraalVM JavaScript as
javax.script scripting engine for JavaScript. In particular we
relax some security settings of GraalVM so that scripts can access
- Ant objects. See the script task manual for additional details.
+ Ant objects.
+
+ Also Ant enables Nashorn compatibility mode by default, you can
+ disable that by setting the magic Ant property
+ ant.disable.graal.nashorn.compat to true.
+
+ See the script task manual for additional details.
* If the magic property ant.tmpdir hasn't been set and Ant can
control the permissions of directories it creates it will create an
diff --git a/build.xml b/build.xml
index 4e157d68f..3e75320c9 100644
--- a/build.xml
+++ b/build.xml
@@ -336,6 +336,10 @@
+
+
+
+
@@ -503,6 +507,9 @@
+
@@ -1870,7 +1877,41 @@ ${antunit.reports}
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
@@ -1907,6 +1948,9 @@ ${antunit.reports}
+
+
+
diff --git a/manual/Tasks/script.html b/manual/Tasks/script.html
index e029cd9db..32c2a91c7 100644
--- a/manual/Tasks/script.html
+++ b/manual/Tasks/script.html
@@ -293,6 +293,13 @@ compatibility script: load("nashorn:mozilla_compat.js");
.
Graal's Nashorn
Migration Guide for more details.
+When using GraalVM JavaScript Ant will enable the
+ feature polyglot.js.allowAllAccess
in order to allow
+ scripts to use Ant objects. By default it will also enable Nashorn
+ compatibility mode, but you can disable this by setting the magic
+ Ant property ant.disable.graal.nashorn.compat
+ to true
.
+
The <script>
task populates the Project instance under the
name project
, so we can use that reference. Another way is to use its
given name or getting its reference from the task itself. The Project provides methods for accessing
diff --git a/manual/running.html b/manual/running.html
index 63647f80c..bca2ea206 100644
--- a/manual/running.html
+++ b/manual/running.html
@@ -479,6 +479,15 @@ And I filtered out the getPropertyHelper
access.
from within the build file.
See also Temporary Directories.
+
+ ant.disable.graal.nashorn.compat |
+ boolean (default false ) |
+ Since Ant 1.10.9
+ By default Ant will enable GraalVM JavaScript's Nashorn
+ compatibility mode for script and friends. You can
+ disable this beahvior by setting this property to true .
+ See also the script task manual. |
+
diff --git a/src/etc/testcases/taskdefs/optional/script/graal.xml b/src/etc/testcases/taskdefs/optional/script/graal.xml
new file mode 100644
index 000000000..42a69c580
--- /dev/null
+++ b/src/etc/testcases/taskdefs/optional/script/graal.xml
@@ -0,0 +1,28 @@
+
+
+
+
+
+
+
diff --git a/src/main/org/apache/tools/ant/MagicNames.java b/src/main/org/apache/tools/ant/MagicNames.java
index 8ced50578..12fbcf731 100644
--- a/src/main/org/apache/tools/ant/MagicNames.java
+++ b/src/main/org/apache/tools/ant/MagicNames.java
@@ -347,5 +347,17 @@ public final class MagicNames {
* @since Ant 1.10.9
*/
public static final String AUTO_TMPDIR = "ant.auto.tmpdir";
+
+ /**
+ * Magic property that can be used to disable Nashorn compatibility mode when using GraalVM JavaScript as script
+ * engine.
+ *
+ *
Set this to "true" if you want to disable Nashorn compatibility mode.
+ *
+ * Value: {@value}
+ * @since Ant 1.10.9
+ */
+ public static final String DISABLE_NASHORN_COMPAT = "ant.disable.graal.nashorn.compat";
+
}
diff --git a/src/main/org/apache/tools/ant/util/optional/JavaxScriptRunner.java b/src/main/org/apache/tools/ant/util/optional/JavaxScriptRunner.java
index a9b565c5a..591744eb4 100644
--- a/src/main/org/apache/tools/ant/util/optional/JavaxScriptRunner.java
+++ b/src/main/org/apache/tools/ant/util/optional/JavaxScriptRunner.java
@@ -193,7 +193,10 @@ public class JavaxScriptRunner extends ScriptRunnerBase {
if (keptEngine != null) {
return keptEngine;
}
- ScriptEngine result =
+ if (languageIsJavaScript()) {
+ maybeEnableNashornCompatibility();
+ }
+ final ScriptEngine result =
new ScriptEngineManager().getEngineByName(getLanguage());
if (result == null && JavaEnvUtils.isAtLeastJavaVersion("15")
&& languageIsJavaScript()) {
@@ -219,6 +222,16 @@ public class JavaxScriptRunner extends ScriptRunnerBase {
}
}
+ private static final String ENABLE_NASHORN_COMPAT_IN_GRAAL = "polyglot.js.nashorn-compat";
+
+ private void maybeEnableNashornCompatibility() {
+ if (getProject() != null) {
+ System.setProperty(ENABLE_NASHORN_COMPAT_IN_GRAAL,
+ Project.toBoolean(getProject().getProperty(MagicNames.DISABLE_NASHORN_COMPAT))
+ ? "false" : "true");
+ }
+ }
+
private final static List JS_LANGUAGES = Arrays.asList("js", "javascript");
private boolean languageIsJavaScript() {
diff --git a/src/tests/junit/org/apache/tools/ant/taskdefs/optional/script/graal/AbstractNashornCompatTest.java b/src/tests/junit/org/apache/tools/ant/taskdefs/optional/script/graal/AbstractNashornCompatTest.java
new file mode 100644
index 000000000..25123d20a
--- /dev/null
+++ b/src/tests/junit/org/apache/tools/ant/taskdefs/optional/script/graal/AbstractNashornCompatTest.java
@@ -0,0 +1,53 @@
+/*
+ * 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
+ *
+ * https://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.tools.ant.taskdefs.optional.script.graal;
+
+import org.apache.tools.ant.BuildFileRule;
+import org.apache.tools.ant.MagicNames;
+
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+
+import static org.hamcrest.Matchers.containsString;
+import static org.junit.Assert.assertThat;
+
+public class AbstractNashornCompatTest {
+
+ private final String magicPropertyValue;
+
+ public AbstractNashornCompatTest(String magicPropertyValue) {
+ this.magicPropertyValue = magicPropertyValue;
+ }
+
+ @Rule
+ public BuildFileRule buildRule = new BuildFileRule();
+
+ @Before
+ public void setUp() {
+ buildRule.configureProject("src/etc/testcases/taskdefs/optional/script/graal.xml");
+ buildRule.getProject().setProperty(MagicNames.DISABLE_NASHORN_COMPAT, magicPropertyValue);
+ }
+
+ @Test
+ public void runSquaresTest() {
+ buildRule.executeTarget("run-squares-test");
+ assertThat("Expecting the square of 7 to be logged", buildRule.getLog(),
+ containsString("49"));
+ }
+}
diff --git a/src/tests/junit/org/apache/tools/ant/taskdefs/optional/script/graal/DefaultNashornCompatTest.java b/src/tests/junit/org/apache/tools/ant/taskdefs/optional/script/graal/DefaultNashornCompatTest.java
new file mode 100644
index 000000000..a5c1771b0
--- /dev/null
+++ b/src/tests/junit/org/apache/tools/ant/taskdefs/optional/script/graal/DefaultNashornCompatTest.java
@@ -0,0 +1,25 @@
+/*
+ * 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
+ *
+ * https://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.tools.ant.taskdefs.optional.script.graal;
+
+public class DefaultNashornCompatTest extends AbstractNashornCompatTest {
+
+ public DefaultNashornCompatTest() {
+ super(null);
+ }
+}
diff --git a/src/tests/junit/org/apache/tools/ant/taskdefs/optional/script/graal/DisableNashornCompatTest.java b/src/tests/junit/org/apache/tools/ant/taskdefs/optional/script/graal/DisableNashornCompatTest.java
new file mode 100644
index 000000000..808bdee54
--- /dev/null
+++ b/src/tests/junit/org/apache/tools/ant/taskdefs/optional/script/graal/DisableNashornCompatTest.java
@@ -0,0 +1,41 @@
+/*
+ * 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
+ *
+ * https://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.tools.ant.taskdefs.optional.script.graal;
+
+import org.apache.tools.ant.BuildException;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+
+public class DisableNashornCompatTest extends AbstractNashornCompatTest {
+
+ @Rule
+ public ExpectedException thrown = ExpectedException.none();
+
+ public DisableNashornCompatTest() {
+ super("true");
+ }
+
+ @Test
+ @Override
+ public void runSquaresTest() {
+ thrown.expect(BuildException.class);
+ thrown.expectMessage("TypeError");
+ super.runSquaresTest();
+ }
+}
diff --git a/src/tests/junit/org/apache/tools/ant/taskdefs/optional/script/graal/EnableNashornCompatTest.java b/src/tests/junit/org/apache/tools/ant/taskdefs/optional/script/graal/EnableNashornCompatTest.java
new file mode 100644
index 000000000..2161b6f97
--- /dev/null
+++ b/src/tests/junit/org/apache/tools/ant/taskdefs/optional/script/graal/EnableNashornCompatTest.java
@@ -0,0 +1,25 @@
+/*
+ * 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
+ *
+ * https://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.tools.ant.taskdefs.optional.script.graal;
+
+public class EnableNashornCompatTest extends AbstractNashornCompatTest {
+
+ public EnableNashornCompatTest() {
+ super("false");
+ }
+}