From 2a364f1142a485e5d650028cb0a9472ba8d5cbd6 Mon Sep 17 00:00:00 2001 From: thc202 Date: Thu, 8 Feb 2024 16:49:08 +0000 Subject: [PATCH] Use GraalJS to verify the JS scripts Use GraalJS instead of Nashorn to support newer syntax and newer Java versions. Signed-off-by: thc202 --- build.gradle.kts | 3 ++ src/test/java/org/zaproxy/VerifyScripts.java | 32 ++++++++++++-------- 2 files changed, 23 insertions(+), 12 deletions(-) diff --git a/build.gradle.kts b/build.gradle.kts index dbd05c31..7e47b7f5 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -56,6 +56,9 @@ dependencies { // The following versions should match the ones of the add-ons. testImplementation("org.codehaus.groovy:groovy-all:3.0.14") + val graalJsVersion = "22.3.3" + testImplementation("org.graalvm.js:js:$graalJsVersion") + testImplementation("org.graalvm.js:js-scriptengine:$graalJsVersion") testImplementation("org.jruby:jruby-complete:1.7.4") testImplementation("org.zaproxy:zest:0.18.0") testImplementation("org.python:jython-standalone:2.7.2") diff --git a/src/test/java/org/zaproxy/VerifyScripts.java b/src/test/java/org/zaproxy/VerifyScripts.java index 302763ea..d61280aa 100644 --- a/src/test/java/org/zaproxy/VerifyScripts.java +++ b/src/test/java/org/zaproxy/VerifyScripts.java @@ -21,6 +21,7 @@ import static org.assertj.core.api.Assertions.assertThat; +import com.oracle.truffle.js.scriptengine.GraalJSScriptEngine; import java.io.File; import java.io.IOException; import java.io.Reader; @@ -35,23 +36,22 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; -import java.util.EnumSet; import java.util.List; import java.util.Locale; import java.util.Optional; import java.util.function.Consumer; import java.util.stream.Stream; import javax.script.Compilable; -import javax.script.ScriptEngineManager; import javax.script.ScriptException; import org.apache.commons.io.IOUtils; import org.apache.commons.lang3.SystemUtils; import org.apache.commons.lang3.mutable.MutableInt; import org.codehaus.groovy.jsr223.GroovyScriptEngineFactory; +import org.graalvm.polyglot.Context; +import org.graalvm.polyglot.Engine; import org.jruby.embed.jsr223.JRubyEngineFactory; import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.BeforeAll; -import org.junit.jupiter.api.condition.JRE; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.Arguments; import org.junit.jupiter.params.provider.MethodSource; @@ -102,15 +102,23 @@ private static Stream scriptsGroovy() { } private static Stream scriptsJavaScript() { - if (!EnumSet.range(JRE.JAVA_8, JRE.JAVA_14).contains(JRE.currentVersion())) { - // Nashorn is not bundled in Java 15+ - getFilesWithExtension(".js"); - return Stream.empty(); - } - - Compilable engine = (Compilable) new ScriptEngineManager().getEngineByName("ECMAScript"); - assertThat(engine).as("ECMAScript script engine exists.").isNotNull(); - return testData(".js", engine); + Engine engine = + Engine.newBuilder() + .allowExperimentalOptions(true) + .option("engine.WarnInterpreterOnly", "false") + .build(); + + Context.Builder contextBuilder = + Context.newBuilder("js") + .allowExperimentalOptions(true) + .option("js.syntax-extensions", "true") + .option("js.load", "true") + .option("js.print", "true") + .option("js.nashorn-compat", "true") + .allowAllAccess(true) + .hostClassLoader(VerifyScripts.class.getClassLoader()); + + return testData(".js", GraalJSScriptEngine.create(engine, contextBuilder)); } private static Stream scriptsPython() {