comparison core/src/luan/modules/PackageLuan.java @ 339:673eebd83b74

remove circular package loading
author Franklin Schmidt <fschmidt@gmail.com>
date Tue, 31 Mar 2015 00:29:06 -0600
parents 0be73ac9103d
children 23b99a5039b5
comparison
equal deleted inserted replaced
338:f05c631ebaaf 339:673eebd83b74
1 package luan.modules; 1 package luan.modules;
2 2
3 import java.io.IOException; 3 import java.io.IOException;
4 import java.util.Arrays; 4 import java.util.Arrays;
5 import java.util.Collections; 5 import java.util.Collections;
6 import java.util.Set;
7 import java.util.HashSet;
8 import luan.Luan; 6 import luan.Luan;
9 import luan.LuanState; 7 import luan.LuanState;
10 import luan.LuanTable; 8 import luan.LuanTable;
11 import luan.LuanFunction; 9 import luan.LuanFunction;
12 import luan.LuanJavaFunction; 10 import luan.LuanJavaFunction;
45 LuanTable loaded = loaded(luan); 43 LuanTable loaded = loaded(luan);
46 Object mod = loaded.get(modName); 44 Object mod = loaded.get(modName);
47 if( mod == null ) { 45 if( mod == null ) {
48 if( modName.startsWith("java:") ) { 46 if( modName.startsWith("java:") ) {
49 mod = JavaLuan.load(luan,modName.substring(5)); 47 mod = JavaLuan.load(luan,modName.substring(5));
50 if( mod != null )
51 loaded.put(modName,mod);
52 } else { 48 } else {
53 String src = read(luan,modName+".luan"); 49 String src = read(luan,modName+".luan");
54 if( src == null ) 50 if( src == null )
55 return null; 51 return null;
56 LuanTable env = Luan.newTable(); 52 LuanTable env = Luan.newTable();
57 LuanFunction loader = BasicLuan.load(luan,src,modName,env,false); 53 LuanFunction loader = BasicLuan.load(luan,src,modName,env,false);
58 loaded.put(modName,env); 54 Object result = Luan.first(
59 @SuppressWarnings("unchecked") 55 luan.call(loader,"<require \""+modName+"\">",new Object[]{modName})
60 Set<String> loading = (Set<String>)luan.registry().get("Package.loading"); 56 );
61 boolean top = loading==null; 57 mod = result!=null ? result : env;
62 if(top) {
63 loading = new HashSet<String>();
64 luan.registry().put("Package.loading",loading);
65 }
66 loading.add(modName);
67 boolean ok = false;
68 try {
69 mod = Luan.first(
70 luan.call(loader,"<require \""+modName+"\">",new Object[]{modName})
71 );
72 ok = true;
73 } finally {
74 if( !ok ) {
75 if(top) {
76 for( String mn : loading ) {
77 loaded.put(mn,null);
78 }
79 } else {
80 loaded.put(modName,null);
81 }
82 }
83 if(top)
84 luan.registry().put("Package.loading",null);
85 }
86 if( mod != null )
87 loaded.put(modName,mod);
88 else
89 mod = env;
90 } 58 }
59 loaded.put(modName,mod);
91 } 60 }
92 return mod; 61 return mod;
93 } 62 }
94 63
95 static String read(LuanState luan,String uri) throws LuanException { 64 static String read(LuanState luan,String uri) throws LuanException {