C Function with variable number of parameters

classic Classic list List threaded Threaded
9 messages Options
Reply | Threaded
Open this post in threaded view
|

C Function with variable number of parameters

Markus Klotzbuecher
Hi,

I have a (dispatching) C function (not an ANSI C varargs) which
depending on the first parameter requires a variable number of
additional parameters. I currently solve this by registering functions
with arity 0 to 4 taking luabind::objects. What I would like to do is
register only one function with one arg which then can inspect the
stack using lua_gettop and call luabind::object::from_stack to get the
remaining arguments. What prevents this however, is luabind checking
for the exact number of parameters and complaining "No matching
overloads found". Does anybody know how to solve this?

Thanks in advance!
Markus

------------------------------------------------------------------------------
The Palm PDK Hot Apps Program offers developers who use the
Plug-In Development Kit to bring their C/C++ apps to Palm for a share
of $1 Million in cash or HP Products. Visit us here for more details:
http://ad.doubleclick.net/clk;226879339;13503038;l?
http://clk.atdmt.com/CRS/go/247765532/direct/01/
_______________________________________________
luabind-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user
Reply | Threaded
Open this post in threaded view
|

Re: C Function with variable number of parameters

Nigel Atkinson-2
Hi Markus,

I think perhaps the simplest way would be to register this particular
function using Lua functionality directly.  You could still use
luabind::object::from_stack within the function.

I.e.

int dispatch( lua_State *L );

and

http://www.lua.org/manual/5.1/manual.html#lua_register

Nigel


On Tue, 2010-07-27 at 15:51 +0200, Markus Klotzbuecher wrote:

> Hi,
>
> I have a (dispatching) C function (not an ANSI C varargs) which
> depending on the first parameter requires a variable number of
> additional parameters. I currently solve this by registering functions
> with arity 0 to 4 taking luabind::objects. What I would like to do is
> register only one function with one arg which then can inspect the
> stack using lua_gettop and call luabind::object::from_stack to get the
> remaining arguments. What prevents this however, is luabind checking
> for the exact number of parameters and complaining "No matching
> overloads found". Does anybody know how to solve this?
>
> Thanks in advance!
> Markus
>
> ------------------------------------------------------------------------------
> The Palm PDK Hot Apps Program offers developers who use the
> Plug-In Development Kit to bring their C/C++ apps to Palm for a share
> of $1 Million in cash or HP Products. Visit us here for more details:
> http://ad.doubleclick.net/clk;226879339;13503038;l?
> http://clk.atdmt.com/CRS/go/247765532/direct/01/
> _______________________________________________
> luabind-user mailing list
> [hidden email]
> https://lists.sourceforge.net/lists/listinfo/luabind-user



------------------------------------------------------------------------------
The Palm PDK Hot Apps Program offers developers who use the
Plug-In Development Kit to bring their C/C++ apps to Palm for a share
of $1 Million in cash or HP Products. Visit us here for more details:
http://ad.doubleclick.net/clk;226879339;13503038;l?
http://clk.atdmt.com/CRS/go/247765532/direct/01/
_______________________________________________
luabind-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user
Reply | Threaded
Open this post in threaded view
|

Re: C Function with variable number of parameters

Jason McKesson
  On 7/28/2010 5:04 AM, Nigel Atkinson wrote:

> Hi Markus,
>
> I think perhaps the simplest way would be to register this particular
> function using Lua functionality directly.  You could still use
> luabind::object::from_stack within the function.
>
> I.e.
>
> int dispatch( lua_State *L );
>
> and
>
> http://www.lua.org/manual/5.1/manual.html#lua_register
>
> Nigel
>
>
> On Tue, 2010-07-27 at 15:51 +0200, Markus Klotzbuecher wrote:
>> Hi,
>>
>> I have a (dispatching) C function (not an ANSI C varargs) which
>> depending on the first parameter requires a variable number of
>> additional parameters. I currently solve this by registering functions
>> with arity 0 to 4 taking luabind::objects. What I would like to do is
>> register only one function with one arg which then can inspect the
>> stack using lua_gettop and call luabind::object::from_stack to get the
>> remaining arguments. What prevents this however, is luabind checking
>> for the exact number of parameters and complaining "No matching
>> overloads found". Does anybody know how to solve this?
>>
>> Thanks in advance!
>> Markus
>>
>> ------------------------------------------------------------------------------
>> The Palm PDK Hot Apps Program offers developers who use the
>> Plug-In Development Kit to bring their C/C++ apps to Palm for a share
>> of $1 Million in cash or HP Products. Visit us here for more details:
>> http://ad.doubleclick.net/clk;226879339;13503038;l?
>> http://clk.atdmt.com/CRS/go/247765532/direct/01/
>> _______________________________________________
>> luabind-user mailing list
>> [hidden email]
>> https://lists.sourceforge.net/lists/listinfo/luabind-user
>
>
> ------------------------------------------------------------------------------
> The Palm PDK Hot Apps Program offers developers who use the
> Plug-In Development Kit to bring their C/C++ apps to Palm for a share
> of $1 Million in cash or HP Products. Visit us here for more details:
> http://ad.doubleclick.net/clk;226879339;13503038;l?
> http://clk.atdmt.com/CRS/go/247765532/direct/01/
> _______________________________________________
> luabind-user mailing list
> [hidden email]
> https://lists.sourceforge.net/lists/listinfo/luabind-user
>
But if its not a Luabind function, then C++ exceptions can pass through it.

