| 1 | |
package org.webslinger.ext.bsf.rhino; |
| 2 | |
|
| 3 | |
import java.util.HashMap; |
| 4 | |
|
| 5 | |
import org.apache.bsf.BSFDeclaredBean; |
| 6 | |
import org.apache.bsf.BSFException; |
| 7 | |
import org.apache.bsf.util.BSFFunctions; |
| 8 | |
|
| 9 | |
import org.mozilla.javascript.Context; |
| 10 | |
import org.mozilla.javascript.Function; |
| 11 | |
import org.mozilla.javascript.Scriptable; |
| 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 | |
import org.webslinger.util.GeneratedResult; |
| 19 | |
|
| 20 | 9 | public final class RhinoCompiler extends Compiler<Function, Function> { |
| 21 | 1 | private static final HashMap<Class, String> compiledMethodNamesMap = new HashMap<Class, String>(); |
| 22 | |
static { |
| 23 | 1 | compiledMethodNamesMap.put(ApplyKey.class, "_BSF_Rhino_Apply_"); |
| 24 | 1 | compiledMethodNamesMap.put(EvalKey.class, "_BSF_Rhino_Eval_"); |
| 25 | 1 | compiledMethodNamesMap.put(ExecKey.class, "_BSF_Rhino_Exec_"); |
| 26 | 1 | } |
| 27 | |
|
| 28 | |
public class RhinoContext extends CompilerContext { |
| 29 | 3 | protected final org.mozilla.javascript.Context cx = org.mozilla.javascript.Context.getCurrentContext(); |
| 30 | |
|
| 31 | 3 | protected RhinoContext(Key<Function> key) throws BSFException { |
| 32 | 3 | super(key); |
| 33 | 3 | } |
| 34 | |
} |
| 35 | |
|
| 36 | |
private final RhinoEngine rhinoEngine; |
| 37 | |
|
| 38 | |
public RhinoCompiler(RhinoEngine engine) { |
| 39 | 3 | super(engine); |
| 40 | 3 | this.rhinoEngine = engine; |
| 41 | 3 | Context.enter(); |
| 42 | |
try { |
| 43 | 3 | Scriptable scope = rhinoEngine.getScope(); |
| 44 | 3 | scope.put("bsf", scope, engine.getBSFFunctions()); |
| 45 | |
} finally { |
| 46 | 3 | Context.exit(); |
| 47 | 3 | } |
| 48 | 3 | } |
| 49 | |
|
| 50 | |
public void declareBean(BSFDeclaredBean bean) throws BSFException { |
| 51 | 0 | Context.enter(); |
| 52 | |
try { |
| 53 | 0 | Scriptable scope = rhinoEngine.getScope(); |
| 54 | 0 | scope.put(bean.name, scope, bean.bean); |
| 55 | |
} finally { |
| 56 | 0 | Context.exit(); |
| 57 | 0 | } |
| 58 | 0 | } |
| 59 | |
|
| 60 | |
public void undeclareBean(BSFDeclaredBean bean) throws BSFException { |
| 61 | 0 | Context.enter(); |
| 62 | |
try { |
| 63 | 0 | rhinoEngine.getScope().delete(bean.name); |
| 64 | |
} finally { |
| 65 | 0 | Context.exit(); |
| 66 | 0 | } |
| 67 | 0 | } |
| 68 | |
|
| 69 | |
public GeneratedResult<Function> regen(Key<Function> key) throws BSFException { |
| 70 | 3 | Context.enter(); |
| 71 | |
try { |
| 72 | 3 | return super.regen(key); |
| 73 | |
} finally { |
| 74 | 3 | Context.exit(); |
| 75 | |
} |
| 76 | |
} |
| 77 | |
|
| 78 | |
|
| 79 | |
protected Function compile(CompilerContext context) throws Throwable { |
| 80 | 3 | return compile((RhinoContext) context); |
| 81 | |
} |
| 82 | |
|
| 83 | |
protected CompilerContext newContext(Key<Function> key) throws BSFException { |
| 84 | 3 | return new RhinoContext(key); |
| 85 | |
} |
| 86 | |
|
| 87 | |
protected Function compile(RhinoContext context) throws Throwable { |
| 88 | 3 | String methodName = compiledMethodNamesMap.get(context.key.getClass()); |
| 89 | 3 | StringBuilder code = context.code; |
| 90 | 3 | code.append("function ").append(methodName); |
| 91 | 3 | super.compile(context); |
| 92 | 3 | code.append(methodName); |
| 93 | 3 | if (engine.isDebugOn()) System.err.println(code); |
| 94 | |
try { |
| 95 | 3 | return (Function) context.cx.evaluateString(rhinoEngine.getScope(), code.toString(), context.key.getId().toString(), 1, null); |
| 96 | 0 | } catch (Exception e) { |
| 97 | 0 | throw (BSFException) new BSFException(BSFException.REASON_EXECUTION_ERROR, e.getMessage(), e).initCause(e); |
| 98 | |
} |
| 99 | |
} |
| 100 | |
|
| 101 | |
protected Function init(CompilerContext context, Function compiled) throws Throwable { |
| 102 | 3 | return compiled; |
| 103 | |
} |
| 104 | |
|
| 105 | |
public Function compileKey(ApplyKey<Function> key, CompilerContext context) throws BSFException { |
| 106 | 3 | StringBuilder code = context.code; |
| 107 | 3 | code.append('('); |
| 108 | 3 | String[] names = key.getNames(); |
| 109 | 3 | Class[] types = key.getTypes(); |
| 110 | 12 | for (int i = 0; i < types.length; i++) { |
| 111 | 9 | if (i != 0) code.append(", "); |
| 112 | 9 | Class type = types[i]; |
| 113 | 9 | code.append(names[i]); |
| 114 | |
} |
| 115 | 3 | code.append(") {"); |
| 116 | 3 | code.append(context.text); |
| 117 | 3 | code.append('}'); |
| 118 | 3 | return null; |
| 119 | |
} |
| 120 | |
|
| 121 | |
public Function compileKey(EvalKey<Function> key, CompilerContext context) throws BSFException { |
| 122 | 0 | context.code.append("() {"); |
| 123 | 0 | context.code.append(context.text); |
| 124 | 0 | context.code.append('}'); |
| 125 | 0 | return null; |
| 126 | |
} |
| 127 | |
|
| 128 | |
public Function compileKey(ExecKey<Function> key, CompilerContext context) throws BSFException { |
| 129 | 0 | context.code.append("() {"); |
| 130 | 0 | context.code.append(context.text); |
| 131 | 0 | context.code.append('}'); |
| 132 | 0 | return null; |
| 133 | |
} |
| 134 | |
} |