comparison core/src/luan/LuanState.java @ 579:f22a09e98b04

clean up LuanState
author Franklin Schmidt <fschmidt@gmail.com>
date Thu, 16 Jul 2015 15:14:31 -0600
parents 60c549d43988
children 859c0dedc8b6
comparison
equal deleted inserted replaced
578:60c549d43988 579:f22a09e98b04
13 import luan.modules.JavaLuan; 13 import luan.modules.JavaLuan;
14 14
15 15
16 public abstract class LuanState implements DeepCloneable { 16 public abstract class LuanState implements DeepCloneable {
17 17
18 final List<StackTraceElement> stackTrace = new ArrayList<StackTraceElement>(); 18 protected final List<StackTraceElement> stackTrace = new ArrayList<StackTraceElement>();
19 19
20 private Map registry; 20 private Map registry;
21 private final List<Reference<Closeable>> onClose = new ArrayList<Reference<Closeable>>(); 21 private final List<Reference<Closeable>> onClose = new ArrayList<Reference<Closeable>>();
22 22
23 protected LuanState() { 23 protected LuanState() {
69 if( obj instanceof Boolean ) 69 if( obj instanceof Boolean )
70 return (Boolean)obj; 70 return (Boolean)obj;
71 throw new LuanException(this, "attempt to use '"+context()+"' (a " + Luan.type(obj) + " value) as a boolean" ); 71 throw new LuanException(this, "attempt to use '"+context()+"' (a " + Luan.type(obj) + " value) as a boolean" );
72 } 72 }
73 73
74 public Boolean checkBoolean(Object obj,LuanElement el) throws LuanException {
75 push(el,null);
76 try {
77 return checkBoolean(obj);
78 } finally {
79 pop();
80 }
81 }
82
83 public String checkString(Object obj) throws LuanException { 74 public String checkString(Object obj) throws LuanException {
84 if( obj instanceof String ) 75 if( obj instanceof String )
85 return (String)obj; 76 return (String)obj;
86 throw new LuanException(this, "attempt to use '"+context()+"' (a " + Luan.type(obj) + " value) as a string" ); 77 throw new LuanException(this, "attempt to use '"+context()+"' (a " + Luan.type(obj) + " value) as a string" );
87 } 78 }
90 if( obj instanceof LuanFunction ) 81 if( obj instanceof LuanFunction )
91 return (LuanFunction)obj; 82 return (LuanFunction)obj;
92 throw new LuanException(this, "attempt to call '"+context()+"' (a " + Luan.type(obj) + " value)" ); 83 throw new LuanException(this, "attempt to call '"+context()+"' (a " + Luan.type(obj) + " value)" );
93 } 84 }
94 85
95 public boolean isLessThan(Object o1,Object o2) throws LuanException { 86 abstract public boolean isLessThan(Object o1,Object o2) throws LuanException;
96 if( o1 instanceof Number && o2 instanceof Number ) {
97 Number n1 = (Number)o1;
98 Number n2 = (Number)o2;
99 return n1.doubleValue() < n2.doubleValue();
100 }
101 if( o1 instanceof String && o2 instanceof String ) {
102 String s1 = (String)o1;
103 String s2 = (String)o2;
104 return s1.compareTo(s2) < 0;
105 }
106 LuanFunction fn = getBinHandler("__lt",o1,o2);
107 if( fn != null )
108 return checkBoolean( Luan.first(fn.call(this,new Object[]{o1,o2})) );
109 throw new LuanException(this, "attempt to compare " + Luan.type(o1) + " with " + Luan.type(o2) );
110 }
111 87
112 public String toString(Object obj) throws LuanException { 88 public String toString(Object obj) throws LuanException {
113 if( obj instanceof LuanTable ) { 89 if( obj instanceof LuanTable ) {
114 LuanTable tbl = (LuanTable)obj; 90 LuanTable tbl = (LuanTable)obj;
115 return tbl.toString(this); 91 return tbl.toString(this);
119 if( obj instanceof Number ) 95 if( obj instanceof Number )
120 return Luan.toString((Number)obj); 96 return Luan.toString((Number)obj);
121 if( obj instanceof byte[] ) 97 if( obj instanceof byte[] )
122 return "binary: " + Integer.toHexString(obj.hashCode()); 98 return "binary: " + Integer.toHexString(obj.hashCode());
123 return obj.toString(); 99 return obj.toString();
124 }
125
126 public String toString(Object obj,LuanElement el) throws LuanException {
127 push(el,null);
128 try {
129 return toString(obj);
130 } finally {
131 pop();
132 }
133 } 100 }
134 101
135 public Object index(Object obj,Object key) throws LuanException { 102 public Object index(Object obj,Object key) throws LuanException {
136 if( obj instanceof LuanTable ) { 103 if( obj instanceof LuanTable ) {
137 LuanTable tbl = (LuanTable)obj; 104 LuanTable tbl = (LuanTable)obj;
144 111
145 public String context() { 112 public String context() {
146 return stackTrace.get(stackTrace.size()-1).call.text(); 113 return stackTrace.get(stackTrace.size()-1).call.text();
147 } 114 }
148 115
149 public void push(LuanElement el,String fnName) {
150 if( el == null ) throw new RuntimeException();
151 stackTrace.add( new StackTraceElement(el,fnName) );
152 }
153
154 public void pop() {
155 stackTrace.remove(stackTrace.size()-1);
156 }
157
158 public LuanFunction getBinHandler(String op,Object o1,Object o2) throws LuanException {
159 if( o1 instanceof LuanTable ) {
160 LuanFunction f1 = getHandlerFunction(op,(LuanTable)o1);
161 if( f1 != null )
162 return f1;
163 }
164 return o2 instanceof LuanTable ? getHandlerFunction(op,(LuanTable)o2) : null;
165 }
166
167 public LuanFunction getHandlerFunction(String op,LuanTable t) throws LuanException {
168 Object f = t.getHandler(op);
169 if( f == null )
170 return null;
171 return checkFunction(f);
172 }
173
174 public void dumpStack() { 116 public void dumpStack() {
175 System.err.println( stackTrace ); 117 System.err.println( stackTrace );
176 } 118 }
177 /* 119 /*
178 public Number checkNumber(Object obj) throws LuanException { 120 public Number checkNumber(Object obj) throws LuanException {