lua-users home
lua-l archive

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


It was thus said that the Great Jim once stated:
> On 5/17/19, William Ahern <william@25thandclement.com> wrote:
> > While the Lua C API has some unpleasant artifacts from incremental
> > improvements, it's actually much nicer than you seem to give it credit for.
> > And in my experience much nicer thanh the vast majority of APIs I've run
> > across.
> 
> here is an example from the SGScript(.org) documentation:
> http://www.sgscript.org/pages/advdocs/sgscript.docs.htm#Argument-handling
> 
> used in
> if ( sgs_LoadArgs ( "s|f", & str, & q ) ) return 0 ;

  Shouldn't this be:

	if (!sgs_LoadArgs("s|f",&str,&q)) return 0;

> sgs_LoadArgs() does type-based argument parsing
> 
>   's' requires a value that can be converted to a string, returns to
> passed char**
>   'f' requires a value that can be converted to a real value, returns
> to passed float*
>   '|' makes all arguments after it optional (always initialize data
> passed to those!)
> 
> there are many more options and features for this function

  I can only speak for myself, but the lack of this function hasn't bothered
me.  For me, it's largely a difference of:

static int xdisplay_reparent(lua_State *L)
{
  XReparentWindow(
          *(Display **)lua_touserdata(L,1),
          *(Window *)luaL_checkudata(L,2,TYPE_XWINDOW),
          *(Window *)luaL_checkudata(L,3,TYPE_XWINDOW),
          luaL_optinteger(L,4,0),
          luaL_optinteger(L,5,0)
  );
  return 0;
}

vs (assuming Lua had a similar funtion):

static int xdisplay_reparent(lua_State *L)
{
  Display *display;
  Window   w;
  Window   parent;
  int      x = 0;
  int      y = 0;
  
  if (lua_loadargs(L,"ooo|ii",&display,&w,&parent,&x,&y))
    XReparentWindow(display,w,parent,x,y);
  else
    luaL_error(L,"bad parameter to xdisplay_reparent()");
  return 0;
}

  It's not like I have tons of functions each with scores of parameters.  
  
> Squirrel provides similar functionality (look it up yourself in its docs).
> this is just an example to show what other languages' C APIs provide.
> 
> BTW:
> 
> it is still not clear to me what a negative return value from a call to a
> Lua C function like "int cfunc ( lua_State * L )" means/represents/is used for.

  The return value for a lua_CFunction is the number of returned
values---negative values are undefined behavior.  For example, when I call
the following function:

	int badret(lua_State *L)
	{
	  return -lua_gettop(L);
	}

I get:

	> badret(1)
	Segmentation fault (core dumped)

Your results may vary.

  -spc (It's undefined!)