Coverage Report - org.webslinger.ext.bsf.jruby.JRubyCompiler
 
Classes in this File Line Coverage Branch Coverage Complexity
JRubyCompiler
0%
0/50
0%
0/4
0
 
 1  
 package org.webslinger.ext.bsf.jruby;
 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.jruby.IRuby;
 10  
 import org.jruby.Ruby;
 11  
 import org.jruby.RubyClass;
 12  
 import org.jruby.RubyMethod;
 13  
 import org.jruby.RubyProc;
 14  
 import org.jruby.javasupport.JavaObject;
 15  
 import org.jruby.javasupport.JavaUtil;
 16  
 import org.jruby.runtime.builtin.IRubyObject;
 17  
 
 18  
 import org.webslinger.bsf.ApplyKey;
 19  
 import org.webslinger.bsf.Compiler;
 20  
 import org.webslinger.bsf.EvalKey;
 21  
 import org.webslinger.bsf.ExecKey;
 22  
 import org.webslinger.bsf.Key;
 23  
 import org.webslinger.util.GeneratedResult;
 24  
 
 25  
 public final class JRubyCompiler extends Compiler<Object, Object> {
 26  0
     private static final HashMap<Class, String> compiledClassNamesMap = new HashMap<Class, String>();
 27  
     static {
 28  0
         compiledClassNamesMap.put(ApplyKey.class, "BSFJRubyApply");
 29  0
         compiledClassNamesMap.put(EvalKey.class, "BSFJRubyEval");
 30  0
         compiledClassNamesMap.put(ExecKey.class, "BSFJRubyExec");
 31  0
     }
 32  
 
 33  
     private final JRubyEngine jrubyEngine;
 34  
 
 35  
     public JRubyCompiler(JRubyEngine engine) {
 36  0
         super(engine);
 37  0
         jrubyEngine = engine;
 38  0
         getRuby().defineReadonlyVariable("$bsf", (IRubyObject) wrap(engine.getBSFFunctions(), BSFFunctions.class));
 39  0
     }
 40  
 
 41  
     public IRuby getRuby() {
 42  0
         return jrubyEngine.getRuby();
 43  
     }
 44  
 
 45  
     public void declareBean(BSFDeclaredBean bean) throws BSFException {
 46  0
         getRuby().defineReadonlyVariable('$' + bean.name, (IRubyObject) wrap(bean.bean, bean.type));
 47  0
     }
 48  
 
 49  
     public void undeclareBean(BSFDeclaredBean bean) throws BSFException {
 50  0
         getRuby().defineReadonlyVariable('$' + bean.name, null);
 51  0
     }
 52  
 
 53  
     protected CompilerContext newContext(Key<Object> key) throws BSFException {
 54  0
         return new CompilerContext(key);
 55  
     }
 56  
 
 57  0
     private int count = 0;
 58  
     protected Object compile(CompilerContext context) throws Throwable {
 59  
         String className;
 60  0
         synchronized (this) {
 61  0
             className = compiledClassNamesMap.get(context.key.getClass()) + count;
 62  0
             count++;
 63  0
         }
 64  0
         StringBuilder code = context.code;
 65  
         if (false) {
 66  
 
 67  
         code.append("class \n");//.append(className).append('\n');
 68  
         code.append("\tdef method");
 69  
         super.compile(context);
 70  
         code.append("\tend\n");
 71  
         code.append("end\n");
 72  
         } else {
 73  0
         code.append("proc {\n");
 74  0
         super.compile(context);
 75  0
         code.append("}\n");
 76  
         }
 77  
         //code.append(className).append('\n');
 78  0
         if (engine.isDebugOn()) System.err.println(code);
 79  0
         IRuby ruby = getRuby();
 80  0
         synchronized (ruby) {
 81  0
             return ruby.evalScript(code.toString());
 82  0
         }
 83  
     }
 84  
 
 85  
     //protected GeneratedResult init(CompilerContext context, Object compiled) throws Throwable {
 86  
     //    return super.init(context, ((RubyClass) compiled).newInstance(new IRubyObject[0]));
 87  
     //}
 88  
 
 89  
     protected Object init(CompilerContext context, Object compiled) throws Throwable {
 90  0
         return compiled;
 91  
     }
 92  
 
 93  
     public Object compileKey(ApplyKey<Object> key, CompilerContext context) throws BSFException {
 94  0
         StringBuilder code = context.code;
 95  0
         code.append('|');
 96  
         //code.append('(');
 97  0
         String[] names = key.getNames();
 98  0
         for (int i = 0; i < names.length; i++) {
 99  0
             if (i != 0) code.append(", ");
 100  0
             code.append(names[i]);
 101  
         }
 102  0
         code.append("|\n");
 103  
         //code.append(")\n");
 104  0
         appendText(code, "\t\t", context.text);
 105  0
         return null;
 106  
     }
 107  
 
 108  
     public Object compileKey(EvalKey<Object> key, CompilerContext context) throws BSFException {
 109  
         //context.code.append("()\n");
 110  0
         appendText(context.code, "\t\t", context.text);
 111  0
         return null;
 112  
     }
 113  
 
 114  
     public Object compileKey(ExecKey<Object> key, CompilerContext context) throws BSFException {
 115  
         //context.code.append("()\n");
 116  0
         appendText(context.code, "\t\t", context.text);
 117  0
         return null;
 118  
     }
 119  
 
 120  
     public Object[] newWrapArray(int length) {
 121  0
         return new IRubyObject[length];
 122  
     }
 123  
 
 124  
     public Object[] newWrapArray(Object[] array) {
 125  0
         return new IRubyObject[array.length];
 126  
     }
 127  
 
 128  
     public Object wrap(Object object, Class type) {
 129  0
         IRubyObject result = JavaUtil.convertJavaToRuby(getRuby(), object);
 130  0
         if (result instanceof JavaObject) {
 131  0
             return getRuby().getModule("JavaUtilities").callMethod("wrap", result);
 132  
         }
 133  0
         return result;
 134  
     }
 135  
 
 136  
     public Object unwrap(Object object) {
 137  0
         return JavaUtil.convertRubyToJava((IRubyObject) object);
 138  
     }
 139  
 }