lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Fabio,

 

First of all , thanks for the detailed response.  I will go ahead and put in what I came up with since posting maybe it will be helpful or at least show you what I was  driving towards.

 

public interface IFoo{

                long getID();

                void OnAction( long id, action_callback /*a delegate*/ callback);

}

public class LuaFoo : IFoo

{

                private delegate void on_action( long id, action_callback callback);

                private delefate long get_id();

               

                private event on_action call_on_action;

                private event get_id call_get_id;

               

                public LuaFoo()

                {
                                load_script();

                }

                public void load_script()

                {             

                                Lua l = new Lua();

                                l.DoFile(“..\..\scripts\script.lua”);

                                call_on_action += (on_action) l.GetFunction( typeof(on_action), “OnAction”);

                                call_get_id += (get_id) l.GetFunction(typeof(get_id), “GetID”);

}

public long GetID()

{

                return call_get_id();

}

public void OnAction(long id, action_callback callback)

{

                call_on_action(id, callback);

}

}

 

 then the lua that drives it all

 

function GetID()

                return 10000;

end

function OnAction(id, callback)

                if id > 0 then

                                callback:Invoke(id) // The callback happens to take a long.

                end

end

 

This allows me to expose the lua functions directly to some other .net objects that don’t even know they are using Lua code.  

 

From: lua-bounces@bazar2.conectiva.com.br [mailto:lua-bounces@bazar2.conectiva.com.br] On Behalf Of Fabio Mascarenhas
Sent: Wednesday, July 02, 2008 4:33 PM
To: Lua list
Subject: Re: LuaInterface Questions

 

Hi, Ben,

The table -> interface translation that LuaInterface does was intended to be used to call methods on .NET objects that take an interface (which are quite rare in the base class library, Microsoft uses delegates for everything). luanet.make_object is for subclassing existing classes, not interfaces. The way to convert a Lua table to an interface reference in an embedded scenario is a little convoluted, then:

---- bar.cs
namespace bar {
  public interface IFoo {
    long get_id();
  }
}

---- foo.cs
using bar;
using LuaInterface;
using System;

public class Foo {
  public static void use_foo(IFoo f) {
    Console.WriteLine(f.get_id());
  }

  public static void Main(string[] args) {
    Lua l = new Lua();
    l.RegisterFunction("use_ifoo", null, typeof(Foo).GetMethod("use_foo"));
    l.DoFile("foo.lua");
  }
}

----- foo.lua
 impl = {}
function impl:get_id()
    return 1.0
end
use_ifoo(impl)

------------ end

No need to use make_object, or even load the assembly. I put a feature request at LuaForge so I can remember to add a convenience method to class Lua to do this more easily.

--
Fabio Mascarenhas

On Wed, Jul 2, 2008 at 12:35 PM, Ben Siemon <bsiemon@redbox.com> wrote:

First a summary. We have an interface IFoo. We want to be able to create implementations of this interface from lua code through LuaInterface and then return this implementation created in lua back to the .net code.

 

Now the example code.

 

In the C# file… in an assembly called bar.dll

Namespace bar

{

 

public interface IFoo

{

                long get_id();

}

}

 

In the lua file…

 

luanet.load_assembly("bar")

 

impl = {}

function impl:get_id()

                return 1.0

end

 

function get_instance()

                return make_object(impl, 'bar.IFoo')

end

 

To summarize, we have an interface IFoo that exists in the .net app domain. We want to be able to create new implementations 'on the fly' by evaling the lua file.  A few notes on the lua code. Originally I followed the luainterface paper and tried to call make_object(impl, IFoo) (no quotes) but the LuaInterface could not find the function. Once I added the quotes as I read in the LuaInterfaceTest project it stopped bombing out in the lua interpreter.

 

Now for the use of LuaInterface that is giving me trouble.

 

Lua l = new Lua();

 

l.DoFile(<the above lua code file>);

 

IFoo f = l.GetFunction("get_instance").Call();

 

Instead of getting a new instance of type IFoo f is just null. Based on what I read in the LuaInterface paper this should work however I have not seen this done in any example code so I am still unsure. 

 

 

--

 

time makes fools of us all,

 

ben siemon

 

 

 

 

 

 

 



The information contained in this e-mail and any accompanying documents is intended solely for the person and/or entity to whom it is addressed (i.e. those identified in the "To" and "cc" box) and may contain confidential and/or legally privileged information. Unauthorized disclosure, dissemination, copying or other use of this communication, or any attachment, is strictly prohibited and may be unlawful. If you have received this e-mail in error, please return the e-mail and attachments to the sender and delete the e-mail and attachments and any copy from your system. redbox thanks you for your cooperation.