lua-users home
lua-l archive

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


Am I reading the API description correctly? …

const char *luaL_optlstring (lua_State *L,
                             int arg,
                             const char *d,
                             size_t *l);

1. If arg is a string on the stack, return it and fill *l with it’s length.
2. If arg is NOT a string on the stack, and d is NOT NULL, return d and fill *l with the length of d.
3a. If arg is NOT a string on the stack, and d is NULL, *l is not set by Lua.
3b. If arg is NOT a string on the stack, and d is NULL, *l is undefined.

If (3a) is indeed what the API does, then I don’t see any need to modify its behavior; presumably the caller can set *l before calling the API and get the desired result, and Lua does not waste cycles setting a value that isn’t necessary.

If (3b) is what the API does, then I think it needs changing since it’s *not* possible to preset *l to a sensible value before calling the API and would need additional testing of d on return before *l is trusted.

(Editorializing: I tend not to like APIs that leave OUT params dangling, unless it’s prohibitively expensive to set them to a sane value even when the API errors.)

—Tim


> On Nov 11, 2015, at 10:49 AM, Roberto Ierusalimschy <roberto@inf.puc-rio.br> wrote:
> 
>> I'd suggest to add to 'luaL_optlstring's doc something like:
>> 
>> "If l is not NULL, fills the position *l with the result's length *** or 0 if the result is NULL *** ."
> 
> Do you need that?
> 
> -- Roberto
>