From: <zy...@us...> - 2009-05-02 06:13:20
|
Revision: 6285 http://jython.svn.sourceforge.net/jython/?rev=6285&view=rev Author: zyasoft Date: 2009-05-02 06:13:13 +0000 (Sat, 02 May 2009) Log Message: ----------- Some initial code, just to get it going. Added Paths: ----------- branches/jsr223/src/org/python/jsr223/ branches/jsr223/src/org/python/jsr223/PyScriptEngine.java branches/jsr223/src/org/python/jsr223/PyScriptEngineFactory.java Added: branches/jsr223/src/org/python/jsr223/PyScriptEngine.java =================================================================== --- branches/jsr223/src/org/python/jsr223/PyScriptEngine.java (rev 0) +++ branches/jsr223/src/org/python/jsr223/PyScriptEngine.java 2009-05-02 06:13:13 UTC (rev 6285) @@ -0,0 +1,110 @@ +package org.python.jsr223; + +import java.io.Reader; +import javax.script.AbstractScriptEngine; +import javax.script.Bindings; +import javax.script.Compilable; +import javax.script.CompiledScript; +import javax.script.Invocable; +import javax.script.ScriptContext; +import javax.script.ScriptEngine; +import javax.script.ScriptEngineFactory; +import javax.script.ScriptException; +import org.python.core.Py; +import org.python.core.PyException; +import org.python.core.PyObject; +import org.python.util.PythonInterpreter; + +public class PyScriptEngine extends AbstractScriptEngine implements Compilable, Invocable { + + private final PythonInterpreter interp; + private final ScriptEngineFactory factory; + + PyScriptEngine(ScriptEngineFactory factory) { + this.factory = factory; + interp = new PythonInterpreter(); + } + + public Object eval(String script, ScriptContext context) throws ScriptException { + throw new UnsupportedOperationException("Not supported yet."); + } + + // it would be nice if we supported a Reader interface in Py.compileFlags, instead of having + // to create a string here + public Object eval(Reader reader, ScriptContext context) throws ScriptException { + throw new UnsupportedOperationException("Not supported yet."); + } + + public Bindings createBindings() { + throw new UnsupportedOperationException("Not supported yet."); + } + + public ScriptEngineFactory getFactory() { + return factory; + } + + // i assume this should simply return a PyModule object or something + public CompiledScript compile(String script) throws ScriptException { + throw new UnsupportedOperationException("Not supported yet."); + } + + public CompiledScript compile(Reader script) throws ScriptException { + throw new UnsupportedOperationException("Not supported yet."); + } + + public Object invokeMethod(Object thiz, String name, Object... args) throws ScriptException, NoSuchMethodException { + try { + if (thiz instanceof PyObject) { + return ((PyObject) thiz).invoke(name, java2py(args)).__tojava__(Object.class); + } + throw new NoSuchMethodException(name); + } catch (PyException pye) { + if (Py.matchException(pye, Py.AttributeError)) { + throw new NoSuchMethodException(name); + } + throw new ScriptException(pye); + } + } + + public Object invokeFunction(String name, Object... args) throws ScriptException, NoSuchMethodException { + try { + return interp.get(name).__call__(java2py(args)).__tojava__(Object.class); + } catch (PyException pye) { + if (Py.matchException(pye, Py.AttributeError)) { + throw new NoSuchMethodException(name); + } + throw new ScriptException(pye); + } + } + + public <T> T getInterface(Class<T> clasz) { + throw new UnsupportedOperationException("Not supported yet."); + } + + public <T> T getInterface(Object thiz, Class<T> clasz) { + throw new UnsupportedOperationException("Not supported yet."); + } + + private static PyObject[] java2py(Object[] args) { + PyObject wrapped[] = new PyObject[args.length]; + for (int i = 0; i < args.length; i++) { + wrapped[i] = Py.java2py(args[i]); + } + return wrapped; + } + + // wraps a PyCode object + private static class PyCompiledScript extends CompiledScript { + + @Override + public Object eval(ScriptContext arg0) throws ScriptException { + throw new UnsupportedOperationException("Not supported yet."); + } + + @Override + public ScriptEngine getEngine() { + throw new UnsupportedOperationException("Not supported yet."); + } + + } +} Added: branches/jsr223/src/org/python/jsr223/PyScriptEngineFactory.java =================================================================== --- branches/jsr223/src/org/python/jsr223/PyScriptEngineFactory.java (rev 0) +++ branches/jsr223/src/org/python/jsr223/PyScriptEngineFactory.java 2009-05-02 06:13:13 UTC (rev 6285) @@ -0,0 +1,95 @@ +package org.python.jsr223; + +import java.util.Arrays; +import java.util.Collections; +import java.util.List; +import javax.script.ScriptEngine; +import javax.script.ScriptEngineFactory; +import org.python.Version; +import org.python.core.Py; + +public class PyScriptEngineFactory implements ScriptEngineFactory { + + public String getEngineName() { + return "jython"; + } + + public String getEngineVersion() { + return String.format("%s.%s.%s", Version.PY_MAJOR_VERSION, Version.PY_MINOR_VERSION, Version.PY_MICRO_VERSION); + } + + public List<String> getExtensions() { + return Collections.unmodifiableList(Arrays.asList("py")); + } + + public String getLanguageName() { + return "python"; + } + + public String getLanguageVersion() { + return "2.5.0"; + } + + public Object getParameter(String key) { + if (key.equals(ScriptEngine.ENGINE)) { + return getEngineName(); + } else if (key.equals(ScriptEngine.ENGINE_VERSION)) { + return getEngineVersion(); + } else if (key.equals(ScriptEngine.NAME)) { + return getEngineName(); + } else if (key.equals(ScriptEngine.LANGUAGE)) { + return getLanguageName(); + } else if (key.equals(ScriptEngine.LANGUAGE_VERSION)) { + return getLanguageVersion(); + } else if (key.equals("THREADING")) { + return "MULTITHREADED"; + } else { + return null; + } + + } + + public String getMethodCallSyntax(String obj, String m, String... args) { + StringBuilder buffer = new StringBuilder(); + buffer.append(String.format("%s.%s(", obj, m)); + int i = args.length; + for (String arg : args) { + buffer.append(arg); + if (i-- > 0) { + buffer.append(", "); + } + } + buffer.append(")"); + return buffer.toString(); + } + + // presumably a unicode string + public String getOutputStatement(String toDisplay) { + StringBuilder buffer = new StringBuilder(toDisplay.length() + 8); + buffer.append("print "); + buffer.append(Py.newUnicode(toDisplay).__repr__()); + return buffer.toString(); + } + + public String getProgram(String... statements) { + StringBuilder buffer = new StringBuilder(); + for (String statement : statements) { + buffer.append(statement); + buffer.append("\n"); + } + return buffer.toString(); + } + + public ScriptEngine getScriptEngine() { + return new PyScriptEngine(this); + } + + public List<String> getMimeTypes() { + return Collections.EMPTY_LIST; + } + + public List<String> getNames() { + return Collections.unmodifiableList(Arrays.asList("python", "jython")); + } + +} This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |