Mercurial Hosting > luan
comparison src/luan/lib/BasicLib.java @ 143:fcb81fa2df0d
handle urls and java resources as files
git-svn-id: https://luan-java.googlecode.com/svn/trunk@144 21e917c8-12df-6dd8-5cb6-c86387c605b9
author | fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9> |
---|---|
date | Fri, 13 Jun 2014 19:04:05 +0000 |
parents | 05f8c21160ef |
children | f99fd64291b3 |
comparison
equal
deleted
inserted
replaced
142:05f8c21160ef | 143:fcb81fa2df0d |
---|---|
48 add( global, "set_metatable", LuanTable.class, LuanTable.class ); | 48 add( global, "set_metatable", LuanTable.class, LuanTable.class ); |
49 add( global, "to_number", Object.class, Integer.class ); | 49 add( global, "to_number", Object.class, Integer.class ); |
50 add( global, "to_string", LuanState.class, Object.class ); | 50 add( global, "to_string", LuanState.class, Object.class ); |
51 add( global, "type", Object.class ); | 51 add( global, "type", Object.class ); |
52 global.put( "_VERSION", Luan.version ); | 52 global.put( "_VERSION", Luan.version ); |
53 add( module, "do_java_resource", LuanState.class, String.class ); | |
54 add( module, "load_java_resource", LuanState.class, String.class ); | |
55 // add( module, "new_luan" ); | |
56 add( module, "values", new Object[0].getClass() ); | 53 add( module, "values", new Object[0].getClass() ); |
57 } catch(NoSuchMethodException e) { | 54 } catch(NoSuchMethodException e) { |
58 throw new RuntimeException(e); | 55 throw new RuntimeException(e); |
59 } | 56 } |
60 return module; | 57 return module; |
80 return LuanCompiler.compileModule(luan,new LuanSource(sourceName,text),allowExpr); | 77 return LuanCompiler.compileModule(luan,new LuanSource(sourceName,text),allowExpr); |
81 } | 78 } |
82 | 79 |
83 public static LuanFunction load_file(LuanState luan,String fileName) throws LuanException { | 80 public static LuanFunction load_file(LuanState luan,String fileName) throws LuanException { |
84 try { | 81 try { |
85 String src = fileName==null ? Utils.readAll(new InputStreamReader(System.in)) : new IoLib.LuanFile(fileName).read_text(); | 82 String src = fileName==null ? Utils.readAll(new InputStreamReader(System.in)) : IoLib.luanIo(luan,fileName).read_text(); |
86 return load(luan,src,fileName,false,false); | 83 return load(luan,src,fileName,false,false); |
87 } catch(IOException e) { | 84 } catch(IOException e) { |
88 throw luan.exception(e); | 85 throw luan.exception(e); |
89 } | 86 } |
90 } | 87 } |
91 | 88 |
92 public static LuanFunction load_java_resource(LuanState luan,String path) throws LuanException { | |
93 try { | |
94 String src = new IoLib.LuanUrl(IoLib.java_resource_to_url(path)).read_text(); | |
95 return load(luan,src,path,false,false); | |
96 } catch(IOException e) { | |
97 throw luan.exception(e); | |
98 } | |
99 } | |
100 | |
101 public static Object do_file(LuanState luan,String fileName) throws LuanException { | 89 public static Object do_file(LuanState luan,String fileName) throws LuanException { |
102 LuanFunction fn = load_file(luan,fileName); | 90 LuanFunction fn = load_file(luan,fileName); |
103 return luan.call(fn); | |
104 } | |
105 | |
106 public static Object do_java_resource(LuanState luan,String path) throws LuanException { | |
107 LuanFunction fn = load_java_resource(luan,path); | |
108 return luan.call(fn); | 91 return luan.call(fn); |
109 } | 92 } |
110 | 93 |
111 private static LuanFunction pairs(final Iterator<Map.Entry<Object,Object>> iter) { | 94 private static LuanFunction pairs(final Iterator<Map.Entry<Object,Object>> iter) { |
112 return new LuanFunction() { | 95 return new LuanFunction() { |
238 return LuanFunction.NOTHING; | 221 return LuanFunction.NOTHING; |
239 return new Object[]{i,args[i-1]}; | 222 return new Object[]{i,args[i-1]}; |
240 } | 223 } |
241 }; | 224 }; |
242 } | 225 } |
243 /* | 226 |
244 public static LuanTable new_luan() { | |
245 return LuanState.newStandard().global(); | |
246 } | |
247 */ | |
248 } | 227 } |