comparison core/src/luan/modules/PackageLuan.java @ 299:a74559240b4f

simplify PackageLuan and remove IO loading param git-svn-id: https://luan-java.googlecode.com/svn/trunk@300 21e917c8-12df-6dd8-5cb6-c86387c605b9
author fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
date Tue, 16 Dec 2014 06:24:49 +0000
parents 899253043270
children a6bf8ff720f8
comparison
equal deleted inserted replaced
298:2ce23c7e2342 299:a74559240b4f
23 throw new RuntimeException(e); 23 throw new RuntimeException(e);
24 } 24 }
25 } 25 }
26 26
27 public static LuanTable loaded(LuanState luan) { 27 public static LuanTable loaded(LuanState luan) {
28 return luan.registryTable("Package.loaded"); 28 LuanTable tbl = (LuanTable)luan.registry().get("Package.loaded");
29 } 29 if( tbl == null ) {
30 30 tbl = Luan.newTable();
31 private static Object pkg(LuanState luan,String key) { 31 luan.registry().put("Package.loaded",tbl);
32 LuanTable t = (LuanTable)loaded(luan).get("Package"); 32 }
33 return t==null ? null : t.get(key); 33 return tbl;
34 } 34 }
35 35
36 public static Object require(LuanState luan,String modName) throws LuanException { 36 public static Object require(LuanState luan,String modName) throws LuanException {
37 Object mod = load(luan,modName); 37 Object mod = load(luan,modName);
38 if( mod==null ) 38 if( mod==null )
39 throw luan.exception( "module '"+modName+"' not found" ); 39 throw luan.exception( "module '"+modName+"' not found" );
40 return mod; 40 return mod;
41 } 41 }
42 42
43 public static Object load(LuanState luan,String modName) throws LuanException { 43 public static Object load(LuanState luan,String modName) throws LuanException {
44 if( modName.startsWith("java:") )
45 return JavaLuan.load(luan,modName.substring(5));
44 LuanTable loaded = loaded(luan); 46 LuanTable loaded = loaded(luan);
45 Object mod = loaded.get(modName); 47 Object mod = loaded.get(modName);
46 if( mod == null ) { 48 if( mod == null ) {
47 Object[] a = search(luan,modName); 49 String src = read(luan,modName+".luan");
48 if( a == null ) 50 if( src == null )
49 return null; 51 return null;
50 LuanFunction loader = (LuanFunction)a[0]; 52 LuanFunction loader = BasicLuan.load(luan,src,modName,null,false);
51 a[0] = modName; 53 mod = Luan.first(luan.call(loader,"<require \""+modName+"\">",new Object[]{modName}));
52 mod = Luan.first(luan.call(loader,"<require \""+modName+"\">",a));
53 if( mod != null ) { 54 if( mod != null ) {
54 loaded.put(modName,mod); 55 loaded.put(modName,mod);
55 } else { 56 } else {
56 mod = loaded.get(modName); 57 mod = loaded.get(modName);
57 if( mod==null ) { 58 if( mod==null ) {
61 } 62 }
62 } 63 }
63 return mod; 64 return mod;
64 } 65 }
65 66
66 static LuanFunction loader(LuanState luan,String name,boolean loading,LuanTable env) throws LuanException { 67 static String read(LuanState luan,String uri) throws LuanException {
67 LuanTable t = IoLuan.Uri(luan,name,loading); 68 LuanTable t = IoLuan.Uri(luan,uri);
68 if( t == null ) 69 if( t == null )
69 return null; 70 return null;
70 LuanFunction loader = (LuanFunction)t.get("loader"); 71 LuanFunction reader = (LuanFunction)t.get("read_text");
71 if( loader == null ) 72 return (String)Luan.first(luan.call(reader));
72 return null;
73 return (LuanFunction)Luan.first(luan.call(loader,new Object[]{name,env}));
74 }
75
76 public static Object[] search(LuanState luan,String modName) throws LuanException {
77 LuanFunction fn = loader(luan,modName,true,null);
78 return fn==null ? null : new Object[]{fn,modName};
79 } 73 }
80 74
81 } 75 }