comparison src/luan/modules/PackageLuan.java @ 1280:781ec0a92bb5

add Boot.luan
author Franklin Schmidt <fschmidt@gmail.com>
date Thu, 20 Dec 2018 13:38:16 -0700
parents 9fa8b8389578
children 503bde9a7c80
comparison
equal deleted inserted replaced
1279:323743a7f317 1280:781ec0a92bb5
1 package luan.modules; 1 package luan.modules;
2 2
3 import java.io.Reader;
4 import java.io.InputStreamReader;
3 import java.io.IOException; 5 import java.io.IOException;
4 import java.util.Arrays; 6 import java.util.Arrays;
5 import java.util.Collections; 7 import java.util.Collections;
6 import luan.Luan; 8 import luan.Luan;
7 import luan.LuanState; 9 import luan.LuanState;
41 43
42 public static Object load(LuanState luan,String modName) throws LuanException { 44 public static Object load(LuanState luan,String modName) throws LuanException {
43 LuanTable loaded = loaded(luan); 45 LuanTable loaded = loaded(luan);
44 Object mod = loaded.rawGet(modName); 46 Object mod = loaded.rawGet(modName);
45 if( mod == null ) { 47 if( mod == null ) {
46 if( modName.startsWith("java:") ) { 48 if( modName.equals("luan:Boot.luan") ) {
49 String src;
50 try {
51 Reader in = new InputStreamReader(ClassLoader.getSystemResourceAsStream("luan/modules/Boot.luan"));
52 src = Utils.readAll(in);
53 in.close();
54 } catch(IOException e) {
55 throw new RuntimeException(e);
56 }
57 LuanFunction loader = Luan.load(src,modName);
58 mod = Luan.first(
59 loader.call(luan,new Object[]{modName})
60 );
61 if( mod == null )
62 throw new RuntimeException();
63 } else if( modName.startsWith("java:") ) {
47 mod = JavaLuan.load(luan,modName.substring(5)); 64 mod = JavaLuan.load(luan,modName.substring(5));
48 if( mod == null ) 65 if( mod == null )
49 mod = Boolean.FALSE; 66 mod = Boolean.FALSE;
50 } else { 67 } else {
51 String src = read(luan,modName); 68 String src = read(luan,modName);
68 } 85 }
69 return mod; 86 return mod;
70 } 87 }
71 88
72 static String read(LuanState luan,String uri) throws LuanException { 89 static String read(LuanState luan,String uri) throws LuanException {
73 LuanTable t = IoLuan.uri(luan,uri,null); 90 LuanTable boot = (LuanTable)PackageLuan.require(luan,"luan:Boot.luan");
74 if( t == null ) 91 LuanFunction read = (LuanFunction)boot.get("read");
75 return null; 92 return (String)Luan.first(read.call(luan,new Object[]{uri}));
76 /*
77 LuanFunction existsFn = (LuanFunction)t.get(luan,"exists");
78 boolean exists = (Boolean)Luan.first(existsFn.call(luan));
79 if( !exists )
80 return null;
81 LuanFunction reader = (LuanFunction)t.get(luan,"read_text");
82 return (String)Luan.first(reader.call(luan));
83 */
84 IoLuan.LuanIn in = (IoLuan.LuanIn)t.rawGet("java");
85 try {
86 if( !in.exists(luan) )
87 return null;
88 return in.read_text(luan);
89 } catch(IOException e) {
90 throw new LuanException(e);
91 }
92 } 93 }
93 94
94 public static void enableLoad(LuanState luan,String... mods) throws LuanException { 95 public static void enableLoad(LuanState luan,String... mods) throws LuanException {
95 if( !luan.isLocked ) 96 if( !luan.isLocked )
96 return; 97 return;