Skip to content

Commit

Permalink
Use GraalJS to verify the JS scripts
Browse files Browse the repository at this point in the history
Use GraalJS instead of Nashorn to support newer syntax and newer Java
versions.

Signed-off-by: thc202 <[email protected]>
  • Loading branch information
thc202 committed Feb 8, 2024
1 parent c66725e commit 2a364f1
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 12 deletions.
3 changes: 3 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
32 changes: 20 additions & 12 deletions src/test/java/org/zaproxy/VerifyScripts.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -102,15 +102,23 @@ private static Stream<Arguments> scriptsGroovy() {
}

private static Stream<Arguments> 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<Arguments> scriptsPython() {
Expand Down

0 comments on commit 2a364f1

Please sign in to comment.