Mercurial Hosting > luan
view src/luan/impl/LuanCompiler.java @ 1353:8d95711f6615
replace java() with require "java"
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Wed, 20 Mar 2019 17:03:29 -0600 |
parents | e0cf0d108a77 |
children | 56fb5cd8228d |
line wrap: on
line source
package luan.impl; import java.lang.ref.WeakReference; import java.lang.reflect.InvocationTargetException; import java.util.Map; import java.util.HashMap; import luan.Luan; import luan.LuanFunction; import luan.LuanException; import luan.LuanTable; import luan.LuanClosure; import luan.modules.JavaLuan; import luan.modules.PackageLuan; 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); boolean javaOk = false; if( env != null && env.closure != null ) javaOk = env.closure.javaOk; LuanClosure closure; try { closure = (LuanClosure)fnClass.getConstructor(Luan.class,Boolean.TYPE,String.class).newInstance(luan,javaOk,sourceName); } catch(NoSuchMethodException e) { throw new RuntimeException(e); } catch(InstantiationException e) { throw new RuntimeException(e); } catch(IllegalAccessException e) { throw new RuntimeException(e); } catch(InvocationTargetException e) { throw new RuntimeException(e); } closure.upValues[0].o = PackageLuan.requireFn(luan); if( env != null ) { closure.upValues[1].o = env; env.closure = closure; } return closure; } 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; } Class cls = getClass(sourceText,sourceName,null); map.put(key,new WeakReference<Class>(cls)); return cls; } private static Class getClass(String sourceText,String sourceName,LuanTable env) throws LuanException { LuanParser parser = new LuanParser(sourceText,sourceName); parser.addVar( "require" ); if( env != null ) parser.addVar( "_ENV" ); try { return parser.RequiredModule(); } catch(ParseException e) { //e.printStackTrace(); throw new LuanException( e.getFancyMessage() ); } } public static String toJava(String sourceText,String sourceName) throws LuanException { LuanParser parser = new LuanParser(sourceText,sourceName); parser.addVar( "require" ); try { return parser.RequiredModuleSource(); } catch(ParseException e) { throw new LuanException( e.getFancyMessage() ); } } private LuanCompiler() {} // never }