Mercurial Hosting > luan
comparison src/luan/lib/PackageLib.java @ 75:aa7538ae5fb6
can now load modules from classpath
git-svn-id: https://luan-java.googlecode.com/svn/trunk@76 21e917c8-12df-6dd8-5cb6-c86387c605b9
author | fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9> |
---|---|
date | Wed, 13 Feb 2013 09:19:15 +0000 |
parents | f003338d503b |
children | 4bf3d0c0b6b9 |
comparison
equal
deleted
inserted
replaced
74:f003338d503b | 75:aa7538ae5fb6 |
---|---|
1 package luan.lib; | 1 package luan.lib; |
2 | 2 |
3 import java.io.File; | 3 import java.io.File; |
4 import java.util.List; | 4 import java.io.IOException; |
5 import java.util.ArrayList; | 5 import java.net.URL; |
6 import java.util.Arrays; | |
6 import luan.Luan; | 7 import luan.Luan; |
7 import luan.LuanState; | 8 import luan.LuanState; |
8 import luan.LuanTable; | 9 import luan.LuanTable; |
9 import luan.LuanFunction; | 10 import luan.LuanFunction; |
10 import luan.LuanJavaFunction; | 11 import luan.LuanJavaFunction; |
18 | 19 |
19 public static final LuanFunction LOADER = new LuanFunction() { | 20 public static final LuanFunction LOADER = new LuanFunction() { |
20 public Object[] call(LuanState luan,Object[] args) throws LuanException { | 21 public Object[] call(LuanState luan,Object[] args) throws LuanException { |
21 LuanTable global = luan.global; | 22 LuanTable global = luan.global; |
22 LuanTable module = new LuanTable(); | 23 LuanTable module = new LuanTable(); |
23 List<Object> searchers = new ArrayList<Object>(); | |
24 module.put("loaded",luan.loaded); | 24 module.put("loaded",luan.loaded); |
25 module.put("preload",luan.preload); | 25 module.put("preload",luan.preload); |
26 module.put("path","?.lua"); | 26 // module.put("path","?.lua"); |
27 try { | 27 try { |
28 add( global, "require", LuanState.class, String.class ); | 28 add( global, "require", LuanState.class, String.class ); |
29 add( module, "module", LuanState.class, String.class ); | 29 add( module, "module", LuanState.class, String.class ); |
30 add( module, "search_path", String.class, String.class ); | 30 add( module, "search_path", String.class, String.class ); |
31 searchers.add( new LuanJavaFunction(PackageLib.class.getMethod("preloadSearcher",LuanState.class,String.class),null) ); | |
32 searchers.add( new LuanJavaFunction(PackageLib.class.getMethod("fileSearcher",LuanState.class,String.class),null) ); | |
33 } catch(NoSuchMethodException e) { | 31 } catch(NoSuchMethodException e) { |
34 throw new RuntimeException(e); | 32 throw new RuntimeException(e); |
35 } | 33 } |
36 module.put("searchers",new LuanTable(searchers)); | 34 module.put("searchers",new LuanTable(Arrays.<Object>asList(preloadSearcher,fileSearcher,javaFileSearcher))); |
37 return new Object[]{module}; | 35 return new Object[]{module}; |
38 } | 36 } |
39 }; | 37 }; |
40 | 38 |
41 private static void add(LuanTable t,String method,Class<?>... parameterTypes) throws NoSuchMethodException { | 39 private static void add(LuanTable t,String method,Class<?>... parameterTypes) throws NoSuchMethodException { |
47 if( mod instanceof LuanTable ) | 45 if( mod instanceof LuanTable ) |
48 luan.global.put(modName,mod); | 46 luan.global.put(modName,mod); |
49 } | 47 } |
50 | 48 |
51 public static Object module(LuanState luan,String modName) throws LuanException { | 49 public static Object module(LuanState luan,String modName) throws LuanException { |
52 LuanTable module = (LuanTable)luan.global.get(NAME); | |
53 Object mod = luan.loaded.get(modName); | 50 Object mod = luan.loaded.get(modName); |
54 if( mod == null ) { | 51 if( mod == null ) { |
55 LuanTable searchers = (LuanTable)module.get("searchers"); | 52 LuanTable searchers = (LuanTable)luan.get("package.searchers"); |
56 for( Object s : searchers.asList() ) { | 53 for( Object s : searchers.asList() ) { |
57 LuanFunction searcher = (LuanFunction)s; | 54 LuanFunction searcher = (LuanFunction)s; |
58 Object[] a = luan.call(searcher,LuanElement.JAVA,"searcher",modName); | 55 Object[] a = luan.call(searcher,LuanElement.JAVA,"searcher",modName); |
59 if( a.length >= 1 && a[0] instanceof LuanFunction ) { | 56 if( a.length >= 1 && a[0] instanceof LuanFunction ) { |
60 LuanFunction loader = (LuanFunction)a[0]; | 57 LuanFunction loader = (LuanFunction)a[0]; |
87 LuanFunction fn = BasicLib.load_file(luan,fileName); | 84 LuanFunction fn = BasicLib.load_file(luan,fileName); |
88 return luan.call(fn,LuanElement.JAVA,modName,args); | 85 return luan.call(fn,LuanElement.JAVA,modName,args); |
89 } | 86 } |
90 }; | 87 }; |
91 | 88 |
92 public static Object[] fileSearcher(LuanState luan,String modName) { | 89 public static final LuanFunction fileSearcher = new LuanFunction() { |
93 LuanTable module = (LuanTable)luan.global.get(NAME); | 90 public Object[] call(LuanState luan,Object[] args) throws LuanException { |
94 String path = (String)module.get("path"); | 91 String modName = (String)args[0]; |
95 String file = search_path(modName,path); | 92 String path = (String)luan.get("package.path"); |
96 return file==null ? LuanFunction.EMPTY_RTN : new Object[]{fileLoader,file}; | 93 if( path==null ) |
97 } | 94 return LuanFunction.EMPTY_RTN; |
95 String file = search_path(modName,path); | |
96 return file==null ? LuanFunction.EMPTY_RTN : new Object[]{fileLoader,file}; | |
97 } | |
98 }; | |
98 | 99 |
99 public static Object preloadSearcher(LuanState luan,String modName) { | 100 public static final LuanFunction preloadSearcher = new LuanFunction() { |
100 return luan.preload.get(modName); | 101 public Object[] call(LuanState luan,Object[] args) throws LuanException { |
101 } | 102 String modName = (String)args[0]; |
102 | 103 return new Object[]{luan.preload.get(modName)}; |
104 } | |
105 }; | |
106 | |
107 | |
108 | |
109 | |
110 private static final LuanFunction javaFileLoader = new LuanFunction() { | |
111 public Object[] call(LuanState luan,Object[] args) throws LuanException { | |
112 try { | |
113 String modName = (String)args[0]; | |
114 URL url = (URL)args[1]; | |
115 String src = Utils.read(url); | |
116 LuanFunction fn = BasicLib.load(luan,src,url.toString()); | |
117 return luan.call(fn,LuanElement.JAVA,modName,args); | |
118 } catch(IOException e) { | |
119 throw new LuanException(luan,LuanElement.JAVA,e); | |
120 } | |
121 } | |
122 }; | |
123 | |
124 public static final LuanFunction javaFileSearcher = new LuanFunction() { | |
125 public Object[] call(LuanState luan,Object[] args) throws LuanException { | |
126 String modName = (String)args[0]; | |
127 String path = (String)luan.get("package.jpath"); | |
128 if( path==null ) | |
129 return LuanFunction.EMPTY_RTN; | |
130 for( String s : path.split(";") ) { | |
131 String file = s.replaceAll("\\?",modName); | |
132 URL url = ClassLoader.getSystemResource(file); | |
133 if( url != null ) { | |
134 return new Object[]{javaFileLoader,url}; | |
135 } | |
136 } | |
137 return LuanFunction.EMPTY_RTN; | |
138 } | |
139 }; | |
140 | |
103 } | 141 } |