comparison core/src/luan/modules/BasicLuan.java @ 578:60c549d43988

remove LuanState.exception()
author Franklin Schmidt <fschmidt@gmail.com>
date Tue, 14 Jul 2015 17:40:48 -0600
parents 7c3ad6db8ac3
children 0742ac78fa69
comparison
equal deleted inserted replaced
577:d7a85fbe15f1 578:60c549d43988
37 public static LuanFunction load_file(LuanState luan,String fileName,Boolean addExtension) throws LuanException { 37 public static LuanFunction load_file(LuanState luan,String fileName,Boolean addExtension) throws LuanException {
38 if( fileName == null ) 38 if( fileName == null )
39 fileName = "stdin:"; 39 fileName = "stdin:";
40 String src = PackageLuan.read(luan,fileName,addExtension); 40 String src = PackageLuan.read(luan,fileName,addExtension);
41 if( src == null ) 41 if( src == null )
42 throw luan.exception( "file '"+fileName+"' not found" ); 42 throw new LuanException(luan, "file '"+fileName+"' not found" );
43 return load(luan,src,fileName,null,false); 43 return load(luan,src,fileName,null,false);
44 } 44 }
45 45
46 public static LuanFunction pairs(final LuanState luan,final LuanTable t) throws LuanException { 46 public static LuanFunction pairs(final LuanState luan,final LuanTable t) throws LuanException {
47 Utils.checkNotNull(luan,t); 47 Utils.checkNotNull(luan,t);
74 } 74 }
75 75
76 public static void set_metatable(LuanState luan,LuanTable table,LuanTable metatable) throws LuanException { 76 public static void set_metatable(LuanState luan,LuanTable table,LuanTable metatable) throws LuanException {
77 Utils.checkNotNull(luan,table); 77 Utils.checkNotNull(luan,table);
78 if( table.getHandler("__metatable") != null ) 78 if( table.getHandler("__metatable") != null )
79 throw luan.exception("cannot change a protected metatable"); 79 throw new LuanException(luan,"cannot change a protected metatable");
80 table.setMetatable(metatable); 80 table.setMetatable(metatable);
81 } 81 }
82 82
83 public static boolean raw_equal(Object v1,Object v2) { 83 public static boolean raw_equal(Object v1,Object v2) {
84 return v1 == v2 || v1 != null && v1.equals(v2); 84 return v1 == v2 || v1 != null && v1.equals(v2);
99 } 99 }
100 if( v instanceof LuanTable ) { 100 if( v instanceof LuanTable ) {
101 LuanTable t = (LuanTable)v; 101 LuanTable t = (LuanTable)v;
102 return t.rawLength(); 102 return t.rawLength();
103 } 103 }
104 throw luan.exception( "bad argument #1 to 'raw_len' (table or string expected)" ); 104 throw new LuanException(luan, "bad argument #1 to 'raw_len' (table or string expected)" );
105 } 105 }
106 106
107 public static String to_string(LuanState luan,Object v) throws LuanException { 107 public static String to_string(LuanState luan,Object v) throws LuanException {
108 return luan.toString(v); 108 return luan.toString(v);
109 } 109 }
110 110
111 public static LuanTable new_error(LuanState luan,Object msg) throws LuanException { 111 public static LuanTable new_error(LuanState luan,Object msg) throws LuanException {
112 return luan.exception(msg).table(); 112 return new LuanException(luan,msg).table();
113 } 113 }
114 114
115 public static String assert_string(LuanState luan,String v) throws LuanException { 115 public static String assert_string(LuanState luan,String v) throws LuanException {
116 Utils.checkNotNull(luan,v); 116 Utils.checkNotNull(luan,v);
117 return v; 117 return v;
149 } 149 }
150 150
151 public static LuanFunction range(LuanState luan,final double from,final double to,Double stepV) throws LuanException { 151 public static LuanFunction range(LuanState luan,final double from,final double to,Double stepV) throws LuanException {
152 final double step = stepV==null ? 1.0 : stepV; 152 final double step = stepV==null ? 1.0 : stepV;
153 if( step == 0.0 ) 153 if( step == 0.0 )
154 throw luan.exception("bad argument #3 (step may not be zero)"); 154 throw new LuanException(luan,"bad argument #3 (step may not be zero)");
155 return new LuanFunction() { 155 return new LuanFunction() {
156 double v = from; 156 double v = from;
157 157
158 @Override public Object call(LuanState luan,Object[] args) { 158 @Override public Object call(LuanState luan,Object[] args) {
159 if( step > 0.0 && v > to || step < 0.0 && v < to ) 159 if( step > 0.0 && v > to || step < 0.0 && v < to )
183 183
184 public static Object try_(LuanState luan,LuanTable blocks) throws LuanException { 184 public static Object try_(LuanState luan,LuanTable blocks) throws LuanException {
185 Utils.checkNotNull(luan,blocks); 185 Utils.checkNotNull(luan,blocks);
186 Object obj = blocks.get(luan,1); 186 Object obj = blocks.get(luan,1);
187 if( obj == null ) 187 if( obj == null )
188 throw luan.exception("missing 'try' value"); 188 throw new LuanException(luan,"missing 'try' value");
189 if( !(obj instanceof LuanFunction) ) 189 if( !(obj instanceof LuanFunction) )
190 throw luan.exception("bad 'try' value (function expected, got "+Luan.type(obj)+")"); 190 throw new LuanException(luan,"bad 'try' value (function expected, got "+Luan.type(obj)+")");
191 LuanFunction tryFn = (LuanFunction)obj; 191 LuanFunction tryFn = (LuanFunction)obj;
192 LuanFunction catchFn = null; 192 LuanFunction catchFn = null;
193 obj = blocks.get(luan,"catch"); 193 obj = blocks.get(luan,"catch");
194 if( obj != null ) { 194 if( obj != null ) {
195 if( !(obj instanceof LuanFunction) ) 195 if( !(obj instanceof LuanFunction) )
196 throw luan.exception("bad 'catch' value (function expected, got "+Luan.type(obj)+")"); 196 throw new LuanException(luan,"bad 'catch' value (function expected, got "+Luan.type(obj)+")");
197 catchFn = (LuanFunction)obj; 197 catchFn = (LuanFunction)obj;
198 } 198 }
199 LuanFunction finallyFn = null; 199 LuanFunction finallyFn = null;
200 obj = blocks.get(luan,"finally"); 200 obj = blocks.get(luan,"finally");
201 if( obj != null ) { 201 if( obj != null ) {
202 if( !(obj instanceof LuanFunction) ) 202 if( !(obj instanceof LuanFunction) )
203 throw luan.exception("bad 'finally' value (function expected, got "+Luan.type(obj)+")"); 203 throw new LuanException(luan,"bad 'finally' value (function expected, got "+Luan.type(obj)+")");
204 finallyFn = (LuanFunction)obj; 204 finallyFn = (LuanFunction)obj;
205 } 205 }
206 try { 206 try {
207 return tryFn.call(luan); 207 return tryFn.call(luan);
208 } catch(LuanException e) { 208 } catch(LuanException e) {