lua-users home
lua-l archive

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


---- Ben Aurel wrote: 
> ....
> What do you (experienced lua user) use when extending Lua with C?
> ....

I wrote a C wrapper library around some of the Lua C API functions.
The goal is to keep direct calls to Lua C API functions out of most of
my code.  It's not a complete library in that it wraps only those
C API functions I need to use.

One heavily used wrapper function is modeled after the code in
section 25.4 "A Generic Call Function" from the 2nd edition of
Programming in Lua.  This wrapper works well for those instances
when the return values from a Lua script don't differ in type
depending on whether the script detected errors.

I have specialized wrapper functions that handle groups of Lua
scripts that return values of different types.  For example, a typical
Lua script might be something like this (as a mix of Lua and
pseudocode):

function compute_a_value( param1, param2 )
  -- Do some calculations with the input parameters.
  -- Check for errors; e.g., divide-by-zero, nil parameter
  if <there weren't any errors> then
    return true, result  -- result is a double
  else
    return false, "Found an error"
  end
end

My wrapper code for this type of script is also based on PIL2
section 25.4 code, but the C wrapper examines the boolean return
value on the stack to decide what the data type of the second
item on the stack must be.

Hope this gives you some ideas.

Mason Deaver