1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
|
/*
* Main.fx
*
* Created on Feb 25, 2009, 8:12:41 AM
*/
package interpreter;
import interpreter.TestJ;
import interpreter.Writer2TextComponent;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.lang.Exception;
import java.lang.Object;
import java.util.Date;
import javafx.ext.swing.SwingButton;
import javafx.scene.layout.VBox;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javax.script.ScriptContext;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineFactory;
import javax.script.ScriptEngineManager;
import javax.script.SimpleScriptContext;
/**
* @author fabriceb
*/
def scriptEngineManager = new ScriptEngineManager();
def factories = scriptEngineManager.getEngineFactories();
println("Available engines:");
for (factory in factories) {
println("Engine: {factory.getEngineName()} {factory.getEngineVersion()} / Language: {factory.getLanguageName()} {factory.getLanguageVersion()}");
def engineNames = factory.getNames();
println("\tAliases:");
for(alias in engineNames) {
println("\t\t{alias}");
}
}
def languages = ["javafx", "javascript"];
for (language in languages) {
var scriptEngineManager: ScriptEngineManager = null;
var scriptEngine: ScriptEngine = null;
var scene: Scene;
def editorNode: EditorNode = EditorNode {
width: bind scene.width;
height: bind scene.height - runButton.boundsInParent.height;
}
def runButton: SwingButton = SwingButton {
text: "Run"
action: function() {
try {
editorNode.clearMessages();
def text = editorNode.scriptArea.getText();
runScript(language, text, editorNode);
} catch (e:Exception) {
/*
var error = new StringWriter();
e.printStackTrace(new PrintWriter(error));
editorNode.errorArea.setText(error.toString());
editorNode.errorArea.setCaretPosition(0);
*/
e.printStackTrace();
}
}
};
Stage {
title: "Script interpreter {language}"
width: 400;
height: 600;
scene: scene = Scene {
content: VBox {
content: [editorNode, runButton];
}
}
}
}
function initScriptEngine(language:String, editorNode:EditorNode):ScriptEngine {
println("Initialization of the scripting engine.");
//var loader = Thread.currentThread().getContextClassLoader();
//scriptEngineManager = new ScriptEngineManager(loader);
//scriptEngine = scriptEngineManager.getEngineByName("ECMAScript");
def scriptEngine = scriptEngineManager.getEngineByName(language);
//var scriptEngine = scriptEngineManager.getEngineByExtension("fx");
//scriptEngine = scriptEngineManager.getEngineByExtension("js");
def bindings = scriptEngine.createBindings();
// JX Bindings.
bindings.put("now", new Date());
bindings.put("out", java.lang.System.out);
bindings.put("err", java.lang.System.err);
bindings.put("testfx", new TestFX());
bindings.put("testj", new TestJ());
def scriptContext = new SimpleScriptContext();
scriptContext.setWriter(new PrintWriter
(new Writer2TextComponent(editorNode.outputArea, "Output:")));
scriptContext.setErrorWriter(new PrintWriter
(new Writer2TextComponent(editorNode.errorArea, "Error output:")));
// Bug workaround.
scriptContext.setBindings(bindings, ScriptContext.GLOBAL_SCOPE);
scriptContext.setBindings(bindings, ScriptContext.ENGINE_SCOPE);
scriptEngine.setContext(scriptContext);
return scriptEngine;
}
function runScript(language:String, script:String, editorNode:EditorNode):Object {
// Lazily initialize the scripting engine.
def scriptEngine = initScriptEngine(language, editorNode);
if (script.trim().isEmpty()) {
return null;
}
println("Will eval:\n{script}");
def returnValue = scriptEngine.eval(script);
println("Returned value: {returnValue}");
return returnValue;
} |
Partager