------------------------------------------------------------------------------
The Palm PDK Hot Apps Program offers developers who use the
Plug-In Development Kit to bring their C/C++ apps to Palm for a share
of $1 Million in cash or HP Products. Visit us here for more details:
http://p.sf.net/sfu/dev2dev-palm
_______________________________________________
luabind-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user
Reply | Threaded
Open this post in threaded view
|

Re: C Function with variable number of parameters

James Porter-2
On 7/28/2010 6:24 PM, Jason McKesson wrote:
> But if its not a Luabind function, then C++ exceptions can pass through it.

Unless things have changed since I last checked, C++ exceptions aren't
really safe to throw in Luabind functions anyway, since they'll blow
through the Lua parts of the call stack and leave it in an inconsistent
state. Things usually work afterwards, but that's more luck than
anything else.

Of course, it's been a while since I looked at this, so it might be
different now.

- Jim


------------------------------------------------------------------------------
The Palm PDK Hot Apps Program offers developers who use the
Plug-In Development Kit to bring their C/C++ apps to Palm for a share
of $1 Million in cash or HP Products. Visit us here for more details:
http://p.sf.net/sfu/dev2dev-palm
_______________________________________________
luabind-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user
Reply | Threaded
Open this post in threaded view
|

Re: C Function with variable number of parameters

Nigel Atkinson-2
What Jim said was my impression too.  I guess you should really catch what you can, perhaps calling lua_error. :-)

Nigel

On Wed, 2010-07-28 at 19:09 -0500, James Porter wrote:
On 7/28/2010 6:24 PM, Jason McKesson wrote:
> But if its not a Luabind function, then C++ exceptions can pass through it.

Unless things have changed since I last checked, C++ exceptions aren't 
really safe to throw in Luabind functions anyway, since they'll blow 
through the Lua parts of the call stack and leave it in an inconsistent 
state. Things usually work afterwards, but that's more luck than 
anything else.

Of course, it's been a while since I looked at this, so it might be 
different now.

- Jim


------------------------------------------------------------------------------
The Palm PDK Hot Apps Program offers developers who use the
Plug-In Development Kit to bring their C/C++ apps to Palm for a share
of $1 Million in cash or HP Products. Visit us here for more details:
http://p.sf.net/sfu/dev2dev-palm
_______________________________________________
luabind-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user


------------------------------------------------------------------------------
The Palm PDK Hot Apps Program offers developers who use the
Plug-In Development Kit to bring their C/C++ apps to Palm for a share
of $1 Million in cash or HP Products. Visit us here for more details:
http://p.sf.net/sfu/dev2dev-palm
_______________________________________________
luabind-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user
Reply | Threaded
Open this post in threaded view
|

Re: C Function with variable number of parameters

Markus Klotzbuecher
On Thu, Jul 29, 2010 at 07:30:05AM +0200, Nigel Atkinson wrote:
> What Jim said was my impression too.  I guess you should really catch what you
> can, perhaps calling lua_error. :-)

Thanks everyone! I'll do it this way and try not to let any exceptions
slip through...

Markus

------------------------------------------------------------------------------
The Palm PDK Hot Apps Program offers developers who use the
Plug-In Development Kit to bring their C/C++ apps to Palm for a share
of $1 Million in cash or HP Products. Visit us here for more details:
http://p.sf.net/sfu/dev2dev-palm
_______________________________________________
luabind-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user
Reply | Threaded
Open this post in threaded view
|

