Mercurial Hosting > luan
comparison src/luan/impl/LuanCompiler.java @ 1330:f41919741100
fix security
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Mon, 11 Feb 2019 01:38:55 -0700 |
parents | ba4daf107e07 |
children | 25746915a241 |
comparison
equal
deleted
inserted
replaced
1329:5a39b006acd1 | 1330:f41919741100 |
---|---|
6 import java.util.HashMap; | 6 import java.util.HashMap; |
7 import luan.LuanFunction; | 7 import luan.LuanFunction; |
8 import luan.LuanState; | 8 import luan.LuanState; |
9 import luan.LuanException; | 9 import luan.LuanException; |
10 import luan.LuanTable; | 10 import luan.LuanTable; |
11 import luan.LuanJavaOk; | 11 import luan.LuanClosure; |
12 import luan.modules.JavaLuan; | 12 import luan.modules.JavaLuan; |
13 import luan.modules.PackageLuan; | 13 import luan.modules.PackageLuan; |
14 | 14 |
15 | 15 |
16 public final class LuanCompiler { | 16 public final class LuanCompiler { |
17 private static final Map<String,WeakReference<Class>> map = new HashMap<String,WeakReference<Class>>(); | 17 private static final Map<String,WeakReference<Class>> map = new HashMap<String,WeakReference<Class>>(); |
18 | 18 |
19 public static LuanFunction compile(String sourceText,String sourceName,LuanTable env) throws LuanException { | 19 public static LuanFunction compile(String sourceText,String sourceName,LuanTable env) throws LuanException { |
20 Class fnClass = env==null ? getClass(sourceText,sourceName) : getClass(sourceText,sourceName,env); | 20 Class fnClass = env==null ? getClass(sourceText,sourceName) : getClass(sourceText,sourceName,env); |
21 LuanJavaOk javaOk; | 21 boolean javaOk = false; |
22 if( env == null ) { | 22 if( env != null && env.closure != null ) |
23 javaOk = new LuanJavaOk(); | 23 javaOk = env.closure.javaOk; |
24 } else { | 24 LuanClosure closure; |
25 javaOk = env.javaOk; | |
26 if( javaOk == null ) { | |
27 javaOk = new LuanJavaOk(); | |
28 env.javaOk = javaOk; | |
29 } | |
30 } | |
31 Closure closure; | |
32 try { | 25 try { |
33 closure = (Closure)fnClass.getConstructor(LuanJavaOk.class).newInstance(javaOk); | 26 closure = (LuanClosure)fnClass.getConstructor(Boolean.TYPE,String.class).newInstance(javaOk,sourceName); |
34 } catch(NoSuchMethodException e) { | 27 } catch(NoSuchMethodException e) { |
35 throw new RuntimeException(e); | 28 throw new RuntimeException(e); |
36 } catch(InstantiationException e) { | 29 } catch(InstantiationException e) { |
37 throw new RuntimeException(e); | 30 throw new RuntimeException(e); |
38 } catch(IllegalAccessException e) { | 31 } catch(IllegalAccessException e) { |
40 } catch(InvocationTargetException e) { | 33 } catch(InvocationTargetException e) { |
41 throw new RuntimeException(e); | 34 throw new RuntimeException(e); |
42 } | 35 } |
43 closure.upValues[0].o = JavaLuan.javaFn; | 36 closure.upValues[0].o = JavaLuan.javaFn; |
44 closure.upValues[1].o = PackageLuan.requireFn; | 37 closure.upValues[1].o = PackageLuan.requireFn; |
45 if( env != null ) closure.upValues[2].o = env; | 38 if( env != null ) { |
39 closure.upValues[2].o = env; | |
40 env.closure = closure; | |
41 } | |
46 return closure; | 42 return closure; |
47 } | 43 } |
48 | 44 |
49 private static synchronized Class getClass(String sourceText,String sourceName) throws LuanException { | 45 private static synchronized Class getClass(String sourceText,String sourceName) throws LuanException { |
50 String key = sourceName + "~~~" + sourceText; | 46 String key = sourceName + "~~~" + sourceText; |