Search in sources :

Example 41 with ScriptObjectMirror

use of jdk.nashorn.api.scripting.ScriptObjectMirror in project java8-tutorial by winterbe.

the class Nashorn6 method main.

public static void main(String[] args) throws Exception {
    ScriptEngine engine = new ScriptEngineManager().getEngineByName("nashorn");
    engine.eval("load('res/nashorn6.js')");
    Invocable invocable = (Invocable) engine;
    Product product = new Product();
    product.setName("Rubber");
    product.setPrice(1.99);
    product.setStock(1337);
    ScriptObjectMirror result = (ScriptObjectMirror) invocable.invokeFunction("calculate", product);
    System.out.println(result.get("name") + ": " + result.get("valueOfGoods"));
}
Also used : Invocable(javax.script.Invocable) ScriptObjectMirror(jdk.nashorn.api.scripting.ScriptObjectMirror) ScriptEngineManager(javax.script.ScriptEngineManager) ScriptEngine(javax.script.ScriptEngine)

Example 42 with ScriptObjectMirror

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

the class WinePrefixContainerController method killProcesses.

public void killProcesses(WinePrefixContainerDTO winePrefix, Runnable doneCallback, Consumer<Exception> errorCallback) {
    final InteractiveScriptSession interactiveScriptSession = scriptInterpreter.createInteractiveSession();
    interactiveScriptSession.eval("include([\"Functions\", \"Engines\", \"Wine\"]);", ignored -> interactiveScriptSession.eval("new Wine()", output -> {
        final ScriptObjectMirror wine = (ScriptObjectMirror) output;
        wine.callMember("prefix", winePrefix.getName());
        wine.callMember("kill");
        doneCallback.run();
    }, errorCallback), errorCallback);
}
Also used : ShortcutCategoryDTO(org.phoenicis.library.dto.ShortcutCategoryDTO) RegistryWriter(org.phoenicis.win32.registry.RegistryWriter) Logger(org.slf4j.Logger) FileUtilities(org.phoenicis.tools.files.FileUtilities) LoggerFactory(org.slf4j.LoggerFactory) ScriptInterpreter(org.phoenicis.scripts.interpreter.ScriptInterpreter) IOException(java.io.IOException) HashMap(java.util.HashMap) File(java.io.File) Consumer(java.util.function.Consumer) TerminalOpener(org.phoenicis.tools.system.terminal.TerminalOpener) List(java.util.List) RegistryParameter(org.phoenicis.containers.wine.parameters.RegistryParameter) ShortcutManager(org.phoenicis.library.ShortcutManager) Map(java.util.Map) WinePrefixContainerDTO(org.phoenicis.containers.dto.WinePrefixContainerDTO) OperatingSystemFetcher(org.phoenicis.tools.system.OperatingSystemFetcher) ScriptObjectMirror(jdk.nashorn.api.scripting.ScriptObjectMirror) InteractiveScriptSession(org.phoenicis.scripts.interpreter.InteractiveScriptSession) LibraryManager(org.phoenicis.library.LibraryManager) ScriptObjectMirror(jdk.nashorn.api.scripting.ScriptObjectMirror) InteractiveScriptSession(org.phoenicis.scripts.interpreter.InteractiveScriptSession)

Example 43 with ScriptObjectMirror

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

the class WinePrefixContainerController method runInPrefix.

