#if !(PCL || ENABLE_DOTNET)
using System;
using MoonSharp.Interpreter.Loaders;
namespace MoonSharp.Interpreter.REPL
{
///
/// A script loader loading scripts directly from the file system (does not go through platform object)
/// AND starts with module paths taken from environment variables (again, not going through the platform object).
///
/// The paths are preconstructed using :
/// * The MOONSHARP_PATH environment variable if it exists
/// * The LUA_PATH_5_2 environment variable if MOONSHARP_PATH does not exists
/// * The LUA_PATH environment variable if LUA_PATH_5_2 and MOONSHARP_PATH do not exists
/// * The "?;?.lua" path if all the above fail
///
/// Also, everytime a module is require(d), the "LUA_PATH" global variable is checked. If it exists, those paths
/// will be used to load the module instead of the global ones.
///
public class ReplInterpreterScriptLoader : FileSystemScriptLoader
{
///
/// Initializes a new instance of the class.
///
public ReplInterpreterScriptLoader()
{
string env = Environment.GetEnvironmentVariable("MOONSHARP_PATH");
if (!string.IsNullOrEmpty(env)) ModulePaths = UnpackStringPaths(env);
if (ModulePaths == null)
{
env = Environment.GetEnvironmentVariable("LUA_PATH_5_2");
if (!string.IsNullOrEmpty(env)) ModulePaths = UnpackStringPaths(env);
}
if (ModulePaths == null)
{
env = Environment.GetEnvironmentVariable("LUA_PATH");
if (!string.IsNullOrEmpty(env)) ModulePaths = UnpackStringPaths(env);
}
if (ModulePaths == null)
{
ModulePaths = UnpackStringPaths("?;?.lua");
}
}
///
/// Resolves the name of a module to a filename (which will later be passed to OpenScriptFile).
/// The resolution happens first on paths included in the LUA_PATH global variable, and -
/// if the variable does not exist - by consulting the
/// ScriptOptions.ModulesPaths array. Override to provide a different behaviour.
///
/// The modname.
/// The global context.
///
public override string ResolveModuleName(string modname, Table globalContext)
{
DynValue s = globalContext.RawGet("LUA_PATH");
if (s != null && s.Type == DataType.String)
return ResolveModuleName(modname, UnpackStringPaths(s.String));
else
return base.ResolveModuleName(modname, globalContext);
}
}
}
#endif