diff 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
line wrap: on
line diff
--- a/src/luan/impl/LuanCompiler.java	Tue Dec 24 17:57:47 2019 -0700
+++ b/src/luan/impl/LuanCompiler.java	Sun Dec 29 15:25:07 2019 -0700
@@ -2,8 +2,11 @@
 
 import java.lang.ref.WeakReference;
 import java.lang.reflect.InvocationTargetException;
+import java.security.MessageDigest;
+import java.security.NoSuchAlgorithmException;
 import java.util.Map;
 import java.util.HashMap;
+import java.util.Base64;
 import luan.Luan;
 import luan.LuanFunction;
 import luan.LuanException;
@@ -16,8 +19,12 @@
 public final class LuanCompiler {
 	private static final Map<String,WeakReference<Class>> map = new HashMap<String,WeakReference<Class>>();
 
-	public static LuanFunction compile(Luan luan,String sourceText,String sourceName,LuanTable env) throws LuanException {
-		Class fnClass = env==null ? getClass(sourceText,sourceName) : getClass(sourceText,sourceName,env);
+	public static LuanFunction compile(Luan luan,String sourceText,String sourceName,boolean persist,LuanTable env)
+		throws LuanException
+	{
+		if( persist && env!=null )
+			throw new LuanException("can't persist with env");
+		Class fnClass = persist ? getClass(sourceText,sourceName) : getClass(sourceText,sourceName,env);
 		boolean javaOk = false;
 		if( env != null && env.closure != null )
 			javaOk = env.closure.javaOk;
@@ -41,20 +48,45 @@
 		return closure;
 	}
 
-	private static synchronized Class getClass(String sourceText,String sourceName) throws LuanException {
+	private static synchronized Class getClass(String sourceText,String sourceName)
+		throws LuanException
+	{
 		String key = sourceName + "~~~" + sourceText;
 		WeakReference<Class> ref = map.get(key);
 		if( ref != null ) {
 			Class cls = ref.get();
 			if( cls != null )
 				return cls;
+		}
+		String fileName;
+		try {
+			byte[] a = MessageDigest.getInstance("MD5").digest(key.getBytes());
+			String s = Base64.getUrlEncoder().encodeToString(a);
+//System.err.println("qqqqqqqqqq "+s);
+			fileName = s + ".luanc";
+		} catch(NoSuchAlgorithmException e) {
+			throw new RuntimeException(e);
+		}
+		Compiled compiled = Compiled.load(fileName,key);
+		//Compiled compiled = null;
+		if( compiled==null ) {
+			compiled = getCompiled(sourceText,sourceName,null);
+			compiled.save(fileName,key);
 		}
-		Class cls = getClass(sourceText,sourceName,null);
+		Class cls = compiled.loadClass();
 		map.put(key,new WeakReference<Class>(cls));
 		return cls;
 	}
 
-	private static Class getClass(String sourceText,String sourceName,LuanTable env) throws LuanException {
+	private static Class getClass(String sourceText,String sourceName,LuanTable env)
+		throws LuanException
+	{
+		return getCompiled(sourceText,sourceName,env).loadClass();
+	}
+
+	private static Compiled getCompiled(String sourceText,String sourceName,LuanTable env)
+		throws LuanException
+	{
 		LuanParser parser = new LuanParser(sourceText,sourceName);
 		parser.addVar( "require" );
 		if( env != null )  parser.addVar( "_ENV" );
@@ -66,7 +98,9 @@
 		}
 	}
 
-	public static String toJava(String sourceText,String sourceName) throws LuanException {
+	public static String toJava(String sourceText,String sourceName)
+		throws LuanException
+	{
 		LuanParser parser = new LuanParser(sourceText,sourceName);
 		parser.addVar( "require" );
 		try {