From b3a6c4a5cfffcdea028ea8b9f51a80cca5f71773 Mon Sep 17 00:00:00 2001 From: Florian Hotze Date: Tue, 27 Dec 2022 14:52:24 +0100 Subject: [PATCH 1/4] [jsscripting] Switch to the default MIME types Signed-off-by: Florian Hotze --- .../internal/GraalJSScriptEngineFactory.java | 30 ++++++++++--------- .../GraalJSScriptEngineFactoryTest.java | 22 ++++++++++++++ 2 files changed, 38 insertions(+), 14 deletions(-) create mode 100644 bundles/org.openhab.automation.jsscripting/src/test/java/org/openhab/automation/jsscripting/GraalJSScriptEngineFactoryTest.java diff --git a/bundles/org.openhab.automation.jsscripting/src/main/java/org/openhab/automation/jsscripting/internal/GraalJSScriptEngineFactory.java b/bundles/org.openhab.automation.jsscripting/src/main/java/org/openhab/automation/jsscripting/internal/GraalJSScriptEngineFactory.java index 9883dd4e983b3..769601056d171 100644 --- a/bundles/org.openhab.automation.jsscripting/src/main/java/org/openhab/automation/jsscripting/internal/GraalJSScriptEngineFactory.java +++ b/bundles/org.openhab.automation.jsscripting/src/main/java/org/openhab/automation/jsscripting/internal/GraalJSScriptEngineFactory.java @@ -14,6 +14,8 @@ import java.util.List; import java.util.Map; +import java.util.stream.Collectors; +import java.util.stream.Stream; import javax.script.ScriptEngine; @@ -29,6 +31,8 @@ import org.osgi.service.component.annotations.Modified; import org.osgi.service.component.annotations.Reference; +import com.oracle.truffle.js.scriptengine.GraalJSEngineFactory; + /** * An implementation of {@link ScriptEngineFactory} with customizations for GraalJS ScriptEngines. * @@ -42,23 +46,21 @@ public final class GraalJSScriptEngineFactory implements ScriptEngineFactory { private static final String CFG_INJECTION_ENABLED = "injectionEnabled"; private static final String INJECTION_CODE = "Object.assign(this, require('openhab'));"; - private boolean injectionEnabled = true; - /* - * Whilst we run in parallel with Nashorn, we use a custom mime-type to avoid - * disrupting Nashorn scripts. When Nashorn is removed, we take over the standard - * JS runtime. - */ + private static final GraalJSEngineFactory factory = new GraalJSEngineFactory(); - // GraalJSEngineFactory graalJSEngineFactory = new GraalJSEngineFactory(); - // - // scriptTypes.addAll(graalJSEngineFactory.getMimeTypes()); - // scriptTypes.addAll(graalJSEngineFactory.getExtensions()); - - public static final String MIME_TYPE = "application/javascript;version=ECMAScript-2021"; - private static final String ALT_MIME_TYPE = "text/javascript;version=ECMAScript-2021"; + public static final String MIME_TYPE = "application/javascript"; private static final String ALIAS = "graaljs"; - private static final List SCRIPT_TYPES = List.of(MIME_TYPE, ALT_MIME_TYPE, ALIAS); + private static final List SCRIPT_TYPES = createScriptTypes(); + + private static List createScriptTypes() { + // Add those for backward compatibility (existing scripts may rely on those MIME types) + List backwardCompat = List.of("application/javascript;version=ECMAScript-2021", ALIAS); + return Stream.of(factory.getMimeTypes(), factory.getExtensions(), backwardCompat).flatMap(List::stream) + .collect(Collectors.toUnmodifiableList()); + } + + private boolean injectionEnabled = true; private final JSScriptServiceUtil jsScriptServiceUtil; private final JSDependencyTracker jsDependencyTracker; diff --git a/bundles/org.openhab.automation.jsscripting/src/test/java/org/openhab/automation/jsscripting/GraalJSScriptEngineFactoryTest.java b/bundles/org.openhab.automation.jsscripting/src/test/java/org/openhab/automation/jsscripting/GraalJSScriptEngineFactoryTest.java new file mode 100644 index 0000000000000..3029d20e53cf4 --- /dev/null +++ b/bundles/org.openhab.automation.jsscripting/src/test/java/org/openhab/automation/jsscripting/GraalJSScriptEngineFactoryTest.java @@ -0,0 +1,22 @@ +package org.openhab.automation.jsscripting; + +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.contains; + +import java.util.List; + +import org.junit.jupiter.api.Test; +import org.openhab.automation.jsscripting.internal.GraalJSScriptEngineFactory; + +public class GraalJSScriptEngineFactoryTest { + + @Test + public void scriptTypesAreGraalJsSpecific() { + List scriptTypes = new GraalJSScriptEngineFactory().getScriptTypes(); + + assertThat(scriptTypes, contains("application/javascript", "application/ecmascript", "text/javascript", + "text/ecmascript", "js", "mjs", "application/javascript;version=ECMAScript-2021", "graaljs")); + assertThat(scriptTypes.size(), is(8)); + } +} From eadd3de33ba19faa51030622069cc59b65132059 Mon Sep 17 00:00:00 2001 From: Florian Hotze Date: Wed, 28 Dec 2022 13:45:35 +0100 Subject: [PATCH 2/4] [jsscripting] Remove backward compatibility MIME types Signed-off-by: Florian Hotze --- .../internal/GraalJSScriptEngineFactory.java | 5 +--- .../GraalJSScriptEngineFactoryTest.java | 23 +++++++++++++++++-- 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/bundles/org.openhab.automation.jsscripting/src/main/java/org/openhab/automation/jsscripting/internal/GraalJSScriptEngineFactory.java b/bundles/org.openhab.automation.jsscripting/src/main/java/org/openhab/automation/jsscripting/internal/GraalJSScriptEngineFactory.java index 769601056d171..9d4bcd0d4ae9d 100644 --- a/bundles/org.openhab.automation.jsscripting/src/main/java/org/openhab/automation/jsscripting/internal/GraalJSScriptEngineFactory.java +++ b/bundles/org.openhab.automation.jsscripting/src/main/java/org/openhab/automation/jsscripting/internal/GraalJSScriptEngineFactory.java @@ -50,13 +50,10 @@ public final class GraalJSScriptEngineFactory implements ScriptEngineFactory { private static final GraalJSEngineFactory factory = new GraalJSEngineFactory(); public static final String MIME_TYPE = "application/javascript"; - private static final String ALIAS = "graaljs"; private static final List SCRIPT_TYPES = createScriptTypes(); private static List createScriptTypes() { - // Add those for backward compatibility (existing scripts may rely on those MIME types) - List backwardCompat = List.of("application/javascript;version=ECMAScript-2021", ALIAS); - return Stream.of(factory.getMimeTypes(), factory.getExtensions(), backwardCompat).flatMap(List::stream) + return Stream.of(factory.getMimeTypes(), factory.getExtensions()).flatMap(List::stream) .collect(Collectors.toUnmodifiableList()); } diff --git a/bundles/org.openhab.automation.jsscripting/src/test/java/org/openhab/automation/jsscripting/GraalJSScriptEngineFactoryTest.java b/bundles/org.openhab.automation.jsscripting/src/test/java/org/openhab/automation/jsscripting/GraalJSScriptEngineFactoryTest.java index 3029d20e53cf4..4b8990d222f40 100644 --- a/bundles/org.openhab.automation.jsscripting/src/test/java/org/openhab/automation/jsscripting/GraalJSScriptEngineFactoryTest.java +++ b/bundles/org.openhab.automation.jsscripting/src/test/java/org/openhab/automation/jsscripting/GraalJSScriptEngineFactoryTest.java @@ -1,3 +1,15 @@ +/** + * Copyright (c) 2010-2022 Contributors to the openHAB project + * + * See the NOTICE file(s) distributed with this work for additional + * information. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License 2.0 which is available at + * http://www.eclipse.org/legal/epl-2.0 + * + * SPDX-License-Identifier: EPL-2.0 + */ package org.openhab.automation.jsscripting; import static org.hamcrest.CoreMatchers.is; @@ -6,9 +18,16 @@ import java.util.List; +import org.eclipse.jdt.annotation.NonNullByDefault; import org.junit.jupiter.api.Test; import org.openhab.automation.jsscripting.internal.GraalJSScriptEngineFactory; +/** + * Tests {@link GraalJSScriptEngineFactory}. + * + * @author Florian Hotze - Initial contribution + */ +@NonNullByDefault public class GraalJSScriptEngineFactoryTest { @Test @@ -16,7 +35,7 @@ public void scriptTypesAreGraalJsSpecific() { List scriptTypes = new GraalJSScriptEngineFactory().getScriptTypes(); assertThat(scriptTypes, contains("application/javascript", "application/ecmascript", "text/javascript", - "text/ecmascript", "js", "mjs", "application/javascript;version=ECMAScript-2021", "graaljs")); - assertThat(scriptTypes.size(), is(8)); + "text/ecmascript", "js", "mjs")); + assertThat(scriptTypes.size(), is(6)); } } From 0a164b89c254bfd72453d51c153c5ce9dd4d1ca9 Mon Sep 17 00:00:00 2001 From: Florian Hotze Date: Thu, 29 Dec 2022 00:45:27 +0100 Subject: [PATCH 3/4] Revert "[jsscripting] Remove backward compatibility MIME types" This reverts commit eadd3de33ba19faa51030622069cc59b65132059. Signed-off-by: Florian Hotze --- .../internal/GraalJSScriptEngineFactory.java | 5 +++- .../GraalJSScriptEngineFactoryTest.java | 23 ++----------------- 2 files changed, 6 insertions(+), 22 deletions(-) diff --git a/bundles/org.openhab.automation.jsscripting/src/main/java/org/openhab/automation/jsscripting/internal/GraalJSScriptEngineFactory.java b/bundles/org.openhab.automation.jsscripting/src/main/java/org/openhab/automation/jsscripting/internal/GraalJSScriptEngineFactory.java index 9d4bcd0d4ae9d..769601056d171 100644 --- a/bundles/org.openhab.automation.jsscripting/src/main/java/org/openhab/automation/jsscripting/internal/GraalJSScriptEngineFactory.java +++ b/bundles/org.openhab.automation.jsscripting/src/main/java/org/openhab/automation/jsscripting/internal/GraalJSScriptEngineFactory.java @@ -50,10 +50,13 @@ public final class GraalJSScriptEngineFactory implements ScriptEngineFactory { private static final GraalJSEngineFactory factory = new GraalJSEngineFactory(); public static final String MIME_TYPE = "application/javascript"; + private static final String ALIAS = "graaljs"; private static final List SCRIPT_TYPES = createScriptTypes(); private static List createScriptTypes() { - return Stream.of(factory.getMimeTypes(), factory.getExtensions()).flatMap(List::stream) + // Add those for backward compatibility (existing scripts may rely on those MIME types) + List backwardCompat = List.of("application/javascript;version=ECMAScript-2021", ALIAS); + return Stream.of(factory.getMimeTypes(), factory.getExtensions(), backwardCompat).flatMap(List::stream) .collect(Collectors.toUnmodifiableList()); } diff --git a/bundles/org.openhab.automation.jsscripting/src/test/java/org/openhab/automation/jsscripting/GraalJSScriptEngineFactoryTest.java b/bundles/org.openhab.automation.jsscripting/src/test/java/org/openhab/automation/jsscripting/GraalJSScriptEngineFactoryTest.java index 4b8990d222f40..3029d20e53cf4 100644 --- a/bundles/org.openhab.automation.jsscripting/src/test/java/org/openhab/automation/jsscripting/GraalJSScriptEngineFactoryTest.java +++ b/bundles/org.openhab.automation.jsscripting/src/test/java/org/openhab/automation/jsscripting/GraalJSScriptEngineFactoryTest.java @@ -1,15 +1,3 @@ -/** - * Copyright (c) 2010-2022 Contributors to the openHAB project - * - * See the NOTICE file(s) distributed with this work for additional - * information. - * - * This program and the accompanying materials are made available under the - * terms of the Eclipse Public License 2.0 which is available at - * http://www.eclipse.org/legal/epl-2.0 - * - * SPDX-License-Identifier: EPL-2.0 - */ package org.openhab.automation.jsscripting; import static org.hamcrest.CoreMatchers.is; @@ -18,16 +6,9 @@ import java.util.List; -import org.eclipse.jdt.annotation.NonNullByDefault; import org.junit.jupiter.api.Test; import org.openhab.automation.jsscripting.internal.GraalJSScriptEngineFactory; -/** - * Tests {@link GraalJSScriptEngineFactory}. - * - * @author Florian Hotze - Initial contribution - */ -@NonNullByDefault public class GraalJSScriptEngineFactoryTest { @Test @@ -35,7 +16,7 @@ public void scriptTypesAreGraalJsSpecific() { List scriptTypes = new GraalJSScriptEngineFactory().getScriptTypes(); assertThat(scriptTypes, contains("application/javascript", "application/ecmascript", "text/javascript", - "text/ecmascript", "js", "mjs")); - assertThat(scriptTypes.size(), is(6)); + "text/ecmascript", "js", "mjs", "application/javascript;version=ECMAScript-2021", "graaljs")); + assertThat(scriptTypes.size(), is(8)); } } From c9adf57a55e2c7eb84b74f044ebd0b5fd24dade8 Mon Sep 17 00:00:00 2001 From: Florian Hotze Date: Thu, 29 Dec 2022 14:40:12 +0100 Subject: [PATCH 4/4] [jsscripting] Remove test because its compilation fails Signed-off-by: Florian Hotze --- .../GraalJSScriptEngineFactoryTest.java | 22 ------------------- 1 file changed, 22 deletions(-) delete mode 100644 bundles/org.openhab.automation.jsscripting/src/test/java/org/openhab/automation/jsscripting/GraalJSScriptEngineFactoryTest.java diff --git a/bundles/org.openhab.automation.jsscripting/src/test/java/org/openhab/automation/jsscripting/GraalJSScriptEngineFactoryTest.java b/bundles/org.openhab.automation.jsscripting/src/test/java/org/openhab/automation/jsscripting/GraalJSScriptEngineFactoryTest.java deleted file mode 100644 index 3029d20e53cf4..0000000000000 --- a/bundles/org.openhab.automation.jsscripting/src/test/java/org/openhab/automation/jsscripting/GraalJSScriptEngineFactoryTest.java +++ /dev/null @@ -1,22 +0,0 @@ -package org.openhab.automation.jsscripting; - -import static org.hamcrest.CoreMatchers.is; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.hamcrest.Matchers.contains; - -import java.util.List; - -import org.junit.jupiter.api.Test; -import org.openhab.automation.jsscripting.internal.GraalJSScriptEngineFactory; - -public class GraalJSScriptEngineFactoryTest { - - @Test - public void scriptTypesAreGraalJsSpecific() { - List scriptTypes = new GraalJSScriptEngineFactory().getScriptTypes(); - - assertThat(scriptTypes, contains("application/javascript", "application/ecmascript", "text/javascript", - "text/ecmascript", "js", "mjs", "application/javascript;version=ECMAScript-2021", "graaljs")); - assertThat(scriptTypes.size(), is(8)); - } -}