comparison core/src/luan/modules/PackageLuan.java @ 426:23a93c118042

fix LuanTable.get() to use metatables
author Franklin Schmidt <fschmidt@gmail.com>
date Fri, 01 May 2015 18:44:20 -0600
parents 23b99a5039b5
children dae264ad6a7b
comparison
equal deleted inserted replaced
425:0a2fb80907f9 426:23a93c118042
22 throw new RuntimeException(e); 22 throw new RuntimeException(e);
23 } 23 }
24 } 24 }
25 25
26 public static LuanTable loaded(LuanState luan) { 26 public static LuanTable loaded(LuanState luan) {
27 LuanTable tbl = (LuanTable)luan.registry().get("Package.loaded"); 27 LuanTable tbl = (LuanTable)luan.registry().rawGet("Package.loaded");
28 if( tbl == null ) { 28 if( tbl == null ) {
29 tbl = new LuanTable(); 29 tbl = new LuanTable();
30 luan.registry().put("Package.loaded",tbl); 30 luan.registry().put("Package.loaded",tbl);
31 } 31 }
32 return tbl; 32 return tbl;
39 return mod; 39 return mod;
40 } 40 }
41 41
42 public static Object load(LuanState luan,String modName) throws LuanException { 42 public static Object load(LuanState luan,String modName) throws LuanException {
43 LuanTable loaded = loaded(luan); 43 LuanTable loaded = loaded(luan);
44 Object mod = loaded.get(modName); 44 Object mod = loaded.rawGet(modName);
45 if( mod == null ) { 45 if( mod == null ) {
46 if( modName.startsWith("java:") ) { 46 if( modName.startsWith("java:") ) {
47 mod = JavaLuan.load(luan,modName.substring(5)); 47 mod = JavaLuan.load(luan,modName.substring(5));
48 } else { 48 } else {
49 String src = read(luan,modName+".luan"); 49 String src = read(luan,modName+".luan");
63 63
64 static String read(LuanState luan,String uri) throws LuanException { 64 static String read(LuanState luan,String uri) throws LuanException {
65 LuanTable t = IoLuan.Uri(luan,uri); 65 LuanTable t = IoLuan.Uri(luan,uri);
66 if( t == null ) 66 if( t == null )
67 return null; 67 return null;
68 LuanFunction existsFn = (LuanFunction)t.get("exists"); 68 LuanFunction existsFn = (LuanFunction)t.get(luan,"exists");
69 boolean exists = (Boolean)Luan.first(luan.call(existsFn)); 69 boolean exists = (Boolean)Luan.first(luan.call(existsFn));
70 if( !exists ) 70 if( !exists )
71 return null; 71 return null;
72 LuanFunction reader = (LuanFunction)t.get("read_text"); 72 LuanFunction reader = (LuanFunction)t.get(luan,"read_text");
73 return (String)Luan.first(luan.call(reader)); 73 return (String)Luan.first(luan.call(reader));
74 } 74 }
75 75
76 } 76 }