lua-users home
lua-l archive

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


Am 24.12.2014 um 07:47 schröbte Steven Degutis:
While working on a Swift wrapper around Lua's C API (code is here[1]),
I came up with a way to extend Lua's argument-checking functionality.

The upside is that now I can assert that argument #2 is a "point",
meaning a table that contains keys "x" and "y" pointing to numbers.
This is admittedly not as efficient as just checking lua_type, but
it's also opt-in.

The downside is that I can't use luaL_checktype for these kinds of
custom types, because that function uses lua_type directly.

What about `luaL_argcheck(L, is_point(L, 1), 1, "point expected")`?


Hoping to find another solution, I dug into the source code of
luaL_checktype, and found that typeerror is exactly what I would want
to use here, asking the custom type for typeerror's tname argument.
But typeerror is a private function.

So in my program, I've modified Lua to contain a new public function,
luaL_typeerror, which just calls typeerror directly. I'm not sure how
generally useful this would be, but since this function would be
purely opt-in and can coexist peacefully with luaL_checktype, would it
be a good idea to create this new function in Lua proper (even if with
a different name)?

In Lua 5.1 we had[1] this function, but it got removed[2] in Lua 5.2. To be fair, it was a trivial function then, but now got a little more complex for Lua 5.3.


Sincerely,
Anonymous Lua User


Philipp

  [1]: http://www.lua.org/source/5.1/lauxlib.c.html#luaL_typerror
  [2]: http://www.lua.org/manual/5.2/manual.html#8.3