Search in sources :

Example 46 with ScriptObjectMirror

use of jdk.nashorn.api.scripting.ScriptObjectMirror in project molgenis by molgenis.

the class NashornScriptEngine method convertNashornValue.

private Object convertNashornValue(Object nashornValue) {
    if (nashornValue == null) {
        return null;
    }
    Object convertedValue;
    String idValueKey = "_idValue";
    if (nashornValue instanceof ScriptObjectMirror) {
        ScriptObjectMirror scriptObjectMirror = (ScriptObjectMirror) nashornValue;
        if (scriptObjectMirror.isArray()) {
            convertedValue = scriptObjectMirror.values().stream().map(this::convertNashornValue).collect(Collectors.toList());
        } else {
            if ("Date".equals(scriptObjectMirror.getClassName())) {
                // convert to Java Interface
                JsDate jsDate = ((Invocable) scriptEngine).getInterface(scriptObjectMirror, JsDate.class);
                return jsDate.getTime();
            } else if (((ScriptObjectMirror) nashornValue).containsKey(idValueKey)) {
                // entity object returned from script
                return ((ScriptObjectMirror) nashornValue).get(idValueKey);
            } else {
                throw new RuntimeException("Unable to convert [ScriptObjectMirror]");
            }
        }
    } else if (nashornValue instanceof Map) {
        Map mapValue = (Map) (nashornValue);
        if (mapValue.get(idValueKey) != null) {
            convertedValue = mapValue.get(idValueKey);
        } else {
            convertedValue = nashornValue;
        }
    } else {
        convertedValue = nashornValue;
    }
    return convertedValue;
}
Also used : ScriptObjectMirror(jdk.nashorn.api.scripting.ScriptObjectMirror) JSObject(jdk.nashorn.api.scripting.JSObject) Map(java.util.Map)

Example 47 with ScriptObjectMirror

use of jdk.nashorn.api.scripting.ScriptObjectMirror in project POL-POM-5 by PhoenicisOrg.

the class EnginesController method deleteEngine.

private void deleteEngine(EngineDTO engineDTO, Consumer<Exception> errorCallback) {
    final InteractiveScriptSession interactiveScriptSession = scriptInterpreter.createInteractiveSession();
    interactiveScriptSession.eval("include([\"Engines\", \"" + engineDTO.getCategory() + "\", \"Engine\", \"Object\"]);", ignored -> interactiveScriptSession.eval("new Wine()", output -> {
        final ScriptObjectMirror wine = (ScriptObjectMirror) output;
        wine.callMember("delete", engineDTO.getCategory(), engineDTO.getSubCategory(), engineDTO.getVersion(), engineDTO.getUserData());
    }, errorCallback), errorCallback);
}
Also used : RepositoryDTO(org.phoenicis.repository.dto.RepositoryDTO) LoggerFactory(org.slf4j.LoggerFactory) AppsController(org.phoenicis.javafx.controller.apps.AppsController) ArrayList(java.util.ArrayList) TypeDTO(org.phoenicis.repository.dto.TypeDTO) EnginesSource(org.phoenicis.engines.EnginesSource) ConfirmMessage(org.phoenicis.javafx.views.common.ConfirmMessage) URI(java.net.URI) Path(java.nio.file.Path) CategoryDTO(org.phoenicis.repository.dto.CategoryDTO) Files(java.nio.file.Files) ErrorMessage(org.phoenicis.javafx.views.common.ErrorMessage) RepositoryManager(org.phoenicis.repository.RepositoryManager) ScriptInterpreter(org.phoenicis.scripts.interpreter.ScriptInterpreter) IOException(java.io.IOException) Localisation.tr(org.phoenicis.configuration.localisation.Localisation.tr) File(java.io.File) Platform(javafx.application.Platform) Consumer(java.util.function.Consumer) List(java.util.List) EngineDTO(org.phoenicis.engines.dto.EngineDTO) Optional(java.util.Optional) ScriptObjectMirror(jdk.nashorn.api.scripting.ScriptObjectMirror) InteractiveScriptSession(org.phoenicis.scripts.interpreter.InteractiveScriptSession) ThemeManager(org.phoenicis.javafx.views.common.ThemeManager) EnginesView(org.phoenicis.javafx.views.mainwindow.engines.EnginesView) ScriptObjectMirror(jdk.nashorn.api.scripting.ScriptObjectMirror) InteractiveScriptSession(org.phoenicis.scripts.interpreter.InteractiveScriptSession)

