comparison src/luan/lib/PackageLib.java @ 86:6db8f286fa6c

_ENV is per module, not global git-svn-id: https://luan-java.googlecode.com/svn/trunk@87 21e917c8-12df-6dd8-5cb6-c86387c605b9
author fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
date Wed, 27 Feb 2013 08:03:51 +0000
parents cca4f8522893
children 6ca02b188dba
comparison
equal deleted inserted replaced
85:b2551f00bc51 86:6db8f286fa6c
2 2
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 luan.Luan; 8 import luan.Luan;
8 import luan.LuanState; 9 import luan.LuanState;
9 import luan.LuanTable; 10 import luan.LuanTable;
10 import luan.LuanFunction; 11 import luan.LuanFunction;
12 import luan.LuanLoader;
11 import luan.LuanJavaFunction; 13 import luan.LuanJavaFunction;
12 import luan.LuanElement; 14 import luan.LuanElement;
13 import luan.LuanException; 15 import luan.LuanException;
14 16
15 17
16 public final class PackageLib { 18 public final class PackageLib {
17 19
18 public static final String NAME = "package"; 20 public static final String NAME = "package";
19 21
20 public static final LuanFunction LOADER = new LuanFunction() { 22 public static final LuanLoader LOADER = new LuanLoader() {
21 public Object[] call(LuanState luan,Object[] args) throws LuanException { 23 @Override protected void load(LuanState luan) {
22 LuanTable global = luan.global();
23 LuanTable module = new LuanTable(); 24 LuanTable module = new LuanTable();
25 LuanTable global = new LuanTable();
26 module.put( LuanState._G, global );
24 module.put("loaded",luan.loaded()); 27 module.put("loaded",luan.loaded());
25 module.put("preload",luan.preload()); 28 module.put("preload",luan.preload());
26 // module.put("path","?.lua"); 29 module.put("path","?.lua");
27 try { 30 try {
28 add( global, "require", LuanState.class, String.class ); 31 add( global, "require", LuanState.class, String.class );
29 add( module, "module", LuanState.class, String.class ); 32 add( global, "module", LuanState.class, String.class );
30 add( module, "search_path", String.class, String.class ); 33 add( module, "search_path", String.class, String.class );
31 } catch(NoSuchMethodException e) { 34 } catch(NoSuchMethodException e) {
32 throw new RuntimeException(e); 35 throw new RuntimeException(e);
33 } 36 }
34 module.put("searchers",new LuanTable(Arrays.<Object>asList(preloadSearcher,fileSearcher,javaFileSearcher))); 37 module.put("searchers",new LuanTable(Arrays.<Object>asList(preloadSearcher,fileSearcher,javaFileSearcher)));
35 return new Object[]{module}; 38 luan.loaded().put(NAME,module);
36 } 39 }
37 }; 40 };
38 41
39 private static void add(LuanTable t,String method,Class<?>... parameterTypes) throws NoSuchMethodException { 42 private static void add(LuanTable t,String method,Class<?>... parameterTypes) throws NoSuchMethodException {
40 t.put( method, new LuanJavaFunction(PackageLib.class.getMethod(method,parameterTypes),null) ); 43 t.put( method, new LuanJavaFunction(PackageLib.class.getMethod(method,parameterTypes),null) );
41 } 44 }
42 45
43 public static void require(LuanState luan,String modName) throws LuanException { 46 public static void module(LuanState luan,String modName) throws LuanException {
44 Object mod = module(luan,modName); 47 LuanTable module = new LuanTable();
45 if( mod instanceof LuanTable ) 48 luan.currentEnvironment().put(modName,module);
46 luan.global().put(modName,mod); 49 luan.loaded().put(modName,module);
47 } 50 }
48 51
49 public static Object module(LuanState luan,String modName) throws LuanException { 52 public static void require(LuanState luan,String modName) throws LuanException {
50 Object mod = luan.loaded().get(modName); 53 require(luan,modName,luan.currentEnvironment());
54 }
55
56 public static void require(LuanState luan,String modName,LuanTable env) throws LuanException {
57 LuanTable mod = (LuanTable)luan.loaded().get(modName);
51 if( mod == null ) { 58 if( mod == null ) {
52 LuanTable searchers = (LuanTable)luan.get("package.searchers"); 59 LuanTable searchers = (LuanTable)luan.get("package.searchers");
60 if( searchers == null )
61 searchers = new LuanTable(Collections.<Object>singletonList(preloadSearcher));
53 for( Object s : searchers.asList() ) { 62 for( Object s : searchers.asList() ) {
54 LuanFunction searcher = (LuanFunction)s; 63 LuanFunction searcher = (LuanFunction)s;
55 Object[] a = luan.call(searcher,LuanElement.JAVA,"searcher",modName); 64 Object[] a = luan.call(searcher,LuanElement.JAVA,"<searcher>",modName);
56 if( a.length >= 1 && a[0] instanceof LuanFunction ) { 65 if( a.length >= 1 && a[0] instanceof LuanFunction ) {
57 LuanFunction loader = (LuanFunction)a[0]; 66 LuanFunction loader = (LuanFunction)a[0];
58 Object extra = a.length >= 2 ? a[1] : null; 67 luan.call(loader,LuanElement.JAVA,"<loader>");
59 mod = Luan.first(luan.call(loader,LuanElement.JAVA,"loader",modName,extra)); 68 mod = (LuanTable)luan.loaded().get(modName);
60 if( mod == null ) 69 if( mod==null )
61 mod = true; 70 throw new LuanException( luan, LuanElement.JAVA, "module '"+modName+"' didn't define its module" );
62 luan.loaded().put(modName,mod); 71 break;
63 } 72 }
64 } 73 }
65 if( mod == null ) 74 if( mod == null )
66 throw new LuanException( luan, LuanElement.JAVA, "module '"+modName+"' not found" ); 75 throw new LuanException( luan, LuanElement.JAVA, "module '"+modName+"' not found" );
67 } 76 }
68 return mod; 77 if( env != null )
78 env.put(modName,mod);
69 } 79 }
70 80
71 public static String search_path(String name,String path) { 81 public static String search_path(String name,String path) {
72 for( String s : path.split(";") ) { 82 for( String s : path.split(";") ) {
73 String file = s.replaceAll("\\?",name); 83 String file = s.replaceAll("\\?",name);
75 return file; 85 return file;
76 } 86 }
77 return null; 87 return null;
78 } 88 }
79 89
80 private static final LuanFunction fileLoader = new LuanFunction() { 90 private static final class FileLoader extends LuanLoader {
81 public Object[] call(LuanState luan,Object[] args) throws LuanException { 91 private final String fileName;
82 String modName = (String)args[0]; 92
83 String fileName = (String)args[1]; 93 FileLoader(String fileName) {
84 LuanFunction fn = BasicLib.load_file(luan,fileName); 94 this.fileName = fileName;
85 return luan.call(fn,LuanElement.JAVA,modName,args); 95 }
96
97 @Override protected void load(LuanState luan) throws LuanException {
98 LuanFunction fn = BasicLib.load_file(luan,fileName,null);
99 fn.call(luan,EMPTY);
86 } 100 }
87 }; 101 };
88 102
89 public static final LuanFunction fileSearcher = new LuanFunction() { 103 public static final LuanFunction fileSearcher = new LuanFunction() {
90 public Object[] call(LuanState luan,Object[] args) throws LuanException { 104 public Object[] call(LuanState luan,Object[] args) throws LuanException {
91 String modName = (String)args[0]; 105 String modName = (String)args[0];
92 String path = (String)luan.get("package.path"); 106 String path = (String)luan.get("package.path");
93 if( path==null ) 107 if( path==null )
94 return LuanFunction.EMPTY_RTN; 108 return LuanFunction.EMPTY;
95 String file = search_path(modName,path); 109 String file = search_path(modName,path);
96 return file==null ? LuanFunction.EMPTY_RTN : new Object[]{fileLoader,file}; 110 return file==null ? LuanFunction.EMPTY : new Object[]{new FileLoader(file)};
97 } 111 }
98 }; 112 };
99 113
100 public static final LuanFunction preloadSearcher = new LuanFunction() { 114 public static final LuanFunction preloadSearcher = new LuanFunction() {
101 public Object[] call(LuanState luan,Object[] args) throws LuanException { 115 public Object[] call(LuanState luan,Object[] args) throws LuanException {
106 }; 120 };
107 121
108 122
109 123
110 124
111 private static final LuanFunction javaFileLoader = new LuanFunction() { 125 private static final class JavaFileLoader extends LuanLoader {
112 public Object[] call(LuanState luan,Object[] args) throws LuanException { 126 private final URL url;
127
128 JavaFileLoader(URL url) {
129 this.url = url;
130 }
131
132 @Override protected void load(LuanState luan) throws LuanException {
113 try { 133 try {
114 String modName = (String)args[0];
115 URL url = (URL)args[1];
116 String src = Utils.read(url); 134 String src = Utils.read(url);
117 LuanFunction fn = BasicLib.load(luan,src,url.toString()); 135 LuanFunction fn = BasicLib.load(luan,src,url.toString(),null);
118 return luan.call(fn,LuanElement.JAVA,modName,args); 136 fn.call(luan,EMPTY);
119 } catch(IOException e) { 137 } catch(IOException e) {
120 throw new LuanException(luan,LuanElement.JAVA,e); 138 throw new LuanException(luan,LuanElement.JAVA,e);
121 } 139 }
122 } 140 }
123 }; 141 };
125 public static final LuanFunction javaFileSearcher = new LuanFunction() { 143 public static final LuanFunction javaFileSearcher = new LuanFunction() {
126 public Object[] call(LuanState luan,Object[] args) throws LuanException { 144 public Object[] call(LuanState luan,Object[] args) throws LuanException {
127 String modName = (String)args[0]; 145 String modName = (String)args[0];
128 String path = (String)luan.get("package.jpath"); 146 String path = (String)luan.get("package.jpath");
129 if( path==null ) 147 if( path==null )
130 return LuanFunction.EMPTY_RTN; 148 return LuanFunction.EMPTY;
131 for( String s : path.split(";") ) { 149 for( String s : path.split(";") ) {
132 String file = s.replaceAll("\\?",modName); 150 String file = s.replaceAll("\\?",modName);
133 URL url = ClassLoader.getSystemResource(file); 151 URL url = ClassLoader.getSystemResource(file);
134 if( url != null ) { 152 if( url != null ) {
135 return new Object[]{javaFileLoader,url}; 153 return new Object[]{new JavaFileLoader(url)};
136 } 154 }
137 } 155 }
138 return LuanFunction.EMPTY_RTN; 156 return LuanFunction.EMPTY;
139 } 157 }
140 }; 158 };
141 159
142 } 160 }