public void runInPrefix(WinePrefixContainerDTO winePrefix, String command, Runnable doneCallback, Consumer<Exception> errorCallback) {
    final InteractiveScriptSession interactiveScriptSession = scriptInterpreter.createInteractiveSession();
    interactiveScriptSession.eval("include([\"Functions\", \"Engines\", \"Wine\"]);", ignored -> interactiveScriptSession.eval("new Wine()", output -> {
        final ScriptObjectMirror wine = (ScriptObjectMirror) output;
        wine.callMember("prefix", winePrefix.getName());
        wine.callMember("run", command);
        wine.callMember("wait");
        doneCallback.run();
    }, errorCallback), errorCallback);
}
Also used : ShortcutCategoryDTO(org.phoenicis.library.dto.ShortcutCategoryDTO) RegistryWriter(org.phoenicis.win32.registry.RegistryWriter) Logger(org.slf4j.Logger) FileUtilities(org.phoenicis.tools.files.FileUtilities) LoggerFactory(org.slf4j.LoggerFactory) ScriptInterpreter(org.phoenicis.scripts.interpreter.ScriptInterpreter) IOException(java.io.IOException) HashMap(java.util.HashMap) File(java.io.File) Consumer(java.util.function.Consumer) TerminalOpener(org.phoenicis.tools.system.terminal.TerminalOpener) List(java.util.List) RegistryParameter(org.phoenicis.containers.wine.parameters.RegistryParameter) ShortcutManager(org.phoenicis.library.ShortcutManager) Map(java.util.Map) WinePrefixContainerDTO(org.phoenicis.containers.dto.WinePrefixContainerDTO) OperatingSystemFetcher(org.phoenicis.tools.system.OperatingSystemFetcher) ScriptObjectMirror(jdk.nashorn.api.scripting.ScriptObjectMirror) InteractiveScriptSession(org.phoenicis.scripts.interpreter.InteractiveScriptSession) LibraryManager(org.phoenicis.library.LibraryManager) ScriptObjectMirror(jdk.nashorn.api.scripting.ScriptObjectMirror) InteractiveScriptSession(org.phoenicis.scripts.interpreter.InteractiveScriptSession)

Example 44 with ScriptObjectMirror

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

the class EnginesController method deleteEngine.

