comparison src/luan/lib/PackageLib.java @ 161:d310ebf4d6e7

add javaSearcher git-svn-id: https://luan-java.googlecode.com/svn/trunk@162 21e917c8-12df-6dd8-5cb6-c86387c605b9
author fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
date Thu, 19 Jun 2014 10:44:20 +0000
parents 2e92f0a6fcac
children 01e9707a2fb0
comparison
equal deleted inserted replaced
160:138b9baee80b 161:d310ebf4d6e7
13 import luan.LuanException; 13 import luan.LuanException;
14 14
15 15
16 public final class PackageLib { 16 public final class PackageLib {
17 17
18 private static final String jpath = "luan.lib.?Lib.LOADER";
19
18 public static final LuanFunction LOADER = new LuanFunction() { 20 public static final LuanFunction LOADER = new LuanFunction() {
19 @Override public Object call(LuanState luan,Object[] args) { 21 @Override public Object call(LuanState luan,Object[] args) {
20 LuanTable module = new LuanTable(); 22 LuanTable module = new LuanTable();
21 LuanTable global = luan.global(); 23 LuanTable global = luan.global();
22 module.put("loaded",luan.loaded()); 24 module.put("loaded",luan.loaded());
23 module.put("preload",luan.preload()); 25 module.put("preload",luan.preload());
24 module.put("path","?.luan;java:luan/modules/?.luan"); 26 module.put("path","?.luan;java:luan/modules/?.luan");
27 module.put("jpath",jpath);
25 try { 28 try {
26 add( global, "require", LuanState.class, String.class ); 29 add( global, "require", LuanState.class, String.class );
27 add( module, "get_loader", String.class ); 30 add( module, "load_lib", String.class );
28 add( module, "search_path", String.class, String.class ); 31 add( module, "search_path", String.class, String.class );
32 add( module, "search", LuanState.class, String.class );
29 } catch(NoSuchMethodException e) { 33 } catch(NoSuchMethodException e) {
30 throw new RuntimeException(e); 34 throw new RuntimeException(e);
31 } 35 }
32 LuanTable searchers = luan.searchers(); 36 LuanTable searchers = luan.searchers();
33 searchers.add(preloadSearcher); 37 searchers.add(preloadSearcher);
34 searchers.add(fileSearcher); 38 searchers.add(fileSearcher);
39 searchers.add(javaSearcher);
35 module.put("searchers",searchers); 40 module.put("searchers",searchers);
36 return module; 41 return module;
37 } 42 }
38 }; 43 };
39 44
43 48
44 public static Object require(LuanState luan,String modName) throws LuanException { 49 public static Object require(LuanState luan,String modName) throws LuanException {
45 LuanTable loaded = luan.loaded(); 50 LuanTable loaded = luan.loaded();
46 Object mod = loaded.get(modName); 51 Object mod = loaded.get(modName);
47 if( mod == null ) { 52 if( mod == null ) {
48 List<Object> list = null; 53 Object[] a = search(luan,modName);
49 String searchFor = modName; 54 if( a == null )
50 LuanTable searchers = (LuanTable)luan.get("Package.searchers"); 55 throw luan.exception( "module '"+modName+"' not found" );
51 if( searchers == null ) { 56 LuanFunction loader = (LuanFunction)a[0];
52 list = Collections.<Object>singletonList(preloadSearcher); 57 a[0] = modName;
58 mod = Luan.first(luan.call(loader,"<require \""+modName+"\">",a));
59 if( mod != null ) {
60 loaded.put(modName,mod);
53 } else { 61 } else {
54 list = searchers.asList(); 62 mod = loaded.get(modName);
63 if( mod==null )
64 loaded.put(modName,true);
55 } 65 }
56 for( Object s : list ) {
57 LuanFunction searcher = (LuanFunction)s;
58 Object[] a = Luan.array(luan.call(searcher,"<searcher>",new Object[]{searchFor}));
59 if( a.length >= 1 && a[0] instanceof LuanFunction ) {
60 LuanFunction loader = (LuanFunction)a[0];
61 a[0] = modName;
62 mod = Luan.first(luan.call(loader,"<require \""+modName+"\">",a));
63 if( mod != null ) {
64 loaded.put(modName,mod);
65 } else {
66 mod = loaded.get(modName);
67 if( mod==null )
68 loaded.put(modName,true);
69 }
70 break;
71 }
72 }
73 if( mod == null )
74 throw luan.exception( "module '"+modName+"' not found" );
75 } 66 }
76 return mod; 67 return mod;
77 } 68 }
69
70 public static Object[] search(LuanState luan,String modName) throws LuanException {
71 List<Object> list = null;
72 LuanTable searchers = (LuanTable)luan.get("Package.searchers");
73 if( searchers == null ) {
74 list = Collections.<Object>singletonList(javaSearcher);
75 } else {
76 list = searchers.asList();
77 }
78 for( Object s : list ) {
79 LuanFunction searcher = (LuanFunction)s;
80 Object[] a = Luan.array(luan.call(searcher,"<searcher>",new Object[]{modName}));
81 if( a.length >= 1 && a[0] instanceof LuanFunction )
82 return a;
83 }
84 return null;
85 }
86
87 public static final LuanFunction preloadSearcher = new LuanFunction() {
88 @Override public Object call(LuanState luan,Object[] args) {
89 String modName = (String)args[0];
90 return luan.preload().get(modName);
91 }
92 };
78 93
79 public static String search_path(String name,String path) { 94 public static String search_path(String name,String path) {
80 for( String s : path.split(";") ) { 95 for( String s : path.split(";") ) {
81 String file = s.replaceAll("\\?",name); 96 String file = s.replaceAll("\\?",name);
82 if( Utils.exists(file) ) 97 if( Utils.exists(file) )
102 String file = search_path(modName,path); 117 String file = search_path(modName,path);
103 return file==null ? LuanFunction.NOTHING : new Object[]{fileLoader,file}; 118 return file==null ? LuanFunction.NOTHING : new Object[]{fileLoader,file};
104 } 119 }
105 }; 120 };
106 121
107 public static final LuanFunction preloadSearcher = new LuanFunction() { 122
108 @Override public Object call(LuanState luan,Object[] args) { 123 public static final LuanFunction javaLoader = new LuanFunction() {
124 @Override public Object call(LuanState luan,Object[] args) throws LuanException {
125 try {
126 String objName = (String)args[1];
127 LuanFunction fn = load_lib(objName);
128 return fn.call(luan,args);
129 } catch(ClassNotFoundException e) {
130 throw new RuntimeException(e);
131 } catch(NoSuchFieldException e) {
132 throw new RuntimeException(e);
133 } catch(IllegalAccessException e) {
134 throw new RuntimeException(e);
135 }
136 }
137 };
138
139 public static final LuanFunction javaSearcher = new LuanFunction() {
140 @Override public Object[] call(LuanState luan,Object[] args) {
109 String modName = (String)args[0]; 141 String modName = (String)args[0];
110 return luan.preload().get(modName); 142 String path = (String)luan.get("Package.jpath");
143 if( path==null )
144 path = jpath;
145 for( String s : path.split(";") ) {
146 String objName = s.replaceAll("\\?",modName);
147 try {
148 load_lib(objName); // throws exception if not found
149 return new Object[]{javaLoader,objName};
150 } catch(ClassNotFoundException e) {
151 } catch(NoSuchFieldException e) {
152 } catch(IllegalAccessException e) {
153 }
154 }
155 return LuanFunction.NOTHING;
111 } 156 }
112 }; 157 };
113 158
114 159
115 160 public static LuanFunction load_lib(String path)
116 public static LuanFunction get_loader(String path)
117 throws ClassNotFoundException, NoSuchFieldException, IllegalAccessException 161 throws ClassNotFoundException, NoSuchFieldException, IllegalAccessException
118 { 162 {
119 int i = path.lastIndexOf('.'); 163 int i = path.lastIndexOf('.');
120 String clsPath = path.substring(0,i); 164 String clsPath = path.substring(0,i);
121 String fld = path.substring(i+1); 165 String fld = path.substring(i+1);