protected override object[] OnInvoke( Lua state, object[] args) { LuaFunction print = state["print"] as LuaFunction; print.Call( string.Format( "CLR function called with: {0}", args[0] ) ); throw new Exception("Some CLR error."); return new object[] { "Test" }; }
public static void Main( string[] args ) { try { TestRemoting( ); using( Lua state = new Lua( ) ) { LuaFunction print = state["print"] as LuaFunction; LuaTable table = state.CreateTable(); table.SetValue( "test", "value" ); state["incomingTable"] = table; state.DoFile( "test.lua" ); var f = new Function(); state["TestClr"] = f; LuaFunction f1 = state["AFunction"] as LuaFunction; state.DoString( "AFunction = nil" ); print.Call( "AFunction returned: " + f1.Call( )[0] ); f1.Dispose( ); LuaFunction f2 = state["BFunction"] as LuaFunction; f2.Call( ); f2.Dispose( ); LuaTable sillytable = state["SillyTable"] as LuaTable; string str = sillytable["aaa"] as string; print.Call( str ); sillytable["aaa"] = 9001; foreach( var val in sillytable ) { Console.WriteLine("{0} = {1}", val.Key, val.Value); } print.Call( state["SillyTable", "aaa"] ); sillytable.Dispose( ); state.CreateTable( "table" ); print.Call( state["table"] as LuaTable ); print.Dispose( ); GC.KeepAlive(f); } } catch( LuaException e ) { Console.WriteLine( "Fail: " + e.Message ); } Console.ReadKey(); }
protected void AssertOutput( string script, string function, string expectedOutput, string message ) { using( Lua lua = new Lua( ) ) { lua.DoString( script ); AssertOutput( lua, function, expectedOutput, message ); } }
protected void AssertOutput( Lua lua, string function, string expectedOutput, string message ) { expectedOutput = expectedOutput.Replace( "\r\n", "\n" ); // Get rid of Windows problems. using( StringWriter writer = new StringWriter( ) ) { PrintFunction func = new PrintFunction( writer ); lua["print"] = func; lua["remote"] = RemoteFunction.Instance; ((LuaFunction)lua[function]).Call(); Assert.AreEqual( expectedOutput, writer.ToString( ).Trim( ), message ); } }
public static void Main( string[] args ) { try { using( Lua state = new Lua( ) ) { state.DoFile( "test.lua" ); LuaFunction f1 = state["AFunction"] as LuaFunction; state.DoString( "AFunction = nil" ); f1.Call( ); f1.Dispose( ); LuaFunction f2 = state["BFunction"] as LuaFunction; f2.Call( ); f2.Dispose( ); LuaFunction print = state["print"] as LuaFunction; LuaTable sillytable = state["SillyTable"] as LuaTable; string str = sillytable["aaa"] as string; print.Call( str ); sillytable["aaa"] = 9001; print.Call( state["SillyTable", "aaa"] ); sillytable.Dispose( ); state.CreateTable( "table" ); print.Call( state["table"] as LuaTable ); print.Dispose( ); } } catch( LuaException e ) { Console.WriteLine( "Fail: " + e.Message ); } }
private void newLua() { lua.Dispose(); lua = new Lua(); setupDummySpringLuaEnvironment(); }
// Does nothing. protected override object[] OnInvoke(Lua state, object[] args) { throw new NotImplementedException (); }
private static void TestRemoting( ) { RemoteFunction.LookupFunction = MainClass.Lookup; using( var lua2 = new Lua( ) ) { using( var lua1 = new Lua( ) ) { LookupTable<string, Lua>.Store( "lua1", lua1 ); LookupTable<string, Lua>.Store( "lua2", lua2 ); lua1["remote"] = RemoteFunction.Instance; lua1.DoString( @"function Test1( a, b ) print( a ); print( b.a.a ); return ""Hello from Lua1 "" .. remote( ""lua2"", ""Test2"", ""a"", ""b"" ); end"); lua2["remote"] = RemoteFunction.Instance; lua2.DoString( @" function Test2( a, b ) print( ""Test2"" .. a .. b ); return ""Hello from Lua2 Called In""; end print( remote( ""lua1"", ""Test1"", ""a"", { a = { a = ""This is in a nested table."" } } ) ); "); } } }
protected void AssertOutput( Lua lua, string function, string expectedOutput ) { AssertOutput( lua, function, expectedOutput, (string)null ); }
protected override object[] OnInvoke(Lua state, object[] args) { if( args.Length != 1 ) throw new ArgumentOutOfRangeException( "args" ); writer.WriteLine( args[0] ); return null; }
/// <summary> /// Called when the function should be invoked. /// </summary> /// <param name='state'> /// The Lua state that is calling the function. /// </param> /// <param name='args'> /// The arguments that were passed to the function. /// </param> /// <returns> /// The output values for the function. If no values are /// returned; null should be used. /// </returns> protected abstract object[] OnInvoke( Lua state, object[] args );