Skip to content

Commit

Permalink
Add a simple Module System for "include" (#2050)
Browse files Browse the repository at this point in the history
- adds a simple module system supporting module.x = <<exported value>>
- add some missing const ...
  • Loading branch information
madoar authored Aug 11, 2019
1 parent d9d8f9a commit b7127c6
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,8 @@ public void deleteContainer(ContainerDTO container, Consumer<ContainerDTO> 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ public void createShortcut(ShortcutDTO shortcutDTO) {
public void uninstallFromShortcut(ShortcutDTO shortcutDTO, Consumer<Exception> 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public void run(String shortcutName, List<String> arguments, Consumer<Exception>
public void run(ShortcutDTO shortcutDTO, List<String> arguments, Consumer<Exception> 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;

Expand All @@ -54,7 +54,7 @@ public void run(ShortcutDTO shortcutDTO, List<String> arguments, Consumer<Except
public void stop(ShortcutDTO shortcutDTO, Consumer<Exception> 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;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
Original file line number Diff line number Diff line change
@@ -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")
Expand All @@ -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");
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit b7127c6

Please sign in to comment.