Example 48 with ScriptObjectMirror

use of jdk.nashorn.api.scripting.ScriptObjectMirror in project POL-POM-5 by PhoenicisOrg.

the class LibraryController method createShortcut.

/**
 * creates a new shortcut
 * @param shortcutCreationDTO DTO describing the new shortcut
 */
private void createShortcut(ShortcutCreationDTO shortcutCreationDTO) {
    // get container
    // TODO: smarter way using container manager
    final String executablePath = shortcutCreationDTO.getExecutable().getAbsolutePath();
    final String pathInContainers = executablePath.replace(containersPath, "");
    final String[] split = pathInContainers.split("/");
    final String engineContainer = split[0];
    final String engine = (Character.toUpperCase(engineContainer.charAt(0)) + engineContainer.substring(1)).replace("prefix", "");
    final String container = split[1];
    final InteractiveScriptSession interactiveScriptSession = scriptInterpreter.createInteractiveSession();
    interactiveScriptSession.eval("include([\"Engines\", \"" + engine + "\", \"Shortcuts\", \"" + engine + "\"]);", ignored -> interactiveScriptSession.eval("new " + engine + "Shortcut()", output -> {
        final ScriptObjectMirror shortcutObject = (ScriptObjectMirror) output;
        shortcutObject.callMember("name", shortcutCreationDTO.getName());
        shortcutObject.callMember("category", shortcutCreationDTO.getCategory());
        shortcutObject.callMember("description", shortcutCreationDTO.getDescription());
        shortcutObject.callMember("miniature", shortcutCreationDTO.getMiniature());
        shortcutObject.callMember("search", shortcutCreationDTO.getExecutable().getName());
        shortcutObject.callMember("prefix", container);
        shortcutObject.callMember("create");
    }, e -> this.showErrorMessage(e, tr("Error while creating shortcut"))), e -> this.showErrorMessage(e, tr("Error while creating shortcut")));
}
Also used : ShortcutCategoryDTO(org.phoenicis.library.dto.ShortcutCategoryDTO) LibraryView(org.phoenicis.javafx.views.mainwindow.library.LibraryView) RepositoryDTO(org.phoenicis.repository.dto.RepositoryDTO) ErrorMessage(org.phoenicis.javafx.views.common.ErrorMessage) ShortcutCreationDTO(org.phoenicis.library.dto.ShortcutCreationDTO) RepositoryManager(org.phoenicis.repository.RepositoryManager) ConsoleController(org.phoenicis.javafx.controller.library.console.ConsoleController) ScriptInterpreter(org.phoenicis.scripts.interpreter.ScriptInterpreter) Localisation.tr(org.phoenicis.configuration.localisation.Localisation.tr) Platform(javafx.application.Platform) Value(org.springframework.beans.factory.annotation.Value) List(java.util.List) ShortcutManager(org.phoenicis.library.ShortcutManager) ShortcutDTO(org.phoenicis.library.dto.ShortcutDTO) ShortcutRunner(org.phoenicis.library.ShortcutRunner) ScriptObjectMirror(jdk.nashorn.api.scripting.ScriptObjectMirror) ConfirmMessage(org.phoenicis.javafx.views.common.ConfirmMessage) InteractiveScriptSession(org.phoenicis.scripts.interpreter.InteractiveScriptSession) LibraryManager(org.phoenicis.library.LibraryManager) Collections(java.util.Collections) ScriptObjectMirror(jdk.nashorn.api.scripting.ScriptObjectMirror) InteractiveScriptSession(org.phoenicis.scripts.interpreter.InteractiveScriptSession)

Example 49 with ScriptObjectMirror

