Skip to content

Commit

Permalink
Fix '...' not working correctly in scripts
Browse files Browse the repository at this point in the history
Made the mechanism by which command-line arguments are passed to scripts match
the mechanism used in original Lua.
  • Loading branch information
Stevie-O committed Sep 10, 2013
1 parent d72822d commit 90ddc4d
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 10 deletions.
6 changes: 4 additions & 2 deletions SharpLua.Interactive/Main.cs
Original file line number Diff line number Diff line change
Expand Up @@ -171,16 +171,18 @@ static void RealMain()
if (argi < args.Length)
{
did_e = true;
// the following is now sorted out by RunFile
/*
LuaTable t = LuaRuntime.GetLua().NewTable("arg");
for (int i3 = 0; i3 < args.Length; i3++)
t[i3 - argi] = args[i3];
t["n"] = args.Length - argi;

*/
if (File.Exists(args[argi]))
LuaRuntime.SetVariable("_WORKDIR", Path.GetDirectoryName(args[argi]));
else
LuaRuntime.SetVariable("_WORKDIR", Path.GetDirectoryName(System.Windows.Forms.Application.ExecutablePath));
LuaRuntime.RunFile(args[argi]);
LuaRuntime.RunFile(args[argi], args, argi);
}

if (GoInteractive == InteractiveOption.Yes ||
Expand Down
2 changes: 1 addition & 1 deletion SharpLua.InterfacingTests/TestLua.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1505,7 +1505,7 @@ public static void Main()


Console.WriteLine("Reading test.lua file...");
l.DoFile("test.lua");
l.DoFile("test.lua", new object[] { "test.lua" }, 0);
double width = l.GetNumber("width");
double height = l.GetNumber("height");
string message = l.GetString("message");
Expand Down
56 changes: 51 additions & 5 deletions SharpLua/Interfacing/Lua.cs
Original file line number Diff line number Diff line change
Expand Up @@ -354,16 +354,59 @@ private int traceback(SharpLua.Lua.LuaState luaState)
return 1;
}

/*
* Excutes a Lua file and returns all the chunk's return
* values in an array
*/
public object[] DoFile(string fileName)
/// <summary>
/// Excutes a Lua file and returns all the chunk's return
/// values in an array
/// </summary>
/// <param name="fileName">File to be executed</param>
/// <param name="args">Optional array of arguments.</param>
/// <param name="arg0_idx">Offset into <paramref name="args"/> where the 'first' argument resides, minus 1.
/// Ignored if args is null or zero-length.
/// </param>
/// <returns></returns>
public object[] DoFile(string fileName, object[] args, int arg0_idx)
{
int pushed_args = 0;
/*
if (args == null || args.Length == 0)
{
args = new object[] { fileName };
arg0_idx = 0;
}
*/

if (args != null && args.Length > 0)
{
// based on 'getargs' from lua.c
// I honestly have no understanding as to how it works, but it seems to, so okay...
int num_real_args = args.Length - (arg0_idx + 1);
for (int i = arg0_idx + 1; i < args.Length; i++)
{
translator.push(luaState, args[i]);
pushed_args++;
}
// This looks like it constructs the 'args' table.
LuaDLL.lua_createtable(luaState, num_real_args, args.Length);
for (int i = 0; i < args.Length; i++)
{
translator.push(luaState, args[i]);
LuaDLL.lua_rawseti(luaState, -2, i - num_real_args);
}
// this was actually in handle_script() from lua.c
LuaDLL.lua_setglobal(luaState, "arg");
}

LuaDLL.lua_pushstdcallcfunction(luaState, tracebackFunction);
int oldTop = LuaDLL.lua_gettop(luaState);
if (LuaDLL.luaL_loadfile(luaState, fileName) == 0)
{
if (pushed_args > 0)
{
// the 'file' now exists as a function at the top of the stack.
// If we have arguments to pass to it, we need to adjust the stack so they are ABOVE our script
// this can be seen in handle_script in lua.c
LuaDLL.lua_insert(luaState, -(pushed_args + 1));
}
executing = true;
try
{
Expand All @@ -376,6 +419,9 @@ public object[] DoFile(string fileName)
}
else
{
if (pushed_args > 0)
LuaDLL.lua_pop(luaState, pushed_args);

//string s = Lua.lua_tostring(luaState, -1);
//if (s.EndsWith("error #-1"))
//{
Expand Down
7 changes: 5 additions & 2 deletions SharpLua/LuaRuntime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,13 @@ public class LuaRuntime
/// Runs a file on the current LuaInterface object.
/// </summary>
/// <param name="luaFile">Path to file.</param>
/// <param name="args">Array of arguments to pass to the file (may be null)</param>
/// <param name="arg0_idx">Index into args where the script filename is stored.
/// May be -1 if it's not actually even in there; should be less than args.Length - 1</param>
/// <returns></returns>
public static object[] RunFile(string luaFile)
public static object[] RunFile(string luaFile, object[] args, int arg0_idx)
{
return _interface.DoFile(luaFile);
return _interface.DoFile(luaFile, args, arg0_idx);
}

/// <summary>
Expand Down

0 comments on commit 90ddc4d

Please sign in to comment.