comparison src/luan/lib/PackageLib.java @ 111:2428ecfed375

change LuanFunction.call() from returning Object[] to returning Object, to reduce garbage collection git-svn-id: https://luan-java.googlecode.com/svn/trunk@112 21e917c8-12df-6dd8-5cb6-c86387c605b9
author fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
date Fri, 23 May 2014 20:40:05 +0000
parents 3c404a296995
children f5af13062b10
comparison
equal deleted inserted replaced
110:7afa6df829f3 111:2428ecfed375
17 public final class PackageLib { 17 public final class PackageLib {
18 18
19 public static final String NAME = "Package"; 19 public static final String NAME = "Package";
20 20
21 public static final LuanFunction LOADER = new LuanFunction() { 21 public static final LuanFunction LOADER = new LuanFunction() {
22 @Override public Object[] call(LuanState luan,Object[] args) { 22 @Override public Object call(LuanState luan,Object[] args) {
23 LuanTable module = new LuanTable(); 23 LuanTable module = new LuanTable();
24 LuanTable global = luan.global(); 24 LuanTable global = luan.global();
25 module.put("loaded",luan.loaded()); 25 module.put("loaded",luan.loaded());
26 module.put("preload",luan.preload()); 26 module.put("preload",luan.preload());
27 module.put("path","?.luan"); 27 module.put("path","?.luan");
34 LuanTable searchers = luan.searchers(); 34 LuanTable searchers = luan.searchers();
35 searchers.add(preloadSearcher); 35 searchers.add(preloadSearcher);
36 searchers.add(fileSearcher); 36 searchers.add(fileSearcher);
37 searchers.add(javaFileSearcher); 37 searchers.add(javaFileSearcher);
38 module.put("searchers",searchers); 38 module.put("searchers",searchers);
39 return new Object[]{module}; 39 return module;
40 } 40 }
41 }; 41 };
42 42
43 public static final LuanFunction require; 43 public static final LuanFunction require;
44 static { 44 static {
60 LuanTable searchers = (LuanTable)luan.get("Package.searchers"); 60 LuanTable searchers = (LuanTable)luan.get("Package.searchers");
61 if( searchers == null ) 61 if( searchers == null )
62 searchers = new LuanTable(Collections.<Object>singletonList(preloadSearcher)); 62 searchers = new LuanTable(Collections.<Object>singletonList(preloadSearcher));
63 for( Object s : searchers.asList() ) { 63 for( Object s : searchers.asList() ) {
64 LuanFunction searcher = (LuanFunction)s; 64 LuanFunction searcher = (LuanFunction)s;
65 Object[] a = luan.JAVA.call(searcher,"<searcher>",modName); 65 Object[] a = Luan.array(luan.JAVA.call(searcher,"<searcher>",modName));
66 if( a.length >= 1 && a[0] instanceof LuanFunction ) { 66 if( a.length >= 1 && a[0] instanceof LuanFunction ) {
67 LuanFunction loader = (LuanFunction)a[0]; 67 LuanFunction loader = (LuanFunction)a[0];
68 a[0] = modName; 68 a[0] = modName;
69 mod = Luan.first(luan.JAVA.call(loader,"<require \""+modName+"\">",a)); 69 mod = Luan.first(luan.JAVA.call(loader,"<require \""+modName+"\">",a));
70 if( mod != null ) { 70 if( mod != null ) {
91 } 91 }
92 return null; 92 return null;
93 } 93 }
94 94
95 public static final LuanFunction fileLoader = new LuanFunction() { 95 public static final LuanFunction fileLoader = new LuanFunction() {
96 @Override public Object[] call(LuanState luan,Object[] args) throws LuanException { 96 @Override public Object call(LuanState luan,Object[] args) throws LuanException {
97 String modName = (String)args[0];
98 String fileName = (String)args[1]; 97 String fileName = (String)args[1];
99 LuanFunction fn = BasicLib.load_file(luan,fileName); 98 LuanFunction fn = BasicLib.load_file(luan,fileName);
100 return fn.call(luan,new Object[]{args[0],fileName}); 99 return fn.call(luan,args);
101 } 100 }
102 }; 101 };
103 102
104 public static final LuanFunction fileSearcher = new LuanFunction() { 103 public static final LuanFunction fileSearcher = new LuanFunction() {
105 @Override public Object[] call(LuanState luan,Object[] args) { 104 @Override public Object[] call(LuanState luan,Object[] args) {
122 121
123 122
124 123
125 124
126 public static final LuanFunction javaFileLoader = new LuanFunction() { 125 public static final LuanFunction javaFileLoader = new LuanFunction() {
127 @Override public Object[] call(LuanState luan,Object[] args) throws LuanException { 126 @Override public Object call(LuanState luan,Object[] args) throws LuanException {
128 String modName = (String)args[0]; 127 String urlStr = (String)args[1];
129 URL url = (URL)args[0];
130 try { 128 try {
129 URL url = new URL(urlStr);
131 String src = Utils.read(url); 130 String src = Utils.read(url);
132 LuanFunction fn = BasicLib.load(luan,src,url.toString(),false); 131 LuanFunction fn = BasicLib.load(luan,src,urlStr,false);
133 return fn.call(luan,new Object[]{args[0],url.toString()}); 132 return fn.call(luan,args);
134 } catch(IOException e) { 133 } catch(IOException e) {
135 throw luan.JAVA.exception(e); 134 throw luan.JAVA.exception(e);
136 } 135 }
137 } 136 }
138 }; 137 };
145 return LuanFunction.EMPTY; 144 return LuanFunction.EMPTY;
146 for( String s : path.split(";") ) { 145 for( String s : path.split(";") ) {
147 String file = s.replaceAll("\\?",modName); 146 String file = s.replaceAll("\\?",modName);
148 URL url = ClassLoader.getSystemResource(file); 147 URL url = ClassLoader.getSystemResource(file);
149 if( url != null ) { 148 if( url != null ) {
150 return new Object[]{javaFileLoader,url}; 149 return new Object[]{javaFileLoader,url.toString()};
151 } 150 }
152 } 151 }
153 return LuanFunction.EMPTY; 152 return LuanFunction.EMPTY;
154 } 153 }
155 }; 154 };