comparison src/luan/modules/PackageLuan.java @ 168:ebe9db183eb7

rename *Lib.java to *Luan.java git-svn-id: https://luan-java.googlecode.com/svn/trunk@169 21e917c8-12df-6dd8-5cb6-c86387c605b9
author fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
date Sun, 22 Jun 2014 04:42:07 +0000
parents src/luan/modules/PackageLib.java@4c0131c2b650
children
comparison
equal deleted inserted replaced
167:4c0131c2b650 168:ebe9db183eb7
1 package luan.modules;
2
3 import java.io.IOException;
4 import java.util.Arrays;
5 import java.util.Collections;
6 import java.util.List;
7 import luan.Luan;
8 import luan.LuanState;
9 import luan.LuanTable;
10 import luan.LuanFunction;
11 import luan.LuanJavaFunction;
12 import luan.LuanElement;
13 import luan.LuanException;
14
15
16 public final class PackageLuan {
17
18 private static final String jpath = "luan.modules.?Luan.LOADER";
19
20 public static final LuanFunction LOADER = new LuanFunction() {
21 @Override public Object call(LuanState luan,Object[] args) {
22 LuanTable module = new LuanTable();
23 module.put("loaded",luan.loaded());
24 module.put("preload",luan.preload());
25 module.put("path","?.luan;java:luan/modules/?.luan");
26 module.put("jpath",jpath);
27 try {
28 add( module, "require", LuanState.class, String.class );
29 add( module, "load_lib", String.class );
30 add( module, "search_path", String.class, String.class );
31 add( module, "search", LuanState.class, String.class );
32 } catch(NoSuchMethodException e) {
33 throw new RuntimeException(e);
34 }
35 LuanTable searchers = luan.searchers();
36 searchers.add(preloadSearcher);
37 searchers.add(fileSearcher);
38 searchers.add(javaSearcher);
39 module.put("searchers",searchers);
40 return module;
41 }
42 };
43
44 private static void add(LuanTable t,String method,Class<?>... parameterTypes) throws NoSuchMethodException {
45 t.put( method, new LuanJavaFunction(PackageLuan.class.getMethod(method,parameterTypes),null) );
46 }
47
48 public static Object require(LuanState luan,String modName) throws LuanException {
49 LuanTable loaded = luan.loaded();
50 Object mod = loaded.get(modName);
51 if( mod == null ) {
52 Object[] a = search(luan,modName);
53 if( a == null )
54 throw luan.exception( "module '"+modName+"' not found" );
55 LuanFunction loader = (LuanFunction)a[0];
56 a[0] = modName;
57 mod = Luan.first(luan.call(loader,"<require \""+modName+"\">",a));
58 if( mod != null ) {
59 loaded.put(modName,mod);
60 } else {
61 mod = loaded.get(modName);
62 if( mod==null )
63 loaded.put(modName,true);
64 }
65 }
66 return mod;
67 }
68
69 public static Object[] search(LuanState luan,String modName) throws LuanException {
70 List<Object> list = null;
71 LuanTable searchers = (LuanTable)luan.get("Package.searchers");
72 if( searchers == null ) {
73 list = Collections.<Object>singletonList(javaSearcher);
74 } else {
75 list = searchers.asList();
76 }
77 for( Object s : list ) {
78 LuanFunction searcher = (LuanFunction)s;
79 Object[] a = Luan.array(luan.call(searcher,"<searcher>",new Object[]{modName}));
80 if( a.length >= 1 && a[0] instanceof LuanFunction )
81 return a;
82 }
83 return null;
84 }
85
86 public static final LuanFunction preloadSearcher = new LuanFunction() {
87 @Override public Object call(LuanState luan,Object[] args) {
88 String modName = (String)args[0];
89 return luan.preload().get(modName);
90 }
91 };
92
93 public static String search_path(String name,String path) {
94 name = name.replace('.','/');
95 for( String s : path.split(";") ) {
96 String file = s.replaceAll("\\?",name);
97 if( Utils.exists(file) )
98 return file;
99 }
100 return null;
101 }
102
103 public static final LuanFunction fileLoader = new LuanFunction() {
104 @Override public Object call(LuanState luan,Object[] args) throws LuanException {
105 String fileName = (String)args[1];
106 LuanFunction fn = BasicLuan.load_file(luan,fileName);
107 return fn.call(luan,args);
108 }
109 };
110
111 public static final LuanFunction fileSearcher = new LuanFunction() {
112 @Override public Object[] call(LuanState luan,Object[] args) {
113 String modName = (String)args[0];
114 String path = (String)luan.get("Package.path");
115 if( path==null )
116 return LuanFunction.NOTHING;
117 String file = search_path(modName,path);
118 return file==null ? LuanFunction.NOTHING : new Object[]{fileLoader,file};
119 }
120 };
121
122
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) {
141 String modName = (String)args[0];
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;
156 }
157 };
158
159
160 public static LuanFunction load_lib(String path)
161 throws ClassNotFoundException, NoSuchFieldException, IllegalAccessException
162 {
163 int i = path.lastIndexOf('.');
164 String clsPath = path.substring(0,i);
165 String fld = path.substring(i+1);
166 Class cls = Class.forName(clsPath);
167 return (LuanFunction)cls.getField(fld).get(null);
168 }
169
170 }