use of jdk.nashorn.api.scripting.ScriptObjectMirror in project POL-POM-5 by PhoenicisOrg.

the class WinePrefixContainerController method runInContainer.

public void runInContainer(ContainerDTO container, String command, Runnable doneCallback, Consumer<Exception> errorCallback) {
    final InteractiveScriptSession interactiveScriptSession = scriptInterpreter.createInteractiveSession();
    interactiveScriptSession.eval("include([\"Engines\", \"" + container.getEngine() + "\", \"Engine\", \"Object\"]);", ignored -> interactiveScriptSession.eval("new " + container.getEngine() + "()", output -> {
        final ScriptObjectMirror wine = (ScriptObjectMirror) output;
        wine.callMember("prefix", container.getName());
        wine.callMember("run", command);
        wine.callMember("wait");
        doneCallback.run();
    }, errorCallback), errorCallback);
}
Also used : Consumer(java.util.function.Consumer) RegistryWriter(org.phoenicis.win32.registry.RegistryWriter) RegistryParameter(org.phoenicis.containers.wine.parameters.RegistryParameter) Logger(org.slf4j.Logger) LoggerFactory(org.slf4j.LoggerFactory) WinePrefixContainerDTO(org.phoenicis.containers.dto.WinePrefixContainerDTO) ScriptInterpreter(org.phoenicis.scripts.interpreter.ScriptInterpreter) ScriptObjectMirror(jdk.nashorn.api.scripting.ScriptObjectMirror) InteractiveScriptSession(org.phoenicis.scripts.interpreter.InteractiveScriptSession) ContainerDTO(org.phoenicis.containers.dto.ContainerDTO) ScriptObjectMirror(jdk.nashorn.api.scripting.ScriptObjectMirror) InteractiveScriptSession(org.phoenicis.scripts.interpreter.InteractiveScriptSession)

Example 50 with ScriptObjectMirror

use of jdk.nashorn.api.scripting.ScriptObjectMirror in project JSCover by tntim96.

the class BranchInstrumentorIntegrationTest method shouldWrapForConditionVariable.

@Test
public void shouldWrapForConditionVariable() throws Exception {
    StringBuilder script = new StringBuilder("var x = true;\n");
    script.append("for (var i = 0; x; i++)\n");
    script.append("  x = false;\n");
    runScript(script.toString(), false);
    ScriptObjectMirror coverageData = (ScriptObjectMirror) engine.eval("_$jscoverage['test.js'].branchData[2][1]");
    assertThat(coverageData.get("evalTrue"), equalTo(1.0));
    assertThat(coverageData.get("evalFalse"), equalTo(1.0));
}
Also used : ScriptObjectMirror(jdk.nashorn.api.scripting.ScriptObjectMirror) Test(org.junit.Test)

Aggregations

ScriptObjectMirror (jdk.nashorn.api.scripting.ScriptObjectMirror)73 Test (org.junit.Test)34 InteractiveScriptSession (org.phoenicis.scripts.interpreter.InteractiveScriptSession)24 ScriptInterpreter (org.phoenicis.scripts.interpreter.ScriptInterpreter)24 Consumer (java.util.function.Consumer)23 List (java.util.List)20 LoggerFactory (org.slf4j.LoggerFactory)19 File (java.io.File)16 IOException (java.io.IOException)16 Logger (org.slf4j.Logger)13 WinePrefixContainerDTO (org.phoenicis.containers.dto.WinePrefixContainerDTO)12 Map (java.util.Map)10 RegistryParameter (org.phoenicis.containers.wine.parameters.RegistryParameter)10 LibraryManager (org.phoenicis.library.LibraryManager)10 ShortcutManager (org.phoenicis.library.ShortcutManager)10 ShortcutCategoryDTO (org.phoenicis.library.dto.ShortcutCategoryDTO)10 RegistryWriter (org.phoenicis.win32.registry.RegistryWriter)10 ArrayList (java.util.ArrayList)9 Platform (javafx.application.Platform)8 Localisation.tr (org.phoenicis.configuration.localisation.Localisation.tr)8