view src/luan/modules/PackageLuan.java @ 1283:503bde9a7c80

add luan.require() and table.call()
author Franklin Schmidt <fschmidt@gmail.com>
date Fri, 21 Dec 2018 09:12:09 -0700
parents 781ec0a92bb5
children 2c8d1d76a82a
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 luan.Luan;
import luan.LuanState;
import luan.LuanTable;
import luan.LuanFunction;
import luan.LuanJavaFunction;
import luan.LuanCloner;
import luan.LuanException;


public final class PackageLuan {

	public static final LuanFunction requireFn;
	static {
		try {
			requireFn = new LuanJavaFunction(PackageLuan.class.getMethod("require",LuanState.class,String.class),null);
		} catch(NoSuchMethodException e) {
			throw new RuntimeException(e);
		}
	}

	public static LuanTable loaded(LuanState luan) {
		LuanTable tbl = (LuanTable)luan.registry().get("Package.loaded");
		if( tbl == null ) {
			tbl = new LuanTable(luan);
			luan.registry().put("Package.loaded",tbl);
		}
		return tbl;
	}

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

	public static Object load(LuanState luan,String modName) throws LuanException {
		LuanTable loaded = loaded(luan);
		Object mod = loaded.rawGet(modName);
		if( mod == null ) {
			if( modName.equals("luan:Boot.luan") ) {
				String src;
				try {
					Reader in = new InputStreamReader(ClassLoader.getSystemResourceAsStream("luan/modules/Boot.luan"));
					src = Utils.readAll(in);
					in.close();
				} catch(IOException e) {
					throw new RuntimeException(e);
				}
				LuanFunction loader = Luan.load(src,modName);
				mod = Luan.first(
					loader.call(luan,new Object[]{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);
				if( src == null ) {
					mod = Boolean.FALSE;
				} else {
					LuanFunction loader = Luan.load(src,modName);
					mod = Luan.first(
						loader.call(luan,new Object[]{modName})
					);
					if( mod == null ) {
						mod = loaded.rawGet(modName);
						if( mod != null )
							return mod;
						throw new LuanException( "module '"+modName+"' returned nil" );
					}
				}
			}
			loaded.rawPut(modName,mod);
		}
		return mod;
	}

	static String read(LuanState luan,String uri) throws LuanException {
		LuanTable boot = (LuanTable)luan.require("luan:Boot.luan");
		return (String)Luan.first(boot.call("read",uri));
	}

	public static void enableLoad(LuanState luan,String... mods) throws LuanException {
		if( !luan.isLocked )
			return;
		LuanTable loaded = loaded(luan);
		for( String mod : mods ) {
			if( loaded.rawGet(mod) == null ) {
				luan.isLocked = false;
				luan.deepenClone(luan,new LuanCloner(LuanCloner.Type.COMPLETE));
				break;
			}
		}
	}

}