| 1 | |
package org.webslinger.ext.bsf.jython; |
| 2 | |
|
| 3 | |
import java.util.HashMap; |
| 4 | |
|
| 5 | |
import org.apache.bsf.BSFDeclaredBean; |
| 6 | |
import org.apache.bsf.BSFException; |
| 7 | |
|
| 8 | |
import org.python.core.Py; |
| 9 | |
import org.python.core.PyException; |
| 10 | |
import org.python.core.PyObject; |
| 11 | |
import org.python.util.InteractiveInterpreter; |
| 12 | |
|
| 13 | |
import org.webslinger.bsf.ApplyKey; |
| 14 | |
import org.webslinger.bsf.Compiler; |
| 15 | |
import org.webslinger.bsf.EvalKey; |
| 16 | |
import org.webslinger.bsf.ExecKey; |
| 17 | |
import org.webslinger.bsf.Key; |
| 18 | |
|
| 19 | 9 | public final class JythonCompiler extends Compiler<PyObject, PyObject> { |
| 20 | 1 | private static final HashMap<Class, String> compiledMethodNamesMap = new HashMap<Class, String>(); |
| 21 | |
static { |
| 22 | 1 | compiledMethodNamesMap.put(ApplyKey.class, "_BSF_Jython_Apply_"); |
| 23 | 1 | compiledMethodNamesMap.put(EvalKey.class, "_BSF_Jython_Eval_"); |
| 24 | 1 | compiledMethodNamesMap.put(ExecKey.class, "_BSF_Jython_Exec_"); |
| 25 | 1 | } |
| 26 | |
|
| 27 | 3 | private BSFPythonInterpreter interp = new BSFPythonInterpreter(); |
| 28 | |
|
| 29 | |
public JythonCompiler(JythonEngine engine) { |
| 30 | 3 | super(engine); |
| 31 | 3 | interp.setErr(System.err); |
| 32 | 3 | interp.setOut(System.out); |
| 33 | 3 | interp.set("bsf", engine.getBSFFunctions()); |
| 34 | 3 | } |
| 35 | |
|
| 36 | |
public void declareBean(BSFDeclaredBean bean) throws BSFException { |
| 37 | 0 | interp.set(bean.name, bean.bean); |
| 38 | 0 | } |
| 39 | |
|
| 40 | |
public void undeclareBean(BSFDeclaredBean bean) throws BSFException { |
| 41 | 0 | interp.set(bean.name, null); |
| 42 | 0 | } |
| 43 | |
|
| 44 | |
protected CompilerContext newContext(Key<PyObject> key) throws BSFException { |
| 45 | 3 | return new CompilerContext(key); |
| 46 | |
} |
| 47 | |
|
| 48 | |
protected PyObject compile(CompilerContext context) throws Throwable { |
| 49 | 3 | String methodName = compiledMethodNamesMap.get(context.key.getClass()); |
| 50 | 3 | StringBuilder code = context.code; |
| 51 | 3 | code.append("def ").append(methodName); |
| 52 | 3 | super.compile(context); |
| 53 | 3 | code.append(methodName); |
| 54 | 3 | if (engine.isDebugOn()) System.err.println(code); |
| 55 | 3 | synchronized (interp) { |
| 56 | 3 | interp.exec(code.toString()); |
| 57 | 3 | return interp.get(methodName); |
| 58 | 0 | } |
| 59 | |
} |
| 60 | |
|
| 61 | |
protected PyObject init(CompilerContext context, PyObject compiled) throws Throwable { |
| 62 | 3 | return compiled; |
| 63 | |
} |
| 64 | |
|
| 65 | |
public PyObject compileKey(ApplyKey<PyObject> key, CompilerContext context) throws BSFException { |
| 66 | 3 | StringBuilder code = context.code; |
| 67 | 3 | code.append('('); |
| 68 | 3 | String[] names = key.getNames(); |
| 69 | 3 | Class[] types = key.getTypes(); |
| 70 | 12 | for (int i = 0; i < types.length; i++) { |
| 71 | 9 | if (i != 0) code.append(", "); |
| 72 | 9 | Class type = types[i]; |
| 73 | |
String typeName, prefix, postfix; |
| 74 | 9 | code.append(names[i]); |
| 75 | |
} |
| 76 | 3 | code.append("):\n"); |
| 77 | 3 | appendText(code, "\t", context.text); |
| 78 | 3 | return null; |
| 79 | |
} |
| 80 | |
|
| 81 | |
public PyObject compileKey(EvalKey<PyObject> key, CompilerContext context) throws BSFException { |
| 82 | 0 | context.code.append("():\n"); |
| 83 | 0 | appendText(context.code, "\t", context.text); |
| 84 | 0 | return null; |
| 85 | |
} |
| 86 | |
|
| 87 | |
public PyObject compileKey(ExecKey<PyObject> key, CompilerContext context) throws BSFException { |
| 88 | 0 | context.code.append("():\n"); |
| 89 | 0 | appendText(context.code, "\t", context.text); |
| 90 | 0 | return null; |
| 91 | |
} |
| 92 | |
|
| 93 | |
public Object[] newWrapArray(int length) { |
| 94 | 0 | return new PyObject[length]; |
| 95 | |
} |
| 96 | |
|
| 97 | |
public Object[] newWrapArray(Object[] array) { |
| 98 | 3 | return new PyObject[array.length]; |
| 99 | |
} |
| 100 | |
|
| 101 | |
public Object wrap(Object object, Class type) { |
| 102 | 9 | return Py.java2py(object); |
| 103 | |
} |
| 104 | |
|
| 105 | |
public Object unwrap(Object object) { |
| 106 | 3 | if (object == null) return null; |
| 107 | 3 | Object ret = ((PyObject) object).__tojava__(Object.class); |
| 108 | 3 | if (ret != Py.NoConversion) return ret; |
| 109 | 0 | return object; |
| 110 | |
} |
| 111 | |
|
| 112 | |
private static class BSFPythonInterpreter extends InteractiveInterpreter { |
| 113 | |
public BSFPythonInterpreter() { |
| 114 | 3 | super(); |
| 115 | 3 | } |
| 116 | |
|
| 117 | |
|
| 118 | |
public void runcode(PyObject code) { |
| 119 | |
try { |
| 120 | 0 | this.exec(code); |
| 121 | 0 | } catch (PyException exc) { |
| 122 | 0 | throw exc; |
| 123 | 0 | } |
| 124 | 0 | } |
| 125 | |
} |
| 126 | |
} |