comparison src/luan/impl/LuanCompiler.java @ 1434:56fb5cd8228d

cache compiled code in temp files
author Franklin Schmidt <fschmidt@gmail.com>
date Sun, 29 Dec 2019 15:25:07 -0700
parents 8d95711f6615
children 6c830be6be98
comparison
equal deleted inserted replaced
1433:5f038be65271 1434:56fb5cd8228d
1 package luan.impl; 1 package luan.impl;
2 2
3 import java.lang.ref.WeakReference; 3 import java.lang.ref.WeakReference;
4 import java.lang.reflect.InvocationTargetException; 4 import java.lang.reflect.InvocationTargetException;
5 import java.security.MessageDigest;
6 import java.security.NoSuchAlgorithmException;
5 import java.util.Map; 7 import java.util.Map;
6 import java.util.HashMap; 8 import java.util.HashMap;
9 import java.util.Base64;
7 import luan.Luan; 10 import luan.Luan;
8 import luan.LuanFunction; 11 import luan.LuanFunction;
9 import luan.LuanException; 12 import luan.LuanException;
10 import luan.LuanTable; 13 import luan.LuanTable;
11 import luan.LuanClosure; 14 import luan.LuanClosure;
14 17
15 18
16 public final class LuanCompiler { 19 public final class LuanCompiler {
17 private static final Map<String,WeakReference<Class>> map = new HashMap<String,WeakReference<Class>>(); 20 private static final Map<String,WeakReference<Class>> map = new HashMap<String,WeakReference<Class>>();
18 21
19 public static LuanFunction compile(Luan luan,String sourceText,String sourceName,LuanTable env) throws LuanException { 22 public static LuanFunction compile(Luan luan,String sourceText,String sourceName,boolean persist,LuanTable env)
20 Class fnClass = env==null ? getClass(sourceText,sourceName) : getClass(sourceText,sourceName,env); 23 throws LuanException
24 {
25 if( persist && env!=null )
26 throw new LuanException("can't persist with env");
27 Class fnClass = persist ? getClass(sourceText,sourceName) : getClass(sourceText,sourceName,env);
21 boolean javaOk = false; 28 boolean javaOk = false;
22 if( env != null && env.closure != null ) 29 if( env != null && env.closure != null )
23 javaOk = env.closure.javaOk; 30 javaOk = env.closure.javaOk;
24 LuanClosure closure; 31 LuanClosure closure;
25 try { 32 try {
39 env.closure = closure; 46 env.closure = closure;
40 } 47 }
41 return closure; 48 return closure;
42 } 49 }
43 50
44 private static synchronized Class getClass(String sourceText,String sourceName) throws LuanException { 51 private static synchronized Class getClass(String sourceText,String sourceName)
52 throws LuanException
53 {
45 String key = sourceName + "~~~" + sourceText; 54 String key = sourceName + "~~~" + sourceText;
46 WeakReference<Class> ref = map.get(key); 55 WeakReference<Class> ref = map.get(key);
47 if( ref != null ) { 56 if( ref != null ) {
48 Class cls = ref.get(); 57 Class cls = ref.get();
49 if( cls != null ) 58 if( cls != null )
50 return cls; 59 return cls;
51 } 60 }
52 Class cls = getClass(sourceText,sourceName,null); 61 String fileName;
62 try {
63 byte[] a = MessageDigest.getInstance("MD5").digest(key.getBytes());
64 String s = Base64.getUrlEncoder().encodeToString(a);
65 //System.err.println("qqqqqqqqqq "+s);
66 fileName = s + ".luanc";
67 } catch(NoSuchAlgorithmException e) {
68 throw new RuntimeException(e);
69 }
70 Compiled compiled = Compiled.load(fileName,key);
71 //Compiled compiled = null;
72 if( compiled==null ) {
73 compiled = getCompiled(sourceText,sourceName,null);
74 compiled.save(fileName,key);
75 }
76 Class cls = compiled.loadClass();
53 map.put(key,new WeakReference<Class>(cls)); 77 map.put(key,new WeakReference<Class>(cls));
54 return cls; 78 return cls;
55 } 79 }
56 80
57 private static Class getClass(String sourceText,String sourceName,LuanTable env) throws LuanException { 81 private static Class getClass(String sourceText,String sourceName,LuanTable env)
82 throws LuanException
83 {
84 return getCompiled(sourceText,sourceName,env).loadClass();
85 }
86
87 private static Compiled getCompiled(String sourceText,String sourceName,LuanTable env)
88 throws LuanException
89 {
58 LuanParser parser = new LuanParser(sourceText,sourceName); 90 LuanParser parser = new LuanParser(sourceText,sourceName);
59 parser.addVar( "require" ); 91 parser.addVar( "require" );
60 if( env != null ) parser.addVar( "_ENV" ); 92 if( env != null ) parser.addVar( "_ENV" );
61 try { 93 try {
62 return parser.RequiredModule(); 94 return parser.RequiredModule();
64 //e.printStackTrace(); 96 //e.printStackTrace();
65 throw new LuanException( e.getFancyMessage() ); 97 throw new LuanException( e.getFancyMessage() );
66 } 98 }
67 } 99 }
68 100
69 public static String toJava(String sourceText,String sourceName) throws LuanException { 101 public static String toJava(String sourceText,String sourceName)
102 throws LuanException
103 {
70 LuanParser parser = new LuanParser(sourceText,sourceName); 104 LuanParser parser = new LuanParser(sourceText,sourceName);
71 parser.addVar( "require" ); 105 parser.addVar( "require" );
72 try { 106 try {
73 return parser.RequiredModuleSource(); 107 return parser.RequiredModuleSource();
74 } catch(ParseException e) { 108 } catch(ParseException e) {