lua-users home
lua-l archive

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


On 10 March 2014 17:07, steve donovan <steve.j.donovan@gmail.com> wrote:
> On Mon, Mar 10, 2014 at 6:53 PM, Andrew Starks <andrew.starks@trms.com> wrote:
>> See? This is exactly what I was hoping to get out of my post. :)
>
> Cool ;) But now that you're here, what would you like to see in a
> higher-level C API?
>
> Basically, I can confess that I did llua because stack operations give
> me a headache...

Nice! I've done something roughly similar to this in a project at work
(where development time was more at a premium than execution time).
Apart from some goodies that handled our OO-in-C classes as special
cases, the API was basically down to two vararg functions,
Bind_arguments and Bind_results. Get from Lua, do work in C, send back
to Lua.

Snippets of use:

static int luagw_Gateway_sendMessage(lua_State* L) {
   Gateway* gw; Message* msg; StringMap* attrs; bool trySpool = true;
   (void) Bind_arguments(L,
"self:Gateway,obj:Message,StringMap,?=bool", &gw, &msg, &attrs,
&trySpool);

   SendResult result = Gateway_sendMessage(gw, msg, attrs, trySpool);
   StringMap_delete(attrs);

   if (result <= SEND_ERR) {
      return Bind_results(L, "cstring", "err");
   } else if (result >= SEND_PENDING) {
      return Bind_results(L, "cstring", "pending");
   } else {
      return Bind_results(L, "cstring", "ok");
   }
}

static int luagw_Message_getRtuData(lua_State* L) {
   Message* msg; int rtuNr;
   (void) Bind_arguments(L, "self:Message,int", &msg, &rtuNr);

   rtuNr--;
   Rtu* rtu = Message_getRtu(msg, rtuNr);

   if (rtu) {
      return Bind_results(L, "int,cstring,cstring,StringMap",
Map_used(rtu->records), rtu->name, rtu->status, rtu->authParameters);
   } else {
      return Bind_results(L, "nil,cstring", 0, "RTU not found");
   }
}

This effectively shielded binding development from having to deal with
the stack (the "self" objects usually contained everything the
function needed to operate).

You see no error handling in these functions because the Bind_*
functions check their arguments and raise luaL_error in case of
problems.

-- Hisham