Mercurial Hosting > luan
comparison src/luan/lib/PackageLib.java @ 136:7e160d2f6d9c
update HttpLib, untested
git-svn-id: https://luan-java.googlecode.com/svn/trunk@137 21e917c8-12df-6dd8-5cb6-c86387c605b9
author | fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9> |
---|---|
date | Wed, 11 Jun 2014 09:33:02 +0000 |
parents | 3119326260ea |
children | 06159094b802 |
comparison
equal
deleted
inserted
replaced
135:3119326260ea | 136:7e160d2f6d9c |
---|---|
3 import java.io.File; | 3 import java.io.File; |
4 import java.io.IOException; | 4 import java.io.IOException; |
5 import java.net.URL; | 5 import java.net.URL; |
6 import java.util.Arrays; | 6 import java.util.Arrays; |
7 import java.util.Collections; | 7 import java.util.Collections; |
8 import java.util.List; | |
8 import luan.Luan; | 9 import luan.Luan; |
9 import luan.LuanState; | 10 import luan.LuanState; |
10 import luan.LuanTable; | 11 import luan.LuanTable; |
11 import luan.LuanFunction; | 12 import luan.LuanFunction; |
12 import luan.LuanJavaFunction; | 13 import luan.LuanJavaFunction; |
32 throw new RuntimeException(e); | 33 throw new RuntimeException(e); |
33 } | 34 } |
34 LuanTable searchers = luan.searchers(); | 35 LuanTable searchers = luan.searchers(); |
35 searchers.add(preloadSearcher); | 36 searchers.add(preloadSearcher); |
36 searchers.add(fileSearcher); | 37 searchers.add(fileSearcher); |
37 searchers.add(javaFileSearcher); | 38 searchers.put("java",javaFileSearcher); |
38 module.put("searchers",searchers); | 39 module.put("searchers",searchers); |
39 return module; | 40 return module; |
40 } | 41 } |
41 }; | 42 }; |
42 | 43 |
46 | 47 |
47 public static Object require(LuanState luan,String modName) throws LuanException { | 48 public static Object require(LuanState luan,String modName) throws LuanException { |
48 LuanTable loaded = luan.loaded(); | 49 LuanTable loaded = luan.loaded(); |
49 Object mod = loaded.get(modName); | 50 Object mod = loaded.get(modName); |
50 if( mod == null ) { | 51 if( mod == null ) { |
52 List<Object> list = null; | |
53 String searchFor = modName; | |
51 LuanTable searchers = (LuanTable)luan.get("Package.searchers"); | 54 LuanTable searchers = (LuanTable)luan.get("Package.searchers"); |
52 if( searchers == null ) | 55 if( searchers == null ) { |
53 searchers = new LuanTable(Collections.<Object>singletonList(preloadSearcher)); | 56 list = Collections.<Object>singletonList(preloadSearcher); |
54 for( Object s : searchers.asList() ) { | 57 } else { |
58 int i = modName.indexOf(':'); | |
59 if( i != -1 ) { | |
60 String prefix = modName.substring(0,i); | |
61 Object searcher = searchers.get(prefix); | |
62 if( searcher != null ) { | |
63 list = Collections.<Object>singletonList(searcher); | |
64 searchFor = modName.substring(i+1); | |
65 } | |
66 } | |
67 if( list == null ) | |
68 list = searchers.asList(); | |
69 } | |
70 for( Object s : list ) { | |
55 LuanFunction searcher = (LuanFunction)s; | 71 LuanFunction searcher = (LuanFunction)s; |
56 Object[] a = Luan.array(luan.call(searcher,"<searcher>",new Object[]{modName})); | 72 Object[] a = Luan.array(luan.call(searcher,"<searcher>",new Object[]{searchFor})); |
57 if( a.length >= 1 && a[0] instanceof LuanFunction ) { | 73 if( a.length >= 1 && a[0] instanceof LuanFunction ) { |
58 LuanFunction loader = (LuanFunction)a[0]; | 74 LuanFunction loader = (LuanFunction)a[0]; |
59 a[0] = modName; | 75 a[0] = modName; |
60 mod = Luan.first(luan.call(loader,"<require \""+modName+"\">",a)); | 76 mod = Luan.first(luan.call(loader,"<require \""+modName+"\">",a)); |
61 if( mod != null ) { | 77 if( mod != null ) { |
125 } | 141 } |
126 }; | 142 }; |
127 | 143 |
128 public static final LuanFunction javaFileSearcher = new LuanFunction() { | 144 public static final LuanFunction javaFileSearcher = new LuanFunction() { |
129 @Override public Object[] call(LuanState luan,Object[] args) { | 145 @Override public Object[] call(LuanState luan,Object[] args) { |
130 String modName = (String)args[0]; | 146 String path = (String)args[0]; |
131 String path = (String)luan.get("Package.jpath"); | 147 String url = IoLib.java_resource_to_url(path); |
132 if( path==null ) | 148 if( url != null ) { |
133 return LuanFunction.NOTHING; | 149 return new Object[]{javaFileLoader,url}; |
134 for( String s : path.split(";") ) { | |
135 String file = s.replaceAll("\\?",modName); | |
136 URL url = ClassLoader.getSystemResource(file); | |
137 if( url != null ) { | |
138 return new Object[]{javaFileLoader,url.toString()}; | |
139 } | |
140 } | 150 } |
141 return LuanFunction.NOTHING; | 151 return LuanFunction.NOTHING; |
142 } | 152 } |
143 }; | 153 }; |
144 | 154 |