Mercurial Hosting > luan
annotate core/src/luan/modules/PackageLuan.java @ 372:f08cefa4594c
start editing manual
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Fri, 17 Apr 2015 07:58:20 -0600 |
parents | 673eebd83b74 |
children | 23b99a5039b5 |
rev | line source |
---|---|
167
4c0131c2b650
merge luan/lib into modules
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
163
diff
changeset
|
1 package luan.modules; |
73
f86e4f77ef32
add package module
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff
changeset
|
2 |
75
aa7538ae5fb6
can now load modules from classpath
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
74
diff
changeset
|
3 import java.io.IOException; |
aa7538ae5fb6
can now load modules from classpath
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
74
diff
changeset
|
4 import java.util.Arrays; |
86
6db8f286fa6c
_ENV is per module, not global
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
84
diff
changeset
|
5 import java.util.Collections; |
73
f86e4f77ef32
add package module
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff
changeset
|
6 import luan.Luan; |
f86e4f77ef32
add package module
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff
changeset
|
7 import luan.LuanState; |
f86e4f77ef32
add package module
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff
changeset
|
8 import luan.LuanTable; |
f86e4f77ef32
add package module
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff
changeset
|
9 import luan.LuanFunction; |
f86e4f77ef32
add package module
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff
changeset
|
10 import luan.LuanJavaFunction; |
f86e4f77ef32
add package module
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff
changeset
|
11 import luan.LuanElement; |
f86e4f77ef32
add package module
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff
changeset
|
12 import luan.LuanException; |
f86e4f77ef32
add package module
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff
changeset
|
13 |
f86e4f77ef32
add package module
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff
changeset
|
14 |
168
ebe9db183eb7
rename *Lib.java to *Luan.java
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
167
diff
changeset
|
15 public final class PackageLuan { |
73
f86e4f77ef32
add package module
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff
changeset
|
16 |
186
cf939124461a
don't import modules by default
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
175
diff
changeset
|
17 public static final LuanFunction requireFn; |
cf939124461a
don't import modules by default
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
175
diff
changeset
|
18 static { |
cf939124461a
don't import modules by default
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
175
diff
changeset
|
19 try { |
cf939124461a
don't import modules by default
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
175
diff
changeset
|
20 requireFn = new LuanJavaFunction(PackageLuan.class.getMethod("require",LuanState.class,String.class),null); |
cf939124461a
don't import modules by default
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
175
diff
changeset
|
21 } catch(NoSuchMethodException e) { |
cf939124461a
don't import modules by default
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
175
diff
changeset
|
22 throw new RuntimeException(e); |
cf939124461a
don't import modules by default
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
175
diff
changeset
|
23 } |
cf939124461a
don't import modules by default
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
175
diff
changeset
|
24 } |
cf939124461a
don't import modules by default
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
175
diff
changeset
|
25 |
202
75750ceb45ee
add LuanState.registry
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
200
diff
changeset
|
26 public static LuanTable loaded(LuanState luan) { |
299
a74559240b4f
simplify PackageLuan and remove IO loading param
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
297
diff
changeset
|
27 LuanTable tbl = (LuanTable)luan.registry().get("Package.loaded"); |
a74559240b4f
simplify PackageLuan and remove IO loading param
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
297
diff
changeset
|
28 if( tbl == null ) { |
a74559240b4f
simplify PackageLuan and remove IO loading param
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
297
diff
changeset
|
29 tbl = Luan.newTable(); |
a74559240b4f
simplify PackageLuan and remove IO loading param
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
297
diff
changeset
|
30 luan.registry().put("Package.loaded",tbl); |
a74559240b4f
simplify PackageLuan and remove IO loading param
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
297
diff
changeset
|
31 } |
a74559240b4f
simplify PackageLuan and remove IO loading param
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
297
diff
changeset
|
32 return tbl; |
202
75750ceb45ee
add LuanState.registry
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
200
diff
changeset
|
33 } |
75750ceb45ee
add LuanState.registry
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
200
diff
changeset
|
34 |
108
3c404a296995
make Package module more standard;
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
96
diff
changeset
|
35 public static Object require(LuanState luan,String modName) throws LuanException { |
175
bdbd4740121f
finish web server
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
171
diff
changeset
|
36 Object mod = load(luan,modName); |
bdbd4740121f
finish web server
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
171
diff
changeset
|
37 if( mod==null ) |
bdbd4740121f
finish web server
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
171
diff
changeset
|
38 throw luan.exception( "module '"+modName+"' not found" ); |
bdbd4740121f
finish web server
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
171
diff
changeset
|
39 return mod; |
bdbd4740121f
finish web server
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
171
diff
changeset
|
40 } |
bdbd4740121f
finish web server
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
171
diff
changeset
|
41 |
bdbd4740121f
finish web server
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
171
diff
changeset
|
42 public static Object load(LuanState luan,String modName) throws LuanException { |
202
75750ceb45ee
add LuanState.registry
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
200
diff
changeset
|
43 LuanTable loaded = loaded(luan); |
108
3c404a296995
make Package module more standard;
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
96
diff
changeset
|
44 Object mod = loaded.get(modName); |
73
f86e4f77ef32
add package module
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff
changeset
|
45 if( mod == null ) { |
301
a6bf8ff720f8
add java security
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
299
diff
changeset
|
46 if( modName.startsWith("java:") ) { |
a6bf8ff720f8
add java security
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
299
diff
changeset
|
47 mod = JavaLuan.load(luan,modName.substring(5)); |
a6bf8ff720f8
add java security
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
299
diff
changeset
|
48 } else { |
a6bf8ff720f8
add java security
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
299
diff
changeset
|
49 String src = read(luan,modName+".luan"); |
a6bf8ff720f8
add java security
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
299
diff
changeset
|
50 if( src == null ) |
a6bf8ff720f8
add java security
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
299
diff
changeset
|
51 return null; |
327
0be73ac9103d
handle circular package loading
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
312
diff
changeset
|
52 LuanTable env = Luan.newTable(); |
0be73ac9103d
handle circular package loading
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
312
diff
changeset
|
53 LuanFunction loader = BasicLuan.load(luan,src,modName,env,false); |
339
673eebd83b74
remove circular package loading
Franklin Schmidt <fschmidt@gmail.com>
parents:
327
diff
changeset
|
54 Object result = Luan.first( |
673eebd83b74
remove circular package loading
Franklin Schmidt <fschmidt@gmail.com>
parents:
327
diff
changeset
|
55 luan.call(loader,"<require \""+modName+"\">",new Object[]{modName}) |
673eebd83b74
remove circular package loading
Franklin Schmidt <fschmidt@gmail.com>
parents:
327
diff
changeset
|
56 ); |
673eebd83b74
remove circular package loading
Franklin Schmidt <fschmidt@gmail.com>
parents:
327
diff
changeset
|
57 mod = result!=null ? result : env; |
136
7e160d2f6d9c
update HttpLib, untested
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
135
diff
changeset
|
58 } |
339
673eebd83b74
remove circular package loading
Franklin Schmidt <fschmidt@gmail.com>
parents:
327
diff
changeset
|
59 loaded.put(modName,mod); |
73
f86e4f77ef32
add package module
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff
changeset
|
60 } |
108
3c404a296995
make Package module more standard;
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
96
diff
changeset
|
61 return mod; |
73
f86e4f77ef32
add package module
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff
changeset
|
62 } |
f86e4f77ef32
add package module
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff
changeset
|
63 |
299
a74559240b4f
simplify PackageLuan and remove IO loading param
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
297
diff
changeset
|
64 static String read(LuanState luan,String uri) throws LuanException { |
a74559240b4f
simplify PackageLuan and remove IO loading param
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
297
diff
changeset
|
65 LuanTable t = IoLuan.Uri(luan,uri); |
264
9e0d4452e649
implement URL style module names
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
260
diff
changeset
|
66 if( t == null ) |
9e0d4452e649
implement URL style module names
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
260
diff
changeset
|
67 return null; |
301
a6bf8ff720f8
add java security
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
299
diff
changeset
|
68 LuanFunction existsFn = (LuanFunction)t.get("exists"); |
a6bf8ff720f8
add java security
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
299
diff
changeset
|
69 boolean exists = (Boolean)Luan.first(luan.call(existsFn)); |
a6bf8ff720f8
add java security
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
299
diff
changeset
|
70 if( !exists ) |
a6bf8ff720f8
add java security
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
299
diff
changeset
|
71 return null; |
299
a74559240b4f
simplify PackageLuan and remove IO loading param
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
297
diff
changeset
|
72 LuanFunction reader = (LuanFunction)t.get("read_text"); |
a74559240b4f
simplify PackageLuan and remove IO loading param
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
297
diff
changeset
|
73 return (String)Luan.first(luan.call(reader)); |
73
f86e4f77ef32
add package module
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff
changeset
|
74 } |
f86e4f77ef32
add package module
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff
changeset
|
75 |
f86e4f77ef32
add package module
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff
changeset
|
76 } |