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