private void deleteEngine(EngineDTO engineDTO, Consumer<Exception> errorCallback) {
    final InteractiveScriptSession interactiveScriptSession = scriptInterpreter.createInteractiveSession();
    interactiveScriptSession.eval("include([\"Functions\", \"Engines\", \"" + engineDTO.getCategory() + "\"]);", 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 : Files(java.nio.file.Files) ErrorMessage(org.phoenicis.javafx.views.common.ErrorMessage) LoggerFactory(org.slf4j.LoggerFactory) ScriptInterpreter(org.phoenicis.scripts.interpreter.ScriptInterpreter) IOException(java.io.IOException) Localisation.tr(org.phoenicis.configuration.localisation.Localisation.tr) AppsController(org.phoenicis.javafx.controller.apps.AppsController) File(java.io.File) Platform(javafx.application.Platform) Consumer(java.util.function.Consumer) List(java.util.List) EngineDTO(org.phoenicis.engines.dto.EngineDTO) EnginesSource(org.phoenicis.engines.EnginesSource) EngineCategoryDTO(org.phoenicis.engines.dto.EngineCategoryDTO) ViewEngines(org.phoenicis.javafx.views.mainwindow.engines.ViewEngines) ScriptObjectMirror(jdk.nashorn.api.scripting.ScriptObjectMirror) ConfirmMessage(org.phoenicis.javafx.views.common.ConfirmMessage) URI(java.net.URI) InteractiveScriptSession(org.phoenicis.scripts.interpreter.InteractiveScriptSession) Path(java.nio.file.Path) ThemeManager(org.phoenicis.javafx.views.common.ThemeManager) ScriptObjectMirror(jdk.nashorn.api.scripting.ScriptObjectMirror) InteractiveScriptSession(org.phoenicis.scripts.interpreter.InteractiveScriptSession)

Example 45 with ScriptObjectMirror

use of jdk.nashorn.api.scripting.ScriptObjectMirror in project sling by apache.

the class Module method runScript.

/**
     *
     * @return @throws ScriptException
     */
public Object runScript() throws ScriptException {
    log.debug("run script with id {}", get("id"));
    ScriptObjectMirror function = factory.getModuleCache().get((String) get(CONTEXT_FIELD_FILENAME));
    Resource moduleResource = (Resource) get(CONTEXT_FIELD_MODULE_RESOURCE);
    Resource resource = (Resource) get(CONTEXT_FIELD_RESOURCE);
    if (function == null) {
        if (moduleScript.isJsFile()) {
            function = Module.this.decorateScript(//readScript(moduleResource)//  
            readScript(moduleResource));
        }
        if (moduleScript.isJsonFile()) {
            String jsonfile = readScript(moduleResource);
            function = decorateScript("module.exports = " + jsonfile, false);
        }
        if (moduleScript.isResourceFile()) {
            Iterator<Resource> resChildren = moduleResource.listChildren();
            ArrayList<Resource> children = new ArrayList<Resource>();
            resChildren.forEachRemaining(children::add);
            put("children", children);
            /*ValueMap map = moduleResource.adaptTo(ValueMap.class);

                 Invocable invocable = (Invocable) factory.getNashornEngine();
                 Object jsonprop = null;
                 try {
                 jsonprop = invocable.invokeMethod(factory.getNashornEngine().eval("JSON"), "stringify", map);
                 } catch (NoSuchMethodException ex) {
                 throw new ScriptException(ex);
                 }
                 */
            SimpleResource simpleResource = moduleResource.adaptTo(SimpleResource.class);
            put("simpleResource", simpleResource);
            /*   String source = "exports.properties =  " + jsonprop + ";"
                 + "exports.path = currentNode.resource.path;"
                 + "exports.simpleResource = this.simpleResource;"
                 + "exports.children = this.children;";*/
            String source = "module.exports = this.simpleResource;";
            function = decorateScript(source, false);
        }
        if (!moduleScript.isResourceFile()) {
            factory.getModuleCache().put(moduleScript.getResource().getPath(), function);
        }
    } else {
        log.debug("module " + get(CONTEXT_FIELD_ID) + " received from cache");
    }
    if (moduleScript.isTextFile()) {
        log.debug("is textfile loaidng file");
        String source = StringEscapeUtils.escapeEcmaScript(readScript(moduleResource));
        log.debug("sourcE: ");
        source = "module.exports = \"" + source + "\";";
        log.debug(source);
        function = Module.this.decorateScript(source);
        factory.getModuleCache().put(moduleScript.getResource().getPath(), function);
    }
    JSObject process = (JSObject) factory.getNashornEngine().eval("Object");
    process.setMember("domain", log);
    if (function != null) {
        SimpleBindings currentNode = new SimpleBindings();
        if (resource != null) {
            currentNode.put("resource", resource);
            currentNode.put("properties", resource.adaptTo(ValueMap.class));
        } else {
            log.debug("module id {} resource is null", get(CONTEXT_FIELD_ID));
        }
        // changed require to be generated within the wrapper
        // TODO: need to be refactored
        function.call(this, get(CONTEXT_FIELD_EXPORTS), this, this, get(CONTEXT_FIELD_FILENAME), ((Resource) get(CONTEXT_FIELD_MODULE_RESOURCE)).getParent().getPath(), currentNode, (ConsoleLog) get(CONTEXT_FIELD_CONSOLE), null, (SlingScriptHelper) get(CONTEXT_FIELD_SLING), resource.adaptTo(SimpleResource.class));
    } else {
        log.warn("function not called because it is null");
    }
    put(CONTEXT_FIELD_EXPORTS, get(CONTEXT_FIELD_EXPORTS));
    return get(CONTEXT_FIELD_EXPORTS);
}
Also used : ScriptObjectMirror(jdk.nashorn.api.scripting.ScriptObjectMirror) SimpleBindings(javax.script.SimpleBindings) ValueMap(org.apache.sling.api.resource.ValueMap) SimpleResource(org.apache.sling.scripting.esx.plugins.SimpleResource) Resource(org.apache.sling.api.resource.Resource) ArrayList(java.util.ArrayList) SimpleResource(org.apache.sling.scripting.esx.plugins.SimpleResource) JSObject(jdk.nashorn.api.scripting.JSObject)

Aggregations

ScriptObjectMirror (jdk.nashorn.api.scripting.ScriptObjectMirror)76 Test (org.junit.Test)34 InteractiveScriptSession (org.phoenicis.scripts.interpreter.InteractiveScriptSession)28 ScriptInterpreter (org.phoenicis.scripts.interpreter.ScriptInterpreter)28 Consumer (java.util.function.Consumer)27 List (java.util.List)23 LoggerFactory (org.slf4j.LoggerFactory)20 File (java.io.File)17 IOException (java.io.IOException)17 Logger (org.slf4j.Logger)14 WinePrefixContainerDTO (org.phoenicis.containers.dto.WinePrefixContainerDTO)12 Map (java.util.Map)11 ArrayList (java.util.ArrayList)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 ShortcutDTO (org.phoenicis.library.dto.ShortcutDTO)10 RegistryWriter (org.phoenicis.win32.registry.RegistryWriter)10 URI (java.net.URI)8