comparison src/luan/lib/PackageLib.java @ 73:f86e4f77ef32

add package module git-svn-id: https://luan-java.googlecode.com/svn/trunk@74 21e917c8-12df-6dd8-5cb6-c86387c605b9
author fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
date Tue, 12 Feb 2013 09:46:45 +0000
parents
children f003338d503b
comparison
equal deleted inserted replaced
72:cd9dbd7477ca 73:f86e4f77ef32
1 package luan.lib;
2
3 import java.io.File;
4 import java.util.List;
5 import java.util.ArrayList;
6 import luan.Luan;
7 import luan.LuanState;
8 import luan.LuanTable;
9 import luan.LuanFunction;
10 import luan.LuanJavaFunction;
11 import luan.LuanElement;
12 import luan.LuanException;
13
14
15 public final class PackageLib {
16
17 public static final String NAME = "package";
18
19 public static final LuanFunction LOADER = new LuanFunction() {
20 public Object[] call(LuanState luan,Object[] args) throws LuanException {
21 LuanTable global = luan.global;
22 LuanTable module = new LuanTable();
23 List<Object> searchers = new ArrayList<Object>();
24 global.put(NAME,module);
25 module.put("loaded",luan.loaded);
26 module.put("preload",luan.preload);
27 module.put("path","?.lua");
28 try {
29 add( global, "require", LuanState.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) {
34 throw new RuntimeException(e);
35 }
36 module.put("searchers",new LuanTable(searchers));
37 return LuanFunction.EMPTY_RTN;
38 }
39 };
40
41 private static void add(LuanTable t,String method,Class<?>... parameterTypes) throws NoSuchMethodException {
42 t.put( method, new LuanJavaFunction(PackageLib.class.getMethod(method,parameterTypes),null) );
43 }
44
45 public static Object require(LuanState luan,String modName) throws LuanException {
46 LuanTable module = (LuanTable)luan.global.get(NAME);
47 Object mod = luan.loaded.get(modName);
48 if( mod == null ) {
49 LuanTable searchers = (LuanTable)module.get("searchers");
50 for( Object s : searchers.asList() ) {
51 LuanFunction searcher = (LuanFunction)s;
52 Object[] a = luan.call(searcher,LuanElement.JAVA,"searcher",modName);
53 if( a.length >= 1 && a[0] instanceof LuanFunction ) {
54 LuanFunction loader = (LuanFunction)a[0];
55 Object extra = a.length >= 2 ? a[1] : null;
56 mod = luan.load(loader,modName,extra);
57 }
58 }
59 if( mod == null )
60 throw new LuanException( luan, LuanElement.JAVA, "module '"+modName+"' not found" );
61 }
62 return mod;
63 }
64
65 public static String search_path(String NAME,String path) {
66 for( String s : path.split(";") ) {
67 String file = s.replaceAll("\\?",NAME);
68 if( new File(file).exists() )
69 return file;
70 }
71 return null;
72 }
73
74 private static final LuanFunction fileLoader = new LuanFunction() {
75 public Object[] call(LuanState luan,Object[] args) throws LuanException {
76 return BasicLib.do_file(luan,(String)args[1]);
77 }
78 };
79
80 public static Object[] fileSearcher(LuanState luan,String modName) {
81 LuanTable module = (LuanTable)luan.global.get(NAME);
82 String path = (String)module.get("path");
83 String file = search_path(modName,path);
84 return file==null ? LuanFunction.EMPTY_RTN : new Object[]{fileLoader,file};
85 }
86
87 public static Object preloadSearcher(LuanState luan,String modName) {
88 return luan.preload.get(modName);
89 }
90
91 }