Permalink
Browse files

Added some safety checking on calling Lua functions from the CLR.

Added LuaRuntimeException.
Fixed up dynamic invocation to only return the first returned value from Lua to avoid having to use the array indexer to get the values.
Lua.CallFunction now returns an object[] to facilitate multiple returns from Lua.
Added LuaCore.Isnil.
Added LuaCore.CheckStack.
  • Loading branch information...
1 parent 431d244 commit 610bf7aac043c0e94f7a7542a5e18485a27e0bb4 @JumpNotZero committed Nov 20, 2011
Showing with 76 additions and 2 deletions.
  1. +2 −1 NLua.TestEnvironment/Program.cs
  2. +24 −1 NLua/Lua.cs
  3. +12 −0 NLua/LuaCore.cs
  4. +37 −0 NLua/LuaRuntimeException.cs
  5. +1 −0 NLua/NLua.csproj
@@ -59,7 +59,8 @@ private static void Main()
lua.DoString("SpecialNumber = 3.14159;");
double number = lua.SpecialNumber;
- lua.add(3, 2);
+ double value = lua.add(3, 2);
+ lua.print("Adding 3 and 2 gives us " + value);
}
Console.ReadLine();
View
@@ -105,7 +105,17 @@ private int Panic(IntPtr state)
public override bool TryInvokeMember(InvokeMemberBinder binder, object[] args, out object result)
{
- result = this.CallFunction(binder.Name, args);
+ object[] results = this.CallFunction(binder.Name, args);
+
+ if (results.Length == 0)
+ {
+ result = null;
+ }
+ else
+ {
+ result = results[0];
+ }
+
return true;
}
@@ -127,6 +137,19 @@ public object[] CallFunction(string name, params object[] args)
LuaHelper.GetGlobal(this.state, name);
+ LuaType type = LuaCore.GetType(this.state, -1);
+ if (type != LuaType.Function)
+ {
+ LuaHelper.Pop(this.state);
+ throw new LuaRuntimeException("The specified global variable is not a function but is of type " + type);
+ }
+
+ if (!LuaCore.CheckStack(this.state, args.Length))
+ {
+ LuaHelper.Pop(this.state);
+ throw new LuaRuntimeException("There was insufficient stack space for the function's arguments.");
+ }
+
foreach (object arg in args)
{
CommonObjectTranslator.Push(this.state, arg);
View
@@ -45,6 +45,12 @@ internal static class LuaCore
[In, MarshalAs(UnmanagedType.I4)] int countArguments,
[In, MarshalAs(UnmanagedType.I4)] int countResults);
+ [DllImport(DllName, EntryPoint = "lua_checkstack", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]
+ [return: MarshalAs(UnmanagedType.Bool)]
+ public static extern bool CheckStack(
+ [In, MarshalAs(UnmanagedType.SysInt)] IntPtr state,
+ [In, MarshalAs(UnmanagedType.I4)] int count);
+
[DllImport(DllName, EntryPoint = "lua_newstate", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]
[return: MarshalAs(UnmanagedType.SysInt)]
public static extern IntPtr NewState(
@@ -77,6 +83,12 @@ internal static class LuaCore
public static extern int GetTop(
[In, MarshalAs(UnmanagedType.SysInt)] IntPtr state);
+ [DllImport(DllName, EntryPoint = "lua_isnil", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]
+ [return: MarshalAs(UnmanagedType.Bool)]
+ public static extern bool IsNil(
+ [In, MarshalAs(UnmanagedType.SysInt)] IntPtr state,
+ [In, MarshalAs(UnmanagedType.I4)] int index);
+
[DllImport(DllName, EntryPoint = "lua_newuserdata", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]
[return: MarshalAs(UnmanagedType.SysInt)]
public static extern IntPtr NewUserData(
@@ -0,0 +1,37 @@
+#region The MIT License
+
+/*
+ * The MIT License
+ * Source: http://www.opensource.org/licenses/mit-license.php
+ *
+ * Copyright (c) 2011-2012 Michael J. Gray
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
+ * and associated documentation files (the "Software"), to deal in the Software without
+ * restriction, including without limitation to the rights to use, copy, modify, merge, publish
+ * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in all copies or
+ * substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
+ * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS NOR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
+ * DAMAGES, NOR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT, OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALING IN THE SOFTWARE.
+*/
+
+#endregion
+
+using System;
+
+namespace NLua
+{
+ public sealed class LuaRuntimeException : Exception
+ {
+ public LuaRuntimeException(string message)
+ : base(message)
+ { }
+ }
+}
View
@@ -65,6 +65,7 @@
<Compile Include="LuaHelper.cs" />
<Compile Include="LuaObjectTranslator.cs" />
<Compile Include="LuaPseudoIndex.cs" />
+ <Compile Include="LuaRuntimeException.cs" />
<Compile Include="LuaStandardLibraries.cs" />
<Compile Include="LuaThreadStatus.cs" />
<Compile Include="LuaType.cs" />

0 comments on commit 610bf7a

Please sign in to comment.