comparison core/src/luan/modules/PackageLuan.java @ 264:9e0d4452e649

implement URL style module names git-svn-id: https://luan-java.googlecode.com/svn/trunk@265 21e917c8-12df-6dd8-5cb6-c86387c605b9
author fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
date Wed, 29 Oct 2014 03:50:59 +0000
parents f1f7d8c7e94e
children 454a486d9c19
comparison
equal deleted inserted replaced
263:54873a389f80 264:9e0d4452e649
13 import luan.LuanException; 13 import luan.LuanException;
14 14
15 15
16 public final class PackageLuan { 16 public final class PackageLuan {
17 17
18 private static final String jpath = "luan.modules.?Luan.LOADER";
19
20 public static final LuanFunction LOADER = new LuanFunction() { 18 public static final LuanFunction LOADER = new LuanFunction() {
21 @Override public Object call(LuanState luan,Object[] args) { 19 @Override public Object call(LuanState luan,Object[] args) {
22 LuanTable module = Luan.newTable(); 20 LuanTable module = Luan.newTable();
23 module.put( "loaded", loaded(luan) ); 21 module.put( "loaded", loaded(luan) );
24 module.put( "path", "?.luan;classpath:luan/modules/?.luan" );
25 module.put( "jpath", jpath );
26 try { 22 try {
27 module.put("require",requireFn); 23 module.put("require",requireFn);
28 add( module, "load", LuanState.class, String.class ); 24 add( module, "load", LuanState.class, String.class );
29 add( module, "load_lib", LuanState.class, String.class ); 25 add( module, "load_lib", LuanState.class, String.class );
30 add( module, "search_path", LuanState.class, String.class, String.class );
31 add( module, "search", LuanState.class, String.class ); 26 add( module, "search", LuanState.class, String.class );
32 } catch(NoSuchMethodException e) { 27 } catch(NoSuchMethodException e) {
33 throw new RuntimeException(e); 28 throw new RuntimeException(e);
34 } 29 }
35 module.put( "searchers", searchers(luan) );
36 return module; 30 return module;
37 } 31 }
38 }; 32 };
39 33
40 public static final LuanFunction requireFn; 34 public static final LuanFunction requireFn;
59 } 53 }
60 54
61 private static Object pkg(LuanState luan,String key) { 55 private static Object pkg(LuanState luan,String key) {
62 LuanTable t = (LuanTable)loaded(luan).get("Package"); 56 LuanTable t = (LuanTable)loaded(luan).get("Package");
63 return t==null ? null : t.get(key); 57 return t==null ? null : t.get(key);
64 }
65
66 private static LuanTable searchers(LuanState luan) {
67 String key = "Package.searchers";
68 LuanTable tbl = (LuanTable)luan.registry().get(key);
69 if( tbl == null ) {
70 tbl = Luan.newTable();
71 tbl.add(fileSearcher);
72 tbl.add(javaSearcher);
73 tbl.add(JavaLuan.javaSearcher);
74 luan.registry().put(key,tbl);
75 }
76 return tbl;
77 } 58 }
78 59
79 public static Object require(LuanState luan,String modName) throws LuanException { 60 public static Object require(LuanState luan,String modName) throws LuanException {
80 Object mod = load(luan,modName); 61 Object mod = load(luan,modName);
81 if( mod==null ) 62 if( mod==null )
105 } 86 }
106 return mod; 87 return mod;
107 } 88 }
108 89
109 public static Object[] search(LuanState luan,String modName) throws LuanException { 90 public static Object[] search(LuanState luan,String modName) throws LuanException {
110 for( Object s : searchers(luan).asList() ) { 91 LuanTable t = IoLuan.get(luan,modName,true);
111 LuanFunction searcher = (LuanFunction)s; 92 if( t == null )
112 Object[] a = Luan.array(luan.call(searcher,"<searcher>",new Object[]{modName})); 93 return null;
113 if( a.length >= 1 && a[0] instanceof LuanFunction ) 94 LuanFunction loader = (LuanFunction)t.get("loader");
114 return a; 95 LuanFunction fn = (LuanFunction)Luan.first(luan.call(loader,new Object[]{modName}));
115 } 96 return new Object[]{fn,modName};
116 return null;
117 } 97 }
118
119 public static String search_path(LuanState luan,String name,String path) throws LuanException {
120 for( String s : path.split(";") ) {
121 String file = s.replaceAll("\\?",name);
122 if( file.indexOf(':') > 0 && IoLuan.get(luan,file) != null )
123 return file;
124 }
125 return null;
126 }
127
128 public static final LuanFunction fileLoader = new LuanFunction() {
129 @Override public Object call(LuanState luan,Object[] args) throws LuanException {
130 String fileName = (String)args[1];
131 LuanFunction fn = BasicLuan.load_file(luan,fileName);
132 return fn.call(luan,args);
133 }
134 };
135
136 public static final LuanFunction fileSearcher = new LuanFunction() {
137 @Override public Object[] call(LuanState luan,Object[] args) throws LuanException {
138 String modName = (String)args[0];
139 String path = (String)pkg(luan,"path");
140 if( path==null )
141 return LuanFunction.NOTHING;
142 String file = search_path(luan,modName,path);
143 return file==null ? LuanFunction.NOTHING : new Object[]{fileLoader,file};
144 }
145 };
146
147
148 public static final LuanFunction javaLoader = new LuanFunction() {
149 @Override public Object call(LuanState luan,Object[] args) throws LuanException {
150 try {
151 String objName = (String)args[1];
152 LuanFunction fn = load_lib(luan,objName);
153 return fn.call(luan,args);
154 } catch(ClassNotFoundException e) {
155 throw new RuntimeException(e);
156 } catch(NoSuchFieldException e) {
157 throw new RuntimeException(e);
158 } catch(IllegalAccessException e) {
159 throw new RuntimeException(e);
160 }
161 }
162 };
163
164 public static final LuanFunction javaSearcher = new LuanFunction() {
165 @Override public Object[] call(LuanState luan,Object[] args)
166 throws LuanException
167 {
168 String modName = (String)args[0];
169 modName = modName.replace('/','.');
170 String path = (String)pkg(luan,"jpath");
171 if( path==null )
172 path = jpath;
173 for( String s : path.split(";") ) {
174 String objName = s.replaceAll("\\?",modName);
175 try {
176 load_lib(luan,objName); // throws exception if not found
177 return new Object[]{javaLoader,objName};
178 } catch(ClassNotFoundException e) {
179 } catch(NoSuchFieldException e) {
180 } catch(IllegalAccessException e) {
181 }
182 }
183 return LuanFunction.NOTHING;
184 }
185 };
186 98
187 99
188 public static void block(LuanState luan,String key) { 100 public static void block(LuanState luan,String key) {
189 blocked(luan).put(key,true); 101 blocked(luan).put(key,true);
190 } 102 }