comparison src/luan/modules/PackageLuan.java @ 1333:25746915a241

merge Luan and LuanState
author Franklin Schmidt <fschmidt@gmail.com>
date Tue, 12 Feb 2019 22:33:40 -0700
parents f41919741100
children e0cf0d108a77
comparison
equal deleted inserted replaced
1332:11b7e11f9ed5 1333:25746915a241
4 import java.io.InputStreamReader; 4 import java.io.InputStreamReader;
5 import java.io.IOException; 5 import java.io.IOException;
6 import java.util.Arrays; 6 import java.util.Arrays;
7 import java.util.Collections; 7 import java.util.Collections;
8 import luan.Luan; 8 import luan.Luan;
9 import luan.LuanState;
10 import luan.LuanTable; 9 import luan.LuanTable;
11 import luan.LuanFunction; 10 import luan.LuanFunction;
12 import luan.LuanJavaFunction; 11 import luan.LuanJavaFunction;
13 import luan.LuanCloner; 12 import luan.LuanCloner;
14 import luan.LuanException; 13 import luan.LuanException;
17 public final class PackageLuan { 16 public final class PackageLuan {
18 17
19 public static final LuanFunction requireFn; 18 public static final LuanFunction requireFn;
20 static { 19 static {
21 try { 20 try {
22 requireFn = new LuanJavaFunction(PackageLuan.class.getMethod("require",LuanState.class,String.class),null); 21 requireFn = new LuanJavaFunction(PackageLuan.class.getMethod("require",Luan.class,String.class),null);
23 } catch(NoSuchMethodException e) { 22 } catch(NoSuchMethodException e) {
24 throw new RuntimeException(e); 23 throw new RuntimeException(e);
25 } 24 }
26 } 25 }
27 26
28 public static LuanTable loaded(LuanState luan) { 27 public static LuanTable loaded(Luan luan) {
29 LuanTable tbl = (LuanTable)luan.registry().get("Package.loaded"); 28 LuanTable tbl = (LuanTable)luan.registry().get("Package.loaded");
30 if( tbl == null ) { 29 if( tbl == null ) {
31 tbl = new LuanTable(luan); 30 tbl = new LuanTable(luan);
32 luan.registry().put("Package.loaded",tbl); 31 luan.registry().put("Package.loaded",tbl);
33 } 32 }
34 return tbl; 33 return tbl;
35 } 34 }
36 35
37 public static Object require(LuanState luan,String modName) throws LuanException { 36 public static Object require(Luan luan,String modName) throws LuanException {
38 Object mod = load(luan,modName); 37 Object mod = load(luan,modName);
39 if( mod.equals(Boolean.FALSE) ) 38 if( mod.equals(Boolean.FALSE) )
40 throw new LuanException( "module '"+modName+"' not found" ); 39 throw new LuanException( "module '"+modName+"' not found" );
41 return mod; 40 return mod;
42 } 41 }
43 42
44 public static Object load(LuanState luan,String modName) throws LuanException { 43 public static Object load(Luan luan,String modName) throws LuanException {
45 LuanTable loaded = loaded(luan); 44 LuanTable loaded = loaded(luan);
46 Object mod = loaded.rawGet(modName); 45 Object mod = loaded.rawGet(modName);
47 if( mod == null ) { 46 if( mod == null ) {
48 if( modName.equals("luan:Boot.luan") ) { 47 if( modName.equals("luan:Boot.luan") ) {
49 String src; 48 String src;
84 loaded.rawPut(modName,mod); 83 loaded.rawPut(modName,mod);
85 } 84 }
86 return mod; 85 return mod;
87 } 86 }
88 87
89 static String read(LuanState luan,String uri) { 88 static String read(Luan luan,String uri) {
90 LuanTable boot; 89 LuanTable boot;
91 try { 90 try {
92 boot = (LuanTable)luan.require("luan:Boot.luan"); 91 boot = (LuanTable)luan.require("luan:Boot.luan");
93 } catch(LuanException e) { 92 } catch(LuanException e) {
94 throw new RuntimeException(e); 93 throw new RuntimeException(e);
102 if( security != null ) 101 if( security != null )
103 Luan.setSecurity(luan,security); 102 Luan.setSecurity(luan,security);
104 } 103 }
105 } 104 }
106 105
107 public static void enableLoad(LuanState luan,String... mods) throws LuanException { 106 public static void enableLoad(Luan luan,String... mods) throws LuanException {
108 if( !luan.isLocked ) 107 if( !luan.isLocked )
109 return; 108 return;
110 LuanTable loaded = loaded(luan); 109 LuanTable loaded = loaded(luan);
111 for( String mod : mods ) { 110 for( String mod : mods ) {
112 if( loaded.rawGet(mod) == null ) { 111 if( loaded.rawGet(mod) == null ) {