From 6d35780da4fa378be2cb6b7648c8d07f6864a333 Mon Sep 17 00:00:00 2001 From: Marc Arndt Date: Mon, 29 Jul 2019 17:40:12 +0200 Subject: [PATCH 1/2] - adds a simple module system supporting module.x = <> --- .../engine/injectors/IncludeInjector.java | 20 ++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/phoenicis-scripts/src/main/java/org/phoenicis/scripts/engine/injectors/IncludeInjector.java b/phoenicis-scripts/src/main/java/org/phoenicis/scripts/engine/injectors/IncludeInjector.java index de770936f21..0e93db2a8c9 100644 --- a/phoenicis-scripts/src/main/java/org/phoenicis/scripts/engine/injectors/IncludeInjector.java +++ b/phoenicis-scripts/src/main/java/org/phoenicis/scripts/engine/injectors/IncludeInjector.java @@ -1,5 +1,6 @@ package org.phoenicis.scripts.engine.injectors; +import org.graalvm.polyglot.Value; import org.phoenicis.scripts.engine.implementation.PhoenicisScriptEngine; import org.phoenicis.scripts.interpreter.ScriptException; import org.phoenicis.scripts.interpreter.ScriptFetcher; @@ -30,9 +31,22 @@ public void injectInto(PhoenicisScriptEngine phoenicisScriptEngine) { throwException(new ScriptException("Script '" + argument + "' is not found")); } - includedScripts.put(argument, - phoenicisScriptEngine.evalAndReturn("//# sourceURL=" + argument + "\n" + script, - this::throwException)); + // wrap the loaded script in a function to prevent it from influencing the main script + String extendedString = String.format("(module) => { %s }", script); + Value includeFunction = (Value) phoenicisScriptEngine.evalAndReturn(extendedString, + this::throwException); + + // create an empty JS object + Value module = (Value) phoenicisScriptEngine.evalAndReturn("({})", this::throwException); + + // execute the included function -> populates "module" + includeFunction.execute(module); + + if (module.hasMember("default")) { + includedScripts.put(argument, module.getMember("default")); + } else { + includedScripts.put(argument, module); + } } return includedScripts.get(argument); From 8ef77766ec50692c502433106790312460e645b3 Mon Sep 17 00:00:00 2001 From: Marc Arndt Date: Sat, 10 Aug 2019 12:54:27 +0200 Subject: [PATCH 2/2] - add some missing const ... --- .../containers/GenericContainersManager.java | 3 ++- .../library/control/LibraryFeaturePanel.java | 3 ++- .../views/mainwindow/library/LibraryView.java | 3 ++- .../org/phoenicis/library/ShortcutManager.java | 2 +- .../java/org/phoenicis/library/ShortcutRunner.java | 4 ++-- .../repository/types/ClasspathRepositoryTest.java | 14 +++++++++----- .../Graphics/Photofiltre/Online/script.js | 5 ++--- 7 files changed, 20 insertions(+), 14 deletions(-) diff --git a/phoenicis-containers/src/main/java/org/phoenicis/containers/GenericContainersManager.java b/phoenicis-containers/src/main/java/org/phoenicis/containers/GenericContainersManager.java index d2b2c777c54..261e63f35d4 100644 --- a/phoenicis-containers/src/main/java/org/phoenicis/containers/GenericContainersManager.java +++ b/phoenicis-containers/src/main/java/org/phoenicis/containers/GenericContainersManager.java @@ -135,7 +135,8 @@ public void deleteContainer(ContainerDTO container, Consumer onSuc final InteractiveScriptSession interactiveScriptSession = this.scriptInterpreter .createInteractiveSession(); - interactiveScriptSession.eval("include(\"engines." + engineId + ".shortcuts.reader\");", + interactiveScriptSession.eval( + "const ShortcutReader = include(\"engines." + engineId + ".shortcuts.reader\");", ignored -> interactiveScriptSession.eval("new ShortcutReader()", output -> { final org.graalvm.polyglot.Value shortcutReader = (org.graalvm.polyglot.Value) output; shortcutReader.invokeMember("of", shortcut); diff --git a/phoenicis-javafx/src/main/java/org/phoenicis/javafx/components/library/control/LibraryFeaturePanel.java b/phoenicis-javafx/src/main/java/org/phoenicis/javafx/components/library/control/LibraryFeaturePanel.java index adb05648777..45b05416b9b 100644 --- a/phoenicis-javafx/src/main/java/org/phoenicis/javafx/components/library/control/LibraryFeaturePanel.java +++ b/phoenicis-javafx/src/main/java/org/phoenicis/javafx/components/library/control/LibraryFeaturePanel.java @@ -148,7 +148,8 @@ public void createShortcut(ShortcutCreationDTO shortcutCreationDTO) { final InteractiveScriptSession interactiveScriptSession = getScriptInterpreter().createInteractiveSession(); - final String scriptInclude = "include(\"engines." + engineId + ".shortcuts." + engineId + "\");"; + final String scriptInclude = "const Shortcut = include(\"engines." + engineId + ".shortcuts." + engineId + + "\");"; interactiveScriptSession.eval(scriptInclude, ignored -> interactiveScriptSession.eval("new " + engine + "Shortcut()", diff --git a/phoenicis-javafx/src/main/java/org/phoenicis/javafx/views/mainwindow/library/LibraryView.java b/phoenicis-javafx/src/main/java/org/phoenicis/javafx/views/mainwindow/library/LibraryView.java index 4e8369706c2..994860e2866 100644 --- a/phoenicis-javafx/src/main/java/org/phoenicis/javafx/views/mainwindow/library/LibraryView.java +++ b/phoenicis-javafx/src/main/java/org/phoenicis/javafx/views/mainwindow/library/LibraryView.java @@ -345,7 +345,8 @@ private void createShortcut(ShortcutCreationDTO shortcutCreationDTO) { final InteractiveScriptSession interactiveScriptSession = scriptInterpreter.createInteractiveSession(); - final String scriptInclude = "include(\"engines." + engineId + "\".shortcuts." + engineId + "\");"; + final String scriptInclude = "const Shortcut = include(\"engines." + engineId + "\".shortcuts." + engineId + + "\");"; interactiveScriptSession.eval(scriptInclude, ignored -> interactiveScriptSession.eval("new " + engine + "Shortcut()", diff --git a/phoenicis-library/src/main/java/org/phoenicis/library/ShortcutManager.java b/phoenicis-library/src/main/java/org/phoenicis/library/ShortcutManager.java index 89dc8bf3839..9fe18c7e57c 100644 --- a/phoenicis-library/src/main/java/org/phoenicis/library/ShortcutManager.java +++ b/phoenicis-library/src/main/java/org/phoenicis/library/ShortcutManager.java @@ -120,7 +120,7 @@ public void createShortcut(ShortcutDTO shortcutDTO) { public void uninstallFromShortcut(ShortcutDTO shortcutDTO, Consumer errorCallback) { final InteractiveScriptSession interactiveScriptSession = scriptInterpreter.createInteractiveSession(); - interactiveScriptSession.eval("include(\"engines.wine.shortcuts.reader\");", + interactiveScriptSession.eval("const ShortcutReader = include(\"engines.wine.shortcuts.reader\");", ignored -> interactiveScriptSession.eval("new ShortcutReader()", output -> { final Value shortcutReader = (Value) output; shortcutReader.invokeMember("of", shortcutDTO); diff --git a/phoenicis-library/src/main/java/org/phoenicis/library/ShortcutRunner.java b/phoenicis-library/src/main/java/org/phoenicis/library/ShortcutRunner.java index 49e0bcae60d..118180c6440 100644 --- a/phoenicis-library/src/main/java/org/phoenicis/library/ShortcutRunner.java +++ b/phoenicis-library/src/main/java/org/phoenicis/library/ShortcutRunner.java @@ -42,7 +42,7 @@ public void run(String shortcutName, List arguments, Consumer public void run(ShortcutDTO shortcutDTO, List arguments, Consumer errorCallback) { final InteractiveScriptSession interactiveScriptSession = scriptInterpreter.createInteractiveSession(); - interactiveScriptSession.eval("include(\"engines.wine.shortcuts.reader\");", + interactiveScriptSession.eval("const ShortcutReader = include(\"engines.wine.shortcuts.reader\");", ignored -> interactiveScriptSession.eval("new ShortcutReader()", output -> { final Value shortcutReader = (Value) output; @@ -54,7 +54,7 @@ public void run(ShortcutDTO shortcutDTO, List arguments, Consumer errorCallback) { final InteractiveScriptSession interactiveScriptSession = scriptInterpreter.createInteractiveSession(); - interactiveScriptSession.eval("include(\"engines.wine.shortcuts.reader\");", + interactiveScriptSession.eval("const ShortcutReader = include(\"engines.wine.shortcuts.reader\");", ignored -> interactiveScriptSession.eval("new ShortcutReader()", output -> { final Value shortcutReader = (Value) output; diff --git a/phoenicis-repository/src/test/java/org/phoenicis/repository/types/ClasspathRepositoryTest.java b/phoenicis-repository/src/test/java/org/phoenicis/repository/types/ClasspathRepositoryTest.java index 15ffd89112a..337fcdf6c57 100644 --- a/phoenicis-repository/src/test/java/org/phoenicis/repository/types/ClasspathRepositoryTest.java +++ b/phoenicis-repository/src/test/java/org/phoenicis/repository/types/ClasspathRepositoryTest.java @@ -73,13 +73,17 @@ public void fetchInstallableApplicationsGraphicsPhotofiltreScriptName() { @Test public void fetchInstallableApplicationsGraphicsPhotofiltreScriptContent() { - assertEquals("include(\"engines.wine.quick_script.online_installer_script\");\n" + "\n" - + "new OnlineInstallerScript()\n" + " .name(\"Photofiltre\")\n" - + " .editor(\"Antonio Da Cruz\")\n" + " .applicationHomepage(\"http://photofiltre.free.fr\")\n" + assertEquals("const OnlineInstallerScript = include(\"engines.wine.quick_script.online_installer_script\");\n" + + "\n" + + "new OnlineInstallerScript()\n" + + " .name(\"Photofiltre\")\n" + + " .editor(\"Antonio Da Cruz\")\n" + + " .applicationHomepage(\"http://photofiltre.free.fr\")\n" + " .author(\"Quentin PÂRIS\")\n" + " .url(\"http://photofiltre.free.fr/utils/pf-setup-fr-652.exe\")\n" - + " .checksum(\"dc965875d698cd3f528423846f837d0dcf39616d\")\n" + " .category(\"Graphics\")\n" - + " .executable(\"PhotoFiltre.exe\")\n" + " .go();", + + " .checksum(\"dc965875d698cd3f528423846f837d0dcf39616d\")\n" + + " .category(\"Graphics\")\n" + + " .executable(\"PhotoFiltre.exe\");", repository.fetchInstallableApplications().getTypes().get(0).getCategories().get(1).getApplications() .get(0).getScripts() .get(0).getScript().trim()); diff --git a/phoenicis-repository/src/test/resources/org/phoenicis/repository/repositoryTest/Applications/Graphics/Photofiltre/Online/script.js b/phoenicis-repository/src/test/resources/org/phoenicis/repository/repositoryTest/Applications/Graphics/Photofiltre/Online/script.js index ad1a1922a97..198c7c64862 100644 --- a/phoenicis-repository/src/test/resources/org/phoenicis/repository/repositoryTest/Applications/Graphics/Photofiltre/Online/script.js +++ b/phoenicis-repository/src/test/resources/org/phoenicis/repository/repositoryTest/Applications/Graphics/Photofiltre/Online/script.js @@ -1,4 +1,4 @@ -include("engines.wine.quick_script.online_installer_script"); +const OnlineInstallerScript = include("engines.wine.quick_script.online_installer_script"); new OnlineInstallerScript() .name("Photofiltre") @@ -8,5 +8,4 @@ new OnlineInstallerScript() .url("http://photofiltre.free.fr/utils/pf-setup-fr-652.exe") .checksum("dc965875d698cd3f528423846f837d0dcf39616d") .category("Graphics") - .executable("PhotoFiltre.exe") - .go(); + .executable("PhotoFiltre.exe");