view src/luan/modules/PackageLuan.java @ 1618:a37ffe2d1b14

fix not_found_handler
author Franklin Schmidt <fschmidt@gmail.com>
date Mon, 14 Jun 2021 18:45:17 -0600
parents 2975c932864d
children
line wrap: on
line source

package luan.modules;

import java.io.Reader;
import java.io.InputStreamReader;
import java.io.IOException;
import java.util.Arrays;
import java.util.Collections;
import java.util.Map;
import java.util.HashMap;
import goodjava.io.IoUtils;
import luan.Luan;
import luan.LuanTable;
import luan.LuanFunction;
import luan.LuanJavaFunction;
import luan.LuanException;


public final class PackageLuan {

	public static LuanFunction requireFn(Luan luan) {
		LuanFunction fn = (LuanFunction)luan.registry().get("Package.require");
		if( fn == null ) {
			try {
				fn = new LuanJavaFunction(PackageLuan.class.getMethod("require",Luan.class,String.class,LuanTable.class),null);
			} catch(NoSuchMethodException e) {
				throw new RuntimeException(e);
			}
			luan.registry().put("Package.require",fn);
		}
		return fn;
	}

	public static Map loaded(Luan luan) {
		Map map = (Map)luan.registry().get("Package.loaded");
		if( map == null ) {
			map = new HashMap();
			luan.registry().put("Package.loaded",map);
		}
		return map;
	}

	public static Object require(Luan luan,String modName,LuanTable options) throws LuanException {
		if( "java".equals(modName) ) {
			JavaLuan.java(luan);
			return true;
		}
		Object mod = load(luan,modName,options);
		if( mod.equals(Boolean.FALSE) )
			throw new LuanException( "module '"+modName+"' not found" );
		return mod;
	}

	public static Object load(Luan luan,String modName,LuanTable options) throws LuanException {
		Map loaded = loaded(luan);
		Object mod = loaded.get(modName);
		if( mod == null ) {
			if( modName.equals("luan:Boot.luan") ) {
				String src;
				try {
					Reader in = new InputStreamReader(ClassLoader.getSystemResourceAsStream("luan/modules/Boot.luan"));
					src = IoUtils.readAll(in);
					in.close();
				} catch(IOException e) {
					throw new RuntimeException(e);
				}
				LuanFunction loader = luan.load(src,modName,true);
				mod = Luan.first(
					loader.call(luan,modName)
				);
				if( mod == null )
					throw new RuntimeException();
			} else if( modName.startsWith("java:") ) {
				mod = JavaLuan.load(luan,modName.substring(5));
				if( mod == null )
					mod = Boolean.FALSE;
			} else {
				String src = read(luan,modName,options);
				if( src == null ) {
					mod = Boolean.FALSE;
				} else {
					LuanFunction loader = luan.load(src,modName,true);
					mod = Luan.first(
						loader.call(luan,modName)
					);
					if( mod == null ) {
						mod = loaded.get(modName);
						if( mod != null )
							return mod;
						throw new LuanException( "module '"+modName+"' returned nil" );
					}
				}
			}
			loaded.put(modName,mod);
		}
		return mod;
	}

	static String read(Luan luan,String uri,LuanTable options) {
		LuanTable boot;
		try {
			boot = (LuanTable)luan.require("luan:Boot.luan");
		} catch(LuanException e) {
			throw new RuntimeException(e);
		}
		Luan.Security security = Luan.setSecurity(luan,null);
		try {
			return (String)Luan.first(boot.fn(luan,"read").call(luan,uri,options));
		} catch(LuanException e) {
			return null;
		} finally {
			if( security != null )
				Luan.setSecurity(luan,security);
		}
	}

}