Mercurial Hosting > luan
view core/src/luan/impl/LuanStateImpl.java @ 618:5e495e4e560b
add lucene indexed_only_fields
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Fri, 01 Jan 2016 01:24:10 -0700 |
parents | f22a09e98b04 |
children | 859c0dedc8b6 |
line wrap: on
line source
package luan.impl; import java.util.List; import java.util.ArrayList; import java.util.Map; import java.util.HashMap; import luan.Luan; import luan.LuanState; import luan.LuanTable; import luan.LuanFunction; import luan.LuanException; import luan.LuanElement; import luan.LuanSource; import luan.DeepCloner; import luan.StackTraceElement; final class LuanStateImpl extends LuanState { private static class Frame { final Frame previousFrame; final Closure closure; final Object[] stack; final Object[] varArgs; UpValue[] downValues = null; Frame( Frame previousFrame, Closure closure, int stackSize, Object[] varArgs) { this.previousFrame = previousFrame; this.closure = closure; this.stack = new Object[stackSize]; this.varArgs = varArgs; } void stackClear(int start,int end) { if( downValues != null ) { for( int i=start; i<end; i++ ) { UpValue downValue = downValues[i]; if( downValue != null ) { downValue.close(); downValues[i] = null; } } } for( int i=start; i<end; i++ ) { stack[i] = null; } } UpValue getUpValue(int index) { if( downValues==null ) downValues = new UpValue[stack.length]; if( downValues[index] == null ) downValues[index] = new UpValue(stack,index); return downValues[index]; } } private Frame frame = null; Object returnValues; Closure tailFn; LuanStateImpl() {} private LuanStateImpl(LuanStateImpl luan) { super(luan); } @Override public LuanState shallowClone() { // if( frame != null ) // throw new IllegalStateException("frame isn't null"); return new LuanStateImpl(this); } // returns stack Object[] newFrame(Closure closure, int stackSize, Object[] varArgs) { returnValues = LuanFunction.NOTHING; tailFn = null; frame = new Frame(frame,closure,stackSize,varArgs); return frame.stack; } void popFrame() { returnValues = LuanFunction.NOTHING; tailFn = null; frame = frame.previousFrame; } Object stackGet(int index) { return frame.stack[index]; } void stackSet(int index,Object value) { frame.stack[index] = value; } void stackClear(int start,int end) { frame.stackClear(start,end); } Object[] varArgs() { return frame.varArgs; } Closure closure() { return frame.closure; } UpValue getUpValue(int index) { return frame.getUpValue(index); } @Override public boolean hasJava() { if( frame==null ) return false; return ((LuanTable)frame.closure.upValues()[0].get()).hasJava; } @Override public void setJava() { ((LuanTable)frame.closure.upValues()[0].get()).hasJava = true; } @Override public LuanSource currentSource(){ if( frame==null ) return null; return frame.closure.fnDef.el().source; } @Override public boolean isLessThan(Object o1,Object o2) throws LuanException { if( o1 instanceof Number && o2 instanceof Number ) { Number n1 = (Number)o1; Number n2 = (Number)o2; return n1.doubleValue() < n2.doubleValue(); } if( o1 instanceof String && o2 instanceof String ) { String s1 = (String)o1; String s2 = (String)o2; return s1.compareTo(s2) < 0; } LuanFunction fn = getBinHandler("__lt",o1,o2); if( fn != null ) return checkBoolean( Luan.first(fn.call(this,new Object[]{o1,o2})) ); throw new LuanException(this, "attempt to compare " + Luan.type(o1) + " with " + Luan.type(o2) ); } LuanFunction getBinHandler(String op,Object o1,Object o2) throws LuanException { if( o1 instanceof LuanTable ) { LuanFunction f1 = getHandlerFunction(op,(LuanTable)o1); if( f1 != null ) return f1; } return o2 instanceof LuanTable ? getHandlerFunction(op,(LuanTable)o2) : null; } LuanFunction getHandlerFunction(String op,LuanTable t) throws LuanException { Object f = t.getHandler(op); if( f == null ) return null; return checkFunction(f); } Boolean checkBoolean(Object obj,LuanElement el) throws LuanException { push(el,null); try { return checkBoolean(obj); } finally { pop(); } } String toString(Object obj,LuanElement el) throws LuanException { push(el,null); try { return toString(obj); } finally { pop(); } } void push(LuanElement el,String fnName) { if( el == null ) throw new RuntimeException(); stackTrace.add( new StackTraceElement(el,fnName) ); } void pop() { stackTrace.remove(stackTrace.size()-1); } }