comparison core/src/luan/impl/LuanCompiler.java @ 672:d3e5414bdf4c

better java permission handling
author Franklin Schmidt <fschmidt@gmail.com>
date Tue, 12 Apr 2016 17:03:30 -0600
parents 58ebfec6178b
children d95caff8ba8c
comparison
equal deleted inserted replaced
671:82f130eba7b0 672:d3e5414bdf4c
4 import java.lang.reflect.InvocationTargetException; 4 import java.lang.reflect.InvocationTargetException;
5 import luan.LuanFunction; 5 import luan.LuanFunction;
6 import luan.LuanState; 6 import luan.LuanState;
7 import luan.LuanException; 7 import luan.LuanException;
8 import luan.LuanTable; 8 import luan.LuanTable;
9 import luan.LuanJava;
9 import luan.modules.JavaLuan; 10 import luan.modules.JavaLuan;
10 import luan.modules.PackageLuan; 11 import luan.modules.PackageLuan;
11 12
12 13
13 public final class LuanCompiler { 14 public final class LuanCompiler {
14 private LuanCompiler() {} // never 15 private LuanCompiler() {} // never
15 16
16 public static LuanFunction compile(LuanState luan,String sourceName,String sourceText,LuanTable env,boolean allowExpr) throws LuanException { 17 public static LuanFunction compile(LuanState luan,String sourceName,String sourceText,LuanTable env,boolean allowExpr) throws LuanException {
17 LuanParser parser = new LuanParser(sourceName,sourceText); 18 LuanParser parser = new LuanParser(sourceName,sourceText);
18 parser.addVar( env!=null ? "_ENV" : null );
19 parser.addVar( "java" ); 19 parser.addVar( "java" );
20 parser.addVar( "require" ); 20 parser.addVar( "require" );
21 if( env != null ) parser.addVar( "_ENV" );
21 Class fnClass = parse(parser,allowExpr); 22 Class fnClass = parse(parser,allowExpr);
22 final LuanStateImpl luanImpl = (LuanStateImpl)luan; 23 LuanJava java;
24 if( env == null ) {
25 java = new LuanJava();
26 } else {
27 java = env.java;
28 if( java == null ) {
29 java = new LuanJava();
30 env.java = java;
31 }
32 }
23 Closure closure; 33 Closure closure;
24 try { 34 try {
25 closure = (Closure)fnClass.getConstructor(LuanState.class).newInstance(luanImpl); 35 closure = (Closure)fnClass.getConstructor(LuanState.class,LuanJava.class).newInstance(luan,java);
26 } catch(NoSuchMethodException e) { 36 } catch(NoSuchMethodException e) {
27 throw new RuntimeException(e); 37 throw new RuntimeException(e);
28 } catch(InstantiationException e) { 38 } catch(InstantiationException e) {
29 throw new RuntimeException(e); 39 throw new RuntimeException(e);
30 } catch(IllegalAccessException e) { 40 } catch(IllegalAccessException e) {
31 throw new RuntimeException(e); 41 throw new RuntimeException(e);
32 } catch(InvocationTargetException e) { 42 } catch(InvocationTargetException e) {
33 throw new RuntimeException(e); 43 throw new RuntimeException(e);
34 } 44 }
35 closure.upValues[0].o = env!=null ? env : new LuanTable(); 45 closure.upValues[0].o = JavaLuan.javaFn;
36 closure.upValues[1].o = JavaLuan.javaFn; 46 closure.upValues[1].o = PackageLuan.requireFn;
37 closure.upValues[2].o = PackageLuan.requireFn; 47 if( env != null ) closure.upValues[2].o = env;
38 return closure; 48 return closure;
39 } 49 }
40 50
41 private static Class parse(LuanParser parser,boolean allowExpr) throws LuanException { 51 private static Class parse(LuanParser parser,boolean allowExpr) throws LuanException {
42 try { 52 try {
49 } catch(ParseException e) { 59 } catch(ParseException e) {
50 e.printStackTrace(); 60 e.printStackTrace();
51 throw new LuanException( e.getFancyMessage() ); 61 throw new LuanException( e.getFancyMessage() );
52 } 62 }
53 } 63 }
54
55 public static LuanState newLuanState() {
56 return new LuanStateImpl();
57 }
58 } 64 }