Re: C Function with variable number of parameters

Jason McKesson
In reply to this post by James Porter-2
  On 7/28/2010 5:09 PM, James Porter wrote:

> On 7/28/2010 6:24 PM, Jason McKesson wrote:
>> But if its not a Luabind function, then C++ exceptions can pass through it.
> Unless things have changed since I last checked, C++ exceptions aren't
> really safe to throw in Luabind functions anyway, since they'll blow
> through the Lua parts of the call stack and leave it in an inconsistent
> state. Things usually work afterwards, but that's more luck than
> anything else.
>
> Of course, it's been a while since I looked at this, so it might be
> different now.
>
> - Jim
Luabind encapsulates all of its function calls in try blocks, and
catches all exceptions and converts them into lua_errors.

Thus, going back to the question: how do you register a multi-variate
function through Luabind?

------------------------------------------------------------------------------
The Palm PDK Hot Apps Program offers developers who use the
Plug-In Development Kit to bring their C/C++ apps to Palm for a share
of $1 Million in cash or HP Products. Visit us here for more details:
http://p.sf.net/sfu/dev2dev-palm
_______________________________________________
luabind-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user
Reply | Threaded
Open this post in threaded view
|

Re: C Function with variable number of parameters

Chris Byrne-6
On Thu, Jul 29, 2010 at 7:26 AM, Jason McKesson <[hidden email]> wrote:
> Luabind encapsulates all of its function calls in try blocks, and
> catches all exceptions and converts them into lua_errors.
>
> Thus, going back to the question: how do you register a multi-variate
> function through Luabind?

I don't believe this is currently possible. At least not with Luabind directly.

Luabind verifies all the arguments being passed to your function
before calling it. Looking at the number of arguments, their types,
determining the appropriate overload to call, and converting arguments
where possible. I'm not aware of any binding policy that bypasses this
mechanism.

We use lua_register to bind our rare variadic functions, then take
care to catch all exceptions and convert them to an appropriate
lua_error call. This is pretty much what luabind itself does for bound
functions that it calls.

from make_function.hpp:

# ifndef LUABIND_NO_EXCEPTIONS
          try
          {
              results = invoke(
                  L, *impl, ctx, impl->f, Signature(), impl->policies);
          }
          catch (...)
          {
              handle_exception_aux(L);
              lua_error(L);
          }
# else


An alternative (which we actually prefer) is to accept one argument as
a table. This table can then be iterated or indexed as needed by the
called function. Allowing for named parameters etc.

// declare
void my_func( const luabind::object& obj ) {
    if ( LUA_TTABLE != luabind::type(obj) ) throw "Expected table
my_func{ args }";
    // ...
}

// bind
.def( "my_func", &my_func )

-- use
my_func{ bar=2, foo="string" }

my_func{ 4, 3, 2, 1, "bacon" }


- CB

------------------------------------------------------------------------------
The Palm PDK Hot Apps Program offers developers who use the
Plug-In Development Kit to bring their C/C++ apps to Palm for a share
of $1 Million in cash or HP Products. Visit us here for more details:
http://p.sf.net/sfu/dev2dev-palm
_______________________________________________
luabind-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user
Reply | Threaded
Open this post in threaded view
|

Re: C Function with variable number of parameters

Nigel Atkinson-2
In reply to this post by Jason McKesson
Ah - so you CAN throw exceptions inside a bound function and expect good behaviour.  Good to know.  I stand corrected. :-)

So  the immediate solution is to use lua_register, and emulate what luabind does with exceptions, but within the bound function, i..e catch them and call lua_error.

If there was such a way to bind a function expecting variable arity, I wonder how it would be best done?  A policy that turns the checking off? Or a policy  to mark parameters after a certain number as optional?

It might be a bit of a major code change - but I have only looked at small parts of the luabind source, so I'm just guessing.

Nigel

On Thu, 2010-07-29 at 07:26 -0700, Jason McKesson wrote:m
Luabind encapsulates all of its function calls in try blocks, and 
catches all exceptions and converts them into lua_errors.

------------------------------------------------------------------------------
The Palm PDK Hot Apps Program offers developers who use the
Plug-In Development Kit to bring their C/C++ apps to Palm for a share
of $1 Million in cash or HP Products. Visit us here for more details:
http://p.sf.net/sfu/dev2dev-palm
_______________________________________________
luabind-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user